<?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>Udi Dahan - The Software Simplist &#187; Search Results  &#187;  fetching strategies</title>
	<atom:link href="http://www.udidahan.com/?s=fetching%20strategies&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.udidahan.com</link>
	<description>Enterprise Development Expert &#38; SOA Specialist</description>
	<lastBuildDate>Tue, 31 Aug 2010 09:56:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Don&#8217;t Create Aggregate Roots</title>
		<link>http://www.udidahan.com/2009/06/29/dont-create-aggregate-roots/</link>
		<comments>http://www.udidahan.com/2009/06/29/dont-create-aggregate-roots/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 11:52:37 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[DDD]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1042</guid>
		<description><![CDATA[
My previous post on Domain Events left some questions about how aggregate roots should be created unanswered. It would actually be more accurate to say how aggregate roots should *not* be created. It turns out that this is one of the less intuitive parts of domain-driven design and has been the source of many arguments [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/roots.jpg" alt="roots" title="roots" width="143" height="150"  style="border-right: 0px; border-top: 0px; margin: 0px 10px; border-left: 0px; border-bottom: 0px" align="right"/></p>
<p>My previous post on <a href="http://www.udidahan.com/2009/06/14/domain-events-salvation">Domain Events</a> left some questions about how aggregate roots should be created unanswered. It would actually be more accurate to say how aggregate roots should *not* be created. It turns out that this is one of the less intuitive parts of domain-driven design and has been the source of many arguments on the matter. Let&#8217;s start with the wrong way:</p>
<p>
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">using</span> (ISession s = sf.OpenSession())</pre>
<pre><span class="lnum">   2:  </span><span class="kwrd">using</span> (ITransaction tx = s.BeginTransaction())</pre>
<pre class="alt"><span class="lnum">   3:  </span>{</pre>
<pre><span class="lnum">   4:  </span>    Customer c = <span class="kwrd">new</span> Customer();</pre>
<pre class="alt"><span class="lnum">   5:  </span>    c.Name = <span class="str">"udi dahan"</span>;</pre>
<pre><span class="lnum">   6:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   7:  </span>    s.Save(c);</pre>
<pre><span class="lnum">   8:  </span>    tx.Commit();</pre>
<pre class="alt"><span class="lnum">   9:  </span>}</pre>
</div>
<p>I understand that the code above is representative of how much code is written when using an object-relational mapper. Many would consider this code to follow DDD principles &#8211; that Customer is an aggregate root. Unfortunately &#8211; that is not the case. The code above is missing the real aggregate root.</p>
<p>There&#8217;s also the inevitable question of validation &#8211; if the customer object isn&#8217;t willing to accept a name with a space in it, should we throw an exception? That would prevent an invalid entity from being saved, which is good. On the other hand, exceptions should be reserved for truly exceptional occurrences. But if we don&#8217;t use exceptions, using Domain Events instead, how do we prevent the invalid entity from being saved?</p>
<p>All of these issues are handled auto-magically once we have a true aggregate root.</p>
<h3>Always Get An Entity</h3>
<p>Let&#8217;s start with the technical guidance &#8211; always get an entity. At least one. Also, don&#8217;t add any objects to the session or unit of work explicitly &#8211; rather, have some other already persistent domain entity create the new entity and add it to a collection property.</p>
<p>Looking at the code above, we see that we&#8217;re not following the technical guidance.</p>
<p>But the question is, which entity could we possibly get from the database in this case? All we&#8217;re doing is adding a customer.</p>
<p>And that&#8217;s exactly where the technical guidance leads us to the business analysis that was missing in this scenario&#8230;</p>
<h3>Business Analysis</h3>
<p>Customers don&#8217;t just appear out of thin air.</p>
<p>Blindingly obvious &#8211; isn&#8217;t it.</p>
<p>So why would we technically model our system as if they did? My guess is that we never really thought about it &#8211; it wasn&#8217;t our job. So here&#8217;s the breaking news &#8211; if we want to successfully apply DDD we do need to think about it, it is our job.</p>
<p>Going back to the critical business question:</p>
<p>Where do customers come from?</p>
<p>In the real world, they stroll into the store. In our overused e-commerce example, they navigate to our website. New customers that haven&#8217;t used our site before don&#8217;t have any cookies or anything we can identify them with. They navigate around, browsing, maybe buying something in the end, maybe not.</p>
<p>Yet, the browsing process is interesting in its own right:</p>
<ul>
<li>Which products did they look at? </li>
<li>Did they use the search feature? </li>
<li>How long did they spend on each page? </li>
<li>Did they scroll down to see the reviews?</li>
</ul>
<p>If and when they do finally buy something, all that history is important and we&#8217;d like to maintain a connection to it.</p>
<p>Actually, even before they buy something, what they put in their cart is the interesting piece. The transition from cart to checkout is another interesting piece. Do they actually complete the checkout process, or do they abandon it midway through?</p>
<p>Add to that when we ask/force them to create a user/login in our system.</p>
<p>Are they actually a customer if they haven&#8217;t bought anything?</p>
<p>We&#8217;re beginning to get an inkling that almost every activity that results in the creation of an entity or storing of additional information can be traced to a transition from a previous business state.</p>
<p>In any transition, the previous state is the aggregate root.</p>
<h3>In the beginning&#8230;</h3>
<p>Let&#8217;s start at the very beginning then &#8211; someone came to our site. Either they navigated here from some other web page, they clicked on an email link someone sent them, or they typed in our URL. This can be designed as follows:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">using</span> (ISession s = sf.OpenSession())</pre>
<pre><span class="lnum">   2:  </span><span class="kwrd">using</span> (ITransaction tx = s.BeginTransaction())</pre>
<pre class="alt"><span class="lnum">   3:  </span>{</pre>
<pre><span class="lnum">   4:  </span>   var referrer = s.Get&lt;Referrer&gt;(msg.URL);</pre>
<pre class="alt"><span class="lnum">   5:  </span>   referrer.BroughtVisitorWithIp(msg.IpAddress);</pre>
<pre><span class="lnum">   6:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   7:  </span>   tx.Commit();</pre>
<pre><span class="lnum">   8:  </span>}</pre>
<pre class="alt"><span class="lnum">   9:  </span>&nbsp;</pre>
</div>
<p>And our referrer code could look something like this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">void</span> BroughtVisitorWithIp(<span class="kwrd">string</span> ipAddress)</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>   <span class="kwrd">var</span> visitor = <span class="kwrd">new</span> Visitor(ipAddress);</pre>
<pre><span class="lnum">   4:  </span>   <span class="kwrd">this</span>.NewVisitors.Add(visitor);</pre>
<pre class="alt"><span class="lnum">   5:  </span>}</pre>
<pre><span class="lnum">   6:  </span>&nbsp;</pre>
</div>
<p>This follows the technical guidance we saw at the beginning.</p>
<p>It also allows us to track which referrer is bringing us which visitors, through tracking those visitors as they become shoppers (by putting stuff in their cart), finally seeing which become customers.</p>
<p>We can solve the situation of not having a referrer by implementing the null object pattern which is well supported by all the standard object-relational mappers these days.</p>
<h3>How it works internally</h3>
<p>When we call a method on a persistent entity retrieved by the object-relational mapper, and the entity modifies its state like when it adds a new entity to one of its collection properties, when the transaction commits, here&#8217;s what happens:</p>
<p>The mapper sees that the persistent entity is dirty, specifically, that its collection property was modified, and notices that there is an object in there that isn&#8217;t persistent. At that point, the mapper knows to persist the new entity without us ever having to explicitly tell it to do so. This is sometimes known as &#8220;persistence by reachability&#8221;.</p>
<h3>Where validation happens</h3>
<p>Let&#8217;s consider the relatively trivial rule that says that a user name can&#8217;t contain a space.</p>
<p>Also, keep in mind that a registered user is the result of a transition from a visitor.</p>
<p>Here&#8217;s *one* way of doing that:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">class</span> Visitor</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>   <span class="kwrd">public</span> <span class="kwrd">void</span> Register(<span class="kwrd">string</span> username, <span class="kwrd">string</span> password)</pre>
<pre><span class="lnum">   4:  </span>   {</pre>
<pre class="alt"><span class="lnum">   5:  </span>      <span class="kwrd">if</span> (username.Contains(<span class="str">" "</span>))</pre>
<pre><span class="lnum">   6:  </span>      {</pre>
<pre class="alt"><span class="lnum">   7:  </span>         DomainEvents.Raise&lt;UsernameCantContainSpace&gt;();</pre>
<pre><span class="lnum">   8:  </span>         <span class="kwrd">return</span>;</pre>
<pre class="alt"><span class="lnum">   9:  </span>      }</pre>
<pre><span class="lnum">  10:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  11:  </span>      <span class="kwrd">var</span> user = <span class="kwrd">new</span> User(username, password);</pre>
<pre><span class="lnum">  12:  </span>      <span class="kwrd">this</span>.RegisteredUser = u;</pre>
<pre class="alt"><span class="lnum">  13:  </span>   }</pre>
<pre><span class="lnum">  14:  </span>}</pre>
<pre class="alt"><span class="lnum">  15:  </span>&nbsp;</pre>
</div>
<p>This actually isn&#8217;t representative of most of the rules that will be found in the domain model, but it illustrates a way of preventing an entity from being created without our service layer needing to know anything. All the service layer does is get the visitor object and call the Register method.</p>
<p>Validation of string lengths, data ranges, etc is not domain logic and is best handled elsewhere (and a topic for a different post). The same goes for uniqueness.</p>
<h3>Summary</h3>
<p>The most important thing to keep in mind is that if your service layer is newing up some entity and saving it &#8211; that entity isn&#8217;t an aggregate root *in that use case*. As we saw above, in the original creation of the Visitor entity by the Referrer, the visitor class wasn&#8217;t the aggregate root. Yet, in the user registration use case, the Visitor entity was the aggregate root.</p>
<p>Aggregate roots aren&#8217;t a structural property of the domain model.</p>
<p>And in any case, don&#8217;t go saving entities in your service layer &#8211; let the domain model manage its own state. The domain model doesn&#8217;t need any references to repositories, services, units of work, or anything else to manage its state.</p>
<p>If you do all this, you&#8217;ll also be able to harness the technique of fetching strategies to get the best performance out of your domain model by representing your use cases as interfaces on the domain model like IRegisterUsers (implemented by Visitor) and IBringVisitors (implemented by Referrer).</p>
<p>And spending some time on business analysis doesn&#8217;t hurt either &#8211; unless customers really do fall out of the sky in your world <img src='http://www.udidahan.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/06/29/dont-create-aggregate-roots/feed/</wfw:commentRss>
		<slash:comments>72</slash:comments>
		</item>
		<item>
		<title>First time here?</title>
		<link>http://www.udidahan.com/first-time-here/</link>
		<comments>http://www.udidahan.com/first-time-here/#comments</comments>
		<pubDate>Sun, 09 Dec 2007 23:04:37 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/first-time-here/</guid>
		<description><![CDATA[Hi there. I&#8217;m Udi. Welcome to my blog.
After blogging since 2003, with over 500 posts I&#8217;ve pulled together the &#8220;best of the best&#8221; for you in the top 3 areas in which my clients use my expertise. Click to skip the introductions.
[ SOA - Domain Models - Smart Clients ]
Service-Oriented Architecture
Before you roll your eyes [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://udidahan.weblogs.us/wp-content/uploads/udi_dahan_headshot.jpg" style="border: 0px none ; margin: 0px 40px" align="right" height="200" width="170" />Hi there. I&#8217;m Udi. Welcome to my blog.</p>
<p>After blogging since 2003, with over 500 posts I&#8217;ve pulled together the &#8220;best of the best&#8221; for you in the top 3 areas in which my clients use my expertise. Click to skip the introductions.<br />
[ <a href="/first-time-here/#soa">SOA</a> - <a href="/first-time-here/#domain_model">Domain Models</a> - <a href="/first-time-here/#smart_client">Smart Clients </a>]</p>
<h3>Service-Oriented Architecture</h3>
<p>Before you roll your eyes at me, believe me I do know that SOA has been hyped to death and that you&#8217;re probably sick of it. Me too. I&#8217;m interested in actually <strong>implementing </strong>SOA, not just talking about it &#8211; it&#8217;s actually what I do for a living; from high-level architecture, to detailed design, to choosing frameworks and technologies, and actually building the thing.</p>
<p>What I&#8217;ve got online includes podcasts on SOA, webcasts, books I&#8217;ve contributed to, full-lenth articles, and open-source frameworks I&#8217;ve written to help developers do the right thing by default.  So, if you&#8217;d like to hear more about the practical aspects of <strong>doing </strong>SOA, you can jump to the relevant part of this page by <a href="/first-time-here/#soa">clicking here</a>.</p>
<h3>Domain Models</h3>
<p>You won&#8217;t find much of the more theoretical aspects of the domain model pattern or other patterns of application architecture &#8211; rather you&#8217;ll find presentations I&#8217;ve given on the subject, samples, and even an extension to NHibernate. All of these are derived from the work I&#8217;ve done around using these patterns &#8211; not the least of which is actually making them <strong>performant</strong>.</p>
<p>Also on the topic of Domain Models and Data Access, you&#8217;ll be able to find information on writing highly <strong>concurrent</strong> systems. This includes an object-oriented development style for service layers that yeild the best <strong>multi-user</strong> throughput. Jump to the Domain Model section <a href="/first-time-here/#domain_model">here</a>.</p>
<h3>Smart / Rich / Occasionally Connected Client</h3>
<p>I know that Microsoft has been pretty active here, putting out the Composite User Interface Application Block (CAB) and the Smart Client Software Factory (SCSF). The only thing is that those are both complex and not thread-safe &#8211; I know, I&#8217;ve been burned.</p>
<p>So, here you can find the set of patterns, technologies, and frameworks that make for a thread-safe, multi-threaded client that a team can develop in parallel. Jump to the Smart Client section <a href="/first-time-here/#smart_client">here</a>.</p>
<hr size="1" /><a title="soa" name="soa"></a></p>
<h3><a title="soa" name="soa"></a>Doing SOA</h3>
<p><a title="soa" name="soa"></a>If you&#8217;re tasked with architecting, designing, or implement a Service-Oriented Architecture then the resources below should help you get to the bottom line of all the theory and get you going in terms of actual development.</p>
<p><a title="soa" name="soa"></a><u>Podcasts:</u></p>
<ul>
<li><a href="http://udidahan.weblogs.us/2006/05/26/podcast-does-soa-mean-the-end-of-oo/">Does SOA mean the end of OO?</a></li>
<li><a href="http://udidahan.weblogs.us/2007/12/05/podcast-migrating-from-n-tier-to-soa/">Migrating from N-Tier to SOA</a></li>
<li><a href="http://udidahan.weblogs.us/2006/06/02/podcast-does-an-soa-require-an-esb/">Does an SOA require an ESB?</a></li>
</ul>
<blockquote><p>If you&#8217;d like to more of these kinds of podcasts, check out the <a href="http://udidahan.weblogs.us/category/ask-udi-podcast/">&#8220;Ask Udi&#8221; podcast.</a></p></blockquote>
<p>I&#8217;ve also be interviewed on SOA on several other <a href="http://udidahan.weblogs.us/podcasts/">podcasts</a>:</p>
<ul>
<li><a href="http://dotnetrocks.com/default.aspx?showID=268">.NET Rocks interview on SOA</a></li>
<li><a href="http://channel9.msdn.com/ShowPost.aspx?PostID=268866">Channel 9&#8217;s Ron Jacobs interview on SOA and Workflow</a></li>
</ul>
<p><u>Webcasts:</u></p>
<ul>
<li>SOA Distilled for UML China [<a href="http://udidahan.weblogs.us/wp-content/uploads/soa_distilled_uml_china.pdf">PDF</a>] [<a href="ed2k://|file|UMLChina%E8%AE%B2%E5%BA%A7%E5%BD%95%E9%9F%B3%E5%8F%8A%E5%B9%BB%E7%81%AF20070613UdiDahanSOA%E7%B2%BE%E7%B2%B9.rar|88323477|39201895A2D613FDD66AAE4CBDEFF3A5|h=TAYDKISS4DQI455ZRUFFXOGMJCU7MGZM|/">Audio</a> - 90MB via EMule]</li>
<li>Why you can&#8217;t do SOA without Message [<a href="http://msevents.microsoft.com/cui/WebCastEventDetails.aspx?EventID=1032273610&amp;EventCategory=5&amp;culture=en-US&amp;CountryCode=US">TechEd USA 2005</a>]</li>
</ul>
<p><u>Books:</u></p>
<ul>
<li><a href="http://www.amazon.com/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1197275327&amp;sr=8-1">Applying Domain-Driven Design and Patterns</a> [Chapter] <a href="http://www.jnsk.se/weblog/posts/GuestAuthors.htm">An introduction to SOA</a></li>
<li><a href="http://www.soa-expertenwissen.de/"> SOA Expertise &#8211; Methods, Concept, &amp; Practice</a> [Article] <a href="http://udidahan.weblogs.us/2007/04/28/the-enterprise-service-bus-and-your-soa/">The ESB and Your SOA—A Great Team</a></li>
</ul>
<p><u>Full-length Articles:</u></p>
<ul>
<li><a href="http://msdn2.microsoft.com/en-us/arcjournal/bb245672">Autonomous Services and Enterprise Entity Aggregation</a> [MS Architecture Journal]</li>
<li><a href="http://www.devx.com/ibm/Article/29325">SOA Support, Therapy, and Treatment </a>[IBM DevX Portal]</li>
</ul>
<p><u>Open Source Frameworks:</u></p>
<ul>
<li><a href="http://udidahan.weblogs.us/category/nservicebus">nServiceBus</a> &#8211; communications framework for making enterprise .NET systems easier to build</li>
</ul>
<hr size="1" /><a title="domain_model" name="domain_model"></a></p>
<h3><a title="domain_model" name="domain_model"></a>Using Domain Models</h3>
<p>If you&#8217;re looking for how to implement business logic and data access in your application, then you&#8217;ve come to the right place. Here you&#8217;ll find the patterns, design, and code that will make your business logic cohesive, your data access simpler, and keep everything performing at its peak.</p>
<p><u>Introductory info:</u></p>
<ul>
<li><a href="http://udidahan.weblogs.us/2007/04/21/domain-model-pattern/">What is a domain model?</a></li>
<li><a href="http://udidahan.weblogs.us/2006/11/09/dataset-%e2%80%93-or-mapping-rumble-at-teched-mvp-dinner/">Where should (and more importantly, shouldn&#8217;t) you use a domain model</a></li>
<li><a href="http://www.developer.com/design/article.php/3531871">Don&#8217;t tie your architecture to a specific object/relational mapping product</a> [Developer.com]</li>
</ul>
<p><u>Make it perform:</u></p>
<ul>
<li><a href="http://udidahan.weblogs.us/2007/04/23/fetching-strategy-design/">Fetching Strategy Design</a> &#8211; UML diagrams describing performance enhancing patterns</li>
<li><a href="http://udidahan.weblogs.us/2007/09/16/fetching-strategy-nhibernate-implementation-available/">Samples + NHibernate modifications for Fetching Strategies</a></li>
<li><a href="http://udidahan.weblogs.us/2007/10/26/teched-speaking-about-high-performance-persistent-domain-models/">Presentation on High-Performance, Persistent Domain Models</a> [TechEd Europe 2007]</li>
</ul>
<p><u>Concurrency for multi-user throughput:</u></p>
<ul>
<li><a href="http://www.developer.com/design/article.php/3705396">Concurrency for Occasionally Connected Systems</a> [Developer.com]</li>
<li><a href="http://udidahan.weblogs.us/2007/03/28/podcast-datasets-web-services/">Podcast on DataSets and Web Services</a> [ <a href="/ask-udi-podcast/">Ask Udi podcast</a> ]</li>
<li><a href="http://udidahan.weblogs.us/2007/01/22/realistic-concurrency/">Realistic Concurrency (difference between optimistic and pessimistic)</a></li>
</ul>
<hr size="1" /><a title="smart_client" name="smart_client"></a></p>
<h3>Multi-core efficient Smart Clients</h3>
<p>If you&#8217;re tasked with building a desktop application that communicates with a server, needs to be able to work offline, makes use of multiple cores for a better user experience, and can be developed without having to remember 5 books worth of theory, then this is for you.</p>
<p><u>Patterns:</u></p>
<ul>
<li><a href="http://udidahan.weblogs.us/2006/03/01/a-model-a-view-and-a-presenter-walk-into-a-bar/">Sample implementation of Model-View-Presenter pattern</a></li>
<li><a href="http://udidahan.weblogs.us/2007/01/16/thoughts-about-usability/">Unit-testing still critical to project success even when using the relevant patterns</a></li>
<li><a href="http://udidahan.weblogs.us/2007/04/30/generic-validation/">Generic validation using the service locator pattern</a></li>
</ul>
<p><u>Occasional Connectivity:</u><a href="http://udidahan.weblogs.us/2007/04/17/podcast-occasionally-connected-smart-clients-and-adonet-sync-services/"><br />
</a></p>
<ul>
<li><a href="http://udidahan.weblogs.us/2007/04/17/podcast-occasionally-connected-smart-clients-and-adonet-sync-services/">Podcast on occasionally connected smart clients</a> (and why ADO.NET Sync Services doesn&#8217;t help)</li>
<li><a href="http://udidahan.weblogs.us/2007/07/12/podcast-passing-data-between-layers-in-soa-modelsmart-client-application/">Passing data from the UI to SOA Services</a> [Podcast]</li>
<li><a href="http://www.developer.com/design/article.php/3708006">Threading issues when smart clients reconnect</a> [Developer.com article]</li>
</ul>
<p><u>Thread-Safety = Just-plain Safe</u></p>
<ul>
<li><a href="http://udidahan.weblogs.us/2007/09/28/objectbuilder-synchronization-features-needed-for-pubsub-ing-smart-clients/">Explanation of threading concerns when using dependency injection and asynchronous messaging</a></li>
<li><a href="http://udidahan.weblogs.us/2007/12/06/object-builder-the-place-to-fix-system-wide-threading-bugs/">Followup on dependency injection, threading capabilities</a></li>
<li><a href="http://udidahan.weblogs.us/2007/10/12/podcast-thread-safe-asynchronous-smart-clients/">Thread-safe, Asynchronous Smart Clients [podcast]</a></li>
<li><a href="http://udidahan.weblogs.us/2007/12/07/eureka-aop-is-the-final-piece-of-the-multi-threaded-smart-client-puzzle/">Prototype of Aspect-Oriented Programming to make views thread-safe by default</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/first-time-here/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fetching Strategy NHibernate Implementation Available</title>
		<link>http://www.udidahan.com/2007/09/16/fetching-strategy-nhibernate-implementation-available/</link>
		<comments>http://www.udidahan.com/2007/09/16/fetching-strategy-nhibernate-implementation-available/#comments</comments>
		<pubDate>Mon, 17 Sep 2007 00:54:02 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2007/09/16/fetching-strategy-nhibernate-implementation-available/</guid>
		<description><![CDATA[A couple of months ago I put out a post discussing one way to implement custom fetching strategies. Anyway, I finally got around to putting my money where my mouth was&#8230;
So, I&#8217;ve implemented the pattern in NHibernate, adding the following method to ISession:
T Create&#60;T&#62;();
As well as adding the following interface to the NHibernate package:

  [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago I put out a <a href="http://udidahan.weblogs.us/2007/04/23/fetching-strategy-design/">post discussing one way to implement custom fetching strategies</a>. Anyway, I finally got around to putting my money where my mouth was&#8230;</p>
<p>So, I&#8217;ve implemented the pattern in NHibernate, adding the following method to ISession:</p>
<div style="border: solid black 1px; background-color:beige; padding: 0em 1em; overflow:auto; width:550; font-family:courier">T Create&lt;T&gt;();</div>
<p>As well as adding the following interface to the NHibernate package:</p>
<div style="border: solid black 1px; background-color:beige; padding: 0em 1em; overflow:auto; width:550; font-family:courier">
    public interface IFetchingStrategy&lt;T&gt;<br />
    {<br />
        ICriteria AddFetchJoinTo(ICriteria criteria);<br />
    }
</div>
<p>All this enables you to have a stronger separation between your service layer classes and your domain model class, as well as for you to express each service-level use case as a domain concept &#8211; an interface.</p>
<p>Once you have such an interface, you can create a fetching strategy for that use case and define exactly how deep of an object graph you want to load so that you only hit the DB once for that use case.</p>
<p>The nice thing is that its all configured with Spring. In other words, if you the entry for your fetching strategy class exists, you get the improved performance, if it doesn&#8217;t, you don&#8217;t. All without touching your service layer classes.</p>
<p>Just as an example, when I&#8217;m in the use case modeled by &#8220;ICustomer&#8221;, I want to get all the customer&#8217;s orders, and their orderlines. This would be done by having a class like this:</p>
<div style="border: solid black 1px; background-color:beige; padding: 0em 1em; overflow:auto; width:550; font-family:courier">
    public class CustomerFetchingStrategy : IFetchingStrategy&lt;ICustomer&gt;<br />
    {<br />
        public ICriteria AddFetchJoinTo(ICriteria criteria)<br />
        {<br />
            criteria.SetFetchMode(&#8221;orders&#8221;, FetchMode.Eager).<br />
                SetFetchMode(&#8221;orders.orderLines&#8221;, FetchMode.Eager);</p>
<p>            return criteria;<br />
        }<br />
    }
</p></div>
<p>And the configuration would look like this (as a part of the regular spring template):</p>
<div style="border: solid black 1px; background-color:beige; padding: 0em 1em; overflow:auto; width:550; font-family:courier">
      &lt;object id=&#8221;CustomerFetchingStrategy&#8221; type=&#8221;Domain.Persistence.CustomerFetchingStrategy, Domain.Persistence&#8221; /&gt;
</div>
<p>If you want to take a look at the full solution, you can find it here. For some reason, the combined file was too big for the upload on my blog so it&#8217;s split into two. Unzip both packages into the same directory. You&#8217;ll find a file called &#8220;db_scripts.sql&#8221; which contains the schema for the DB. Don&#8217;t forget to update your connection string in the &#8220;hibernate.cfg.xml&#8221;. If you&#8217;re looking for the changes I made to the NHibernate source, you can find it in the &#8220;Updated NHibernate Files&#8221; directory. The only real change is to the &#8220;SessionImpl.cs&#8221; file.</p>
<p><a href='http://udidahan.weblogs.us/wp-content/uploads/libraries.zip' title='libraries.zip'>Relevant NHibernate and Spring binaries</a>.</p>
<p><a href='http://udidahan.weblogs.us/wp-content/uploads/objectrelationalmappingdemo4.zip' title='objectrelationalmappingdemo.zip'>Source code of example</a>.</p>
<p>BTW, there is some intelligent thread-safe caching going on in SessionImpl now so that you get a much smaller performance hit (in terms of code that uses reflection) on subsequent usages of the same interfaces.</p>
<p>Let me know what you think.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2007/09/16/fetching-strategy-nhibernate-implementation-available/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Fetching Strategy Design</title>
		<link>http://www.udidahan.com/2007/04/23/fetching-strategy-design/</link>
		<comments>http://www.udidahan.com/2007/04/23/fetching-strategy-design/#comments</comments>
		<pubDate>Mon, 23 Apr 2007 20:18:56 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2007/04/23/fetching-strategy-design/</guid>
		<description><![CDATA[Following up on my previous post on Better Domain-Driven Design Implementation, I wanted to show some more detail on how this actually works. There are two main concepts here.
The first is that of keeping Service Layer classes independent of Domain Model classes. The reason this is desirable is that the two families change on independent [...]]]></description>
			<content:encoded><![CDATA[<p>Following up on my previous post on <a href="http://udidahan.weblogs.us/2007/03/06/better-domain-driven-design-implementation/">Better Domain-Driven Design Implementation</a>, I wanted to show some more detail on how this actually works. There are two main concepts here.</p>
<p>The first is that of keeping Service Layer classes independent of <a href="http://udidahan.weblogs.us/2007/04/21/domain-model-pattern/">Domain Model</a> classes. The reason this is desirable is that the two families change on independent axis. Service Layer classes are affected by changes to the service&#8217;s external interface, as well as the interfaces of other services it depends on. Domain Model classes change to support changing business rules internal to the service. The solution is quite simply to introduce a set of interfaces between the two.</p>
<p>The second is tied to performance. When retrieving data from the database, we&#8217;d like to cross the wire only once bringing with us all the data we need in order to perform the work required. Lazy loading helps us in one way while hurting us in another. For connected objects that we don&#8217;t need when retrieving our target object, lazy loading prevents them from being loaded. However, for connected objects that we do need, lazy loading will cause us to return to the database to retrieve each of those objects in turn (sets of the same kind of object are still one DB call). If those objects need to be traversed as well in order to retrieve other objects, we can see that one simple request can cause many (MANY!) calls to the DB. These problems of latency and throughput are solved by eagerly (in one DB call) fetching and loading all the objects we need.</p>
<p>Here&#8217;s the package diagram for the solution so that we&#8217;ll have a reference for the following discussion.</p>
<p><a href="http://udidahan.weblogs.us/wp-content/uploads/ea2.png" target="_blank"><img src='http://udidahan.weblogs.us/wp-content/uploads/ea2.thumbnail.png' title='fetching strategy package diagram' alt='fetching strategy package diagram - opens in a new window' /></a></p>
<p>Now, the class that is best suited to issue this call to eagerly load all objects is the service layer class, since it is aware of what specific use case we&#8217;re in. However, the service layer class does not necessarily know exactly what classes will be used in the domain to handle that request. Obviously, this set of classes may change as the domain model and the database schema change. We can therefore say that is not the service layer class&#8217; responsibility to handle the definition of which classes need to be loaded. Only the Domain Model has that knowledge. So we need some way to pass the knowledge of the request type into the Domain Model.</p>
<p>In Object/Role Modeling terms, we represent that request type as a role. And roles are represented by interfaces. So we create an interface in the Domain Model which represents the request type. That interface will most likely contain only one method &#8211; the method which the service layer class will call.</p>
<p>After we have the first role, we can build other things around it, like fetching strategies. We can then define other classes who fullfil the role of &#8220;I&#8217;m his fetching strategy&#8221;. Those classes will expose a property defining the exact set of classes to load, when using <a href="http://udidahan.weblogs.us/category/nhibernate/">NHibernate</a> we use HQL.</p>
<p>Here&#8217;s the sequence diagram showing how this works.</p>
<p><a href="http://udidahan.weblogs.us/wp-content/uploads/ea12.png" target="_blank"><img src='http://udidahan.weblogs.us/wp-content/uploads/ea12.thumbnail.png' title='fetching strategy sequence diagram' alt='fetching strategy sequence diagram - opens in a new window' /></a></p>
<p>One question that you might ask is what if there is more than one class that implements the same interface? How could the infrastructure know which class it should be loading? The answer is simple. You shouldn&#8217;t do that. Having two classes fulfilling the same role will get you in trouble even if you don&#8217;t use this design. The Single Responsibility Principle should be our guiding light.</p>
<p>In summary, achieving high performance is possible when using Domain Models and Object/Relational Mapping, but requires minimizing calls to the database. This design decreases coupling between all the cooperating parts of the solution without giving up the ability to optimize over a specific technology.</p>
<p><a href="http://udidahan.weblogs.us/wp-content/uploads/fetching-strategy-design.zip" class="attachmentlink">Download the full detailed design (in HTML format)</a>. I&#8217;ll be offering the edittable <a href="http://www.sparxsystems.com/">Enterprise Architect</a> format shortly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2007/04/23/fetching-strategy-design/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Don, Spring, OR mapping, Lazy Loading, in a short post</title>
		<link>http://www.udidahan.com/2004/11/23/don-spring-or-mapping-lazy-loading-in-a-short-post/</link>
		<comments>http://www.udidahan.com/2004/11/23/don-spring-or-mapping-lazy-loading-in-a-short-post/#comments</comments>
		<pubDate>Wed, 24 Nov 2004 00:47:54 +0000</pubDate>
		<dc:creator>thesoftwaresimplist</dc:creator>
				<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Simplicity]]></category>

		<guid isPermaLink="false">http://wp_630.weblogs.us/archives/130</guid>
		<description><![CDATA[After listening to the DNR episode with Don, Ted, and Mark I got to thinking about the issue of lazy loading in OR mappers. This was unanimously(?) described as being one of the imperfections inherent in the O/R mapping world. What I wondered was if that TransparentProxy could be used somehow to work around it. [...]]]></description>
			<content:encoded><![CDATA[<p>After listening to the DNR episode with <a href="http://pluralsight.com/blogs/dbox/">Don</a>, <a href="http://www.neward.net/ted/weblog/index.jsp">Ted</a>, and Mark I got to thinking about the issue of <a href="http://udidahan.weblogs.us/2007/04/15/lazy-loading-and-how-messaging-fixes-everything-again/">lazy loading</a> in OR mappers. This was unanimously(?) described as being one of the imperfections inherent in the <a href="http://udidahan.weblogs.us/category/nhibernate/">O/R mapping</a> world. What I wondered was if that TransparentProxy could be used somehow to work around it. I submit this to the ObjectSpaces gods as an open question &#8211; how are you guys going about it?</p>
<p>There is a different, more <a href="http://udidahan.weblogs.us/category/oo/">OO</a> way of handling this. By separating out the interface of the class you want to be lazy loaded &#8211; make an IPerson interface &#8211; and have the client code be tied to that. In that way, your <a href="http://udidahan.weblogs.us/category/dependency-injection/">dependency injection container</a> (<a href="http://www.springFramework.net">Spring</a>, for example) could instantiate an implementation that handles lazy loading without the client having to know about it. This implementation is absolutely something that could be handled by an OR mapping tool.</p>
<p>So, I suppose that even some of the more difficult problems of OR mapping could be handled. All that is needed is not to add more to the tool itself, but rather to more formally structure how to work with it. My opinion, anyway.</p>
<p><B><U>Update</U></B></p>
<p>The next part around the discussion about lazy loading and <a href="http://udidahan.weblogs.us/2007/03/06/better-domain-driven-design-implementation/">fetching strategies</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2004/11/23/don-spring-or-mapping-lazy-loading-in-a-short-post/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
