<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nathan Bridgewater &#187; Serialization</title>
	<atom:link href="http://www.integratedwebsystems.com/category/dotnet/serialization/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.integratedwebsystems.com</link>
	<description>My Little .NET Sandbox</description>
	<lastBuildDate>Thu, 19 Jan 2012 20:37:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>WCF Data Contracts and &quot;k__BackingField&quot; Property Naming</title>
		<link>http://www.integratedwebsystems.com/2009/05/wcf-data-contracts-and-k__backingfield-property-naming/</link>
		<comments>http://www.integratedwebsystems.com/2009/05/wcf-data-contracts-and-k__backingfield-property-naming/#comments</comments>
		<pubDate>Thu, 28 May 2009 15:43:25 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[Serialization]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DataContract]]></category>
		<category><![CDATA[DataMember]]></category>

		<guid isPermaLink="false">http://www.integratedwebsystems.com/?p=257</guid>
		<description><![CDATA[So recently, I was playing around with WCF, and I created a...]]></description>
			<content:encoded><![CDATA[<p>So recently, I was playing around with WCF, and I created a few classes that I wanted to expose in operations. Usually when I play with plain old objects, I tend to type them up pretty lazily and use any code shortcut I can. In this case I defined my properties using the 3.0 auto-generated field method, which looks like an interface property. The C# compiler automatically generates the backing field on the fly for me at compile time.&#160; Here is a sample class I&#8217;m using in the silverlight music project.   </p>
<pre class="brush: csharp; gutter: false; toolbar: false;">[Serializable]
public class FileBrowser
{
    public string LocalMusicRoot { get; set; }
    public string WebMusicRoot { get; set; }
    public List&lt;string&gt; Files { get; set; }
}</pre>
<p><a href="http://www.integratedwebsystems.com/wp-content/uploads/image8.png"><img style="border-bottom: 0pt; border-left: 0pt; border-top: 0pt; border-right: 0pt" border="0" alt="image" align="right" src="http://www.integratedwebsystems.com/wp-content/uploads/image-thumb7.png" /></a>The catch comes in when you expose it to WCF. When serializing these objects, the serializer will look at all FIELDS of the class no matter their scope:&#160; public, private, etc.&#160; This is because it uses System.Runtime.Serialization, not System.Xml.Serialization. It does not serialize properties since properties really are just accessor functions.&#160; So what you get is a funky looking proxy class with some properties you didn&#8217;t want and some cryptic naming. I.E. k__BackingField appended to your field names. The serializer simply takes what it has which is the auto-generated field name the C# compiler made for us.</p>
<p>So how do I clean this up?!?!&#160;&#160; Easy!&#160; Use a data contract.&#160;&#160; By defining your classes using the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx" target="_blank">DataContract attribute</a> from System.Runtime.Serialization, the serializer will interpret your <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx" target="_blank">DataMembers</a> as fields on the class and use your naming. It will also ignore anything that&#8217;s not defined as a DataMember; so that hidden stuff will remain hidden and not be included in the proxy class. Take a look at this.&#160; We modified our original class with these attributes and the generated proxy class on the right now uses the names and fields we want!&#160; Easy peasy!</p>
<p><span id="more-257"></span></p>
<pre class="brush: csharp; gutter: false; toolbar: false;">[DataContract]
public class FileBrowser
{
    [DataMember]
    public string LocalMusicRoot { get; set; }

    [DataMember]
    public string WebMusicRoot { get; set; }

    [DataMember]
    public List&lt;string&gt; Files { get; set; }
}</pre>
<p><a href="http://www.integratedwebsystems.com/wp-content/uploads/image9.png"><img style="border-bottom: 0pt; border-left: 0pt; border-top: 0pt; border-right: 0pt" border="0" alt="image" align="right" src="http://www.integratedwebsystems.com/wp-content/uploads/image-thumb8.png" /></a>So now&#8230;&#160; When you&#8217;re defining your entity classes that you want to pass around WCF; just be sure to take advantage of DataContract attributes to clean up the client proxy classes.</p>
<p>By the way&#8230; If you&#8217;re playing around with <a href="http://blog.wekeroad.com/subsonic/subsonic-3-alpha-updated/" target="_blank">SubSonic 3 Alpha</a>, this is a really sweet tweak you can make to your <a href="/classes.txt" target="_blank">classes.tt T4 template</a>.&#160; This will make them all Data Contracts and put DataMember attributes on each of the data fields. This will hide the foreign key properties from the WCF generated proxies.&#160; Good stuff! <img src='http://www.integratedwebsystems.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.integratedwebsystems.com/2009/05/wcf-data-contracts-and-k__backingfield-property-naming/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

