<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Roger Alsing Weblog</title>
	
	<link>http://rogeralsing.com</link>
	<description />
	<lastBuildDate>Sun, 08 Nov 2009 14:05:36 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain="rogeralsing.com" port="80" path="/?rsscloud=notify" registerProcedure="" protocol="http-post" />
<image>
		<url>http://www.gravatar.com/blavatar/06cede321232671798eb555e7b7ee9bc?s=96&amp;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Roger Alsing Weblog</title>
		<link>http://rogeralsing.com</link>
	</image>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/RogerAlsingWeblog" type="application/rss+xml" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">RogerAlsingWeblog</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Two flavors of DDD</title>
		<link>http://rogeralsing.com/2009/11/08/two-flavors-of-ddd/</link>
		<comments>http://rogeralsing.com/2009/11/08/two-flavors-of-ddd/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 12:40:32 +0000</pubDate>
		<dc:creator>Roger Alsing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[O/R Mapping]]></category>

		<guid isPermaLink="false">http://rogeralsing.com/?p=673</guid>
		<description><![CDATA[I have been trying to practice domain driven design for the last few years.
During this time, I have learnt that there are almost as many ways to implement DDD as there are practitioners.
After studying a lot of different implementations I have seen two distinct patterns.
I call the first pattern &#8220;Aggregate Graph&#8221;:
When applying aggregate graphs, you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=673&subd=rogeralsing&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I have been trying to practice domain driven design for the last few years.<br />
During this time, I have learnt that there are almost as many ways to implement DDD as there are practitioners.</p>
<p>After studying a lot of different implementations I have seen two distinct patterns.</p>
<p>I call the first pattern <strong>&#8220;Aggregate Graph&#8221;</strong>:</p>
<p>When applying aggregate graphs, you allow members of one aggregate to have direct associations to another aggregate.<br />
For example, an &#8220;Order&#8221; entity which is part of a &#8220;Order aggregate&#8221; might have a &#8220;Customer&#8221; property which leads directly to a &#8220;Customer&#8221; entity that is part of a &#8220;Customer aggregate&#8221;.</p>
<p> <img class="alignnone size-full wp-image-677" title="aggregate-graph" src="http://rogeralsing.files.wordpress.com/2009/11/aggregate-graph.gif?w=583&#038;h=362" alt="aggregate-graph" width="583" height="362" /></p>
<p>According to Evans book this is completely legal, any member of an aggregate may point to the root of any other aggregate.<br />
Evans is very clear on the matter that aggregate root identities are global while identity of non root entities are local to the aggregate itself.</p>
<p>The opposite pattern would be what I call <strong>&#8220;Aggregate Documents&#8221;</strong>:</p>
<p>Here the aggregates never relate _directly_ to other aggregate roots.<br />
Instead, the associations may be designed as &#8220;snapshots&#8221; where you store light weight value object clones of the related aggregate roots.<br />
An &#8220;Order&#8221; entity would have a &#8220;Customer&#8221; property which leads to a &#8220;CustomerSnapshot&#8221; value object instead of a Customer entity.<br />
This way each aggregate instance becomes more of a free-floating document.</p>
<p><img class="alignnone size-full wp-image-679" title="aggregate-document" src="http://rogeralsing.files.wordpress.com/2009/11/aggregate-document.gif?w=583&#038;h=648" alt="aggregate-document" width="583" height="648" /></p>
<p>Since I have been applying both of these patterns, I will try to highlight the pros and cons of them in the rest of this post.</p>
<h2>Aggregate Graph</h2>
<p>The Aggregate Graph pattern is the approach I used when I first started doing DDD and I think that it is the most common way to implement DDD.<br />
Since I was an O/RM developer (NPersist) this felt very natural to me, I could design my object graph in our design tool and then draw a few boxes on top of it and claim that those were my aggregates.<br />
I most often used eager load inside the aggregates and lazy load between aggregates in order to avoid that the entire database was fetches when one aggregate instance was loaded.</p>
<p>This had a very nice &#8220;OOP&#8221; feel to it, I was working with objects and associations and I could ignore that there even was a database involved.</p>
<p>My &#8220;Repositories&#8221; were mere windows into my object graph, I could ask a repository to give me one or more aggregate roots and from those object I could pretty much navigate to any other object in the graph due to the spider web nature of the aggregate graph.</p>
<p><img class="alignnone size-full wp-image-680" title="repository-window" src="http://rogeralsing.files.wordpress.com/2009/11/repository-window.gif?w=448&#038;h=402" alt="repository-window" width="448" height="402" /></p>
<p>The pros of this approach is that it is easy to understand, you design your domain model just like any other class model.<br />
It also works very well with O/R mappers, features like Lazy Load and Dirty Tracking makes it all work for you.</p>
<p>However, there are a few problems with this approach too.<br />
Firstly, Lazy Load in O/R mappers is an implicit feature, there is no way for a developer to know at what point he will trigger a roundtrip to the database just by reading the code.<br />
It always looks like you are traversing a fully loaded object graph while you are in fact not.<br />
This often leads to severe performance problems if your development team don&#8217;t fully understand this.</p>
<p>I have seen reports over this kind domain models where the implicit nature of Lazy Load have caused some 700 round-trip to the database in a single web page.</p>
<p>This is what you get when you try to solve an explicit problem in an implicit way.</p>
<p>If you are going to use Lazy Load, make sure your team understands how it works and where you use it.</p>
<p>Another problem with this approach arise when you need to fill your entities with data from multiple sources.<br />
Many of the applications I build nowadays relies on data from multiple sources, it could be a combination of services and internal databases.</p>
<p>When using Lazy Load to get related aggregates, there is no natural point where you can trigger calls to the other data sources and fill additional properties.<br />
You will most likely have to hook into your O/R mapper in order to intercept a lazy load and call the services from there.<br />
nowadays, I mostly use the second approach, Aggregate Documents.</p>
<h2>Aggregate Document</h2>
<p>Aggregate Document approach is much more explicit in its design.<br />
For example, if you want to find the orders for a specific customer;<br />
Instead of navigating the &#8220;Orders&#8221; collection of &#8220;Customer&#8221;, you will have to call a &#8220;FindOrdersByCustomer&#8221; query on the &#8220;OrderRepository&#8221;.</p>
<p>While I do agree that this looks less object oriented than the first approach, this allows developers to reason about the code in a different way.<br />
They can see important design decisions and hopefully avoid pitfalls like ripple loading.</p>
<p>Another benefit is that since you only work with islands of data, you can now aggregate data from multiple sources much easier.<br />
You can simply let your repositories aggregate the data into your entities.<br />
<em>(If you do it inside the actual repository or let the repository use some data access class that does it for it is up to you)</em><br />
<img class="alignnone size-full wp-image-688" title="repo-prism" src="http://rogeralsing.files.wordpress.com/2009/11/repo-prism.gif?w=458&#038;h=140" alt="repo-prism" width="458" height="140" /><br />
You don&#8217;t have to hook into any O/RM infrastructure since you no longer rely on lazy load between aggregates.</p>
<p>Personally I use eager load inside my aggregates, that is, I fetch &#8220;Order&#8221; and &#8220;Order Detail&#8221; together as a whole.<br />
A side effect of this is that since I don&#8217;t use Lazy Load between aggregates and don&#8217;t use Lazy Load inside my aggregates, my need for O/R mapping frameworks drops.<br />
I can apply this design without using a full-fledged O/R mapper framework.<br />
I&#8217;m not saying that you should avoid O/R mapping, just that it is much easier to apply this pattern if you can&#8217;t use an O/R mapper for some reason.</p>
<p>This also makes it easier to expose your domain model in an SOA environment.<br />
You can easily expose your entities or DTO versions of them in a service.</p>
<p>Lazy Load and services don&#8217;t play that well together.</p>
<p>Maybe it looks like I dislike the first approach, this is not the case, I may very well consider it in a smaller project where there is just one data source and where the development team is experienced with O/R mapping.<br />
You can also create hybrids of the two approaches;<br />
e.g. In Jimmy Nilsson&#8217;s book &#8220;Applying Domain Driven Design and Patterns&#8221; there are examples where an &#8220;Order&#8221; aggregate have a direct relation to the &#8220;Product&#8221; aggregate while the same &#8220;Order&#8221; aggregate uses snapshots instead of direct references to the &#8220;Customer&#8221; aggregate.</p>
<p>Snapshots also comes with the benefit of allowing you to store historical data.<br />
The snapshot can for example store both the CustomerId and the name of the customer at the time the order was placed.</p>
<p>Thats all for now.</p>
<p>//Roger</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rogeralsing.wordpress.com/673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rogeralsing.wordpress.com/673/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rogeralsing.wordpress.com/673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rogeralsing.wordpress.com/673/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rogeralsing.wordpress.com/673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rogeralsing.wordpress.com/673/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rogeralsing.wordpress.com/673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rogeralsing.wordpress.com/673/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rogeralsing.wordpress.com/673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rogeralsing.wordpress.com/673/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=673&subd=rogeralsing&ref=&feed=1" /></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=LP87JDS1fiA:NbOgi78wsKI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=LP87JDS1fiA:NbOgi78wsKI:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=LP87JDS1fiA:NbOgi78wsKI:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=LP87JDS1fiA:NbOgi78wsKI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=LP87JDS1fiA:NbOgi78wsKI:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=LP87JDS1fiA:NbOgi78wsKI:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=LP87JDS1fiA:NbOgi78wsKI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=LP87JDS1fiA:NbOgi78wsKI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=LP87JDS1fiA:NbOgi78wsKI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RogerAlsingWeblog/~4/LP87JDS1fiA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rogeralsing.com/2009/11/08/two-flavors-of-ddd/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ac3d313189eb4d3fe101e3aadcd08e2?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">roggan</media:title>
		</media:content>

		<media:content url="http://rogeralsing.files.wordpress.com/2009/11/aggregate-graph.gif" medium="image">
			<media:title type="html">aggregate-graph</media:title>
		</media:content>

		<media:content url="http://rogeralsing.files.wordpress.com/2009/11/aggregate-document.gif" medium="image">
			<media:title type="html">aggregate-document</media:title>
		</media:content>

		<media:content url="http://rogeralsing.files.wordpress.com/2009/11/repository-window.gif" medium="image">
			<media:title type="html">repository-window</media:title>
		</media:content>

		<media:content url="http://rogeralsing.files.wordpress.com/2009/11/repo-prism.gif" medium="image">
			<media:title type="html">repo-prism</media:title>
		</media:content>
	</item>
		<item>
		<title>Composite Oriented Programming: QI4J running on .NET</title>
		<link>http://rogeralsing.com/2009/06/25/composite-oriented-programming-qi4j-running-on-net/</link>
		<comments>http://rogeralsing.com/2009/06/25/composite-oriented-programming-qi4j-running-on-net/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 19:20:57 +0000</pubDate>
		<dc:creator>Roger Alsing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Aspect Oriented Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[composite oriented programming]]></category>
		<category><![CDATA[cop]]></category>
		<category><![CDATA[qi4j]]></category>

		<guid isPermaLink="false">http://rogeralsing.com/?p=662</guid>
		<description><![CDATA[For the last month I have been spending my spare time porting the awesome Java framework QI4J to .NET.
QI4J is the brain child of Rickard Öberg and Niclas Hedhman and it attempts to enable Composite Oriented Programming for the Java platform.
(For more info regarding Composite Oriented Programming see the QI4J website: http://www.qi4j.org/ )
I&#8217;m well aware that others [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=662&subd=rogeralsing&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>For the last month I have been spending my spare time porting the awesome Java framework QI4J to .NET.<br />
QI4J is the brain child of Rickard Öberg and Niclas Hedhman and it attempts to enable Composite Oriented Programming for the Java platform.<br />
(For more info regarding Composite Oriented Programming see the QI4J website: <a href="http://www.qi4j.org/">http://www.qi4j.org/</a> )</p>
<p>I&#8217;m well aware that others have been doing spikes on COP for .NET, a few of those attempts can be found here:<br />
<a href="http://stackoverflow.com/questions/152196/composite-oriented-programming-cop-net-4-0-mef-and-the-oslo-repository">http://stackoverflow.com/questions/152196/composite-oriented-programming-cop-net-4-0-mef-and-the-oslo-repository</a></p>
<p>However, I think it is sad to not reuse all of the effort and brain power that has been put into QI4J, and thus I decided to port it instead.</p>
<p>The code is currently only available from my SVN repository at google code:<br />
<a href="http://code.google.com/p/alsing/source/checkout">http://code.google.com/p/alsing/source/checkout</a></p>
<p><strong>Please note that the code will be released under the same license as the Java version (Apache License version 2.0)<br />
And copyright notices for the ported code will also be applied to give credit where credit is due.</strong></p>
<p>The .NET version is largely identical to the Java version as it is pretty much a plain class by class port.<br />
However there are a few exceptions:</p>
<p>The concept of &#8220;Property&lt;T&gt;&#8221; is not available in the .NET version since C# and most .NET languages does support properties out of the box and it would feel awkward to write things like:</p>
<p><strong>&#8220;order.Customer.Set(theCustomer)&#8221;</strong> rather than <strong>&#8220;order.Customer = theCustomer&#8221;</strong></p>
<p><strong></strong>However, the framework does rely on the Property&lt;T&gt; internally and thus most of the state holding infrastructure is also identical to the Java version.</p>
<p>The Java version relies on its own set of query expressions while my plan is to possibly reuse those internally but rather expose a LINQ API for querying.<br />
(I have not yet started to build that)</p>
<p>Currently supported concepts:</p>
<ul>
<li><a href="http://www.qi4j.org/48.html">Transient Composites</a>.</li>
<li>Prototyping</li>
<li><a href="http://www.qi4j.org/50.html">Typed and Generic Concerns.</a></li>
<li><a href="http://www.qi4j.org/63.html">Typed and Generic Side Effects.</a></li>
<li><a href="http://www.qi4j.org/58.html">Typed Mixins</a>, <a href="http://www.qi4j.org/55.html">Generic Mixins</a> and <a href="http://www.qi4j.org/220.html">Private Mixins</a>.</li>
<li>Most of the QI4J injection annotations.</li>
</ul>
<p>Soon to come:</p>
<ul>
<li><a href="http://www.qi4j.org/65.html">Value Composites</a></li>
<li><a href="http://www.qi4j.org/62.html">Service Composites</a></li>
<li>Immutable properties</li>
<li><a href="http://www.qi4j.org/44.html">Abstract Mixins </a>(which sadly won&#8217;t be as nice as in Java due to language differences)</li>
</ul>
<p>And I will ofcourse post a few samples of what you can do with this ASAP, I just wanted to drop a little sneak peek :-)</p>
<p>//Roger</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rogeralsing.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rogeralsing.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rogeralsing.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rogeralsing.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rogeralsing.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rogeralsing.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rogeralsing.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rogeralsing.wordpress.com/662/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rogeralsing.wordpress.com/662/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rogeralsing.wordpress.com/662/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=662&subd=rogeralsing&ref=&feed=1" /></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=jN3vjIQMgtY:UArbEVvsu7s:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=jN3vjIQMgtY:UArbEVvsu7s:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=jN3vjIQMgtY:UArbEVvsu7s:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=jN3vjIQMgtY:UArbEVvsu7s:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=jN3vjIQMgtY:UArbEVvsu7s:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=jN3vjIQMgtY:UArbEVvsu7s:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=jN3vjIQMgtY:UArbEVvsu7s:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=jN3vjIQMgtY:UArbEVvsu7s:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=jN3vjIQMgtY:UArbEVvsu7s:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RogerAlsingWeblog/~4/jN3vjIQMgtY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rogeralsing.com/2009/06/25/composite-oriented-programming-qi4j-running-on-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ac3d313189eb4d3fe101e3aadcd08e2?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">roggan</media:title>
		</media:content>
	</item>
		<item>
		<title>Entity Framework 4 – Entity Dependency Injection</title>
		<link>http://rogeralsing.com/2009/05/30/entity-framework-4-entity-dependency-injection/</link>
		<comments>http://rogeralsing.com/2009/05/30/entity-framework-4-entity-dependency-injection/#comments</comments>
		<pubDate>Sat, 30 May 2009 11:35:57 +0000</pubDate>
		<dc:creator>Roger Alsing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://rogeralsing.com/?p=646</guid>
		<description><![CDATA[When dealing with a fat domain model, there is often a need to be able to inject different services into your entities.
e.g. you might want to inject some domain service like &#8220;ITaxCalcualtorService&#8221; or an infrastructure service like &#8220;IEmailNotificationService&#8221; into your entities.
If we rely completely on eager loading, then this is not a big problem, we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=646&subd=rogeralsing&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>When dealing with a fat domain model, there is often a need to be able to inject different services into your entities.<br />
e.g. you might want to inject some domain service like &#8220;ITaxCalcualtorService&#8221; or an infrastructure service like &#8220;IEmailNotificationService&#8221; into your entities.</p>
<p>If we rely completely on eager loading, then this is not a big problem, we can simply let or repositories iterate over each entity once we have fetched them from the DB and inject our services.<br />
But when it comes to Lazy Load, we can no longer do this, in this case we need to get notified by the O/R mapper when an entity have been materialized so that we can inject our services into it.</p>
<p>If you are aiming to use Entity Framework 4 once it is released, you can accomplish this with the following code snippet:</p>
<pre class="brush: csharp;">

..inside your own EF container class..

public MyContext()
    : base(&quot;name=MyContext&quot;, &quot;MyModelContainer&quot;)
{
    ...
    ObjectStateManager.ObjectStateManagerChanged +=
    ObjectStateManagerChanged;
}

// this handler gets called each time the
// containers statemanager is changed
void ObjectStateManagerChanged(object sender,
                          CollectionChangeEventArgs e)
{
    // we are only interested in entities that
    // have been added to the state manager
    if (e.Action != CollectionChangeAction.Add)
        return;

    var state = ObjectStateManager
                    .GetObjectStateEntry(e.Element).State;

    // we are only interested in entities that
    // are unchanged (that is; loaded from DB)
if (state != System.Data.EntityState.Unchanged)
        return;

    OnEntityMaterialized(e.Element);
}

// this method gets called each time
// an entity have been materialized
private void OnEntityMaterialized(object entity)
{
    if (entity is Order)
    {
        Order order = entity as Order;
        // use property injection to assign
        // a taxcalculator service to the order
        order.TaxCalculatorService =
SomeDIContainer.GetObject&lt;ITaxCalculatorService&gt;();
    }
}
</pre>
<p>The above is a very naïve example, but it does show how you can catch the materialization of a specific entity type and then configure that entity.</p>
<p>This allows us to add complex domain logic to our entities.<br />
We can for example call a method like: &#8220;order.CalculateTotals()&#8221; where the CalculateTotals method now uses the ITaxCalculatorService.</p>
<p>HTH.</p>
<p>//Roger</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rogeralsing.wordpress.com/646/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rogeralsing.wordpress.com/646/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rogeralsing.wordpress.com/646/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rogeralsing.wordpress.com/646/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rogeralsing.wordpress.com/646/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rogeralsing.wordpress.com/646/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rogeralsing.wordpress.com/646/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rogeralsing.wordpress.com/646/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rogeralsing.wordpress.com/646/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rogeralsing.wordpress.com/646/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=646&subd=rogeralsing&ref=&feed=1" /></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=IXjtBYERTIk:VegDfgUZHKY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=IXjtBYERTIk:VegDfgUZHKY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=IXjtBYERTIk:VegDfgUZHKY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=IXjtBYERTIk:VegDfgUZHKY:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=IXjtBYERTIk:VegDfgUZHKY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=IXjtBYERTIk:VegDfgUZHKY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=IXjtBYERTIk:VegDfgUZHKY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=IXjtBYERTIk:VegDfgUZHKY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=IXjtBYERTIk:VegDfgUZHKY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RogerAlsingWeblog/~4/IXjtBYERTIk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rogeralsing.com/2009/05/30/entity-framework-4-entity-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ac3d313189eb4d3fe101e3aadcd08e2?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">roggan</media:title>
		</media:content>
	</item>
		<item>
		<title>Entity Framework 4 – “Almost” POCO</title>
		<link>http://rogeralsing.com/2009/05/29/entity-framework-4-almost-poco/</link>
		<comments>http://rogeralsing.com/2009/05/29/entity-framework-4-almost-poco/#comments</comments>
		<pubDate>Fri, 29 May 2009 20:14:11 +0000</pubDate>
		<dc:creator>Roger Alsing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://rogeralsing.com/?p=641</guid>
		<description><![CDATA[This is a short rant..
I have been very impressed with EF4 so far, but I&#8217;ve now found out that EF4 will NOT support enums.
I find this is very strange, I can&#8217;t see how Microsoft can claim POCO support and not support one of the most common code constructs.
More info here:
http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/7659feab-d348-4367-b2cd-0456b20262fe
Someone might claim that you can create a private [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=641&subd=rogeralsing&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This is a short rant..</p>
<p>I have been very impressed with EF4 so far, but I&#8217;ve now found out that <strong>EF4 will NOT support enums</strong>.<br />
I find this is very strange, I can&#8217;t see how Microsoft can claim POCO support and not support one of the most common code constructs.</p>
<p>More info here:</p>
<p><a href="http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/7659feab-d348-4367-b2cd-0456b20262fe">http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/7659feab-d348-4367-b2cd-0456b20262fe</a></p>
<p>Someone might claim that you can create a private property containing the mapped integer value and then make a public property exposing the enum.<br />
But this comes with two major drawbacks:</p>
<p>1) You can&#8217;t create Linq queries that are executed at DB level if you use unmapped properties.<br />
The Linq query would have to use the integer property, and thus loosing it&#8217;s semantics.</p>
<p>2) That is not POCO, that is mapper requirements leaking all over the place.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rogeralsing.wordpress.com/641/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rogeralsing.wordpress.com/641/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rogeralsing.wordpress.com/641/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rogeralsing.wordpress.com/641/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rogeralsing.wordpress.com/641/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rogeralsing.wordpress.com/641/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rogeralsing.wordpress.com/641/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rogeralsing.wordpress.com/641/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rogeralsing.wordpress.com/641/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rogeralsing.wordpress.com/641/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=641&subd=rogeralsing&ref=&feed=1" /></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=48PVjjVsWDU:e2SNacoWrqQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=48PVjjVsWDU:e2SNacoWrqQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=48PVjjVsWDU:e2SNacoWrqQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=48PVjjVsWDU:e2SNacoWrqQ:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=48PVjjVsWDU:e2SNacoWrqQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=48PVjjVsWDU:e2SNacoWrqQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=48PVjjVsWDU:e2SNacoWrqQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=48PVjjVsWDU:e2SNacoWrqQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=48PVjjVsWDU:e2SNacoWrqQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RogerAlsingWeblog/~4/48PVjjVsWDU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rogeralsing.com/2009/05/29/entity-framework-4-almost-poco/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ac3d313189eb4d3fe101e3aadcd08e2?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">roggan</media:title>
		</media:content>
	</item>
		<item>
		<title>Entity Framework 4 – Using Eager Loading</title>
		<link>http://rogeralsing.com/2009/05/24/entity-framework-4-using-eager-loading/</link>
		<comments>http://rogeralsing.com/2009/05/24/entity-framework-4-using-eager-loading/#comments</comments>
		<pubDate>Sun, 24 May 2009 15:36:10 +0000</pubDate>
		<dc:creator>Roger Alsing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[O/R Mapping]]></category>

		<guid isPermaLink="false">http://rogeralsing.com/?p=634</guid>
		<description><![CDATA[When Linq To Sql was released we were told that it did support eager loading.
Which was a bit misleading, it did allow us to fetch the data we wanted upfront, but it did so by issuing one database query per object in the result set.
That is, one query per collection per object, which is a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=634&subd=rogeralsing&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>When Linq To Sql was released we were told that it did support eager loading.<br />
Which was a bit misleading, it did allow us to fetch the data we wanted upfront, but it did so by issuing one database query per object in the result set.<br />
That is, one query per collection per object, which is a complete performance nightmare. (Ripple loading)</p>
<p>Now in Entity Framework 4, we can actually do <em>true</em> eager loading.<br />
EF4 will issue a single query that fetches the data for all the objects in a graph.<br />
This have been possible in other mappers for a long time, but I still think it is awesome that Microsoft have finally listened to the community and created a framework that from what I&#8217;ve seen so far, does exactly what we want.</p>
<p>So how do you use eager loading in EF4 ?</p>
<p>Eager loading is activated by calling <strong>&#8220;ObjectSet[of T].Include(&#8220;Details.Product&#8221;)&#8221;</strong>, that is, a dot separated property path.<br />
You can also call include multiple times if you want to load different paths in the same query.</p>
<p>There are also a few attempts out in the blog world to try to make it easier to deal with eager loading, e.g. by trying to remove the untyped string and use lambda expressions instead.</p>
<p>I personally don&#8217;t like the lambda approach since you can&#8217;t traverse a collection property that way; &#8220;Orders.Details.Product&#8221; , there is no way to write that as a short and simple lambda.</p>
<p>My own take on this is to use extension methods instead.<br />
I always use eager loading on my aggregates, so I want a simple way to tell my EF context to add the load spans for my aggregates when I issue a query.<br />
(Aggregates are about consistency, and Lazy Load causes consistency issues within the aggregate, so I try to avoid that)</p>
<p>Here is how I create my exstension methods for loading complete aggregates:</p>
<pre class="brush: csharp;">
public static class ContextExtensions
{
  public static ObjectQuery&lt;Order&gt;
           AsOrderAggregate(this ObjectSet&lt;Order&gt; self)
  {
    return self
        .Include(&quot;Details.ProductSnapshot&quot;)
        .Include(&quot;CustomerSnapshot&quot;);
  }
}
</pre>
<p>This makes it possible to use the load spans directly on my context without adding anything special to the context itself.<br />
(You can of course add this very same method inside your context if you want, I simply like small interfaces that I can extend from the outside)</p>
<p>This way, you can now issue a query using load spans like this:</p>
<pre class="brush: csharp;">
var orders = from order in context.OrderSet.AsOrderAggregate()
             select order;
</pre>
<p>And if you want to make a projection query you can simply drop the &#8220;AsOrderAggregate&#8221; and fetch what you want.</p>
<p>HTH.</p>
<p>//Roger</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rogeralsing.wordpress.com/634/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rogeralsing.wordpress.com/634/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rogeralsing.wordpress.com/634/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rogeralsing.wordpress.com/634/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rogeralsing.wordpress.com/634/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rogeralsing.wordpress.com/634/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rogeralsing.wordpress.com/634/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rogeralsing.wordpress.com/634/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rogeralsing.wordpress.com/634/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rogeralsing.wordpress.com/634/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=634&subd=rogeralsing&ref=&feed=1" /></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=3WcL3jaYkSE:P3lzYovAQdQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=3WcL3jaYkSE:P3lzYovAQdQ:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=3WcL3jaYkSE:P3lzYovAQdQ:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=3WcL3jaYkSE:P3lzYovAQdQ:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=3WcL3jaYkSE:P3lzYovAQdQ:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=3WcL3jaYkSE:P3lzYovAQdQ:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=3WcL3jaYkSE:P3lzYovAQdQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=3WcL3jaYkSE:P3lzYovAQdQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=3WcL3jaYkSE:P3lzYovAQdQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RogerAlsingWeblog/~4/3WcL3jaYkSE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rogeralsing.com/2009/05/24/entity-framework-4-using-eager-loading/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ac3d313189eb4d3fe101e3aadcd08e2?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">roggan</media:title>
		</media:content>
	</item>
		<item>
		<title>Entity Framework 4 – Managing inverse properties</title>
		<link>http://rogeralsing.com/2009/05/22/entity-framework-4-managing-inverse-properties/</link>
		<comments>http://rogeralsing.com/2009/05/22/entity-framework-4-managing-inverse-properties/#comments</comments>
		<pubDate>Fri, 22 May 2009 10:04:28 +0000</pubDate>
		<dc:creator>Roger Alsing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[O/R Mapping]]></category>

		<guid isPermaLink="false">http://rogeralsing.com/?p=621</guid>
		<description><![CDATA[[EDIT]
I was wrong!
It is perfectly possible to do one directional associations in EF4 and POCO mode.
You simply have to manually remove the &#8220; &#60;NavigationProperty ..&#8221; tags from your mapping files.
Awesome work EF4 design team :-)
[/EDIT]
Original post:
To my surprise I&#8217;ve found out that Entity Framework 4 don&#8217;t support one directional collection properties.
That is, if you have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=621&subd=rogeralsing&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>[EDIT]<br />
I was wrong!</strong></p>
<p><strong>It is perfectly possible to do one directional associations in EF4 and POCO mode.<br />
You simply have to manually remove the &#8220;<em> &lt;NavigationProperty </em>..&#8221; tags from your mapping files.</strong></p>
<p><strong>Awesome work EF4 design team :-)</strong></p>
<p><strong>[/EDIT]</strong></p>
<p><strong>Original post:</strong><br />
To my surprise I&#8217;ve found out that Entity Framework 4 don&#8217;t support one directional collection properties.<br />
That is, if you have the entity &#8220;Order&#8221; which has an &#8220;Details&#8221; property, then the &#8220;OrderDetail&#8221; entity _<em>must</em>_ have an &#8220;Order&#8221; property.</p>
<p>To make things worse, those properties do not have any auto sync mechanism if you are using POCO entities.<br />
They could very well have supported this by adding an inverse management aspect to their run-time proxies that they use for lazy loading in POCO.</p>
<p>While I do think this is a lacking feature, it is not really a show stopper for me.<br />
We can work around this problem by applying the &#8220;Law of Demeter&#8221; principle.</p>
<p>We can design our entities like this:</p>
<p><strong>OrderDetail:</strong></p>
<pre class="brush: csharp;">
public class OrderDetail
{
    ...properties...

    [Obsolete(&quot;For EF4 Only!&quot;,true)]
    public OrderDetail()
    { }

    public OrderDetail(Order order)
    {
        this.Order = order;
    }
}
</pre>
<p><strong>Order:</strong></p>
<pre class="brush: csharp;">
public class Order
{
    ...properties...

    public void AddProduct(Product product,
                                   double quantity,
                                   double itemPrice)
    {
        var detail = new OrderDetail(this)
        {

//offtopic: you might want to associate
//the product via ID or via a snapshot instead
//depending on how you deal with cross aggregate references
            Product = product,

            Quantity = quantity,
            ItemPrice = itemPrice,
        };

        Details.Add(detail);
    }
}
</pre>
<p>This way, we get a whole bunch of positive effects:</p>
<p>We solve the problem with inverse properties, inverse management is handled inside the &#8220;AddProduct&#8221; method in the order.</p>
<p>We get a nice way to handle consistency in our aggregate roots, the methods can easily update any accumulated values in the order or change status of the order when we add or remove order details.<br />
This is what aggregates in DDD is all about so you should probably do this anyway, regardless if EF4 did support inverse property management or not.</p>
<p>We add domain semantics to the model, &#8220;AddProduct&#8221; or &#8220;ChangeQuantity&#8221; have meaning in our model, thus we get a more self explaining model.</p>
<p>This is a quite nice example of how lacking framework features can force you to write better code.<br />
If we did have support for inverse property management, we might get sloppy and just go the path of least resistance.</p>
<p>//Roger</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rogeralsing.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rogeralsing.wordpress.com/621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rogeralsing.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rogeralsing.wordpress.com/621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rogeralsing.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rogeralsing.wordpress.com/621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rogeralsing.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rogeralsing.wordpress.com/621/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rogeralsing.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rogeralsing.wordpress.com/621/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=621&subd=rogeralsing&ref=&feed=1" /></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Md8GVQuU7pQ:BM0oqyac4LI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Md8GVQuU7pQ:BM0oqyac4LI:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=Md8GVQuU7pQ:BM0oqyac4LI:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Md8GVQuU7pQ:BM0oqyac4LI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Md8GVQuU7pQ:BM0oqyac4LI:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=Md8GVQuU7pQ:BM0oqyac4LI:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Md8GVQuU7pQ:BM0oqyac4LI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=Md8GVQuU7pQ:BM0oqyac4LI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Md8GVQuU7pQ:BM0oqyac4LI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RogerAlsingWeblog/~4/Md8GVQuU7pQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rogeralsing.com/2009/05/22/entity-framework-4-managing-inverse-properties/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ac3d313189eb4d3fe101e3aadcd08e2?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">roggan</media:title>
		</media:content>
	</item>
		<item>
		<title>Entity Framework 4 – Where Entity.Id in Array</title>
		<link>http://rogeralsing.com/2009/05/21/entity-framework-4-where-entity-id-in-array/</link>
		<comments>http://rogeralsing.com/2009/05/21/entity-framework-4-where-entity-id-in-array/#comments</comments>
		<pubDate>Thu, 21 May 2009 19:06:54 +0000</pubDate>
		<dc:creator>Roger Alsing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[O/R Mapping]]></category>

		<guid isPermaLink="false">http://rogeralsing.com/?p=618</guid>
		<description><![CDATA[Here is a little trick if you want to issue a query to the database and select a batch of entities by ID only:

//assemble an array of ID values
int[] customerIds= new int[] { 1, 2, 3 };

var customers = from customer in context.CustomerSet
             [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=618&subd=rogeralsing&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Here is a little trick if you want to issue a query to the database and select a batch of entities by ID only:</p>
<pre class="brush: csharp;">
//assemble an array of ID values
int[] customerIds= new int[] { 1, 2, 3 };

var customers = from customer in context.CustomerSet
                where customerIds.Contains(customer.Id)
                select customer;
</pre>
<p>This will make Entity Framework issue an SQL query with the where clause <strong>&#8220;where customerId in (1,2,3)&#8221;</strong>, and thus, you can batch select specific entities with a single query.<br />
I will get back to this idea in a later post because this is related to how I design my entities and aggregates in DDD.</p>
<p>//Rogerr</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rogeralsing.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rogeralsing.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rogeralsing.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rogeralsing.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rogeralsing.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rogeralsing.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rogeralsing.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rogeralsing.wordpress.com/618/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rogeralsing.wordpress.com/618/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rogeralsing.wordpress.com/618/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=618&subd=rogeralsing&ref=&feed=1" /></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=rJbMK877N4s:nal16zCgX8o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=rJbMK877N4s:nal16zCgX8o:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=rJbMK877N4s:nal16zCgX8o:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=rJbMK877N4s:nal16zCgX8o:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=rJbMK877N4s:nal16zCgX8o:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=rJbMK877N4s:nal16zCgX8o:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=rJbMK877N4s:nal16zCgX8o:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=rJbMK877N4s:nal16zCgX8o:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=rJbMK877N4s:nal16zCgX8o:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RogerAlsingWeblog/~4/rJbMK877N4s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rogeralsing.com/2009/05/21/entity-framework-4-where-entity-id-in-array/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ac3d313189eb4d3fe101e3aadcd08e2?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">roggan</media:title>
		</media:content>
	</item>
		<item>
		<title>Entity Framework 4 – Immutable Value Objects</title>
		<link>http://rogeralsing.com/2009/05/21/entity-framework-4-immutable-value-objects/</link>
		<comments>http://rogeralsing.com/2009/05/21/entity-framework-4-immutable-value-objects/#comments</comments>
		<pubDate>Thu, 21 May 2009 16:27:38 +0000</pubDate>
		<dc:creator>Roger Alsing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[O/R Mapping]]></category>

		<guid isPermaLink="false">http://rogeralsing.com/?p=615</guid>
		<description><![CDATA[Ok, the title is not quire accurate, I&#8217;m not aware of any way to accomplish truly immutable types for Entity Framework.
However, this is a quite nice attempt IMO:

    public class Address
    {
        //Private setters to avoid external changes
      [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=615&subd=rogeralsing&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Ok, the title is not quire accurate, I&#8217;m not aware of any way to accomplish truly immutable types for Entity Framework.</p>
<p>However, this is a quite nice attempt IMO:</p>
<pre class="brush: csharp;">
    public class Address
    {
        //Private setters to avoid external changes
        public string StreetName { get;private set; }
        public string City { get; private set; }
        public string ZipCode { get; private set; }

        //Provide a default ctor for EF4
        [Obsolete(&quot;For EF4 Only&quot;,true)]
        public Address() {}

        //Force values to be set via ctor.
        public Address(string streetName, string city, string zipCode)
        {
            StreetName = streetName;
            City = city;
            ZipCode = zipCode;
        }

        ...equality overrides and such...
    }
</pre>
<p>This works very well with Entity Framework 4 and I think it is a fair compromise.</p>
<p>//Roger</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rogeralsing.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rogeralsing.wordpress.com/615/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rogeralsing.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rogeralsing.wordpress.com/615/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rogeralsing.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rogeralsing.wordpress.com/615/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rogeralsing.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rogeralsing.wordpress.com/615/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rogeralsing.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rogeralsing.wordpress.com/615/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=615&subd=rogeralsing&ref=&feed=1" /></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=GoQzOsomZ0c:eN5vVB2G104:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=GoQzOsomZ0c:eN5vVB2G104:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=GoQzOsomZ0c:eN5vVB2G104:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=GoQzOsomZ0c:eN5vVB2G104:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=GoQzOsomZ0c:eN5vVB2G104:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=GoQzOsomZ0c:eN5vVB2G104:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=GoQzOsomZ0c:eN5vVB2G104:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=GoQzOsomZ0c:eN5vVB2G104:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=GoQzOsomZ0c:eN5vVB2G104:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RogerAlsingWeblog/~4/GoQzOsomZ0c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rogeralsing.com/2009/05/21/entity-framework-4-immutable-value-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ac3d313189eb4d3fe101e3aadcd08e2?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">roggan</media:title>
		</media:content>
	</item>
		<item>
		<title>Entity Framework 4 – First Impressions</title>
		<link>http://rogeralsing.com/2009/05/21/entity-framework-4-first-impressions/</link>
		<comments>http://rogeralsing.com/2009/05/21/entity-framework-4-first-impressions/#comments</comments>
		<pubDate>Thu, 21 May 2009 15:24:06 +0000</pubDate>
		<dc:creator>Roger Alsing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[O/R Mapping]]></category>

		<guid isPermaLink="false">http://rogeralsing.com/?p=607</guid>
		<description><![CDATA[I downloaded the VS 2010 beta1 today and started to dissect Entity Framework 4.
(Also note the name: EF4.. The new name comes from .NET _4_ )
Here are my initial impressions:
POCO Support.
POCO comes in two flavors: Run-time POCO and Compile-time POCO:
Run-time POCO is where the framework interact with your POCO types directly.
This approach lacks support for lazy load [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=607&subd=rogeralsing&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I downloaded the VS 2010 beta1 today and started to dissect Entity Framework 4.</p>
<p>(Also note the name: EF4.. The new name comes from .NET _4_ )</p>
<p>Here are my initial impressions:</p>
<h3>POCO Support.</h3>
<p>POCO comes in two flavors: Run-time POCO and Compile-time POCO:</p>
<p>Run-time POCO is where the framework interact with your POCO types directly.<br />
This approach lacks support for lazy load due to the fact that there is no lazy loading code in your POCO classes.</p>
<p>Compile-time POCO is where the framework create run-time proxies on-top of your POCO entities and applies the lazy load support inside the proxies.<br />
Thus, the entities used in run-time are far from POCO.</p>
<p>The POCO support also include ValueObjects, so you can map POCO value objects too.</p>
<p>I&#8217;m very pleased to see that these features are finally coming.<br />
(We did have this in NPersist in 2005, but it&#8217;s still nice ;-) )<br />
 </p>
<h3>Lacking support for extensibility.</h3>
<p>This was both expected and sad to see at the same time.<br />
The entire materialization mechanism of EF4 is black boxed, there are no extension points at all.</p>
<p>I was hoping for some materialization event where one could override the default object creation process.<br />
And thus apply more behaviors to the objects, e.g. via NAspect and proxify the entities even more.</p>
<p>(This feature is missing from Linq to Sql and MEF too, so I wasn&#8217;t too surprised)</p>
<p> </p>
<h3>Design by Contract .</h3>
<p>This is not really a feature of EF4 but rather of .NET framework 4.<br />
But as far as I can tell, contracts <span style="text-decoration:underline;">seems</span> to work fine within your entities.<br />
I still need to do a few more tests and see how well the contracts play with the run-time proxies though.<br />
More to come in later posts&#8230;</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rogeralsing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rogeralsing.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rogeralsing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rogeralsing.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rogeralsing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rogeralsing.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rogeralsing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rogeralsing.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rogeralsing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rogeralsing.wordpress.com/607/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=607&subd=rogeralsing&ref=&feed=1" /></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=HrSrzYvE1Vo:xL-pUv6rtbY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=HrSrzYvE1Vo:xL-pUv6rtbY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=HrSrzYvE1Vo:xL-pUv6rtbY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=HrSrzYvE1Vo:xL-pUv6rtbY:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=HrSrzYvE1Vo:xL-pUv6rtbY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=HrSrzYvE1Vo:xL-pUv6rtbY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=HrSrzYvE1Vo:xL-pUv6rtbY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=HrSrzYvE1Vo:xL-pUv6rtbY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=HrSrzYvE1Vo:xL-pUv6rtbY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RogerAlsingWeblog/~4/HrSrzYvE1Vo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rogeralsing.com/2009/05/21/entity-framework-4-first-impressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ac3d313189eb4d3fe101e3aadcd08e2?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">roggan</media:title>
		</media:content>
	</item>
		<item>
		<title>Pleasing the O/R Mapper – Default Constructor Hack</title>
		<link>http://rogeralsing.com/2009/05/11/pleasing-the-or-mapper-default-constructor-hack/</link>
		<comments>http://rogeralsing.com/2009/05/11/pleasing-the-or-mapper-default-constructor-hack/#comments</comments>
		<pubDate>Mon, 11 May 2009 17:02:05 +0000</pubDate>
		<dc:creator>Roger Alsing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[O/R Mapping]]></category>

		<guid isPermaLink="false">http://rogeralsing.com/?p=595</guid>
		<description><![CDATA[Hack of the day:
Most O/R mapper frameworks require entities to implement a default constructor.
This is most often not a big deal, most entities would support a default constructor no matter if the mapper needed it or not.
However, if you intend to expose an entity that only support a copy constructor.
Let&#8217;s say for the sake of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=595&subd=rogeralsing&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2>Hack of the day:</h2>
<p>Most O/R mapper frameworks require entities to implement a default constructor.</p>
<p>This is most often not a big deal, most entities would support a default constructor no matter if the mapper needed it or not.<br />
However, if you intend to expose an entity that only support a copy constructor.</p>
<p>Let&#8217;s say for the sake of the argument that we have an immutable &#8220;Order&#8221; class, that can only be created by passing an &#8220;OrderRequest&#8221;:</p>
<pre class="brush: csharp;">
public class Order
{
   public Order( OrderRequest request )
   {
         this.Customer = request.Customer;
         this.Details =
                  request
                  .Details
                  .Select ( requestDetail =&gt; 
                                new OrderDetail (requestDetail))
                  .ToList();
         ...
   }

   ....
}
</pre>
<p>No matter if the order is only partially or completely immutable, you want to force developers to instantiate the order by the above constructor.</p>
<p>But if the mapper forces you to supply a default constructor, then you can no longer guarantee that the class is used correctly.</p>
<p>So what can be done about it?</p>
<p>I&#8217;ve come up with a hack that prevents this problem, but be aware that this is a pure hack ;-)<br />
You can provide a default constructor and mark it with the &#8220;Obsolete&#8221; attribute.</p>
<p>This can prevent your code from calling the constructor while allowing the mapper framework to create the entities through reflection (which most POCO mappers already do).<br />
You can even separate out this hack code from the actual entity using partial classes:</p>
<pre class="brush: csharp;">
public partial class Order
{
   public Order( OrderRequest request )
   {
         this.Customer = request.Customer;
         this.Details =
                  request
                  .Details
                  .Select ( requestDetail =&gt; 
                                new OrderDetail (requestDetail))
                  .ToList();
         ...
   }

   ....
}

--other file--

public partial class Order
{
     [Obsolete(&quot;For NHibernate use only!&quot;,true)]
     public Order() {}
}
</pre>
<p>To keep the hack code away and a safe distance from the actual implementation you could place all those partial default ctor snippets in a separate file or so.</p>
<p>e.g. &#8220;NHibernateHack.cs&#8221; or something similair.</p>
<p>Also note that for NHibernate, there are other options see: <a href="http://nhforge.org/blogs/nhibernate/archive/2008/12/12/entities-behavior-injection.aspx">http://nhforge.org/blogs/nhibernate/archive/2008/12/12/entities-behavior-injection.aspx</a></p>
<p>But this trick is applies to any mapper that creates instances through reflection and requires a default ctor.<br />
Or maybe this is an old trick, but it does fill it&#8217;s purpose.</p>
<p>//Roger</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rogeralsing.wordpress.com/595/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rogeralsing.wordpress.com/595/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rogeralsing.wordpress.com/595/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rogeralsing.wordpress.com/595/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rogeralsing.wordpress.com/595/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rogeralsing.wordpress.com/595/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rogeralsing.wordpress.com/595/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rogeralsing.wordpress.com/595/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rogeralsing.wordpress.com/595/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rogeralsing.wordpress.com/595/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rogeralsing.com&blog=2473556&post=595&subd=rogeralsing&ref=&feed=1" /></div><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Fox070IhSY8:-aUt7mMn8Bw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Fox070IhSY8:-aUt7mMn8Bw:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=Fox070IhSY8:-aUt7mMn8Bw:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Fox070IhSY8:-aUt7mMn8Bw:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Fox070IhSY8:-aUt7mMn8Bw:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=Fox070IhSY8:-aUt7mMn8Bw:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Fox070IhSY8:-aUt7mMn8Bw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?i=Fox070IhSY8:-aUt7mMn8Bw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?a=Fox070IhSY8:-aUt7mMn8Bw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/RogerAlsingWeblog?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RogerAlsingWeblog/~4/Fox070IhSY8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://rogeralsing.com/2009/05/11/pleasing-the-or-mapper-default-constructor-hack/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7ac3d313189eb4d3fe101e3aadcd08e2?s=96&amp;d=identicon&amp;r=G" medium="image">
			<media:title type="html">roggan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
