<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Brand&#039;s memorandum</title>
	<atom:link href="http://dotbrand.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://dotbrand.wordpress.com</link>
	<description>Random notes from the life of a .NET developer</description>
	<lastBuildDate>Thu, 15 Dec 2011 20:22:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='dotbrand.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/042377a1f03089b58f5f63e3ef163c5d?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Brand&#039;s memorandum</title>
		<link>http://dotbrand.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://dotbrand.wordpress.com/osd.xml" title="Brand&#039;s memorandum" />
	<atom:link rel='hub' href='http://dotbrand.wordpress.com/?pushpress=hub'/>
		<item>
		<title>AutoMapper makes you lazy</title>
		<link>http://dotbrand.wordpress.com/2010/07/07/automapper-makes-you-lazy/</link>
		<comments>http://dotbrand.wordpress.com/2010/07/07/automapper-makes-you-lazy/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 18:55:22 +0000</pubDate>
		<dc:creator>Brand</dc:creator>
				<category><![CDATA[Automapper]]></category>

		<guid isPermaLink="false">http://dotbrand.wordpress.com/?p=35</guid>
		<description><![CDATA[I love AutoMapper. BUT! It can give you a false sense of security. Take the two following classes: Lazy as I am I create a mapping: and then run: to be sure all fields are mapped. The test passes! Fortunately I have written a mapping test with actual values: This test fails saying that the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotbrand.wordpress.com&amp;blog=10450367&amp;post=35&amp;subd=dotbrand&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I love AutoMapper. BUT! It can give you a false sense of security. Take the two following classes:</p>
<p><pre class="brush: csharp; gutter: false;">
    public class Product
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
    }

    public class ProductDTO
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string ProductType { get; set; }
    }
</pre></p>
<p>Lazy as I am I create a mapping:</p>
<p><pre class="brush: csharp; gutter: false;">
    Mapper.CreateMap&lt;ProductDTO, Product&gt;();
</pre></p>
<p>and then run:</p>
<p><pre class="brush: csharp; gutter: false;">
    Mapper.AssertConfigurationIsValid();
</pre></p>
<p>to be sure all fields are mapped. The test passes!<br />
Fortunately I have written a mapping test with actual values:</p>
<p><pre class="brush: csharp; gutter: false;">
    Assert.That(product.Type, Is.EqualTo(&quot;MyProductType&quot;));
