<?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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Udi Dahan - The Software Simplist</title>
	
	<link>http://www.udidahan.com</link>
	<description>Enterprise Development Expert &amp; SOA Specialist</description>
	<lastBuildDate>Mon, 02 Nov 2009 06:18:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<image><link>http://udidahan.weblogs.us</link><url>http://udidahan.weblogs.us/wp-content/uploads/2007/04/udi_on_white_left.JPG</url><title>Udi Dahan</title></image><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/UdiDahan-TheSoftwareSimplist" type="application/rss+xml" /><feedburner:emailServiceId>UdiDahan-TheSoftwareSimplist</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site, subject to copyright and fair use.</feedburner:browserFriendly><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Search and Messaging</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/P2rJ9ploUzk/</link>
		<comments>http://www.udidahan.com/2009/11/01/search-and-messaging/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 05:33:35 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Usability]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1134</guid>
		<description><![CDATA[
One question that I get asked about quite a bit with relation to messaging is about search. Isn&#8217;t search inherently request/response? Doesn&#8217;t it have to return immediately? Wouldn&#8217;t messaging in this case hurt our performance?
While I tend to put search in the query camp in the when keeping the responsibility of commands and queries separate, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/search.png" width="200" height="204" style="float:right; margin-left:10px; margin-bottom:10px;" alt="search" title="search" /><br />
One question that I get asked about quite a bit with relation to messaging is about search. Isn&#8217;t search inherently request/response? Doesn&#8217;t it have to return immediately? Wouldn&#8217;t messaging in this case hurt our performance?</p>
<p>While I tend to put search in the query camp in the when keeping the responsibility of commands and queries separate, and often recommend that those queries be done without messaging, there are certain types of search where messaging does make sense.</p>
<p>In this post, I&#8217;ll describe certain properties of the problem domain that make messaging a good candidate for a solution.</p>
<h3>Searching is besides the point &#8211; Finding is what it&#8217;s all about</h3>
<p>Remember that search is only a means to an end in the eyes of the user &#8211; they want to find something. One of the difficulties we users have is expressing what we want to find in ways that machines can understand.</p>
<p>In thinking about how we build systems to interact with users, we need to take this fuzziness into account. The more data that we have, the less homogeneous it is, the harder this problem becomes.</p>
<p>When talking about speed, while users are sensitive to the technical interactivity, the thing that matters most is the total time it takes for them to find what they want. If the result of each search screen pops up in 100ms, but the user hasn&#8217;t found what they&#8217;re looking for after clicking through 20 screens, the search function is ultimately broken.</p>
<p>Notice that the finding process isn&#8217;t perceived as &#8220;immediate&#8221; in the eyes of the user &#8211; the evaluation they do in their heads of the search results is as much a part of finding as the search itself.</p>
<p>Also, if the user needs to refine their search terms in order to find what they want, we&#8217;re now talking about a multi-request/multi-response process. There is nothing in the problem domain which indicates that finding is inherently request/response.</p>
<h3>Relationships in the data</h3>
<p>When bringing back data as the result of a search, what we&#8217;re saying is that there is a property which is the same across the result elements. But there may be more than one such property. For example, if we search for &#8220;blue&#8221; on Google Images, we get back pictures of the sky, birds, flowers, and more. Obvious so far &#8211; but let&#8217;s exploit the obvious a bit.</p>
<p>When the user sees that too many irrelevant results come back, they&#8217;ll want to refine their search. One way they can do that is to perform a new search and put in a more specific search phrase &#8211; like &#8220;blue sky&#8221;. Another way is for them to indicate this is by selecting an image and saying &#8220;not like this&#8221; or &#8220;more of these&#8221;. Then we can use the additional properties we know about those images to further refine the result group &#8211; either adding more images of one kind, or removing images of another.</p>
<p>Here&#8217;s something else that&#8217;s obvious:</p>
<p>Users often click or change their search before the entire result screen is shown. </p>
<p>It&#8217;s beginning to sound like users are already interacting with search in an asynchronous manner. What if we actually designed a system that played to that kind of interaction model?</p>
<h3>Data-space partitioning</h3>
<p>Once we accept the fact that the user is willing to have more results appear in increments, we can talk about having multiple servers processing the search in parallel. For large data spaces, it is unlikely for us to be able to store all the required meta data for search on one server anyway.</p>
<p>All we really need is a way to index these independent result-sets so that the user can access them. This can be done simply by allocating a GUID/UUID for the search request and storing the result-sets along with that ID.</p>
<h3>Browser interaction</h3>
<p>When the browser calls a server with the search request the first time, that server allocates an ID to that request, returns a URL containing that ID to the browser, and publishes an event containing the search term and the ID. Each of our processing nodes is subscribed to that event, performs the search on its part of the data-space, and writes its results (likely to a distributed cache) along with that ID. </p>
<p>The browser polls the above URL, which queries the cache (give me everything with this ID), and the browser sees which resources have been added since the last time it polled, and shows them to the user.</p>
<p>If the user clicks &#8220;more of these&#8221;, that initiates a new search request to the server, which follows the same pattern as before, just that the system is able to pull more relevant information. When implementing &#8220;not like this&#8221;, this performs a similar search but, instead of adding to the list of items shown, we&#8217;re removing items from the list shown based on the response from the server.</p>
<p>In this kind of user-system interaction model, having the user page through the result set doesn&#8217;t make very much sense as we&#8217;re not capturing the intent of the user, which is &#8220;you&#8217;re not showing me what I want&#8221;. By making it easy for the user to fine tune the result set, we get them closer to finding what they want. By performing work in parallel in a non-blocking manner on smaller sets of data, we greatly decrease the &#8220;time to first byte&#8221; as well as the time when the user can refine their search.</p>
<h3>But Google doesn&#8217;t work like that</h3>
<p>I know that this isn&#8217;t like the search UI we&#8217;ve all grown used to.</p>
<p>But then again, the search that you&#8217;re providing your users is more specific &#8211; not just pages on the web. If you&#8217;re a retailer allowing your users to search for a gift, this kind of &#8220;more like this, less like that&#8221; model is how users would interact with a real sales-person when shopping in a store. Why not model your system after the ways that people behave in the real world?</p>
<h3>In closing</h3>
<p>If we were to try to make use of messaging underneath &#8220;classical&#8221; search interaction models, it probably wouldn&#8217;t have been the greatest fit. If all we&#8217;re doing at a logical level is blocking RPC, then messaging would probably make the system slower. The real power that you get from messaging is being able to technically do things in parallel &#8211; that&#8217;s how it makes things faster. If you can find ways to see that parallelism in your problem domain, not only will messaging make sense technically &#8211; it will really be the only way to build that kind of system.</p>
<p>Learning how to disconnect from seeing the world through the RPC-tinted glasses of our technical past takes time. Focusing on the problem domain, seeing it from the user&#8217;s perspective without any technical constraints &#8211; that&#8217;s the key to finding elegant solutions. More often than not, you&#8217;ll see that the real world is non-blocking and parallel, and then you&#8217;ll be able to make the best use of messaging and other related patterns.</p>
<p>What are your thought? Post a comment and let me know.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=P2rJ9ploUzk:37ZgfmmkSyg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=P2rJ9ploUzk:37ZgfmmkSyg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=P2rJ9ploUzk:37ZgfmmkSyg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=P2rJ9ploUzk:37ZgfmmkSyg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=P2rJ9ploUzk:37ZgfmmkSyg:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/P2rJ9ploUzk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/11/01/search-and-messaging/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/11/01/search-and-messaging/</feedburner:origLink></item>
		<item>
		<title>MySpace Architecture Considered Expensive</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/TIqo1RJ2XCk/</link>
		<comments>http://www.udidahan.com/2009/10/09/myspace-architecture-considered-expensive/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 21:24:09 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1126</guid>
		<description><![CDATA[I just finished listening to the Microsoft presentation on how they use the Concurrency &#038; Coordination Runtime (CCR) in MySpace (the stated largest web site running .NET).
Some interesting numbers were stated in the talk.

Tens of thousands to hundreds of thousands of requests per second
Over 3 thousand web servers
Over a thousand mid-tier servers

No wonder most big [...]]]></description>
			<content:encoded><![CDATA[<p>I just finished listening to the Microsoft <a href="http://channel9.msdn.com/shows/Communicating/CCR-at-MySpace/">presentation</a> on how they use the <a href="http://msdn.microsoft.com/en-us/library/bb905470.aspx">Concurrency &#038; Coordination Runtime (CCR)</a> in MySpace (the stated largest web site running .NET).</p>
<p>Some interesting numbers were stated in the talk.</p>
<ul>
<li>Tens of thousands to hundreds of thousands of requests per second</li>
<li>Over 3 thousand web servers</li>
<li>Over a thousand mid-tier servers</li>
</ul>
<p>No wonder most big web sites don&#8217;t run .NET. The Windows licenses would put them out of business.</p>
<p>Well, that is if you follow those same architectural practices.</p>
<p>I&#8217;ve written in the past of alternative architectural approaches that can scale to those levels at easily an order of magnitude less hardware (I think it&#8217;s closer to two OOMs) &#8211; here&#8217;s one of them on the topic of weather:</p>
<p><a href="http://www.udidahan.com/2008/12/29/building-super-scalable-web-systems-with-rest/">Building Super-Scalable Web Systems with REST</a>.</p>
<p>By the way, the client quoted in that post is now well above 60 million users with only small incremental increases in hardware. Oh, and their running everything on Windows and .NET. The question is not &#8220;can it scale&#8221;, but rather &#8220;how much will it cost to scale&#8221;.</p>
<p>Architecture pays itself back faster than ever in the Web 2.0 world.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=TIqo1RJ2XCk:ZXdwYWCGVZ4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=TIqo1RJ2XCk:ZXdwYWCGVZ4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=TIqo1RJ2XCk:ZXdwYWCGVZ4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=TIqo1RJ2XCk:ZXdwYWCGVZ4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=TIqo1RJ2XCk:ZXdwYWCGVZ4:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/TIqo1RJ2XCk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/10/09/myspace-architecture-considered-expensive/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/10/09/myspace-architecture-considered-expensive/</feedburner:origLink></item>
		<item>
		<title>[Article] EDA: SOA through the looking glass</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/Sc6QkqkDOxs/</link>
		<comments>http://www.udidahan.com/2009/09/29/article-eda-soa-through-the-looking-glass/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 11:05:33 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Articles]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1117</guid>
		<description><![CDATA[

My latest article has been published in issue 21 of the Microsoft Architecture Journal:
EDA: SOA Through The Looking Glass

While event-driven architecture (EDA) is a broadly known topic, both giving up ACID integrity guarantees and introducing eventual consistency make many architects uncomfortable. Yet it is exactly these properties that can direct architectural efforts toward identifying coarsely [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/architecture/aa699424.aspx"><br />
<img src="http://www.udidahan.com/wp-content/uploads/arcjournal21.png" style="float:right; margin-left:20px; margin-bottom:10px; border:1px solid black" alt="Microsoft Architecture Journal" title="Microsoft Architecture Journal" /></a></p>
<p>My latest article has been published in issue 21 of the Microsoft Architecture Journal:</p>
<p><u>EDA: SOA Through The Looking Glass</u></p>
<div style="font-size:12px">
While event-driven architecture (EDA) is a broadly known topic, both giving up ACID integrity guarantees and introducing eventual consistency make many architects uncomfortable. Yet it is exactly these properties that can direct architectural efforts toward identifying coarsely grained business-service boundaries—services that will result in true IT-business alignment.</p>
<p>Business events create natural temporal boundaries across which there is no business expectation of immediate consistency or confirmation. When they are mapped to technical solutions, the loosely coupled business domains on either side of business events simply result in autonomous, loosely coupled services whose contracts explicitly reflect the inherent publish/subscribe nature of the business.</p>
<p>This article will describe how all of these concepts fit together, as well as how they solve thorny issues such as high availability and fault tolerance.</p>
<p><a href="http://msdn.microsoft.com/en-us/architecture/aa699424.aspx">Continue reading&#8230;</a>
</div>
<p>Please leave questions and comments here.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=Sc6QkqkDOxs:_QiFFxt3xuc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=Sc6QkqkDOxs:_QiFFxt3xuc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=Sc6QkqkDOxs:_QiFFxt3xuc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=Sc6QkqkDOxs:_QiFFxt3xuc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=Sc6QkqkDOxs:_QiFFxt3xuc:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/Sc6QkqkDOxs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/09/29/article-eda-soa-through-the-looking-glass/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/09/29/article-eda-soa-through-the-looking-glass/</feedburner:origLink></item>
		<item>
		<title>Progressive .NET Wrap-up</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/65By2Vpkbw4/</link>
		<comments>http://www.udidahan.com/2009/09/07/progressive-net-wrap-up/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 06:06:30 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Pub/Sub]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1103</guid>
		<description><![CDATA[So, I&#8217;ve gotten back from a most enjoyable couple of days in Sweden where I gave two half-day tutorials, the first being the SOA and UI composition talk I gave at the European Virtual ALT.NET meeting (which you can find online here) and the other on DDD in enterprise apps (the first time I&#8217;ve done [...]]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;ve gotten back from a most enjoyable couple of days in Sweden where I gave two half-day tutorials, the first being the SOA and UI composition talk I gave at the European Virtual ALT.NET meeting (which you can find online <a href="http://www.vimeo.com/5022174">here</a>) and the other on DDD in enterprise apps (the first time I&#8217;ve done this talk).</p>
<p>I&#8217;ve gotten some questions about my DDD presentation there based on <a href="http://codebetter.com/blogs/aaron.jensen/">Aaron Jensen&#8217;s</a> pictures:</p>
<p><img src="http://www.udidahan.com/wp-content/uploads/cqs_udi_dahan_presentation.jpg" alt="cqs_udi_dahan_presentation" title="cqs_udi_dahan_presentation" width="500" height="332" class="alignnone size-full wp-image-1104" /></p>
<p>Yes &#8211; I talk with my hands. All the time.</p>
<p>That slide is quite an important one &#8211; I talked about it for at least 2 hours.</p>
<p>Here it is again, this time in full:</p>
<p><img src="http://www.udidahan.com/wp-content/uploads/cqs.jpg" alt="cqs" title="cqs" width="500" height="374" class="alignnone size-full wp-image-1107" /></p>
<p>You may notice that the nice clean layered abstraction that the industry has gotten so comfortable with doesn&#8217;t quite sit right when looking at it from this perspective. The reason for that is that this perspective takes into account physical distribution while layers don&#8217;t.</p>
<p>I&#8217;ll have some more posts on this topic as well as giving a session in TechEd Europe this November.</p>
<p>Oh &#8211; and please do feel free to already send your questions in.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=65By2Vpkbw4:k6fLyR7bJH4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=65By2Vpkbw4:k6fLyR7bJH4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=65By2Vpkbw4:k6fLyR7bJH4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=65By2Vpkbw4:k6fLyR7bJH4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=65By2Vpkbw4:k6fLyR7bJH4:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/65By2Vpkbw4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/09/07/progressive-net-wrap-up/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/09/07/progressive-net-wrap-up/</feedburner:origLink></item>
		<item>
		<title>Don’t Delete – Just Don’t</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/UtJGgoADBzA/</link>
		<comments>http://www.udidahan.com/2009/09/01/dont-delete-just-dont/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 12:04:48 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1097</guid>
		<description />
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/no_delete.png" style="float:right; margin-left:10px; margin-bottom:10px; alt="no deletes" title="no deletes" /><br />
After reading Ayende&#8217;s <a href="http://ayende.com/Blog/archive/2009/08/30/avoid-soft-deletes.aspx">post</a> advocating against &#8220;soft deletes&#8221; I felt that I should add a bit more to the topic as there were some important business semantics missing. As developers discuss the pertinence of using an IsDeleted column in the database to mark deletion, and the way this relates to reporting and auditing concerns is weighed, the core domain concepts rarely get a mention. Let&#8217;s first understand the business scenarios we&#8217;re modeling, the why behind them, before delving into the how of implementation.</p>
<h3>The real world doesn&#8217;t cascade</h3>
<p>Let&#8217;s say our marketing department decides to delete an item from the catalog. Should all previous orders containing that item just disappear? And cascading farther, should all invoices for those orders be deleted as well? Going on, would we have to redo the company&#8217;s profit and loss statements?</p>
<p>Heaven forbid.</p>
<p>So, is Ayende wrong? Do we really need soft deletes after all?</p>
<p>On the one hand, we don&#8217;t want to leave our database in an inconsistent state with invoices pointing to non-existent orders, but on the other hand, our users did ask us to delete an entity.</p>
<p>Or did they?</p>
<h3>When all you have is a hammer&#8230;</h3>
<p>We&#8217;ve been exposing users to entity-based interfaces with &#8220;create, read, update, delete&#8221; semantics in them for so long that they have started presenting us requirements using that same language, even though it&#8217;s an extremely poor fit.</p>
<p>Instead of accepting &#8220;delete&#8221; as a normal user action, let&#8217;s go into why users &#8220;delete&#8221; stuff, and what they actually intend to do.</p>
<p>The guys in marketing can&#8217;t actually make all physical instances of a product disappear &#8211; nor would they want to. In talking with these users, we might discover that their intent is quite different:</p>
<blockquote><p>“What I mean by &#8216;delete&#8217; is that the product should be discontinued. We don&#8217;t want to sell this line of product anymore. We want to get rid of the inventory we have, but not order any more from our supplier. The product shouldn&#8217;t appear any more when customers do a product search or category listing, but the guys in the warehouse will still need to manage these items in the interim. It&#8217;s much shorter to just say &#8216;delete&#8217; though.”</p></blockquote>
<p>There seem to be quite a few interesting business rules and processes there, but nothing that looks like it could be solved by a single database column.</p>
<h3>Model the task, not the data</h3>
<p>Looking back at the story our friend from marketing told us, his intent is to discontinue the product &#8211; not to delete it in any technical sense of the word. As such, we probably should provide a more explicit representation of this task in the user interface than just selecting a row in some grid and clicking the &#8216;delete&#8217; button (and &#8220;Are you sure?&#8221; isn&#8217;t it).</p>
<p>As we broaden our perspective to more parts of the system, we see this same pattern repeating:</p>
<blockquote><p>
Orders aren&#8217;t deleted &#8211; they&#8217;re cancelled. There may also be fees incurred if the order is canceled too late.</p>
<p>Employees aren&#8217;t deleted &#8211; they&#8217;re fired (or possibly retired). A compensation package often needs to be handled.</p>
<p>Jobs aren&#8217;t deleted &#8211; they&#8217;re filled (or their requisition is revoked).
</p></blockquote>
<p>In all cases, the thing we should focus on is the task the user wishes to perform, rather than on the technical action to be performed on one entity or another. In almost all cases, more than one entity needs to be considered.</p>
<h3>Statuses</h3>
<p>In all the examples above, what we see is a replacement of the technical action &#8216;delete&#8217; with a relevant business action. At the entity level, instead of having a (hidden) technical WasDeleted status, we see an explicit business status that users need to be aware of.</p>
<p>The manager of the warehouse needs to know that a product is discontinued so that they don&#8217;t order any more stock from the supplier. In today&#8217;s world of retail with Vendor Managed Inventory, this often happens together with a modification to an agreement with the vendor, or possibly a cancellation of that agreement. </p>
<p>This isn&#8217;t just a case of transactional or reporting boundaries &#8211; users in different contexts need to see different things at different times as the status changes to reflect the entity&#8217;s place in the business lifecycle. Customers shouldn&#8217;t see discontinued products at all. Warehouse workers should, that is, until the corresponding Stock Keeping Unit (SKU) has been revoked (another status) after we&#8217;ve sold all the inventory we wanted (and maybe returned the rest back to the supplier).</p>
<h3>Rules and Validation</h3>
<p>When looking at the world through over-simplified-delete-glasses, we may consider the logic dictating when we can delete to be quite simple: do some role-based-security checks, check that the entity exists, delete. Piece of cake.</p>
<p>The real world is a bigger, more complicated cake.</p>
<p>Let&#8217;s consider deleting an order, or rather, canceling it. On top of the regular security checks, we&#8217;ve got some rules to consider:</p>
<blockquote><p>
If the order has already been delivered, check if the customer isn&#8217;t happy with what they got, and go about <b>returning</b> the order. </p>
<p>If the order contained products &#8220;made to order&#8221;, charge the customer for a portion (or all) of the order (based on other rules).</p>
<p>And more&#8230;
</p></blockquote>
<p>Deciding what the next status should be may very well depend on the current business status of the entity. Deciding if that change of state is allowed is context and time specific &#8211; at one point in time the task may have been allowed, but later not. The logic here is not necessarily entirely related to the entity being &#8220;deleted&#8221; &#8211; there may be other entities which need to be checked, and whose status may also need  to be changed as well.</p>
<h3>Summary</h3>
<p>I know that some of you are thinking, &#8220;my system isn&#8217;t that complex &#8211; we can just delete and be done with it&#8221;.</p>
<p>My question to you would be, have you asked your users <b>why</b> they&#8217;re deleting things? Have you asked them about additional statuses and rules dictating how entities move as groups between them? You don&#8217;t want the success of your project to be undermined by that kind of unfounded assumption, do you?</p>
<p>The reason we&#8217;re given budgets to build business applications is because of the richness in business rules and statuses that ultimately provide value to users and a competitive advantage to the business. If that value wasn&#8217;t there, wouldn&#8217;t we be serving our users better by just giving them Microsoft Access?</p>
<p>In closing, given that you&#8217;re not giving your users MS Access, don&#8217;t think about deleting entities. Look for the reason why. Understand the different statuses that entities move between. Ask which users need to care about which status. I know it doesn&#8217;t show up as nicely on your resume as &#8220;3 years WXF&#8221;, but &#8220;saved the company $4 million in wasted inventory&#8221; does speak volumes.</p>
<p>One last sentence: Don&#8217;t delete. Just don&#8217;t.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=UtJGgoADBzA:82TlH8PkT8w:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=UtJGgoADBzA:82TlH8PkT8w:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=UtJGgoADBzA:82TlH8PkT8w:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=UtJGgoADBzA:82TlH8PkT8w:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=UtJGgoADBzA:82TlH8PkT8w:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/UtJGgoADBzA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/09/01/dont-delete-just-dont/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/09/01/dont-delete-just-dont/</feedburner:origLink></item>
		<item>
		<title>Hanselminutes on NServiceBus</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/oRdZGexe4-w/</link>
		<comments>http://www.udidahan.com/2009/08/21/hanselminutes-on-nservicebus/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 19:53:11 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Community]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1087</guid>
		<description><![CDATA[
Yesterday me and Scott virtually sat down to have a chat about NServiceBus and service buses in general. While we didn&#8217;t get in to many of the more advanced parts, you may find it an interesting introduction to the topic as well as saving yourself the costly mistake of implementing a broker instead of a [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.hanselman.com/blog/images/author.jpg" style="float:right; margin-left:10px; margin-bottom:10px;" /><br />
Yesterday me and Scott virtually sat down to have a chat about NServiceBus and service buses in general. While we didn&#8217;t get in to many of the more advanced parts, you may find it an interesting introduction to the topic as well as saving yourself the costly mistake of implementing a broker instead of a bus (yes &#8211; they&#8217;re actually two different things).</p>
<p><a href="http://www.hanselminutes.com/default.aspx?showID=194">Take a listen.</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=oRdZGexe4-w:sCC2XgIyy3c:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=oRdZGexe4-w:sCC2XgIyy3c:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=oRdZGexe4-w:sCC2XgIyy3c:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=oRdZGexe4-w:sCC2XgIyy3c:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=oRdZGexe4-w:sCC2XgIyy3c:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/oRdZGexe4-w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/08/21/hanselminutes-on-nservicebus/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/08/21/hanselminutes-on-nservicebus/</feedburner:origLink></item>
		<item>
		<title>Convention over Configuration – The Next Generation?</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/TzVpX7Da-Wo/</link>
		<comments>http://www.udidahan.com/2009/08/15/convention-over-configuration-the-next-generation/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 18:13:24 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1081</guid>
		<description><![CDATA[
Convention over configuration describes a style of development made popular by Ruby on Rails which has gained a great deal of traction in the .net ecosystem. After using frameworks designed in this way, I can say that the popularity is justified &#8211; it is much more pleasurable developing this way. 
The thing is, when looking [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/PicardKirk.jpg" alt="PicardKirk" title="PicardKirk" width="160" height="103" style="float:right; margin-left:10px; margin-bottom:10px; " /><br />
Convention over configuration describes a style of development made popular by Ruby on Rails which has gained a great deal of traction in the .net ecosystem. After using frameworks designed in this way, I can say that the popularity is justified &#8211; it is much more pleasurable developing this way. </p>
<p>The thing is, when looking at this in light of the full software development lifecycle, there are signs that the waters run deeper than we might have originally thought.</p>
<p>Let&#8217;s take things one step at a time though&#8230;</p>
<h3>What is it?</h3>
<p><a href="http://en.wikipedia.org/wiki/Convention_over_configuration">Wikipedia tells us</a>:</p>
<blockquote><p>&#8220;Convention over Configuration (aka Coding by convention) is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility. The phrase essentially means a developer only needs to specify unconventional aspects of the application.&#8221;</p></blockquote>
<p>What this means is that frameworks built in this way have default implementations that can be swapped out if needed. So far so good.</p>
<h3>For example&#8230;</h3>
<p>In <a href="http://www.NServiceBus.com">NServiceBus</a>, there is an abstraction for how subscription data is stored and multiple implementations &#8211; one in-memory, another using a durable MSMQ queue, and a third which uses a database. The convention for that part of the system is that the MSMQ implementation will be used, unless something else is specified. </p>
<p>Developers wishing to specify a different implementation can specify the desired implementation in the container &#8211; either one that comes out of the box, or their own implementation of ISubscriptionStorage.</p>
<p>Things get more interesting when we consider the full lifecycle.</p>
<h3>Lifecycle effects</h3>
<p>When developers are in the early phases of writing a new service, they want to focus primarily on what the service does &#8211; its logic. They don&#8217;t want to muck around with MSMQ queues for storing subscriptions and would much rather use the in-memory storage. </p>
<p>As the service takes shape and the developers want to run the full service on their machine, possibly testing basic fault-tolerance behaviors &#8211; kill one service, see that the others get a timeout, bring the service back up, wanting it to maintain all the previous subscriptions.</p>
<p>Moving on from there, our developers want to take the same system they just tested on their machine and move it into a staging environment. There, they don&#8217;t want to use the MSMQ implementation for subscription storage, but rather the database implementation &#8211; as will be used in the production environment. </p>
<p>While it may not sound like a big deal &#8211; changing the code which specifies which implementation to use when moving from one environment to another, consider that on top of just subscription storage, there is logging (output to console, file, db?), saga persistence (in-memory, file-based DB, relational DB), and more.</p>
<p>It&#8217;s actually quite likely that something will get missed as we move the system between environments. Can there be a better way?</p>
<h3>What if&#8230;</h3>
<p>What if there was some way for the developer to express their intent to the system, and the system could change its conventions, without the developer having to change any code or configuration files?</p>
<p>You might compare this (in concept) to debug builds and release builds. Same code, same config, but the runtime behaves different between the two.</p>
<p>As I mulled over how we could capture that intent without any code or config changes, the solution that I kept coming to seemed too trivial at first, so I dismissed it. Yet, it was the simplest one that would work for console and WinForms applications, as well as windows services &#8211; command line arguments. The only thing is that I don&#8217;t think those are available for web applications.</p>
<p>But since we&#8217;re still in &#8220;what if&#8221; land, and I&#8217;m more thinking out loud here than providing workable solutions for tomorrow morning, let&#8217;s &#8220;what if&#8221; command line arguments worked for web apps too.</p>
<h3>Command-Line Intent</h3>
<p>Going back to our original scenario, when developers are working on the logic of the service, they run it using the generic NServiceBus host process, passing it the command line parameter /lite (or whatever). The host then automatically configures all the in-memory implementations. </p>
<p>As the system progresses, when the developer wants to run everything on their machine, they run the processes with /integration. The host then configures the appropriate implementations (MSMQ for subscription storage, SQLite for saga persistence, etc. </p>
<p>When the developers want to run the system in production, they could specify /production (or maybe that could be the default?), and the database backed implementations would be configured.</p>
<h3>Imagine&#8230;</h3>
<p>Imagine being able to move that fluidly from one environment to another. Not needing to pore over configuration files or startup script code which configures a zillion implementation details. Not needing to worry that as you moved the system to staging something would break.</p>
<p>Imagine short, frictionless iterations even for large scale systems.</p>
<p>Imagine &#8211; lifecycle-aware frameworks making all this imagination a reality.</p>
<h3>In Closing</h3>
<p>We&#8217;re not there yet &#8211; but we&#8217;re not that far either. The generic host we&#8217;re providing with NServiceBus 2.0 is now being extended to support exactly these scenarios. </p>
<p>It&#8217;s my hope that as more of us think about this challenge, we&#8217;ll come up with better solutions and more intelligent frameworks. Just as convention came to our rescue before, breaking us out of the pain of endless XML configuration, I hope this new family of lifecycle-aware frameworks will make the friction of moving a system through dev, test, staging, and production a thing of the past.</p>
<p>A worthy problem for us all to solve, don&#8217;t you think?</p>
<p>Any ideas on how to make it a reality?<br />
Send them in &#8211; leave a comment below.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=TzVpX7Da-Wo:z-vJuQBRU0w:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=TzVpX7Da-Wo:z-vJuQBRU0w:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=TzVpX7Da-Wo:z-vJuQBRU0w:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=TzVpX7Da-Wo:z-vJuQBRU0w:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=TzVpX7Da-Wo:z-vJuQBRU0w:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/TzVpX7Da-Wo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/08/15/convention-over-configuration-the-next-generation/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/08/15/convention-over-configuration-the-next-generation/</feedburner:origLink></item>
		<item>
		<title>[Training] Public Courses</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/xBFYUA8nHxo/</link>
		<comments>http://www.udidahan.com/2009/08/09/training-public-courses/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 09:57:39 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Courses]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1070</guid>
		<description><![CDATA[I&#8217;ve been getting more questions from readers as to when I&#8217;ll be giving a public course in their area, so I thought I&#8217;d put up a quick little post on the topic.
Advanced Distributed Systems Design with SOA &#038; DDD


Sep 7th, London UK


Sep 21, Brussels Belgium


Nov 30, Austin Texas &#8211; USA


Loosely-Coupled Messaging with NServiceBus


Sep 16th, London [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been getting more questions from readers as to when I&#8217;ll be giving a public course in their area, so I thought I&#8217;d put up a quick little post on the topic.</p>
<h3>Advanced Distributed Systems Design with SOA &#038; DDD</h3>
<ul style="list-style-type : none;">
<li><a href="http://skillsmatter.com/course/open-source-dot-net/advanced-distributed-systems-design-with-soa"><br />
Sep 7th, London UK<br />
</a></li>
<li><a href="http://skillsmatter.com/course/open-source-dot-net/advanced-distributed-systems-design-with-soa"><br />
Sep 21, Brussels Belgium<br />
</a></li>
<li><a href="http://www.headspringsystems.com/services/agile-training/soa-training/"><br />
Nov 30, Austin Texas &#8211; USA</a></li>
</ul>
<p><br/></p>
<h3>Loosely-Coupled Messaging with NServiceBus</h3>
<ul style="list-style-type : none;">
<li><a href="http://skillsmatter.com/course/open-source-dot-net/loosely-coupled-messaging-with-nservicebus"><br />
Sep 16th, London UK<br />
</a></li>
</ul>
<p>***</p>
<p>There&#8217;s also work afoot to bring the SOA course to Australia in October &#8211; if you&#8217;re interested, let the organizers know <a href="http://www.doodle.com/gd4msqs2e9suu9ta">here</a>.</p>
<p>Those interested in a private course for their company can be contact me <a href="mailto:training@UdiDahan.com">here</a>.</p>
<p>And a quick, final note in closing &#8211; those of you interested in NServiceBus should now be able to find more information about it as well as how it relates to current and future Microsoft technologies on the improved <a href="http://www.NServiceBus.com">NServiceBus.com</a> site.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=xBFYUA8nHxo:X7fJMAij4No:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=xBFYUA8nHxo:X7fJMAij4No:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=xBFYUA8nHxo:X7fJMAij4No:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=xBFYUA8nHxo:X7fJMAij4No:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=xBFYUA8nHxo:X7fJMAij4No:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/xBFYUA8nHxo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/08/09/training-public-courses/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/08/09/training-public-courses/</feedburner:origLink></item>
		<item>
		<title>MSDN Magazine Domain Model Article</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/7D7jOlBZVDg/</link>
		<comments>http://www.udidahan.com/2009/08/02/msdn-magazine-domain-model-article/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 14:11:45 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Articles]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1063</guid>
		<description><![CDATA[
My article on “employing the domain model pattern” has been published in the August edition of MSDN Magazine.
Here’s a short excerpt:
“In this article, we’ll go through the reasons to (and not to) employ the domain model pattern, the benefits it brings, as well as provide some practical tips on keeping the overall solution as simple [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/magazine/ee236415.aspx"><img title="MSDN magazine" style="border-right: 0px; border-top: 0px; display: inline; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="346" alt="MSDN magazine" src="http://www.udidahan.com/wp-content/uploads/msdn_magazine_domain_model.gif" width="263" align="right" border="0" /></a></p>
<p>My article on “employing the domain model pattern” has been published in the August edition of MSDN Magazine.</p>
<p>Here’s a short excerpt:</p>
<blockquote><p>“In this article, we’ll go through the reasons to (and not to) employ the domain model pattern, the benefits it brings, as well as provide some practical tips on keeping the overall solution as simple as possible.”</p></blockquote>
<p><a href="http://msdn.microsoft.com/en-us/magazine/ee236415.aspx">Continue reading… </a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=7D7jOlBZVDg:5rqIm0Fx3KI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=7D7jOlBZVDg:5rqIm0Fx3KI:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=7D7jOlBZVDg:5rqIm0Fx3KI:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=7D7jOlBZVDg:5rqIm0Fx3KI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=7D7jOlBZVDg:5rqIm0Fx3KI:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/7D7jOlBZVDg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/08/02/msdn-magazine-domain-model-article/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/08/02/msdn-magazine-domain-model-article/</feedburner:origLink></item>
		<item>
		<title>Don’t Create Aggregate Roots</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/fBtt7asqx5g/</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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=fBtt7asqx5g:hfuHISIFbpk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=fBtt7asqx5g:hfuHISIFbpk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=fBtt7asqx5g:hfuHISIFbpk:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=fBtt7asqx5g:hfuHISIFbpk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=fBtt7asqx5g:hfuHISIFbpk:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/fBtt7asqx5g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/06/29/dont-create-aggregate-roots/feed/</wfw:commentRss>
		<slash:comments>64</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/06/29/dont-create-aggregate-roots/</feedburner:origLink></item>
		<item>
		<title>Domain Events – Salvation</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/0psBj_05BQc/</link>
		<comments>http://www.udidahan.com/2009/06/14/domain-events-salvation/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 06:25:31 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1029</guid>
		<description><![CDATA[
I&#8217;ve been hearing from people that have had a great deal of success using the Domain Event pattern and the infrastructure I previously provided for it in Domain Events &#8211; Take 2. I&#8217;m happy to say that I&#8217;ve got an improvement that I think you&#8217;ll like. The main change is that now we&#8217;ll be taking [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/sphere1.jpg" alt="sphere" title="sphere" width="198" height="201"  style="border-right: 0px; border-top: 0px; margin: 0px 10px; border-left: 0px; border-bottom: 0px" align="right"/><br />
I&#8217;ve been hearing from people that have had a great deal of success using the Domain Event pattern and the infrastructure I previously provided for it in <a href="http://www.udidahan.com/2008/08/25/domain-events-take-2/">Domain Events &#8211; Take 2</a>. I&#8217;m happy to say that I&#8217;ve got an improvement that I think you&#8217;ll like. The main change is that now we&#8217;ll be taking an approach that is reminiscent to how events are published in <a href="http://www.NServiceBus.com">NServiceBus</a>.</p>
<h3>Background</h3>
<p>Before diving right into the code, I wanted to take a minute to recall how we got here.</p>
<p>It started by looking for <a href="http://www.udidahan.com/2008/02/29/how-to-create-fully-encapsulated-domain-models/">how to create fully encapsulated domain models</a>.</p>
<p>The main assertion being that you do *not* need to inject anything into your domain entities.</p>
<p>Not services. Not repositories. Nothing.</p>
<p>Just pure domain model goodness.</p>
<h3>Make Roles Explicit</h3>
<p>I&#8217;m going to take the advice I so often give. A domain event is a role, and thus should be represented explicitly:</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">interface</span> IDomainEvent {}</pre>
</div>
<p>If this reminds you of the IMessage marker interface in nServiceBus, you&#8217;re beginning to see where this is going&#8230;</p>
<h3>How to define domain events</h3>
<p>A domain event is just a simple POCO that represents an interesting occurence in the domain. For example:</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> CustomerBecamePreferred : IDomainEvent </pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="kwrd">public</span> Customer Customer { get; set; }</pre>
<pre><span class="lnum">   4:  </span>}</pre>
</div>
<p>For those of you concerned about the number of events you may have, and therefore are thinking about bunching up these events by namespaces or things like that, slow down. The number of domain events and their cohesion is directly related to that of the domain model. </p>
<p>If you feel the need to split your domain events up, there&#8217;s a good chance that you should be looking at splitting your domain model too. This is the bottom-up way of identifying bounded contexts.</p>
<h3>How to raise domain events</h3>
<p>In your domain entities, when a significant state change happens you&#8217;ll want to raise your domain events 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">class</span> Customer</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> DoSomething()</pre>
<pre><span class="lnum">   4:  </span>    {</pre>
<pre class="alt"><span class="lnum">   5:  </span>        DomainEvents.Raise(<span class="kwrd">new</span> CustomerBecamePreferred() { Customer = <span class="kwrd">this</span> });</pre>
<pre><span class="lnum">   6:  </span>    }</pre>
<pre class="alt"><span class="lnum">   7:  </span>}</pre>
</div>
<p>We&#8217;ll look at the DomainEvents class in just a second, but I&#8217;m guessing that some of you are wondering &#8220;how did that entity get a reference to that?&#8221; The answer is that DomainEvents is a static class. &#8220;OMG, static?! But doesn&#8217;t that hurt testability?!&#8221; No, it doesn&#8217;t. Here, look:</p>
<h3>Unit testing with domain events</h3>
<p>One of the things we&#8217;d like to check when unit testing our domain entities is that the appropriate events are raised along with the corresponding state changes. Here&#8217;s an example:</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> DoSomethingShouldMakeCustomerPreferred()</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    var c = <span class="kwrd">new</span> Customer();</pre>
<pre><span class="lnum">   4:  </span>    Customer preferred = <span class="kwrd">null</span>;</pre>
<pre class="alt"><span class="lnum">   5:  </span>&nbsp;</pre>
<pre><span class="lnum">   6:  </span>    DomainEvents.Register&lt;CustomerBecamePreferred&gt;(</pre>
<pre class="alt"><span class="lnum">   7:  </span>        p =&gt; preferred = p.Customer</pre>
<pre><span class="lnum">   8:  </span>            );</pre>
<pre class="alt"><span class="lnum">   9:  </span>&nbsp;</pre>
<pre><span class="lnum">  10:  </span>    c.DoSomething();</pre>
<pre class="alt"><span class="lnum">  11:  </span>    Assert(preferred == c &amp;&amp; c.IsPreferred);</pre>
<pre><span class="lnum">  12:  </span>}</pre>
</div>
<p>As you can see, the static DomainEvents class is used in unit tests as well. Also notice that you don&#8217;t need to mock anything &#8211; pure testable bliss.</p>
<h3>Who handles domain events</h3>
<p>First of all, consider that when some service layer object calls the DoSomething method of the Customer class, it doesn&#8217;t necessarily know which, if any, domain events will be raised. All it wants to do is its regular schtick:</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> Handle(DoSomethingMessage msg)</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="kwrd">using</span> (ISession session = SessionFactory.OpenSession())</pre>
<pre><span class="lnum">   4:  </span>    <span class="kwrd">using</span> (ITransaction tx = session.BeginTransaction())</pre>
<pre class="alt"><span class="lnum">   5:  </span>    {</pre>
<pre><span class="lnum">   6:  </span>        var c = session.Get&lt;Customer&gt;(msg.CustomerId);</pre>
<pre class="alt"><span class="lnum">   7:  </span>        c.DoSomething();</pre>
<pre><span class="lnum">   8:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   9:  </span>        tx.Commit();</pre>
<pre><span class="lnum">  10:  </span>    }</pre>
<pre class="alt"><span class="lnum">  11:  </span>}</pre>
</div>
<p>The above code complies with the Single Responsibility Principle, so the business requirement which states that when a customer becomes preferred, they should be sent an email belongs somewhere else. </p>
<p>Notice that the key word in the requirement &#8211; &#8220;when&#8221;.</p>
<p>Any time you see that word in relation to your domain, consider modeling it as a domain event.</p>
<p>So, here&#8217;s the handling code:</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> CustomerBecamePreferredHandler : Handles&lt;CustomerBecamePreferred&gt;</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> Handle(CustomerBecamePreferred args)</pre>
<pre><span class="lnum">   4:  </span>   {</pre>
<pre class="alt"><span class="lnum">   5:  </span>      <span class="rem">// send email to args.Customer</span></pre>
<pre><span class="lnum">   6:  </span>   }</pre>
<pre class="alt"><span class="lnum">   7:  </span>} </pre>
</div>
<p>This code will run no matter which service layer object we came in through.</p>
<p>Here&#8217;s the interface it implements:</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">interface</span> Handles&lt;T&gt; <span class="kwrd">where</span> T : IDomainEvent</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="kwrd">void</span> Handle(T args); </pre>
<pre><span class="lnum">   4:  </span>} </pre>
</div>
<p>Fairly simple.</p>
<p>Please be aware that the above code will be run on the same thread within the same transaction as the regular domain work so you should avoid performing any blocking activities, like using SMTP or web services. Instead, prefer using one-way messaging to communicate to something else which does those blocking activities.</p>
<p>Also, you can have multiple classes handling the same domain event. If you need to send email *and* call the CRM system *and* do something else, etc, you don&#8217;t need to change any code &#8211; just write a new handler. This keeps your system quite a bit more stable than if you had to mess with the original handler or, heaven forbid, service layer code.</p>
<h3>Where domain event handlers go</h3>
<p>These handler classes do not belong in the domain model.</p>
<p>Nor do they belong in the service layer.</p>
<p>Well, that&#8217;s not entirely accurate &#8211; you see, there&#8217;s no *the* service layer. There is the part that accepts messages from clients and calls methods on the domain model. And there is another, independent part that handles events from the domain. Both of these will probably make use of a message bus, but that implementation detail shouldn&#8217;t deter you from keeping each in their own package.</p>
<h3>The infrastructure</h3>
<p>I know you&#8217;ve been patient, reading through all my architectural blah-blah, so here it is:</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">static</span> <span class="kwrd">class</span> DomainEvents</pre>
<pre><span class="lnum">   2:  </span>{ </pre>
<pre class="alt"><span class="lnum">   3:  </span>    [ThreadStatic] <span class="rem">//so that each thread has its own callbacks</span></pre>
<pre><span class="lnum">   4:  </span>    <span class="kwrd">private</span> <span class="kwrd">static</span> List&lt;Delegate&gt; actions;</pre>
<pre class="alt"><span class="lnum">   5:  </span>&nbsp;</pre>
<pre><span class="lnum">   6:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> IContainer Container { get; set; } <span class="rem">//as before</span></pre>
<pre class="alt"><span class="lnum">   7:  </span>&nbsp;</pre>
<pre><span class="lnum">   8:  </span>    <span class="rem">//Registers a callback for the given domain event</span></pre>
<pre class="alt"><span class="lnum">   9:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Register&lt;T&gt;(Action&lt;T&gt; callback) <span class="kwrd">where</span> T : IDomainEvent</pre>
<pre><span class="lnum">  10:  </span>    {</pre>
<pre class="alt"><span class="lnum">  11:  </span>       <span class="kwrd">if</span> (actions == <span class="kwrd">null</span>)</pre>
<pre><span class="lnum">  12:  </span>          actions = <span class="kwrd">new</span> List&lt;Delegate&gt;();</pre>
<pre class="alt"><span class="lnum">  13:  </span>&nbsp;</pre>
<pre><span class="lnum">  14:  </span>       actions.Add(callback);</pre>
<pre class="alt"><span class="lnum">  15:  </span>   }</pre>
<pre><span class="lnum">  16:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  17:  </span>   <span class="rem">//Clears callbacks passed to Register on the current thread</span></pre>
<pre><span class="lnum">  18:  </span>   <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> ClearCallbacks ()</pre>
<pre class="alt"><span class="lnum">  19:  </span>   {</pre>
<pre><span class="lnum">  20:  </span>       actions = <span class="kwrd">null</span>;</pre>
<pre class="alt"><span class="lnum">  21:  </span>   }</pre>
<pre><span class="lnum">  22:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  23:  </span>   <span class="rem">//Raises the given domain event</span></pre>
<pre><span class="lnum">  24:  </span>   <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Raise&lt;T&gt;(T args) <span class="kwrd">where</span> T : IDomainEvent</pre>
<pre class="alt"><span class="lnum">  25:  </span>   {</pre>
<pre><span class="lnum">  26:  </span>      <span class="kwrd">if</span> (Container != <span class="kwrd">null</span>)</pre>
<pre class="alt"><span class="lnum">  27:  </span>         <span class="kwrd">foreach</span>(var handler <span class="kwrd">in</span> Container.ResolveAll&lt;Handles&lt;T&gt;&gt;())</pre>
<pre><span class="lnum">  28:  </span>            handler.Handle(args);</pre>
<pre class="alt"><span class="lnum">  29:  </span>&nbsp;</pre>
<pre><span class="lnum">  30:  </span>      <span class="kwrd">if</span> (actions != <span class="kwrd">null</span>)</pre>
<pre class="alt"><span class="lnum">  31:  </span>          <span class="kwrd">foreach</span> (var action <span class="kwrd">in</span> actions)</pre>
<pre><span class="lnum">  32:  </span>              <span class="kwrd">if</span> (action <span class="kwrd">is</span> Action&lt;T&gt;)</pre>
<pre class="alt"><span class="lnum">  33:  </span>                  ((Action&lt;T&gt;)action)(args);</pre>
<pre><span class="lnum">  34:  </span>   }</pre>
<pre class="alt"><span class="lnum">  35:  </span>} </pre>
</div>
<p>Notice that while this class *can* use a container, the container isn&#8217;t needed for unit tests which use the Register method.</p>
<p>When used server side, please make sure that you add a call to ClearCallbacks in your infrastructure&#8217;s end of message processing section. In nServiceBus this is done with a message module like the one below:</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> DomainEventsCleaner : IMessageModule</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> HandleBeginMessage() { }</pre>
<pre><span class="lnum">   4:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   5:  </span>    <span class="kwrd">public</span> <span class="kwrd">void</span> HandleEndMessage()</pre>
<pre><span class="lnum">   6:  </span>    {</pre>
<pre class="alt"><span class="lnum">   7:  </span>        DomainEvents.ClearCallbacks();</pre>
<pre><span class="lnum">   8:  </span>    }</pre>
<pre class="alt"><span class="lnum">   9:  </span>}</pre>
</div>
<p>The main reason for this cleanup is that someone just might want to use the Register API in their original service layer code rather than writing a separate domain event handler.</p>
<h3>Summary</h3>
<p>Like all good things in life, 3rd time&#8217;s the charm.</p>
<p>It took a couple of iterations, and the API did change quite a bit, but the overarching theme has remained the same &#8211; keep the domain model focused on domain concerns. While some might say that there&#8217;s only a slight technical difference between calling a service (IEmailService) and using an event to dispatch it elsewhere, I beg to differ.</p>
<p>These domain events are a part of the ubiquitous language and should be represented explicitly.</p>
<p>CustomerBecamePreferred is nothing at all like IEmailService.</p>
<p>In working with your domain experts or just going through a requirements document, pay less attention to the nouns and verbs that Object-Oriented Analysis &#038; Design call attention to, and keep an eye out for the word &#8220;when&#8221;. It&#8217;s a critically important word that enables us to model important occurrences and state changes.</p>
<p>What do you think? Are you already using this approach? Have you already tried it and found it broken in some way? Do you have any suggestions on how to improve it?</p>
<p>Let me know &#8211; leave a comment below.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=0psBj_05BQc:Lud1ZFHTQ7o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=0psBj_05BQc:Lud1ZFHTQ7o:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=0psBj_05BQc:Lud1ZFHTQ7o:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=0psBj_05BQc:Lud1ZFHTQ7o:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=0psBj_05BQc:Lud1ZFHTQ7o:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/0psBj_05BQc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/06/14/domain-events-salvation/feed/</wfw:commentRss>
		<slash:comments>99</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/06/14/domain-events-salvation/</feedburner:origLink></item>
		<item>
		<title>The Fallacy Of ReUse</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/CB2RzsqQKwE/</link>
		<comments>http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 08:40:16 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1026</guid>
		<description><![CDATA[This industry is pre-occupied with reuse.
There&#8217;s this belief that if we just reused more code, everything would be better.
Some even go so far as saying that the whole point of object-orientation was reuse &#8211; it wasn&#8217;t, encapsulation was the big thing. After that component-orientation was the thing that was supposed to make reuse happen. Apparently [...]]]></description>
			<content:encoded><![CDATA[<p>This industry is pre-occupied with reuse.</p>
<p>There&#8217;s this belief that if we just reused more code, everything would be better.</p>
<p>Some even go so far as saying that the whole point of object-orientation was reuse &#8211; it wasn&#8217;t, encapsulation was the big thing. After that component-orientation was the thing that was supposed to make reuse happen. Apparently that didn&#8217;t pan out so well either because here we are now pinning our reuseful hopes on service-orientation.</p>
<p>Entire books of patterns have been written on how to achieve reuse with the orientation of the day.<br />
Services have been classified every which way in trying to achieve this, from entity services and activity services, through process services and orchestration services. Composing services has been touted as the key to reusing, and creating reusable services.</p>
<p>I might as well let you in on the dirty-little secret:</p>
<h3>Reuse is a fallacy</h3>
<p>Before running too far ahead, let&#8217;s go back to what the actual goal of reuse was: getting done faster.</p>
<p>That&#8217;s it.</p>
<p>It&#8217;s a fine goal to have.</p>
<p>And here&#8217;s how reuse fits in to the picture:</p>
<blockquote><p>
If we were to write all the code of a system, we&#8217;d write a certain amount of code.<br />
If we could reuse some code from somewhere else that was written before, we could write less code.<br />
The more code we can reuse, the less code we write.<br />
The less code we write, the sooner we&#8217;ll be done!
</p></blockquote>
<p>However, the above logical progression is based on another couple of fallacies:</p>
<h3>Fallacy: All code takes the same amount of time to write</h3>
<h3>Fallacy: Writing code is the primary activity in getting a system done</h3>
<p>Anyone who&#8217;s actually written some code that&#8217;s gone into production knows this.</p>
<p>There&#8217;s the time it takes us to understand what the system should do.<br />
Multiply that by the time it takes the users to understand what the system should do <img src='http://www.udidahan.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
Then there&#8217;s the integrating that code with all the other code, databases, configuration, web services, etc.<br />
Debugging. Deploying. Debugging. Rebugging. Meetings. Etc.</p>
<p>Writing code is actually the least of our worries.<br />
We actually spend less time writing code than&#8230;</p>
<h3>Rebugging code</h3>
<p>Also known as bug regressions.</p>
<p>This is where we fix one piece of code, and in the process break another piece of code.<br />
It&#8217;s not like we do it on purpose. It&#8217;s all those dependencies between the various bits of code.<br />
The more dependencies there are, the more likely something&#8217;s gonna break.<br />
Especially when we have all sorts of hidden dependencies,<br />
like when other code uses stuff we put in the database without asking us what it means,<br />
or, heaven forbid, changing it without telling us.</p>
<p>These debugging/rebugging cycles can make stabilizing a system take a long time.</p>
<p>So, how does reuse help/hinder with that?</p>
<p>Here&#8217;s how:</p>
<h3>Dependencies multiply by reuse</h3>
<p>It&#8217;s to be expected. If you wrote the code all in one place, there are no dependencies. By reusing code, you&#8217;ve created a dependency. The more you reuse, the more dependencies you have. The more dependencies, the more rebugging.</p>
<p>Of course, we need to keep in mind the difference between&#8230;</p>
<h3>Reuse &#038; Use</h3>
<p>Your code <b>uses</b> the runtime API (JDK, .NET BCL, etc).<br />
Likewise other frameworks like (N)Hibernate, Spring, WCF, etc.</p>
<p>Reuse happens when you extend and override existing behaviors within other code.<br />
This is most often done by inheritance in OO languages.</p>
<p>Interestingly enough, by the above generally accepted definition, most web services &#8220;reuse&#8221; is actually really use.</p>
<p>Let&#8217;s take a look at the characteristics of the code we&#8217;re using and reusing to see where we get the greatest value:</p>
<h3>The value of (re)use</h3>
<p>If we were to (re)use a piece of code in only one part of our system, it would be safe to say that we would get less value than if we could (re)use it in more places. For example, we could say that for many web applications, the web framework we use provides more value than a given encryption algorithm that we may use in only a few places.</p>
<p>So, what characterizes the code we use in many places?</p>
<p>Well, it&#8217;s very <b>generic</b>.</p>
<p>Actually, the more generic a piece of code, the less likely it is that we&#8217;ll be changing something in it when fixing a bug in the system.</p>
<p><b>That&#8217;s important</b>.</p>
<p>However, when looking at the kind of code we reuse, and the reasons around it, we tend to see very <b>non-generic</b> code &#8211; something that deals with the domain-specific behaviors of the system. Thus, the likelihood of a bug fix needing to touch that code is higher than in the generic/use-not-reuse case, often much higher.</p>
<h3>How it all fits together</h3>
<blockquote><p>
Goal:&#09;Getting done faster<br />
Via:&#09;Spending less time debugging/rebugging/stabilizing<br />
Via:&#09;Having less dependencies reasonably requiring a bug fix to touch the dependent side<br />
Via:&#09;Not reusing non-generic code
</p></blockquote>
<p>This doesn&#8217;t mean you shouldn&#8217;t use generic code / frameworks where applicable &#8211; absolutely, you should.<br />
Just watch the number of kind of dependencies you introduce.</p>
<h3>Back to services</h3>
<p>So, if we follow the above advice with services, we wouldn&#8217;t want domain specific services reusing each other.<br />
If we could get away with it, we probably wouldn&#8217;t even want them using each other either.</p>
<p>As use and reuse go down, we can see that service autonomy goes up. And vice-versa.<br />
Luckily, we have service interaction mechanisms from Event-Driven Architecture that enable use without breaking autonomy.<br />
Autonomy is actually very similar to the principle of encapsulation that drove object-orientation in the first place.<br />
Interesting, isn&#8217;t it?</p>
<h3>In summary</h3>
<p>We all want to get done faster.</p>
<p>Way back when, someone told us reuse was the way to do that.</p>
<p>They were wrong.</p>
<p>Reuse may make sense in the most tightly coupled pieces of code you have, but not very much anywhere else.</p>
<p>When designing services in your SOA, stay away from reuse, and minimize use (with EDA patterns).</p>
<p>The next time someone pulls the &#8220;reuse excuse&#8221;, you&#8217;ll be ready.</p>
<hr size="1" />
<h3>Further Reading</h3>
<ul>
<li><a href="http://www.udidahan.com/2008/10/22/additional-logic-required-for-service-autonomy/">Additional logic required for service autonomy</a></li>
<li><a href="http://www.udidahan.com/2008/12/13/self-contained-events-and-soa/">Self-contained events &#038; SOA</a></li>
<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://udidahan.weblogs.us/2006/05/26/podcast-does-soa-mean-the-end-of-oo/">Does SOA mean the end of OO?</a> [Podcast]</li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=CB2RzsqQKwE:2wAfjjaxamE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=CB2RzsqQKwE:2wAfjjaxamE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=CB2RzsqQKwE:2wAfjjaxamE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=CB2RzsqQKwE:2wAfjjaxamE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=CB2RzsqQKwE:2wAfjjaxamE:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/CB2RzsqQKwE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/</feedburner:origLink></item>
		<item>
		<title>WebCast on SOA in the E-VAN</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/i0JZv5BZwyg/</link>
		<comments>http://www.udidahan.com/2009/06/06/webcast-on-soa-in-the-e-van/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 06:14:24 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Presentations]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1022</guid>
		<description><![CDATA[I&#8217;ll be doing a webcast tomorrow on SOA for the European Virtual Alt.Net (E-VAN).
I&#8217;ll be discussing business service boundaries, publish/subscribe eventing, and business activity monitoring &#8211; with some time for questions at end.
Update: Recording is now online &#8211; here.
Hope to virtually see you there.
Here&#8217;s the details of the live meeting:
Start Time: Monday, June 01, 2009 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll be doing a webcast tomorrow on SOA for the European Virtual Alt.Net (E-VAN).</p>
<p>I&#8217;ll be discussing business service boundaries, publish/subscribe eventing, and business activity monitoring &#8211; with some time for questions at end.</p>
<p><B>Update:</B> Recording is now online &#8211; <a href="http://www.vimeo.com/5022174">here</a>.</p>
<p>Hope to virtually see you there.</p>
<p>Here&#8217;s the details of the live meeting:</p>
<p>Start Time: Monday, June 01, 2009 07:00 PM GMT<br />
End Time: Monday, June 01, 2009 08:30 PM GMT</p>
<p>Attendee URL: <a href="http://snipr.com/virtualaltnet">http://snipr.com/virtualaltnet</a> (Live Meeting) </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=i0JZv5BZwyg:eW1R0zhY7j4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=i0JZv5BZwyg:eW1R0zhY7j4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=i0JZv5BZwyg:eW1R0zhY7j4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=i0JZv5BZwyg:eW1R0zhY7j4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=i0JZv5BZwyg:eW1R0zhY7j4:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/i0JZv5BZwyg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/06/06/webcast-on-soa-in-the-e-van/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/06/06/webcast-on-soa-in-the-e-van/</feedburner:origLink></item>
		<item>
		<title>A Queue Isn’t An Implementation Detail</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/h3Lb3FPkQqs/</link>
		<comments>http://www.udidahan.com/2009/05/25/a-queue-isnt-an-implementation-detail/#comments</comments>
		<pubDate>Mon, 25 May 2009 18:15:01 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1015</guid>
		<description><![CDATA[It&#8217;s hard to believe that this continues to pop up even as WCF is reaching its fourth version (emphasis mine):
&#8220;A common complaint is that the first call on a client object takes some disproportionately large amount of time, usually ten seconds or more, while successive calls are instantaneous. There are many reasons why this might [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s hard to believe that this continues to pop up even as WCF is reaching its fourth version (emphasis mine):</p>
<blockquote><p>&#8220;A common complaint is that the first call on a client object takes some disproportionately large amount of time, <b>usually ten seconds or more</b>, while successive calls are instantaneous. There are many reasons why this might happen so <b>there&#8217;s no generic resolution for this problem</b>.&#8221; &#8212; <a href="http://blogs.msdn.com/drnick/archive/2009/05/22/tripping-over-missing-servers.aspx">Nicholas Allen</a></p></blockquote>
<p>The thing is that there <b>IS</b> a generic solution to this problem.</p>
<p>It&#8217;s queued messaging.</p>
<p>The only thing is that you have to give up talking to your services as if they were regular objects &#8211; calling methods on them and expecting a response. In other words, designing a distributed systems isn&#8217;t like designing a regular OO system just with some WCF sprinkled on top.</p>
<p>Even when trying to do fire and forget messaging on top of WCF (void method calls with the OneWay attribute), the underlying channel can still block your thread, as Nick mentioned. </p>
<p>A queue isn&#8217;t an implementation detail.<br />
It&#8217;s the primary architectural abstraction of a distributed system.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=h3Lb3FPkQqs:HD8ChpkQfHA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=h3Lb3FPkQqs:HD8ChpkQfHA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=h3Lb3FPkQqs:HD8ChpkQfHA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=h3Lb3FPkQqs:HD8ChpkQfHA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=h3Lb3FPkQqs:HD8ChpkQfHA:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/h3Lb3FPkQqs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/05/25/a-queue-isnt-an-implementation-detail/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/05/25/a-queue-isnt-an-implementation-detail/</feedburner:origLink></item>
		<item>
		<title>Projects, Assemblies, and Namespaces – oh my</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/7Tg6jQo7o_Y/</link>
		<comments>http://www.udidahan.com/2009/05/03/projects-assemblies-and-namespaces-oh-my/#comments</comments>
		<pubDate>Sun, 03 May 2009 19:45:34 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Simplicity]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1008</guid>
		<description><![CDATA[Every once in a while this topic pops up, and since the nServiceBus code base doesn&#8217;t follow the apparently accepted practice, and I do get asked about it, here goes.
First of all, the conventional wisdom:
&#8220;If you don&#8217;t choose assembly to represent component, the natural artifact candidate is then namespace.&#8221;
There&#8217;s only one minor assumption here that [...]]]></description>
			<content:encoded><![CDATA[<p>Every once in a while this topic pops up, and since the nServiceBus code base doesn&#8217;t follow the apparently accepted practice, and I do get asked about it, here goes.</p>
<p>First of all, the <a href="http://codebetter.com/blogs/patricksmacchia/archive/2009/05/03/can-we-avoid-tooling-to-prevent-spaghetti-code.aspx">conventional wisdom</a>:</p>
<blockquote><p>&#8220;If you don&#8217;t choose assembly to represent component, the natural artifact candidate is then namespace.&#8221;</p></blockquote>
<p>There&#8217;s only one minor assumption here that deserves being dragged out into the light.</p>
<p>While Visual Studio creates an assembly from every project by default, you <i>can</i> take those assemblies and merge them together into a single assembly using <a href="http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx">this nice little utility</a> from Microsoft. It is likely that each project would have its own namespace too, so we should still be aligned with the conventional wisdom.</p>
<p>In other words, we <i>could</i> choose a Visual Studio project to represent a logical component and still be in the same camp as <a href="http://codebetter.com/blogs/jeremy.miller/archive/2008/10/10/183438.aspx">Jeremy</a>:</p>
<blockquote><p>&#8220;I&#8217;m very firmly in the camp that says you should only split assemblies by deployment targets&#8221;</p></blockquote>
<p>What everyone agrees about seems to be that <b>coupling hurts, and should be managed</b>.</p>
<p>Where does coupling come from? Well, from references between two pieces of code. <b>If</b> we were to represent our logical components as Visual Studio projects, we could easily see those references without the help of any 3rd party tools. The compiler would even yell at us if we were to (accidentally) create an evil circular reference.</p>
<p>While some might complain about the long compile time when we have many projects in a single solution, good componentization often doesn&#8217;t require us to put all projects in a single solution. In fact, each component could theoretically have its own solution &#8211; since it&#8217;s reasonable to assume we&#8217;d really only be working on one component at a time. In which case, compile time per developer task would be a non-issue.</p>
<p>Going through the whole code base is usually only needed when doing a full-system debug when trying to track down a problem. This wouldn&#8217;t need to be done against a solution with all projects. We&#8217;d do this using PDBs of the merged projects (as that&#8217;s what actually got delivered, and where the bug was found). After spelunking through those PDBs, we&#8217;d eventually find the problematic component (or 2, or 3, or &#8230;), and open up developer tasks for each component. </p>
<p>Regardless of if we&#8217;re putting out a patch for an existing customer or rolling these changes into a release with other tasks, all the logical components would be built into a physical system (merged as necessary) and the system would be put through QA.</p>
<p>In short, it looks like just a bit of unconventional wisdom gets us a nice balance.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=7Tg6jQo7o_Y:7cdHDlPM-fg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=7Tg6jQo7o_Y:7cdHDlPM-fg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=7Tg6jQo7o_Y:7cdHDlPM-fg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=7Tg6jQo7o_Y:7cdHDlPM-fg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=7Tg6jQo7o_Y:7cdHDlPM-fg:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/7Tg6jQo7o_Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/05/03/projects-assemblies-and-namespaces-oh-my/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/05/03/projects-assemblies-and-namespaces-oh-my/</feedburner:origLink></item>
		<item>
		<title>Saga Persistence and Event-Driven Architectures</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/B-um4XofEr4/</link>
		<comments>http://www.udidahan.com/2009/04/20/saga-persistence-and-event-driven-architectures/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 11:50:44 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=992</guid>
		<description><![CDATA[When working with clients, I run into more than a couple of people that have difficulty with event-driven architecture (EDA). Even more people have difficulty understanding what sagas really are, let alone why they need to use them. I&#8217;d go so far to say that many people don&#8217;t realize the importance of how sagas are [...]]]></description>
			<content:encoded><![CDATA[<p><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 10px 10px; border-right-width: 0px" height="128" alt="image" src="http://www.udidahan.com/wp-content/uploads/saga_persistence.jpg" width="200" align="right" border="0" />When working with clients, I run into more than a couple of people that have difficulty with event-driven architecture (EDA). Even more people have difficulty understanding what sagas really are, let alone why they need to use them. I&#8217;d go so far to say that many people don&#8217;t realize the importance of how sagas are persisted in making it all work (including the Workflow Foundation team).</p>
<h3>The common e-commerce example</h3>
<p>We accept orders, bill the customer, and then ship them the product.</p>
<p>Fairly straight-forward.</p>
<p>Since each part of that process can be quite complex, let&#8217;s have each step be handled by a service:</p>
<p>Sales, Billing, and Shipping. Each of these services will publish an event when it&#8217;s done its part. Sales will publish OrderAccepted containing all the order information &#8211; order Id, customer Id, products, quantities, etc. Billing will publish CustomerBilledForOrder containing the customer Id, order Id, etc. And Shipping will publish OrderShippedToCustomer with its data.</p>
<p>So far, so good. EDA and SOA seem to be providing us some value.</p>
<h3>Where&#8217;s the saga?</h3>
<p>Well, let&#8217;s consider the behavior of the Shipping service. It shouldn&#8217;t ship the order to the customer until it has received the CustomerBilledForOrder event as well as the OrderAccepted event. In other words, Shipping needs to hold on to the state that came in the first event until the second event comes in. And this is exactly what sagas are for.</p>
<p>Let&#8217;s take a look at the saga code that implements this. In order to simplify the sample a bit, I&#8217;ll be omitting the product quantities.</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> ShippingSaga : Saga&lt;ShippingSagaData&gt;,</pre>
<pre><span class="lnum">   2:  </span>        ISagaStartedBy&lt;OrderAccepted&gt;,</pre>
<pre class="alt"><span class="lnum">   3:  </span>        ISagaStartedBy&lt;CustomerBilledForOrder&gt;</pre>
<pre><span class="lnum">   4:  </span>    {</pre>
<pre class="alt"><span class="lnum">   5:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(OrderAccepted message)</pre>
<pre><span class="lnum">   6:  </span>        {</pre>
<pre class="alt"><span class="lnum">   7:  </span>            <span class="kwrd">this</span>.Data.ProductIdsInOrder = message.ProductIdsInOrder;</pre>
<pre><span class="lnum">   8:  </span>        }</pre>
<pre class="alt"><span class="lnum">   9:  </span>&nbsp;</pre>
<pre><span class="lnum">  10:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(CustomerBilledForOrder message)</pre>
<pre class="alt"><span class="lnum">  11:  </span>        {</pre>
<pre><span class="lnum">  12:  </span>             <span class="kwrd">this</span>.Bus.Send&lt;ShipOrderToCustomer&gt;(</pre>
<pre class="alt"><span class="lnum">  13:  </span>                (m =&gt;</pre>
<pre><span class="lnum">  14:  </span>                {</pre>
<pre class="alt"><span class="lnum">  15:  </span>                    m.CustomerId = message.CustomerId;</pre>
<pre><span class="lnum">  16:  </span>                    m.OrderId = message.OrderId;</pre>
<pre class="alt"><span class="lnum">  17:  </span>                    m.ProductIdsInOrder = <span class="kwrd">this</span>.Data.ProductIdsInOrder;</pre>
<pre><span class="lnum">  18:  </span>                }</pre>
<pre class="alt"><span class="lnum">  19:  </span>                ));</pre>
<pre><span class="lnum">  20:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  21:  </span>            <span class="kwrd">this</span>.MarkAsComplete();</pre>
<pre><span class="lnum">  22:  </span>        }</pre>
<pre class="alt"><span class="lnum">  23:  </span>&nbsp;</pre>
<pre><span class="lnum">  24:  </span>        <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Timeout(<span class="kwrd">object</span> state)</pre>
<pre class="alt"><span class="lnum">  25:  </span>        {</pre>
<pre><span class="lnum">  26:  </span>            </pre>
<pre class="alt"><span class="lnum">  27:  </span>        }</pre>
<pre><span class="lnum">  28:  </span>    }</pre>
</div>
<p>First of all, this looks fairly simple and straightforward, which is good.<br/><br />
It&#8217;s also wrong, which is not so good.</p>
<p>One problem we have here is that events may arrive out of order &#8211; first CustomerBilledForOrder, and only then OrderAccepted. What would happen in the above saga in that case? Well, we wouldn&#8217;t end up shipping the products to the customer, and customers tend not to like that (for some reason).</p>
<p>There&#8217;s also another problem here. See if you can spot it as I go through the explanation of ISagaStartedBy&lt;T&gt;.</p>
<h3>Saga start up and correlation</h3>
<p>The &#8220;ISagaStartedBy&lt;T&gt;&#8221; that is implemented for both messages indicates to the infrastructure (NServiceBus) that when a message of that type arrives, if an existing saga instance cannot be found, that a new instance should be started up. Makes sense, doesn&#8217;t it? For a given order, when the OrderAccepted event arrives first, Shipping doesn&#8217;t currently have any sagas handling it, so it starts up a new one. After that, when the CustomerBilledForOrder event arrives for that same order, the event should be handled by the saga instance that handled the first event &#8211; not by a new one.</p>
<p>I&#8217;ll repeat the important part: &#8220;the event should be handled by the saga instance that handled the first event&#8221;.</p>
<p>Since the only information we stored in the saga was the list of products, how would we be able to look up that saga instance when the next event came in containing an order Id, but no saga Id?</p>
<p>OK, so we need to store the order Id from the first event so that when the second event comes along we&#8217;ll be able to find the saga based on that order Id. Not too complicated, but something to keep in mind.</p>
<p>Let&#8217;s look at the updated code:</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> ShippingSaga : Saga&lt;ShippingSagaData&gt;,</pre>
<pre><span class="lnum">   2:  </span>        ISagaStartedBy&lt;OrderAccepted&gt;,</pre>
<pre class="alt"><span class="lnum">   3:  </span>        ISagaStartedBy&lt;CustomerBilledForOrder&gt;</pre>
<pre><span class="lnum">   4:  </span>    {</pre>
<pre class="alt"><span class="lnum">   5:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(CustomerBilledForOrder message)</pre>
<pre><span class="lnum">   6:  </span>        {</pre>
<pre class="alt"><span class="lnum">   7:  </span>            <span class="kwrd">this</span>.Data.CustomerHasBeenBilled = <span class="kwrd">true</span>;</pre>
<pre><span class="lnum">   8:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   9:  </span>            <span class="kwrd">this</span>.Data.CustomerId = message.CustomerId;</pre>
<pre><span class="lnum">  10:  </span>            <span class="kwrd">this</span>.Data.OrderId = message.OrderId;</pre>
<pre class="alt"><span class="lnum">  11:  </span>&nbsp;</pre>
<pre><span class="lnum">  12:  </span>            <span class="kwrd">this</span>.CompleteIfPossible();</pre>
<pre class="alt"><span class="lnum">  13:  </span>        }</pre>
<pre><span class="lnum">  14:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  15:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(OrderAccepted message)</pre>
<pre><span class="lnum">  16:  </span>        {</pre>
<pre class="alt"><span class="lnum">  17:  </span>            <span class="kwrd">this</span>.Data.ProductIdsInOrder = message.ProductIdsInOrder;</pre>
<pre><span class="lnum">  18:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  19:  </span>            <span class="kwrd">this</span>.Data.CustomerId = message.CustomerId;</pre>
<pre><span class="lnum">  20:  </span>            <span class="kwrd">this</span>.Data.OrderId = message.OrderId;</pre>
<pre class="alt"><span class="lnum">  21:  </span>&nbsp;</pre>
<pre><span class="lnum">  22:  </span>            <span class="kwrd">this</span>.CompleteIfPossible();</pre>
<pre class="alt"><span class="lnum">  23:  </span>        }</pre>
<pre><span class="lnum">  24:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  25:  </span>        <span class="kwrd">private</span> <span class="kwrd">void</span> CompleteIfPossible()</pre>
<pre><span class="lnum">  26:  </span>        {</pre>
<pre class="alt"><span class="lnum">  27:  </span>            <span class="kwrd">if</span> (<span class="kwrd">this</span>.Data.ProductIdsInOrder != <span class="kwrd">null</span> &amp;&amp; <span class="kwrd">this</span>.Data.CustomerHasBeenBilled)</pre>
<pre><span class="lnum">  28:  </span>            {</pre>
<pre><span class="lnum">  29:  </span>                <span class="kwrd">this</span>.Bus.Send&lt;ShipOrderToCustomer&gt;(</pre>
<pre class="alt"><span class="lnum">  30:  </span>                   (m =&gt;</pre>
<pre><span class="lnum">  31:  </span>                   {</pre>
<pre class="alt"><span class="lnum">  32:  </span>                       m.CustomerId = <span class="kwrd">this</span>.Data.CustomerId;</pre>
<pre><span class="lnum">  33:  </span>                       m.OrderId = <span class="kwrd">this</span>.Data.OrderId;</pre>
<pre class="alt"><span class="lnum">  34:  </span>                       m.ProductIdsInOrder = <span class="kwrd">this</span>.Data.ProductIdsInOrder;</pre>
<pre><span class="lnum">  35:  </span>                   }</pre>
<pre class="alt"><span class="lnum">  36:  </span>                   ));</pre>
<pre><span class="lnum">  37:  </span>                <span class="kwrd">this</span>.MarkAsComplete();</pre>
<pre class="alt"><span class="lnum">  38:  </span>            }</pre>
<pre><span class="lnum">  39:  </span>        }</pre>
<pre class="alt"><span class="lnum">  40:  </span>    }</pre>
</div>
<p>And that brings us to&#8230;</p>
<h3>Saga persistence</h3>
<p>We already saw why Shipping needs to be able to look up its internal sagas using data from the events, but what that means is that simple blob-type persistence of those sagas is out. NServiceBus comes with an NHibernate-based saga persister for exactly this reason, though any persistence mechanism which allows you to query on something other than saga Id would work just as well.</p>
<p>Let&#8217;s take a quick look at the saga data that we&#8217;ll be storing and see how simple it is:</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> ShippingSagaData : ISagaEntity</pre>
<pre><span class="lnum">   2:  </span>    {</pre>
<pre class="alt"><span class="lnum">   3:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> Guid Id { get; set; }</pre>
<pre><span class="lnum">   4:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> Originator { get; set; }</pre>
<pre class="alt"><span class="lnum">   5:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> Guid OrderId { get; set; }</pre>
<pre><span class="lnum">   6:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> Guid CustomerId { get; set; }</pre>
<pre class="alt"><span class="lnum">   7:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> List&lt;Guid&gt; ProductIdsInOrder { get; set; }</pre>
<pre><span class="lnum">   8:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">bool</span> CustomerHasBeenBilled { get; set; }</pre>
<pre class="alt"><span class="lnum">   9:  </span>    }</pre>
</div>
<p>You might have noticed the &#8220;Originator&#8221; property in there and wondered what it is for. First of all, the ISagaEntity interface requires the two properties Id and Originator. Originator is used to store the return address of the message that started the saga. Id is for what you think it&#8217;s for. In this saga, we don&#8217;t need to send any messages back to whoever started the saga, but in many others we do. In those cases, we&#8217;ll often be handling a message from some other endpoint when we want to possibly report some status back to the client that started the process. By storing that client&#8217;s address the first time, we can then &#8220;ReplyToOriginator&#8221; at any point in the process.</p>
<p>The manufacturing sample that comes with <a href="http://www.NServiceBus.com">NServiceBus</a> shows how this works.</p>
<h3>Saga Lookup</h3>
<p>Earlier, we saw the need to search for sagas based on order Id. The way to hook into the infrastructure and perform these lookups is by implementing &#8220;IFindSagas&lt;T&gt;.Using&lt;M&gt;&#8221; where T is the type of the saga data and M is the type of message. In our example, doing this using NHibernate would look 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">class</span> ShippingSagaFinder : </pre>
<pre><span class="lnum">   2:  </span>        IFindSagas&lt;ShippingSagaData&gt;.Using&lt;OrderAccepted&gt;,</pre>
<pre class="alt"><span class="lnum">   3:  </span>        IFindSagas&lt;ShippingSagaData&gt;.Using&lt;CustomerBilledForOrder&gt;</pre>
<pre><span class="lnum">   4:  </span>    {</pre>
<pre class="alt"><span class="lnum">   5:  </span>        <span class="kwrd">public</span> ShippingSagaData FindBy(CustomerBilledForOrder message)</pre>
<pre><span class="lnum">   6:  </span>        {</pre>
<pre class="alt"><span class="lnum">   7:  </span>            <span class="kwrd">return</span> FindBy(message.OrderId)</pre>
<pre><span class="lnum">   8:  </span>        }</pre>
<pre class="alt"><span class="lnum">   9:  </span>&nbsp;</pre>
<pre><span class="lnum">  10:  </span>        <span class="kwrd">public</span> ShippingSagaData FindBy(OrderAccepted message)</pre>
<pre class="alt"><span class="lnum">  11:  </span>        {</pre>
<pre><span class="lnum">  12:  </span>            <span class="kwrd">return</span> FindBy(message.OrderId)</pre>
<pre class="alt"><span class="lnum">  13:  </span>        }</pre>
<pre><span class="lnum">  14:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  15:  </span>        <span class="kwrd">private</span> ShippingSagaData FindBy(Guid orderId)</pre>
<pre><span class="lnum">  16:  </span>        {</pre>
<pre class="alt"><span class="lnum">  17:  </span>            <span class="kwrd">return</span> sessionFactory.GetCurrentSession().CreateCriteria(<span class="kwrd">typeof</span>(ShippingSagaData))</pre>
<pre><span class="lnum">  18:  </span>                .Add(Expression.Eq(<span class="str">"OrderId"</span>, orderId))</pre>
<pre class="alt"><span class="lnum">  19:  </span>                .UniqueResult&lt;ShippingSagaData&gt;();</pre>
<pre><span class="lnum">  20:  </span>        }</pre>
<pre class="alt"><span class="lnum">  21:  </span>&nbsp;</pre>
<pre><span class="lnum">  22:  </span>        <span class="kwrd">private</span> ISessionFactory sessionFactory;</pre>
<pre class="alt"><span class="lnum">  23:  </span>&nbsp;</pre>
<pre><span class="lnum">  24:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> ISessionFactory SessionFactory</pre>
<pre class="alt"><span class="lnum">  25:  </span>        {</pre>
<pre><span class="lnum">  26:  </span>            get { <span class="kwrd">return</span> sessionFactory; }</pre>
<pre class="alt"><span class="lnum">  27:  </span>            set { sessionFactory = <span class="kwrd">value</span>; }</pre>
<pre><span class="lnum">  28:  </span>        }</pre>
<pre class="alt"><span class="lnum">  29:  </span>    }</pre>
</div>
<p>For a performance boost, we&#8217;d probably index our saga data by order Id.</p>
<h3>On concurrency</h3>
<p>Another important note is that for this saga, if both messages were handled in parallel on different machines, the saga could get stuck. The persistence mechanism here needs to prevent this. When using NHibernate over a database with the appropriate isolation level (Repeatable Read &#8211; the default in NServiceBus), this &#8220;just works&#8221;. If/When implementing your own saga persistence mechanism, it is important to understand the kind of concurrency your business logic can live with.</p>
<p>Take a look at Ayende&#8217;s example for <a href="http://ayende.com/Blog/archive/2009/01/23/rhino-dht-concurrency-handling-example-ndash-the-phone-billing-system.aspx">mobile phone billing</a> to get a feeling for what that&#8217;s like.</p>
<h3>Summary</h3>
<p>In almost any event-driven architecture, you&#8217;ll have services correlating multiple events in order to make decisions. The saga pattern is a great fit there, and not at all difficult to implement. You do need to take into account that events may arrive out of order and implement the saga logic accordingly, but it&#8217;s really not that big a deal. Do take the time to think through what data will need to be stored in order for the saga to be fault-tolerant, as well as a persistence mechanism that will allow you to look up that data based on event data.</p>
<p>If you feel like giving this approach a try, but don&#8217;t have an environment handy for this, download <a href="http://www.NServiceBus.com">NServiceBus</a> and take a look at the samples. It&#8217;s really quick and easy to get set up.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=B-um4XofEr4:2te2j8_8Wv4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=B-um4XofEr4:2te2j8_8Wv4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=B-um4XofEr4:2te2j8_8Wv4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=B-um4XofEr4:2te2j8_8Wv4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=B-um4XofEr4:2te2j8_8Wv4:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/B-um4XofEr4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/04/20/saga-persistence-and-event-driven-architectures/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/04/20/saga-persistence-and-event-driven-architectures/</feedburner:origLink></item>
		<item>
		<title>NServiceBus 1.9 RTM</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/tlOiJMAYDOw/</link>
		<comments>http://www.udidahan.com/2009/04/15/nservicebus-19-rtm/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 10:07:46 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[NServiceBus]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=990</guid>
		<description><![CDATA[After an additional 3 months of stability seen on the release candidate, I&#8217;m happy to say that nServiceBus has now reached a full version 1.9 release. 
Very little has changed, so the version 1.9 story described here is still accurate.
Just last week one of my clients went live with a rollout to one of the [...]]]></description>
			<content:encoded><![CDATA[<p>After an additional 3 months of stability seen on the release candidate, I&#8217;m happy to say that nServiceBus has now reached a full version 1.9 release. </p>
<p>Very little has changed, so the version 1.9 story described <a href="http://www.udidahan.com/2009/02/07/nservicebus-19/">here</a> is still accurate.</p>
<p>Just last week one of my clients went live with a rollout to one of the world&#8217;s biggest names in the hospitality industry and things are looking good. Since stability is such a big deal to them (and many of my other clients), they&#8217;ve rolled out on nServiceBus 1.8 but now are ready to make the move to 1.9. Hopefully we&#8217;ll be able to get a case study out of them <img src='http://www.udidahan.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>For more information, go to the <a href="http://www.NServiceBus.com">NServiceBus site</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=tlOiJMAYDOw:lBl2yDdIdsg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=tlOiJMAYDOw:lBl2yDdIdsg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=tlOiJMAYDOw:lBl2yDdIdsg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=tlOiJMAYDOw:lBl2yDdIdsg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=tlOiJMAYDOw:lBl2yDdIdsg:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/tlOiJMAYDOw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/04/15/nservicebus-19-rtm/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/04/15/nservicebus-19-rtm/</feedburner:origLink></item>
		<item>
		<title>Backwards-Compatibility: Why Most Versioning Problems Aren’t</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/Yb_MXBPiPTs/</link>
		<comments>http://www.udidahan.com/2009/04/10/backwards-compatibility-why-most-versioning-problems-arenrsquot/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 13:17:17 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Simplicity]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/04/10/backwards-compatibility-why-most-versioning-problems-arenrsquot/</guid>
		<description><![CDATA[
I’ve been to too many clients where I’ve been brought in to help them with their problems around service versioning when the solution I propose is simply to have version N+1 of the system be backwards-compatible with version N. If two adjacent versions of a given system aren’t compatible with each other, it is practically [...]]]></description>
			<content:encoded><![CDATA[<p><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 10px 10px; border-right-width: 0px" height="244" alt="image" src="http://www.udidahan.com/wp-content/ServicesVersioningPubSubandMultipleInher_11E4C/image.png" width="244" align="right" border="0" />
<p>I’ve been to too many clients where I’ve been brought in to help them with their problems around service versioning when the solution I propose is simply to have version N+1 of the system be backwards-compatible with version N. If two adjacent versions of a given system aren’t compatible with each other, it is practically impossible to solve versioning issues.</p>
<p>Here’s what happens when versions aren’t compatible:</p>
<blockquote><p>Admins stop the system from accepting any new requests, and wait until all current requests are done processing. They take a backup/snapshot of all relevant parts of the system (like data in the DB). Then, bring down the system – all of it. Install the new version on all machines. Bring everything back up. Let the users back in.</p></blockquote>
<p>If, heaven-forbid, problems were uncovered with the new version (since some problems only appear in production), the admins have to roll back to the previous version – once again bringing everything down.</p>
<p>This scenario is fairly catastrophic for any company that requires not-even high availability, but pretty continuous availability – like public facing web apps.</p>
<p>If adjacent versions were compatible with each other, we could upgrade the system piece-meal – machine by machine, where both the old and new versions will be running side by side, communicating with each other. While the system’s performance may be sub-optimal, it will continue to be available throughout upgrades as well as downgrades.</p>
<p>This isn’t trivial to do.</p>
<p>It impacts how you decide what is (and more importantly, what isn’t) nullable.</p>
<p>It may force you to spread certain changes to features across more versions (aka releases).</p>
<p>As such, you can expect this to affect how you do release and feature planning.</p>
<p>However, if you do not take these factors into account, it’s almost a certainty that your versioning problems will persist and no technology (new or old) will be able to solve them.</p>
<p>Coming next… Units of versioning – inside and outside a service.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=Yb_MXBPiPTs:V7EU0C0IQOU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=Yb_MXBPiPTs:V7EU0C0IQOU:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=Yb_MXBPiPTs:V7EU0C0IQOU:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=Yb_MXBPiPTs:V7EU0C0IQOU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=Yb_MXBPiPTs:V7EU0C0IQOU:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/Yb_MXBPiPTs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/04/10/backwards-compatibility-why-most-versioning-problems-arenrsquot/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/04/10/backwards-compatibility-why-most-versioning-problems-arenrsquot/</feedburner:origLink></item>
		<item>
		<title>MSDN Magazine Smart Client Article</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/4bQFRoCjsOc/</link>
		<comments>http://www.udidahan.com/2009/03/28/msdn-magazine-smart-client-article/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 19:16:39 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[Smart Client]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/03/28/msdn-magazine-smart-client-article/</guid>
		<description><![CDATA[
My article on “optimizing a large-scale Software+Services application” has been published in the April edition of MSDN Magazine.
Here’s a short excerpt:
“We had to juggle occasional connectivity, data synchronization, and publish/subscribe all at the same time. We learned that we couldn’t solve all problems either client-side or server-side, but rather that an integrated approach was needed [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/magazine/dd569749.aspx"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://www.udidahan.com/wp-content/uploads/MSDNMagazineSmartClientArticle_13E17/image.png" width="189" align="right" border="0" /></a></p>
<p>My article on “optimizing a large-scale Software+Services application” has been published in the April edition of MSDN Magazine.</p>
<p>Here’s a short excerpt:</p>
<blockquote><p>“We had to juggle occasional connectivity, data synchronization, and publish/subscribe all at the same time. We learned that we couldn’t solve all problems either client-side or server-side, but rather that an integrated approach was needed since any changes on one side needed corresponding changes on the other side.”</p></blockquote>
<p><a href="http://msdn.microsoft.com/en-us/magazine/dd569749.aspx">Continue reading… </a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=4bQFRoCjsOc:bzR4IVm3JwM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=4bQFRoCjsOc:bzR4IVm3JwM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=4bQFRoCjsOc:bzR4IVm3JwM:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=4bQFRoCjsOc:bzR4IVm3JwM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=4bQFRoCjsOc:bzR4IVm3JwM:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/4bQFRoCjsOc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/03/28/msdn-magazine-smart-client-article/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/03/28/msdn-magazine-smart-client-article/</feedburner:origLink></item>
		<item>
		<title>ALT.NET Seattle 2009</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/-Zl0ay_nsyE/</link>
		<comments>http://www.udidahan.com/2009/02/25/altnet-seattle-2009/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 09:40:39 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Community]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/02/25/altnet-seattle-2009/</guid>
		<description><![CDATA[I&#8217;ll be coming in to Seattle for the ALT.NET summit this Friday. It&#8217;ll be giving a half-day tutorial on SOA and look forward to highlighting the number one fallacy that gets people thinking the wrong way about services.
Those of you following me on twitter (here) have an inkling that it has to do with the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll be coming in to Seattle for the ALT.NET summit this Friday. It&#8217;ll be giving a half-day tutorial on SOA and look forward to highlighting the number one fallacy that gets people thinking the wrong way about services.</p>
<p>Those of you following me on twitter (<a href="http://twitter.com/UdiDahan">here</a>) have an inkling that it has to do with the connection between user interfaces and services &#8211; but that&#8217;ll have to wait. Don&#8217;t want to steal my own thunder.</p>
<p>A nice meaty blog post on the topic is coming.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=-Zl0ay_nsyE:9bMPJz_SE0U:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=-Zl0ay_nsyE:9bMPJz_SE0U:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=-Zl0ay_nsyE:9bMPJz_SE0U:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=-Zl0ay_nsyE:9bMPJz_SE0U:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=-Zl0ay_nsyE:9bMPJz_SE0U:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/-Zl0ay_nsyE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/02/25/altnet-seattle-2009/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/02/25/altnet-seattle-2009/</feedburner:origLink></item>
		<item>
		<title>Messaging ROI</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/bU6tC-Gd-Rs/</link>
		<comments>http://www.udidahan.com/2009/02/22/messaging-roi/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 10:12:59 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/02/22/messaging-roi/</guid>
		<description><![CDATA[There&#8217;s been some recent discussion as to the &#8220;cost&#8221; of messaging:
Greg Young asserts: 
&#8220;I believe that this shows there to be a rather negligible cost associated with the use of such a model. There is however a small cost, this cost however I believe only exists when one looks at the system in isolation.&#8221;

Ayende adds [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s been some recent discussion as to the &#8220;cost&#8221; of messaging:</p>
<p>Greg Young <a href="http://codebetter.com/blogs/gregyoung/archive/2009/02/09/cost.aspx">asserts</a>:<a href="http://codebetter.com/blogs/gregyoung/archive/2009/02/09/cost.aspx"><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="79" alt="image" src="http://www.udidahan.com/wp-content/uploads/image54.png" width="79" align="right" border="0"></a> </p>
<blockquote><p>&#8220;I believe that this shows there to be a rather negligible cost associated with the use of such a model. There is however a small cost, this cost however I believe only exists when one looks at the system in isolation.&#8221;</p>
</blockquote>
<p>Ayende adds <a href="http://ayende.com/Blog/archive/2009/02/09/the-cost-of-messaging.aspx">his perspective</a>:<a href="http://ayende.com/Blog/archive/2009/02/09/the-cost-of-messaging.aspx"><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="77" alt="image" src="http://www.udidahan.com/wp-content/uploads/image55.png" width="85" align="right" border="0"></a> </p>
<blockquote><p>&#8220;The cost of messaging, and a very real one, comes when you need to understand the system. In a system where message exchange is the form of communication, it can be significantly harder to understand what is going on.&#8221;</p>
</blockquote>
<p>Of course, both these intelligent fellows are right. The reason for the apparent disparity in viewpoints has to do with which part of the following graph you look at. Ayende zooms in on the left side:</p>
<p><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="225" alt="left graph" src="http://www.udidahan.com/wp-content/uploads/image56.png" width="404" border="0"> </p>
<p>As systems get larger, though, the only way to understand them is by working at higher levels of abstraction. That&#8217;s where messaging really shines, as the incremental complexity remains the same by maintaining the same modularity as before:</p>
<p><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="232" alt="full graph" src="http://www.udidahan.com/wp-content/uploads/image57.png" width="404" border="0"> </p>
<p>In Ayende&#8217;s post, he follows the design I described a while back on using messaging for user management and <a href="http://www.udidahan.com/2007/11/10/asynchronous-high-performance-login-for-web-farms/">login for a high-scale web scenario</a>. In his comments, he agrees with the above stating:</p>
<blockquote><p>&#8220;I certainly think that a similar solution using RPC would be much more complex and likely more brittle.&#8221;</p>
</blockquote>
<p>I feel quite conservative in saying the most enterprise solutions fall on the right side of the intersection in the graph.</p>
<p>That being said, don&#8217;t underestimate the learning curve developers go through with messaging. While the mechanics are similar, the mindset is very different. Think about it like this:<a href="http://www.udidahan.com/wp-content/uploads/image58.png"><img style="border-right: 0px; border-top: 0px; margin: 5px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="100" alt="image" src="http://www.udidahan.com/wp-content/uploads/image-thumb36.png" width="80" align="right" border="0"></a> </p>
<blockquote><p>You&#8217;ve driven a car for years in the US. It&#8217;s practically second nature. Then you fly to the UK, rent a car, and all of a sudden, your brain is in meltdown. (or vice versa for those going from the UK to the US)</p>
</blockquote>
<h3>Summary</h3>
<p>If you are going down the messaging route, please be aware that there are shades of gray there as well. You don&#8217;t <em>have</em> to implement your user management and login the way I outlined in my post if you don&#8217;t require such high levels of scalability, but even lower levels of scalability can benefit from messaging.</p>
<p>Just as there isn&#8217;t a single correct design for non-messaging solutions, the same is true for those using messaging. Finding the right balance is tricky, and critical. </p>
<p>When the code is simple in every part of the system, and the asynchronous interactions are what provide for the necessary complexity the problem domain requires, that&#8217;s when you know you&#8217;ve got it just right.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=bU6tC-Gd-Rs:Z--t5W-WwOg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=bU6tC-Gd-Rs:Z--t5W-WwOg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=bU6tC-Gd-Rs:Z--t5W-WwOg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=bU6tC-Gd-Rs:Z--t5W-WwOg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=bU6tC-Gd-Rs:Z--t5W-WwOg:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/bU6tC-Gd-Rs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/02/22/messaging-roi/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/02/22/messaging-roi/</feedburner:origLink></item>
		<item>
		<title>97 Things</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/1I4NClRpMAE/</link>
		<comments>http://www.udidahan.com/2009/02/15/97-things/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 16:04:37 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Community]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/02/15/97-things/</guid>
		<description><![CDATA[It looks like one of the community projects that I&#8217;ve been involved with has reached maturity:

97 Things Every Software Architect Should Know

Collective Wisdom from the Experts


Definitely worth checking out.
]]></description>
			<content:encoded><![CDATA[<p>It looks like one of the community projects that I&#8217;ve been involved with has reached maturity:</p>
<p>
<div style="font-size:20px; font-weight:bold;">97 Things Every Software Architect Should Know</div>
<p><br/>
<div style="font-size:16px; font-weight:bold;">Collective Wisdom from the Experts</div>
</p>
<p><a href="http://www.amazon.com/dp/059652269X"><img src="http://www.udidahan.com/wp-content/uploads/97_things_architect.jpg" width="153" height="227" style="border:none;" /></a></p>
<p>Definitely worth checking out.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=1I4NClRpMAE:7GS-avBKpP0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=1I4NClRpMAE:7GS-avBKpP0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=1I4NClRpMAE:7GS-avBKpP0:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=1I4NClRpMAE:7GS-avBKpP0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=1I4NClRpMAE:7GS-avBKpP0:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/1I4NClRpMAE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/02/15/97-things/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/02/15/97-things/</feedburner:origLink></item>
		<item>
		<title>Architecture Days in Spain</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/sQBb4fM3veU/</link>
		<comments>http://www.udidahan.com/2009/02/11/architecture-days-in-spain/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 09:06:53 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Courses]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/02/11/architecture-days-in-spain/</guid>
		<description><![CDATA[The good folks from iMeta will be having me over in Spain to give my full-day SOA+DDD tutorial. Price looks pretty attractive too. Hope to see you there.
Register here

]]></description>
			<content:encoded><![CDATA[<p>The good folks from iMeta will be having me over in Spain to give my full-day SOA+DDD tutorial. Price looks pretty attractive too. Hope to see you there.</p>
<p><a href="http://apps.imeta.com/events/Home/EventDetails/1">Register here</a></p>
<p><a href="http://www.udidahan.com/wp-content/uploads/image53.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="376" alt="Architecture days" src="http://www.udidahan.com/wp-content/uploads/image-thumb35.png" width="404" border="0"></a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=sQBb4fM3veU:arWGOuck5uc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=sQBb4fM3veU:arWGOuck5uc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=sQBb4fM3veU:arWGOuck5uc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=sQBb4fM3veU:arWGOuck5uc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=sQBb4fM3veU:arWGOuck5uc:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/sQBb4fM3veU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/02/11/architecture-days-in-spain/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/02/11/architecture-days-in-spain/</feedburner:origLink></item>
		<item>
		<title>NServiceBus 1.9</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/XuaNU6bnuhg/</link>
		<comments>http://www.udidahan.com/2009/02/07/nservicebus-19/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 20:13:28 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[NServiceBus]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/02/07/nservicebus-19/</guid>
		<description><![CDATA[It&#8217;s been about 6 months since my last post on nServiceBus. In that time about 1000 people have subscribed to this blog and many of them don&#8217;t know anything about it. Also, version 1.9 of nServiceBus appears to be solid enough to drop its &#8220;release candidate&#8221; qualification so this seems like a good time for [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been about 6 months since my last post on nServiceBus. In that time about 1000 people have subscribed to this blog and many of them don&#8217;t know anything about it. Also, version 1.9 of nServiceBus appears to be solid enough to drop its &#8220;release candidate&#8221; qualification so this seems like a good time for this kind of post.<a title="NServiceBus" href="http://www.nservicebus.com"><img style="border-right: 0px; border-top: 0px; margin: 0px 5px 10px 10px; border-left: 0px; border-bottom: 0px" height="73" alt="nServiceBus_banner_2" src="http://www.udidahan.com/wp-content/uploads/nservicebus-banner-2.png" width="244" align="right" border="0"></a> </p>
<h3>What is it?</h3>
<p>From the <a href="http://www.nservicebus.com/">NServiceBus.com</a> site:</p>
<blockquote><p>&#8220;NServiceBus is a powerful, yet lightweight, open source messaging framework <br />for designing distributed .NET enterprise systems. Entirely pluggable yet simple to use, NServiceBus gives programmers a head-start on developing robust, scalable, <br />and maintainable service-layers and long-running business processes.&#8221;</p>
</blockquote>
<p>One of the developers who downloaded nServiceBus, Jürgen, sent me this in an email:</p>
<blockquote><p>&#8220;I took the samples, swapped in my own code, and had a machines subscribing, publishing, messaging, in like 15 minutes. </p>
<p>I&#8217;ve got to tell you &#8211; from reading the architecture stuff on your blog I always thought this stuff was going to be hard. I know you always say its supposed to be simple but I never really believed it. I mean, seriously, if I had to do this stuff with web services or WCF &#8211; well, I wouldn&#8217;t know where to begin.&#8221;</p>
</blockquote>
<p>(His English totally surprised me &#8211; not what you&#8217;d expect from someone called Jürgen. Born in Sweden but grew up in the US.)</p>
<p>BTW, <a href="http://samgentile.com/Web/neuron-esb/enterprise-service-buses-esbs-drive-soa-adoption-part-4/">Sam&#8217;s showed where to begin with WCF:</a> basic pub/sub without durability is 480 LOC. </p>
<h3>What&#8217;s different &#8211; Containers</h3>
<p>People who looked at earlier versions of nServiceBus will see many incremental improvements that smooth over previously rough parts of the framework.</p>
<p>Chris Patterson, co-founder of MassTransit mentioned one such area in his <a href="http://www.lostechies.com/blogs/chris_patterson/archive/2008/12/26/masstransit-turns-one-year-old-celebrations-held-around-the-world.aspx">blog post</a>:</p>
<blockquote><p>&#8220;Both Dru and I needed a framework for asynchronous messaging to address some work-related application requirements. While MSMQ is provided out of the box [on Windows], it doesn&#8217;t directly encourage some good distributed application practices such a loose coupling. Our goal was to abstract the messaging aspects so the services could be built to deal with plain old objects (POCOs) instead of lower level transport messages.</p>
<p>Originally, we both looked at <a href="http://www.nservicebus.com/">NServiceBus</a> as a way to make this happen. I&#8217;ve followed Udi&#8217;s blog for a while and have really gained a lot of knowledge from his posts and presentations. However, our lack of experience in Spring.NET, along with a general lack of understanding of all the complexity of such a framework led us down the path of building our own framework.&#8221;</p>
</blockquote>
<p>Version 1.9 takes a totally different approach (than v1.6/1.7 at which I believe Chris was looking back then) to dependency injection frameworks (like Spring.NET) and decreases their footprint. Developers don&#8217;t need to know anything about Spring, Castle, or any other container to get started, but always have the ability to configure it however they want and even swap in their container of choice.</p>
<h3>What&#8217;s different &#8211; DLL Footprint</h3>
<p>Another difference is the number of assemblies that come with nServiceBus. Nathan Stults had <a href="http://www.thefreakparade.com/2008/06/simple-service-bus-on-codeplex-a-fork-of-nservicebus/">this to say</a> about version 1.8:<br />
<blockquote>
<p>&#8220;NServiceBus (for valid architectural reasons) is split up into eleven thousand, two hundred and nine different DLL’s. That may be an exaggeration, but many of the dll’s have only one or two code files in them, with only a few lines of code each.&#8221;</p>
</blockquote>
<p>NServiceBus has always supported swapping out various technological implementations and will continue do so. For that reason, the development environment is organized into 79 projects whose dependencies are managed very strictly. As of version 1.9, most of these assemblies including many supporting libraries like Spring and Castle have been merged into NServiceBus.dll. There&#8217;s also NServiceBus.Testing.dll which supports fluent-unit-testing entire business processes.
<p>There is one non-optional external dependency which hasn&#8217;t been merged and that is log4net &#8211; although if you configure in a Common.Logging provider to your own logging infrastructure, you can do without it as well. With log4net, the minimum deployment footprint is 2 assemblies. The other dependency which <em>is</em> optional is NHibernate. The reason for leaving these out is that many teams depend on specific versions of those assemblies.
<p>In short &#8211; you reference ONE assembly. Just one.<br />
<h3>&nbsp;</h3>
<h3>What else?</h3>
<p>There is quite a lot in there. Ayende&#8217;s put out several posts describing those features and similarities to the bus he&#8217;s working on:</p>
<ul>
<li><a href="http://ayende.com/Blog/archive/2009/01/14/rhino-service-bus-managing-timeouts.aspx">Managing Timeouts</a></li>
<li><a href="http://ayende.com/Blog/archive/2009/01/16/rhino-service-bus-saga-and-state.aspx">Long-running processes</a></li>
<li><a href="http://ayende.com/Blog/archive/2008/03/24/NServiceBus-Distributor-Review.aspx">Load Balancing</a></li>
</ul>
<p>Ayende&#8217;s description of the NServiceBus load-balancing capability was:</p>
<blockquote><p>&#8220;The distributor section of NServiceBus is a thing of beauty.&#8221;</p>
</blockquote>
<h3>Performance</h3>
<p>You may be surprised by the kind of performance we&#8217;re able to wring out of &#8220;basic&#8221; MSMQ. Keep in mind, though, that you can swap in your own transport &#8211; there are already some others out there on the <a href="http://code.google.com/p/nservicebus-contrib/">Contrib site</a> including ActiveMQ and shared memory.</p>
<p>The most thorough performance numbers I&#8217;ve seen on nServiceBus have been written up <a href="http://www.udidahan.com/2008/05/21/nservicebus-performance/">here</a>:</p>
<blockquote><p>&#8220;total throughput over 1 billion messages an hour. That was about 100 million per hour durable, 900 million per hour non-durable&#8221;</p>
</blockquote>
<p>This was on an 3 blade centers (48 blades), 30 pizza boxes (1U), and 20 clusters, version 1.8.</p>
<p>On <a href="http://gojko.net/2008/12/02/asynchronous-net-applications-with-nservicebus/">Gojko&#8217;s blog</a> there is a video of talk on nServiceBus where Dave de Florinier mentioned throughputs of 600,000 messages per hour (no mention of supporting hardware) and I think it was version 1.7 or 1.8 of nServiceBus.</p>
<p>Recently, Raymond Lewallen posted his numbers for 1.9 to the <a href="http://tech.groups.yahoo.com/group/nservicebus/message/1791">discussion group</a>:</p>
<blockquote><p>&#8220;We load tested our full duplex and pub/sub scenarios the other day at about 500 messages per second with no hiccups at all.&#8221; (save yourself the math, its 1.8M/hr)</p>
</blockquote>
<p>The really interesting numbers are coming in from one of my clients (on 1.9) where the focus is on business process (saga) throughput where they&#8217;re seeing millions completing each day on top of IO intensive technical processes. I&#8217;m excited.</p>
<h3>Looking forward</h3>
<p>After working with clients using nServiceBus over 4 years now (even before it was open source or had a real name), it just keeps getting better. It&#8217;s also really great to see more open source projects coming on the scene &#8211; MassTransit (<a href="http://www.lostechies.com/blogs/chris_patterson/archive/2008/12/26/masstransit-turns-one-year-old-celebrations-held-around-the-world.aspx">now a year old</a>), and <a href="http://ayende.com/Blog/archive/2008/12/17/rhino-service-bus.aspx">Rhino Service Bus</a> (young, but in production with NH-Prof). </p>
<p>The mutual <strike>stealing</strike> cross-pollination is increasing the pace all around.</p>
<p>I intend to take up <a href="http://code.google.com/p/topshelf/">TopShelf</a> (which came out of MassTransit) as the generic service host. That combined with the robust distributor hosting model will make &#8220;grid-friendly&#8221; nServiceBus endpoints much easier. Ayende&#8217;s posts about automatically creating queues and other &#8220;first-time-developer&#8221; features have really worked to decrease the nServiceBus learning curve &#8211; and it shows.</p>
<p>Documentation is a notorious problem in open-source projects and nServiceBus hasn&#8217;t escaped it. API and internal documentation is only now getting close to 100% and the samples now give developers a good start on using it. Those developers looking at swapping out certain bits of functionality have a harder time, but that&#8217;s slowly improving as well. The <a href="http://tech.groups.yahoo.com/group/nservicebus/">discussion group</a> is the best place to get those hard-to-find answers now with a community over 200 strong.</p>
<p>The difficulty developers have in adopting nServiceBus is giving up &#8220;request/response&#8221; thinking. This requires an adjustment to a system&#8217;s architecture and is what most of this blog has been about. Conversely, if you have been following this blog and this thinking resonates with you, you&#8217;ll find it very simple and straight-forward. The <a href="http://www.nservicebus.com/Overview.aspx">overview page</a> on NServiceBus.com also gives a good description of messaging basics &#8211; stuff that I&#8217;ve kind of glossed over on this blog so far.</p>
<p>Go on then. <strong><a href="http://www.nservicebus.com">Take it for a spin</a></strong>. Write a review. Sam Gentile had <a href="http://samgentile.com/blogs/samgentile/archive/2008/06/24/looking-at-nservicebus-added-to-tonight-s-presentation.aspx">this to say</a> about v1.8:</p>
<blockquote><p>&#8220;The bottom line is: I like what I see. Although it&#8217;s a framework, not an ESB product like Neuron, it&#8217;s a powerful framework that takes the right approach on SOA and enforces a paradigm of reliable one-way, *non-blocking* calls.&#8221;</p>
</blockquote>
<p>Can&#8217;t wait to hear the response to 1.9.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=XuaNU6bnuhg:9PIs0GO-Rew:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=XuaNU6bnuhg:9PIs0GO-Rew:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=XuaNU6bnuhg:9PIs0GO-Rew:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=XuaNU6bnuhg:9PIs0GO-Rew:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=XuaNU6bnuhg:9PIs0GO-Rew:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/XuaNU6bnuhg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/02/07/nservicebus-19/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/02/07/nservicebus-19/</feedburner:origLink></item>
		<item>
		<title>ALT.NET DDD Podcast</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/52Ke-0c6nxQ/</link>
		<comments>http://www.udidahan.com/2009/01/26/altnet-ddd-podcast/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 20:36:08 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[DDD]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/01/26/altnet-ddd-podcast/</guid>
		<description><![CDATA[ I finally got around to listening to the alt.net podcast on domain driven design and heard Rob Conery telling about his experiences with DDD. I&#8217;ve met a fair amount of developers that went through a similar process and thought that I could help fix some of the common misconceptions that pop up when developers [...]]]></description>
			<content:encoded><![CDATA[<p> I finally got around to listening to the alt.net podcast on domain driven design and heard Rob Conery telling about his experiences with DDD. I&#8217;ve met a fair amount of developers that went through a similar process and thought that I could help fix some of the common misconceptions that pop up when developers start down the DDD path.</p>
<p><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 0px 20px; border-left: 0px; border-bottom: 0px" height="308" alt="fish_boy_cat_different_perspectives" src="http://www.udidahan.com/wp-content/uploads/fish-boy-cat-different-perspectives.jpg" width="504" border="0"></p>
<h3>Factory methods on repositories</h3>
<p>In the podcast, Rob describes an ecommerce application with orders, customers, products &#8211; all the stuff you&#8217;ve come to expect. In his pre-DDD design, the order class had multiple constructors representing different rules. Feeling the pain in testing and maintainability, Rob looked to use DDD principles. What he did to get rid of these constructors was to make use of the repository by creating methods for the various cases, like <font face="Courier New" size="2">OrderRepository.CreateOrderForGovernmentCustomer(/*data*/);</font> .</p>
<p>While this is better than the multiple constructors, it still has a way to go. Analyzing what&#8217;s going on here we understand that the way the order is created is dependent upon who the user is. The rules dictating terms of payment are probably different for government customers. Not only that but we know that the order created needs to be connected to that user.</p>
<h3>Aggregate Roots &amp; Polymorphism</h3>
<p>For all these reasons, it looks like user, or customer, is our aggregate root. Thus, rather than our service layer calling the above method on the repository, we first get the user object by id, then create the order like so:</p>
<blockquote><p><font face="Courier New" size="2">IUser u = session.Get&lt;IUser&gt;(IdOfUserLoggedIn);<br />u.CreateOrder(/* data */);</font></p></blockquote>
<p>This way, our service layer doesn&#8217;t need to perform all sorts of business logic (if the user is a gov&#8217;t user, do this, a corporate customer, do that, etc). All of that gets encapsulated by the domain. By leveraging some polymorphism, the session will return an instance of the correct class when we ask it for a user by id. Thus, logic relating to how gov&#8217;t users create orders is encapsulated in the GovernmentUser class.</p>
<p>I&#8217;ve found that this pattern of having polymorphic aggregate roots is very useful and broadly applicable.</p>
<h3>Bounded Contexts</h3>
<p>Further into the podcast, Rob talks about how separating his system into 2 bounded contexts simplified the code greatly. The understanding that accepting an order and fulfilling an order require different logic, different data, and thus, different domain model objects is DDD at its finest.</p>
<p>However, when talking about the total cost of the order, it wasn&#8217;t clear what was responsible for that. From a pure programming perspective, we might think that Total was simply a property on Order or, at most, a GetTotal() method. Yet by looking at what is involved in calculating the total of an order, a different picture emerges:</p>
<p>The total cost of an order obviously includes all relevant taxes. We need to take into account state and federal taxes, tax-free items at each level, etc. There&#8217;s a fair amount of logic here. Once we start to take into account promotions like &#8220;buy one, get one free&#8221;, things get even more involved. There are also cases where refunds are applied within the same order that impact the total and tax (no tax on refunds). Finally, when we include shipping charges, tax on shipping, and other rules between all of the above, it&#8217;s clear that if we put all of this in a single method, we&#8217;ve got ourselves a big bloated sack of &#8230; well, you get the picture.</p>
<p>Separating all of this logic into different bounded contexts makes sense.</p>
<p>That is, until you think about how you&#8217;re going to take these and stitch out of them a single result shown to the user. This is the advanced side of DDD and ties into SOA, so I&#8217;ll leave it for a different post.</p>
<h3>In Closing</h3>
<p>Working with DDD provides a great deal of value by tying our code much more closely to business concepts and encapsulating business rules in the domain model.</p>
<p>Starting down the DDD path is intuitive and the code that results (like u.CreateOrder) is very understandable. Yet, as more DDD principles like Bounded Contexts are put to use, developers often find themselves in unfamiliar, less-intuitive waters. This is to be expected. </p>
<p>Some developers (and vendors) look at DDD as nothing more than the domain model and repository patterns. The truth is that they&#8217;re just the beginning. </p>
<p>I hope that this post has given those of you just starting down the DDD path some feeling for how deep the rabbit hole goes, and I assure you that there are patterns in place to answer all your questions. While those beginning DDD often say that it gives names to things they&#8217;ve always been doing, or always wanted to do, I can assure you that the further down you go, that is less and less the case.</p>
<p>Be ready to have some basic architectural assumptions shaken <img src='http://www.udidahan.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=52Ke-0c6nxQ:v3I3x7OXfBE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=52Ke-0c6nxQ:v3I3x7OXfBE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=52Ke-0c6nxQ:v3I3x7OXfBE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=52Ke-0c6nxQ:v3I3x7OXfBE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=52Ke-0c6nxQ:v3I3x7OXfBE:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/52Ke-0c6nxQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/01/26/altnet-ddd-podcast/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		<feedburner:origLink>http://www.udidahan.com/2009/01/26/altnet-ddd-podcast/</feedburner:origLink></item>
	</channel>
</rss>