</pre></p>
<p>This test fails saying that the actual value is “ProductDTO”. WTF?<br />
Well, AutoMapper actually does what the convention tells it to do:</p>
<ul>
<li>Finds a destination property named Type on Product</li>
<li>Looks for a property or method on source object ProductDTO named Type (none found)</li>
<li>Tries a regex on all properties and methods on ProductDTO which matches Get + destination name (GetType() inherited from Object found)</li>
<li>Gets the result from GetType() (which is the full name of the class type = &#8220;ProductDTO&#8221;)</li>
<li>Sets the property Type on Product</li>
</ul>
<p>Two lessons:<br />
<strong>Don’t be lazy! Write mapping tests!</strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotbrand.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotbrand.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dotbrand.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dotbrand.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dotbrand.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dotbrand.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dotbrand.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dotbrand.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dotbrand.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dotbrand.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dotbrand.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dotbrand.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dotbrand.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dotbrand.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotbrand.wordpress.com&amp;blog=10450367&amp;post=35&amp;subd=dotbrand&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotbrand.wordpress.com/2010/07/07/automapper-makes-you-lazy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebebd21527476ae1c864b6927a70aae2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dotbrand</media:title>
		</media:content>
	</item>
		<item>
		<title>Entities with private/protected setters in RavenDb</title>
		<link>http://dotbrand.wordpress.com/2010/07/05/entities-with-privateprotected-setters-in-ravendb/</link>
		<comments>http://dotbrand.wordpress.com/2010/07/05/entities-with-privateprotected-setters-in-ravendb/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 21:41:42 +0000</pubDate>
		<dc:creator>Brand</dc:creator>
				<category><![CDATA[DDD]]></category>
		<category><![CDATA[Json.Net]]></category>
		<category><![CDATA[RavenDb]]></category>

		<guid isPermaLink="false">https://dotbrand.wordpress.com/2010/07/05/entities-with-privateprotected-setters-in-ravendb/</guid>
		<description><![CDATA[Orientation When designing a domain model according to DDD you strive to let all data changes internal to an entity come from events and method calls instead of setting fields and/or setters from outside the entity. One way of aiding this design is to, as much as possible, avoid having public fields and setters. Example: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotbrand.wordpress.com&amp;blog=10450367&amp;post=14&amp;subd=dotbrand&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Orientation</h2>
<p>When designing a domain model according to DDD you strive to let all data changes internal to an entity come from events and method calls instead of setting fields and/or setters from outside the entity. One way of aiding this design is to, as much as possible, avoid having public fields and setters.</p>
<p>Example:</p>
<p><pre class="brush: csharp; gutter: false;">
public class Employee
{
   protected Employee() { }

   public Employee(string employeeNumber)
   {
   Number = employeeNumber;
   }

   public string Id { get; protected set; }
   public string Number { get; protected set; }
   public string FirstName { get; protected set; }
   public string LastName { get; protected set; }
}
</pre></p>
<h2>Problem</h2>
<p>RavenDb uses JSON as storage format and JSON.NET as it’s serializing/deserializing engine. The default deserialization strategy unfortunately (for me) ignores non-public setters and fields. In practice this means that the former mentioned Employee class would save itself correctly to RavenDb but when I retrieve it again everything but the Id will be null (Id is a special property and is not treated the same way).</p>
<h2>Solution</h2>
<p>The most obvious solution is to mark all your properties with the attribute JsonProperty or DataMember. Both of them will force JSON.NET to serialize and deserialize your property whether it’s public or not.</p>
<h3>Solution 1:</h3>
<p><pre class="brush: csharp; gutter: false;">
[JsonProperty]
public string FirstName { get; protected set; }
</pre></p>
<p>But we don’t want to clutter our domain model with attributes concerning persistance if we can avoid it. Fortunately RavenDb supports manually setting your custom Contract Resolver (which is responsible, among other things, for deciding what members get serialized/deserialized).</p>
<p>What I did was inheriting DefaultContractResolver adding <span style="font-family:Consolas, Monaco, 'Courier New', Courier, monospace;line-height:18px;font-size:12px;white-space:pre;">BindingFlags.NonPublic</span> to the <span style="font-family:Consolas, Monaco, 'Courier New', Courier, monospace;line-height:18px;font-size:12px;white-space:pre;">DefaultMembersSearchFlags</span> to include private and protected members when reflecting. Also I overrided the method <span style="font-family:Consolas, Monaco, 'Courier New', Courier, monospace;line-height:18px;font-size:12px;white-space:pre;">GetSerializableMembers</span> calling the base implementation and then removing all backing fields that was included.</p>
<h3>Solution 2:</h3>
<p><pre class="brush: csharp; gutter: false; highlight: [7,17,22];">
public static class DocumentStoreFactory
{
   public static IDocumentStore Create()
   {
      var documentStore = new DocumentStore { Url = &quot;http://localhost:8080/&quot; };
      documentStore.Initialize();
      documentStore.Conventions.JsonContractResolver = new IncludeNonPublicMembersContractResolver();

      return documentStore;
   }
}

public class IncludeNonPublicMembersContractResolver : DefaultContractResolver
{
    public IncludeNonPublicMembersContractResolver()
    {
        DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
    }
    protected override List&lt;MemberInfo&gt; GetSerializableMembers(Type objectType)
    {
        var members = base.GetSerializableMembers(objectType);
        return members.Where(m=&gt; !m.Name.EndsWith(&quot;k__BackingField&quot;)).ToList();
    }
}

</pre></p>
<p>This solution was the quickest I could come up with that didn&#8217;t force me to change my domain objects. Please comment if you find a better way!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotbrand.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotbrand.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dotbrand.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dotbrand.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dotbrand.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dotbrand.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dotbrand.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dotbrand.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dotbrand.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dotbrand.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dotbrand.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dotbrand.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dotbrand.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dotbrand.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotbrand.wordpress.com&amp;blog=10450367&amp;post=14&amp;subd=dotbrand&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotbrand.wordpress.com/2010/07/05/entities-with-privateprotected-setters-in-ravendb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebebd21527476ae1c864b6927a70aae2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dotbrand</media:title>
		</media:content>
	</item>
	</channel>
</rss>
