<?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: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>.Net Development Expert &amp; SOA Specialist</description>
	<pubDate>Mon, 29 Jun 2009 11:52:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</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>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 - that Customer is an aggregate root. Unfortunately - 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 - 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 - always get an entity. At least one. Also, don&#8217;t add any objects to the session or unit of work explicitly - 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 - 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 - it wasn&#8217;t our job. So here&#8217;s the breaking news - 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 - 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 - 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 - 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 - 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>
		<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 - 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 - 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 - 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 - &#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 - 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 - 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 - 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 - 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>
		<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 - 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 - 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 - 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 - 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>
		<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 - with some time for questions at end.
Update: Recording is now online - 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 - with some time for questions at end.</p>
<p><B>Update:</B> Recording is now online - <a href="http://tinyurl.com/ozafm3">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>
		<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 - 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>
		<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 - 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>
		<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 - 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 - 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 - 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 - 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>
		<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>
		<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>
		<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>
		<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 - 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:J1-_-mIQ_as: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:J1-_-mIQ_as:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=-Zl0ay_nsyE:J1-_-mIQ_as:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=-Zl0ay_nsyE:J1-_-mIQ_as:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=-Zl0ay_nsyE:J1-_-mIQ_as: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>
		<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>
		<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>
		<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>
		<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 - 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 - well, I wouldn&#8217;t know where to begin.&#8221;</p>
</blockquote>
<p>(His English totally surprised me - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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:QwQC2qkAaBU: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:QwQC2qkAaBU:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=XuaNU6bnuhg:QwQC2qkAaBU:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=XuaNU6bnuhg:QwQC2qkAaBU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=XuaNU6bnuhg:QwQC2qkAaBU: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>
		<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 - 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:CmeyXe-YWsM: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:CmeyXe-YWsM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=52Ke-0c6nxQ:CmeyXe-YWsM:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=52Ke-0c6nxQ:CmeyXe-YWsM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=52Ke-0c6nxQ:CmeyXe-YWsM: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>
		<feedburner:origLink>http://www.udidahan.com/2009/01/26/altnet-ddd-podcast/</feedburner:origLink></item>
		<item>
		<title>DDD &amp; Many to Many Object Relational Mapping</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/ziW65XmjxyY/</link>
		<comments>http://www.udidahan.com/2009/01/24/ddd-many-to-many-object-relational-mapping/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 19:47:02 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
		
		<category><![CDATA[Architecture]]></category>

		<category><![CDATA[DDD]]></category>

		<category><![CDATA[Data Access]]></category>

		<category><![CDATA[Databases]]></category>

		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/01/24/ddd-many-to-many-object-relational-mapping/</guid>
		<description><![CDATA[ The ability to map entity relationships is broadly supported by many O/RM tools. For some reason, though, many developers run into issues when trying to map a many-to-many relationship between entities. Although much has already been written about the technological aspects of it, I thought I&#8217;d take more of an architectural / DDD perspective [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 10px 10px; border-right-width: 0px" height="150" alt="many to many" src="http://www.udidahan.com/wp-content/uploads/image52.png" width="150" align="right" border="0"> The ability to map entity relationships is broadly supported by many O/RM tools. For some reason, though, many developers run into issues when trying to map a many-to-many relationship between entities. Although much has already been written about the technological aspects of it, I thought I&#8217;d take more of an architectural / DDD perspective on it here.</p>
<h3>Value Objects Don&#8217;t Count</h3>
<p>While the canonical example presented is Customer -&gt; Address, and has a good treatment <a href="http://devlicio.us/blogs/billy_mccafferty/archive/2008/07/11/when-to-use-many-to-one-s-vs-many-to-many-with-nhibernate.aspx">here</a> for nHibernate, it isn&#8217;t architecturally representative.</p>
<p>Addresses are value objects. What this means is that if we have to instance of the Address class, and they both have the same business data, they are semantically equivalent. Customers, on the other had, are not value objects - they&#8217;re entities. If we have two customers with the same business data (both of them called Bob Smith), that does not mean they are semantically equivalent - they are not the same person.</p>
<h3>All Entities</h3>
<p>Therefore, for our purposes here we&#8217;ll use something different. Say we have an entity called Job which is something that a company wants to hire for. It has a title, description, skill level, and a bunch of other data. Say we also have another entity called Job Board which is where the company posts jobs so that applicants can see them, like Monster.com. A job board has a name, description, web site, referral fee, and a bunch of other data.</p>
<p>A job can be posted to multiple job boards. And a job board can have multiple jobs posted. A regular many to many relationship. At this point, we&#8217;re not even going to complicate the association.</p>
<p>This is simply represented in the DB with an association table containing two columns for each of the entity tables&#8217; ids. </p>
<p>In the domain model, developers can also represent this with the Job class containing a list of JobBoard instances, and the JobBoard class containing a list of jobs.</p>
<p>It&#8217;s intuitive. Simple. Easy to implement. And wrong.</p>
<p>In order to make intelligent DDD choices, we&#8217;re going to first take what may seem to be a tangential course, but I assure you that your aggregate roots depend on it.</p>
<h3>Moving forward with our example</h3>
<p>Let&#8217;s say the user picks a job, and then ticks off the job boards where they want the job posted, and clicks submit.</p>
<p>For simplicity&#8217;s sake, at this point, let&#8217;s ignore the communication with the actual job sites, assuming that if we can get the association into the DB, magic will happen later causing the job to appear on all the sites.</p>
<p>Our well-intentioned developer takes the job ID, and all the job board IDs, opens a transaction, gets the job object, gets the job board objects, adds all the job board objects to the job, and commits, as follows:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ -->
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> PostJobToBoards(Guid jobId, <span class="kwrd">params</span> Guid[] boardIds)</pre>
<pre><span class="lnum">   2:  </span>        {</pre>
<pre class="alt"><span class="lnum">   3:  </span>            <span class="kwrd">using</span> (ISession s = <span class="kwrd">this</span>.SessionFactory.OpenSession())</pre>
<pre><span class="lnum">   4:  </span>            <span class="kwrd">using</span> (ITransaction tx = s.BeginTransaction())</pre>
<pre class="alt"><span class="lnum">   5:  </span>            {</pre>
<pre><span class="lnum">   6:  </span>                var job = s.Get&lt;Job&gt;(jobId);</pre>
<pre class="alt"><span class="lnum">   7:  </span>                var boards = <span class="kwrd">new</span> List&lt;JobBoard&gt;();</pre>
<pre><span class="lnum">   8:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   9:  </span>                <span class="kwrd">foreach</span>(Guid id <span class="kwrd">in</span> boardIds)</pre>
<pre><span class="lnum">  10:  </span>                    boards.Add(s.Get&lt;JobBoard&gt;(id));</pre>
<pre class="alt"><span class="lnum">  11:  </span>&nbsp;</pre>
<pre><span class="lnum">  12:  </span>                job.PostTo(boards);</pre>
<pre class="alt"><span class="lnum">  13:  </span>&nbsp;</pre>
<pre><span class="lnum">  14:  </span>                tx.Commit();</pre>
<pre class="alt"><span class="lnum">  15:  </span>            }</pre>
<pre><span class="lnum">  16:  </span>        }</pre>
</div>
<p>In this code, Job is our aggregate root. You can see that is the case since Job is the entry point that the service layer code uses to interact with the domain model. Soon we&#8217;ll see why this is wrong.</p>
<p>** Notice that in this service layer code, our well-intentioned developer is following the rule that while you can get as many objects as you like, you are only allowed one method call on one domain object. The code called in line 12 is what you&#8217;d pretty much expect:</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> PostTo(IList&lt;JobBoard&gt; boards)</pre>
<pre><span class="lnum">   2:  </span>        {</pre>
<pre class="alt"><span class="lnum">   3:  </span>            <span class="kwrd">foreach</span>(JobBoard jb <span class="kwrd">in</span> boards)</pre>
<pre><span class="lnum">   4:  </span>            {</pre>
<pre class="alt"><span class="lnum">   5:  </span>                <span class="kwrd">this</span>.JobBoards.Add(jb);</pre>
<pre><span class="lnum">   6:  </span>                jb.Jobs.Add(<span class="kwrd">this</span>);</pre>
<pre class="alt"><span class="lnum">   7:  </span>            }</pre>
<pre><span class="lnum">   8:  </span>        }</pre>
</div>
<p>Only that as we were committing, someone deleted one of the job boards just then. Or that someone updated the job board causing a concurrency conflict. Or anything that would cause one single association to not be created.</p>
<p>That would cause the whole transaction to fail and all changes to roll back.</p>
<p>Rightly so, thinks our well-intentioned developer.</p>
<p>But users don&#8217;t think like well-intentioned developers.</p>
<h3>Partial Failures</h3>
<p>If I were to go to the grocery store with the list my wife gave me, finding that they&#8217;re out of hazelnuts (the last item on the list), would NOT buy all the other groceries and go home empty handed, what do you think would happen?</p>
<p>Right. That&#8217;s how users look at us developers. Before running off and writing a bunch of code, we need to understand the business semantics of users actions, including asking about partial failures.</p>
<p>The list isn&#8217;t a unit of work that needs to succeed or rollback atomically. It&#8217;s actually many units of work. I mean, I wouldn&#8217;t want my wife to send me to the store 10 times to buy 10 items, so the list is really just a kind of user shortcut. Therefore, in the job board scenario, each job to job board connection is its own transaction.</p>
<p>This is more common than you might think.</p>
<p>Once you go looking for cases where the domain is forgiving of partial failures, you may start seeing more and more of them.</p>
<h3>Aggregate Roots</h3>
<p>In the original transaction where we tried to connect many job boards to a single job, we saw that the single job is the aggregate root. However, once we have multiple transactions, each connecting one job and one job board, the job board is just as likely an aggregate root as the job.</p>
<p>We can do&nbsp;&nbsp; <font face="Courier">jobBoard.Post(job);</font>&nbsp;&nbsp;&nbsp; or&nbsp;&nbsp;&nbsp;&nbsp; <font face="Courier">job.PostTo(jobBoard);</font></p>
<p>But we need just a bit more analysis to come to the right decision.</p>
<p>While we could just leave the bi-directional/circular dependency between them, it would be preferable if we could make it uni-directional instead. To do that, we need to understand their relationship:</p>
<p>If there was no such thing as &#8220;job&#8221;, would there be meaning to &#8220;job board&#8221; ? Probably not.</p>
<p>If there was no such thing as &#8220;job board&#8221;, would there be meaning to &#8220;job&#8221; ? Probably. Yes. Our company can handle the hiring process of a job regardless of whether the candidate came in through Monster.com or not.</p>
<p>From this we understand that the uni-directional relationship can be modelled as one-to-many from job board to job. The Job class would no longer have a collection of Job Board objects. In fact, it could even be in an assembly separate from Job Board and not reference Job Board in any way. Job Board, on the other hand, would still have a collection of Job objects.</p>
<p>Going back to the code above we see that the right choice is&nbsp;&nbsp; <font face="Courier">jobBoard.Post(job);</font>&nbsp;&nbsp;&nbsp; </p>
<p>Job Board is the aggregate root in this case. Also, the many-to-many mapping has now dissolved, leaving behind a single one-to-many mapping.</p>
<p>Let that sink in a second.</p>
<h3>But Wait&#8230;</h3>
<p>While the GUI showing which jobs are posted on a given job board are well served by the above decision (simply traversing the object graph from Job Board to its collection of Jobs), that&#8217;s not the whole story. Another GUI needs to show administrative users which Job Boards a given Job has been posted to. Since we no longer have the domain-level connection, we can&#8217;t traverse myJob.JobBoards.</p>
<p>Our only option is to perform a query. That&#8217;s not so bad, but not as pretty as object traversal. </p>
<p>The real benefit is in chopping apart the Gordian M-to-N mapping knot and getting a cleaner, more well factored domain model. </p>
<p>That gives us much greater leverage for bigger, system-level decomposition.</p>
<p>We&#8217;re now all set to move up to a pub/sub solution between these aggregate roots, effectively upgrading them to Bounded Contexts. From there, we can move to full-blown internet-scale caching with REST for extra scalability on showing a job board with all its jobs.</p>
<h3>In Closing</h3>
<p>We often look at many-to-many relationships just like any other relationship. And from a purely technical perspective, we&#8217;re not wrong. However, the business reality around these relationships is often very different - forgiving of partial failures, to the point of actually requiring them.</p>
<p>Since the business folks who provide us with requirements rarely think of failure scenarios, they don&#8217;t specify that &#8220;between these two entities here, I don&#8217;t want transactional atomicity&#8221; (rolling our technical eyes - the idiots [sarcasm, just to make sure you don't misread me]).</p>
<p>Yet, if we were to spell out what the system will do under failure conditions when transactionally atomic, those same business folks will be rolling our eyes back to us.</p>
<p>What I&#8217;ve found surprises some DDD practitioners is how critical this issue really is to arriving at the correct aggregate roots and bounded contexts. </p>
<p>It&#8217;s also simple, and practical, so you won&#8217;t be offending the YAGNI police. </p>
<hr size="1">
<h3>Related Content</h3>
<blockquote>
<p><a href="http://www.udidahan.com/2008/02/15/from-crud-to-domain-driven-fluency/">From CRUD to Domain-Driven Fluency</a></p>
<p><a href="http://www.udidahan.com/2007/09/12/podcast-domain-models-soa-and-the-single-version-of-the-truth/">[Podcast] Domain Models, SOA, and The Single Version of the Truth</a></p>
</blockquote>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=ziW65XmjxyY:3YDeQ9GCRCM: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=ziW65XmjxyY:3YDeQ9GCRCM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=ziW65XmjxyY:3YDeQ9GCRCM:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=ziW65XmjxyY:3YDeQ9GCRCM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=ziW65XmjxyY:3YDeQ9GCRCM:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/ziW65XmjxyY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/01/24/ddd-many-to-many-object-relational-mapping/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.udidahan.com/2009/01/24/ddd-many-to-many-object-relational-mapping/</feedburner:origLink></item>
		<item>
		<title>Building Super-Scalable Web Systems with REST</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/OX6ONHINt0U/</link>
		<comments>http://www.udidahan.com/2008/12/29/building-super-scalable-web-systems-with-rest/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 21:38:58 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
		
		<category><![CDATA[Architecture]]></category>

		<category><![CDATA[Caching]]></category>

		<category><![CDATA[Performance]]></category>

		<category><![CDATA[REST]]></category>

		<category><![CDATA[Scalability]]></category>

		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/12/29/building-super-scalable-web-systems-with-rest/</guid>
		<description><![CDATA[I&#8217;ve been consulting with a client who has a wildly successful web-based system, with well over 10 million users and looking at a tenfold growth in the near future. One of the recent features in their system was to show users their local weather and it almost maxed out their capacity. That raised certain warning [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been consulting with a client who has a wildly successful web-based system, with well over 10 million users and looking at a tenfold growth in the near future. One of the recent features in their system was to show users their local weather and it almost maxed out their capacity. That raised certain warning flags as to the ability of their current architecture to scale to the levels that the business was taking them.</p>
<p> <center><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="139" alt="danger" src="http://www.udidahan.com/wp-content/uploads/image51.png" width="408" border="0"></center>
</p>
<h3>On Web 2.0 Mashups</h3>
<p>One would think that sites like Weather.com and friends would be the first choice for implementing such a feature. Only thing is that they were strongly against being mashed-up Web 2.0 style on the client - they had enough scalability problems of their own. Interestingly enough (or not), these partners were quite happy to publish their weather data to us and let us handle the whole scalability issue.</p>
<h3>Implementation 1.0</h3>
<p>The current implementation was fairly straightforward - client issues a regular web service request to the GetWeather webmethod, the server uses the user&#8217;s IP address to find out their location, then use that location to find the weather for that location in the database, and return that to the user. Standard fare for most dynamic data and the way most everybody would tell you to do it.</p>
<p>Only thing is that it scales like a dog.</p>
<h3>Add Some Caching</h3>
<p>The first thing you do when you have scalability problems and the database is the bottleneck is to cache, well, that&#8217;s what everybody says (same everybody as above).</p>
<p>The thing is that holding all the weather of the entire globe in memory, well, takes a lot of memory. More than is reasonable. In which case, there&#8217;s a fairly decent chance that a given request can&#8217;t be served from the cache, resulting in a query to the database, an update to the cache, which bumps out something else, in short, not a very good hit rate.</p>
<p>Not much bang for the buck.</p>
<p>If you have a single datacenter, having a caching tier that stores this data is possible, but costly. If you want a highly available, business continuity supportable, multi-datacenter infrastructure, the costs add up quite a bit quicker - to the point of not being cost effective (&#8221;You need HOW much money for weather?! We&#8217;ve got dozens more features like that in the pipe!&#8221;)</p>
<p>What we can do is to tell the client we&#8217;re responding to that they can cache the result, but that isn&#8217;t close to being enough for us to scale.</p>
<h3>Look at the Data, Leverage the Internet</h3>
<p>When you find yourself in this sort of situation, there&#8217;s really only one thing to do:</p>
<div style="border-right: black 1px solid; border-top: black 1px solid; float: right; margin-left: 5px; border-left: black 1px solid; width: 220px; border-bottom: black 1px solid; background-color: beige">
<div style="font-size: 12px; margin: 5px">
<p>In order to save on bandwidth, the most precious commodity of the internet, the various ISPs and backbone providers cache aggressively. In fact, HTTP is designed exactly for that. </p>
<p>If user A asks for some html page, the various intermediaries between his browser and the server hosting that page will cache that page (based on HTTP headers). When user B asks for that same page, and their request goes through one of the intermediaries that user A&#8217;s request went through, that intermediary will serve back its cached copy of the page rather than calling the hosting server.</p>
<p>Also, users located in the same geographic region by and large go through the same intermediaries when calling a remote site.</p>
</div>
</div>
<p>Leverage the Internet</p>
<p>The internet is the biggest, most scalable data serving infrastructure that mankind was lucky enough to have happen to it. However, in order to leverage it - you need to understand your data and how your users use it, and finally align yourself with the way the internet works.</p>
<p>Let&#8217;s say we have 1,000 users in London. All of them are going to have the same weather. If all these users come to our site in the period of a few hours and ask for the weather, they all are going to get the exact same data. The thing is that the response semantics of the GetWeather webmethod must prevent intermediaries from caching so that users in Dublin and Glasgow don&#8217;t get London weather (although at times I bet they&#8217;d like to).</p>
<h3>REST Helps You Leverage the Internet</h3>
<p>Rather than thinking of getting the weather as an operation/webmethod, we can represent the various locations weather data as explicit web resources, each with its own URI. Thus, the weather in London would be <strong>http://weather.myclient.com/UK/London</strong>.</p>
<p>If we were able to make our clients in London perform an HTTP GET on <strong>http://weather.myclient.com/UK/London</strong> then we could return headers in the HTTP response telling the intermediaries that they can cache the response for an hour, or however long we want.</p>
<p>That way, after the first user in London gets the weather from our servers, all the other 999 users will be getting the same data served to them from one of the intermediaries. Instead of getting hammered by millions of requests a day, the internet would shoulder easily 90% of that load making it much easier to scale. <a href="http://www.perkel.com/politics/gore/internet.htm">Thanks Al</a>.</p>
<p>This isn&#8217;t a &#8220;cheap trick&#8221;. While being straight forward for something like weather, understanding the nature of your data and intelligently mapping that to a URI space is critical to building a scalable system, and reaping the benefits of REST.</p>
<h3>What&#8217;s left?</h3>
<p>The only thing that&#8217;s left is to get the client to know which URI to call. A simple matter, really. </p>
<p>When the user logs in, we perform the IP to location lookup and then write a cookie to the client with their location (UK/London). That cookie then stays with the user saving us from having to perform that IP to location lookup all the time. On subsequent logins, if the cookie is already there, we don&#8217;t do the lookup.</p>
<blockquote><p>BTW, we also show the user &#8220;you&#8217;re in London, <font color="#0000ff"><strong><u>aren&#8217;t you</u></strong></font>?&#8221; with the link allowing the user to change their location, which we then update the cookie with and change the URI we get the weather from.</p>
</blockquote>
<h3>In Closing</h3>
<p>While web services are great for getting a system up and running quickly and interoperably, scalability often suffers. Not so much as to be in your face, but after you&#8217;ve gone quite a ways and invested a fair amount of development in it, you find it standing between you and the scalability you seek.</p>
<p>Moving to REST is not about turning on the &#8220;make it restful&#8221; switch in your technology stack (ASP.NET MVC and WCF, I&#8217;m talking to you). Just like with databases there is no &#8220;make it go fast&#8221; switch - you really do need to understand your data, the various users access patterns, and the volatility of the data so that you can map it to the &#8220;right&#8221; resources and URIs.</p>
<p>If you do walk the RESTful path, you&#8217;ll find that the scalability that was once so distant is now within your grasp.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=OX6ONHINt0U:ZogmJ0APOiI: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=OX6ONHINt0U:ZogmJ0APOiI:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=OX6ONHINt0U:ZogmJ0APOiI:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=OX6ONHINt0U:ZogmJ0APOiI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=OX6ONHINt0U:ZogmJ0APOiI:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/OX6ONHINt0U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/12/29/building-super-scalable-web-systems-with-rest/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.udidahan.com/2008/12/29/building-super-scalable-web-systems-with-rest/</feedburner:origLink></item>
		<item>
		<title>Featured in the Microsoft Architecture Journal</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/ZB9ottacLxw/</link>
		<comments>http://www.udidahan.com/2008/12/20/featured-in-the-microsoft-architecture-journal/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 19:52:49 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
		
		<category><![CDATA[Community]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/12/20/featured-in-the-microsoft-architecture-journal/</guid>
		<description><![CDATA[
For those of you who might be interested in hearing a bit more of my back-story, how I got into architecture, etc - you can find all that out in the current issue of the Microsoft Architecture Journal. Here&#8217;s a quick sneak-peek:
Q: It’s sometimes frustrating when you try to keep updated on latest technologies and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.msarchitecturejournal.com/pdf/Journal18.pdf"><img style="margin: 0px 0px 10px 10px" height="240" alt="Microsoft Architecture Journal" src="http://blufiles.storage.msn.com/y1p3Bs2VhU_71I-5O6qJb3SnCppmpc4eWlV6dAA0NWY4mDjj1BKsOzyNcl4z_kXMMOO?PARTNER=WRITER" width="179" align="right"></a>
<p>For those of you who might be interested in hearing a bit more of my back-story, how I got into architecture, etc - you can find all that out in the current issue of the Microsoft Architecture Journal. Here&#8217;s a quick sneak-peek:</p>
<blockquote><p>Q: It’s sometimes frustrating when you try to keep updated on latest technologies and trends but after investing the effort on experiencing yourself (coding, watching webcasts, testing demos, etc), there’s a lot more of new technologies in the pipeline (just look back to the last PDC). Do you have any recipe to efficiently deal with such avalanche? </p>
<p>A: There is indeed a continuous flood of information coming at developers today. Never has so much information been so readily available and accessible to so many. It turns out, though, that much of that information is about <i>how</i> something works, less about what problem it is intended to solve, and much less about which problems it isn’t suited for. As an architect looking at choosing the right tool for the right job, I focus a lot more on the last two. Often, I go digging to find out <i>why</i> a tool isn’t a good fit for a job. So, I guess my recipe is to mostly ignore the avalanche and tap my network to find out the stuff the avalanche won’t tell you.</p>
</blockquote>
<p><a href="http://msdn.microsoft.com/en-us/architecture/bb410935.aspx">Read on&#8230;</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=ZB9ottacLxw:rp4CdJ4h6rk: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=ZB9ottacLxw:rp4CdJ4h6rk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=ZB9ottacLxw:rp4CdJ4h6rk:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=ZB9ottacLxw:rp4CdJ4h6rk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=ZB9ottacLxw:rp4CdJ4h6rk:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/ZB9ottacLxw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/12/20/featured-in-the-microsoft-architecture-journal/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.udidahan.com/2008/12/20/featured-in-the-microsoft-architecture-journal/</feedburner:origLink></item>
		<item>
		<title>[Course] Master Enterprise .NET Solutions</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/uZEoDUhpLX0/</link>
		<comments>http://www.udidahan.com/2008/12/15/course-master-enterprise-net-solutions/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 09:00:04 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
		
		<category><![CDATA[Training]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/12/15/course-master-enterprise-net-solutions/</guid>
		<description><![CDATA[ I&#8217;m happy to announce that I will be coming to Norway this March to give a 3 day course in cooperation with Program Utvikling.
Here&#8217;s the info:
Scalability is the watch-word of enterprise development. With increasing numbers of users, performing more complex tasks involving more data, collaborating with each other inside the organization and across the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.programutvikling.no/kurskalenderoversikt.aspx?mid_1=1352&amp;mid=1535&amp;id=387056"><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="92" alt="Program Utvikling" src="http://www.udidahan.com/wp-content/uploads/image50.png" width="177" align="right" border="0"></a> I&#8217;m happy to announce that I will be coming to Norway this March to give a 3 day course in cooperation with Program Utvikling.</p>
<p>Here&#8217;s the info:</p>
<blockquote><p>Scalability is the watch-word of enterprise development. With increasing numbers of users, performing more complex tasks involving more data, collaborating with each other inside the organization and across the internet, almost all enterprise systems are being distributed to more machines, and even to data-centres across the globe. With the increased size of these systems, more developers, testers, architects, and administrators are required to bring them to production and operate them.
<p>This 3–day course will equip you with skills in service–oriented architecture &amp; design, transaction management, reliability &amp; robustness, high availability &amp; capacity planning, as well as many other design principles and practices key to tackling large-scale enterprise projects.
<p>On top of learning how to handle the technical complexities of building enterprise–class systems, you will see the use of Domain-Driven Design and other business analysis techniques needed to decompose large intricate business domains into smaller, more manageable sub–domains. You&#8217;ll also see how to balance data–consistency and data freshness against costs as well as how to work with stakeholders to uncover the cost-effective level of each non-functional requirement.
<p>&nbsp;
<p>By the end of this course you will be able to:
<ol>
<li>Avoid common pitfalls in distributed systems </li>
<li>Use loosely coupled messaging communication </li>
<li>Identify and allocated business logic to services </li>
<li>Decompose services into layers, tiers, assemblies, and processes </li>
<li>Design for service management and monitoring in production environments</li>
</ol>
</blockquote>
<p>Click here <a href="http://www.programutvikling.no/kurskalenderoversikt.aspx?mid_1=1352&amp;mid=1535&amp;id=387056">for more information and registration</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=uZEoDUhpLX0:xpof3XuufPM: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=uZEoDUhpLX0:xpof3XuufPM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=uZEoDUhpLX0:xpof3XuufPM:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=uZEoDUhpLX0:xpof3XuufPM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=uZEoDUhpLX0:xpof3XuufPM:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/uZEoDUhpLX0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/12/15/course-master-enterprise-net-solutions/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.udidahan.com/2008/12/15/course-master-enterprise-net-solutions/</feedburner:origLink></item>
		<item>
		<title>SOA, REST, and Pub/Sub</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/EqIbBpyovQw/</link>
		<comments>http://www.udidahan.com/2008/12/15/soa-rest-and-pubsub/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 08:34:24 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
		
		<category><![CDATA[Architecture]]></category>

		<category><![CDATA[EDA]]></category>

		<category><![CDATA[Integrated Simplicity]]></category>

		<category><![CDATA[Pub/Sub]]></category>

		<category><![CDATA[REST]]></category>

		<category><![CDATA[SOA]]></category>

		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/12/15/soa-rest-and-pubsub/</guid>
		<description><![CDATA[From Integrated Simplicity:
 
The question of how web-based (or 3rd party) consumers can work with pub/sub based services comes up a lot.
Many developers are used to implementing web services exposing methods on them like GetAllCustomers.
When moving to pub/sub and other more loosely coupled messaging patterns, developers look to implement the same pattern, opting for something [...]]]></description>
			<content:encoded><![CDATA[<p>From <a href="http://www.IntegratedSimplicity.com">Integrated Simplicity</a>:</p>
<p><a href="http://www.udidahan.com/wp-content/uploads/image49.png"><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px; border-left: 0px; border-bottom: 0px" height="277" alt="SOA &amp; Web" src="http://www.udidahan.com/wp-content/uploads/image-thumb34.png" width="526" border="0"></a> </p>
<p>The question of how web-based (or 3rd party) consumers can work with pub/sub based services comes up a lot.</p>
<p>Many developers are used to implementing web services exposing methods on them like GetAllCustomers.</p>
<p>When moving to pub/sub and other more loosely coupled messaging patterns, developers look to implement the same pattern, opting for something like duplex GetCustomersRequest and GetCustomersResponse. The reasoning is simple and straightforward - it is difficult to push data over the web to consumers.</p>
<p>However, there are still ways to disconnect the preparation of the data from its usage thus gaining many of the advantages of pub/sub.</p>
<p>By employing REST principles and modelling our customer list as an explicit resource, web-based consumers would simply perform regular HTTP GET operations on the URI to get the list of customers.</p>
<p>The resource itself could be a simple XML file - it wouldn&#8217;t need to be dynamic at all.</p>
<p>You can get all the scalability benefits of pub/sub for web based consumers. All you need is a bit of REST <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=EqIbBpyovQw:c0TZFlwN0Ys: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=EqIbBpyovQw:c0TZFlwN0Ys:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=EqIbBpyovQw:c0TZFlwN0Ys:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=EqIbBpyovQw:c0TZFlwN0Ys:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=EqIbBpyovQw:c0TZFlwN0Ys:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/EqIbBpyovQw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/12/15/soa-rest-and-pubsub/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.udidahan.com/2008/12/15/soa-rest-and-pubsub/</feedburner:origLink></item>
		<item>
		<title>[Presentation] Ness Tziona User Group</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/5mFcUfIgpOA/</link>
		<comments>http://www.udidahan.com/2008/12/14/presentation-ness-tziona-user-group/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 08:28:51 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
		
		<category><![CDATA[Community]]></category>

		<category><![CDATA[Presentations]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/12/14/presentation-ness-tziona-user-group/</guid>
		<description><![CDATA[Last Wednesday I gave my &#8220;Avoid a Failed SOA&#8221; presentation to the Ness Tziona user group led by Ohad Israeli and wanted to thank everybody who came out.

The quantity of deep, insightful questions was impressive and I hope that everybody got their most pressing issues addressed. I know that by the time you got home [...]]]></description>
			<content:encoded><![CDATA[<p>Last Wednesday I gave my &#8220;Avoid a Failed SOA&#8221; presentation to the Ness Tziona user group led by <a href="http://blogs.microsoft.co.il/blogs/ohad">Ohad Israeli</a> and wanted to thank everybody who came out.</p>
<p><img height="357" src="http://weblogs.asp.net/blogs/israelio/WindowsLiveWriter/AvoidaFailedSOANessTzionaUsergroupmeetin_14336/IMG_7642_thumb.jpg" width="533"></p>
<p>The quantity of deep, insightful questions was impressive and I hope that everybody got their most pressing issues addressed. I know that by the time you got home you probably had a whole bunch more - that happens all the time. This SOA/EDA style often gives a whole new perspective on system design.</p>
<p>You can find the slides online <a href="http://cid-c8ad44874742a74d.skydrive.live.com/self.aspx/Blog/Avoid_a_failed_SOA.ppsx">here</a>.</p>
<p>Anyway, please feel free to send me your questions and I&#8217;ll do my best to answer them here.</p>
<p>When Ohad gets the recording online, I&#8217;ll make sure to link to it for those of you who couldn&#8217;t make it.</p>
<hr size="1">
<h4>Feedback from attendees</h4>
<p>From <a href="http://blogs.microsoft.co.il/blogs/ohad/archive/2008/12/10/avoid-a-failed-soa-ness-tziona-usergroup-meeting-2.aspx">Ohad&#8217;s blog</a>: &#8220;The talk was awesome &#8230; Udi really made our second usergroup meeting a huge success !&#8221;</p>
<p><a href="http://blogs.microsoft.co.il/blogs/rotemb/">Rotem Bloom</a>: &#8220;היה פשוט הרצאה פצצה מי שלא מגיע מפסיד&#8221;, translated, &#8220;The presentation was &#8216;da bomb&#8217;, if you weren&#8217;t there, you missed out&#8221;</p>
<p><a href="http://blogs.microsoft.co.il/blogs/adlaim/archive/2008/12/10/188701.aspx">Adlai Maschiach</a>: &#8220;הרצאה נ-ה-ד-ר-ת !&#8221;, translated, &#8220;A W-O-N-D-E-R-F-U-L presentation!&#8221;</p>
<p><a href="http://blogs.microsoft.co.il/blogs/yitzhak/archive/2008/12/11/avoid-a-failed-soa.aspx">Yitzhak Gootvilig</a>: &#8220;הרצאה מעולה. מומלץ למצוא את הדרך להאזין להקלטה &#8220;, translated, &#8220;Excellent presentation. I strongly suggest finding a way to listen to the recording&#8221;</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=5mFcUfIgpOA:aSY0gvUi8fA: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=5mFcUfIgpOA:aSY0gvUi8fA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=5mFcUfIgpOA:aSY0gvUi8fA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=5mFcUfIgpOA:aSY0gvUi8fA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=5mFcUfIgpOA:aSY0gvUi8fA:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/5mFcUfIgpOA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/12/14/presentation-ness-tziona-user-group/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.udidahan.com/2008/12/14/presentation-ness-tziona-user-group/</feedburner:origLink></item>
		<item>
		<title>Self-Contained Events and SOA</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/ikpKgFNrxPo/</link>
		<comments>http://www.udidahan.com/2008/12/13/self-contained-events-and-soa/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 23:35:08 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
		
		<category><![CDATA[Architecture]]></category>

		<category><![CDATA[Autonomous Services]]></category>

		<category><![CDATA[EDA]]></category>

		<category><![CDATA[Master Data Management]]></category>

		<category><![CDATA[Pub/Sub]]></category>

		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/12/13/self-contained-events-and-soa/</guid>
		<description><![CDATA[In the architectural principle of fully self contained messages, events &#8220;can - instantly and in future - be interpreted as the respective event without the need to rely on additional data stores that would need to be in time-sync with the event during message-processing.&#8221;
Also, &#8220;passing reference data in a message makes the message-consuming systems dependent [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="237" alt="diamond" src="http://www.udidahan.com/wp-content/uploads/diamond.jpg" width="214" align="right" border="0">In <a href="http://soa-eda.blogspot.com/2008/11/architectural-principle-of-fully-self.html">the architectural principle of fully self contained messages</a>, events &#8220;can - instantly and in future - be interpreted as the respective event without the need to rely on additional data stores that would need to be in time-sync with the event during message-processing.&#8221;</p>
<p>Also, &#8220;passing reference data in a message makes the message-consuming systems dependent on the knowledge and availability of actual persistent data that is stored “somewhere”. This data must separately be accessed for the sake of understanding the event that is represented by the message.&#8221; </p>
<p>The discussion of self-contained events can be compared to <a href="http://martinfowler.com/bliki/IntegrationDatabase.html">integration databases</a> vs <a href="http://martinfowler.com/bliki/ApplicationDatabase.html">application databases</a>. </p>
<h3>Centralized Integration - Pros &amp; Cons</h3>
<p>If everything in a system can access a central datastore, it is enough for one party to publish an event containing only the ID of an entity that that party previously entered/updated. Upon receiving that event, a subscriber would go to the central datastore and get the fields its interested in for that ID. The advantage of this approach is that the minimal amount of data necessary crosses the network, as subscribers only retrieve the fields that interest them. Martin Fowler describes the disadvantages as:</p>
<blockquote><p>&#8220;An integration database needs a schema that takes all its client applications into account. The resulting schema is either more general, more complex or both. The database usually is controlled by a separate group to the applications and database changes are more complex because they have to be negotiated between the database group and the various applications.&#8221;</p>
</blockquote>
<p>This is far from being aligned with the principle of autonomy so important to SOA. In that respect, the architectural principle of self-contained messages points us away from those problems and towards more autonomous services.</p>
<p>However, once we have these autonomous business services in place, we may find that we don&#8217;t need 100% fully self-contained messages anymore. </p>
<h3>A Real-World Example</h3>
<p>Let&#8217;s say we have 3 business services, Sales, Fulfillment, and Billing.</p>
<p>Sales publishes an OrderAccepted event when it accepts an order. That event contains all the order information.</p>
<p>Both Fulfillment and Billing are subscribed to this event, and thus receive it. </p>
<p>Fulfillment does not ship products to the customer until the customer has been billed, so it just stores the order information internally, and is done.</p>
<p>Billing starts the process of billing the customer for their order, possibly joining several orders into a single bill. After completing this process, it publishes a CustomerBilled event containing all billing information, as well as the IDs of the orders in that bill. It does not put all the order information in that event, as it is not the authoritative owner of that data.</p>
<p>When Fulfillment receives the CustomerBilled event, it uses the IDs of the orders contained in the event to find the order information it previously stored internally. It does not need to call the Sales service for this information or contact some central Master Data Management system. It uses the data it has, and goes about fulfilling the orders and shipping the products to the customer, finally publishing its own OrderShipped event.</p>
<p>Notice, as well, that in the original OrderAccepted event there were the IDs of products the customer ordered. These product IDs originated from another service, Merchandising, responsible for the product catalog. The same thing can be said for the customer ID originating from another service - Customer Care.</p>
<h3>The Issue of Time</h3>
<p>One could argue that since subscribers use previously cached data when processing new events, that data might not be up to date. Also, we may have race conditions between our services. In the above example, if Billing was extremely fast and more highly available than Fulfillment. Billing could have received the OrderAccepted event, processed it, and published the CustomerBilled event before Fulfillment had received the OrderAccepted event. In short, the CustomerBilled and OrderAccepted messages could be out of order in Fulfillment’s queue.</p>
<p>What would Fulfillment do when trying to process the CustomerBilled message when it doesn’t have the order information?
<p>Well, it knows that the world is parallel and non-sequential, so it does NOT return/log an error, but rather puts that message in the back of the queue to be processed again later (or maybe in some other temporary holding area). This enables the OrderAccepted message to be processed before the CustomerBilled message is retried. When the retry occurs, well, everything’s OK – it’s worked itself out over time.
<p>In the case where we retry again and again and things don’t work themselves out (maybe the OrderAccepted event was lost), we move that message off to a different queue for something else to resolve the conflict (maybe a person, maybe software). If/when the conflict is resolved (got the Sales system / messaging system to replay the OrderAccepted event), the conflict resolver returns the CustomerBilled message to the queue, and now everything works just fine.
<p>As all of this is occurring, the only thing that’s visible to external parties is that it happens to be taking longer than usual for the OrderShipped event to be published. In other words, time is the only difference.<br />
<h3>&nbsp;</h3>
<h3>Summary</h3>
<p>The problem of non-self-contained events is mitigated first and foremost by business services in SOA, and the apparent issue of time-synchronization by business logic inside these services.</p>
<p>Don&#8217;t be afraid to put IDs in your messages and events.</p>
<p>Do be afraid of using those IDs to access datastores shared by multiple &#8220;services&#8221;.</p>
<p>Using IDs to correlated current events to data from previous events is not only OK, it&#8217;s to be expected.</p>
<p>The architectural principle of fully self-contained messages steers us away from the problems of Integration Databases and towards Application Databases, autonomous services, and a better SOA implementation. From there, following the principle of autonomy from a business perspective, will lead us to services not publishing data in their messages that is owned by other services, taking us the next step of our journey to SOA.</p>
<hr size="1">
<h3> Related Content</h3>
<blockquote><p><a href="http://www.udidahan.com/2008/01/01/podcast-message-ordering-is-it-cost-effective/">[Podcast] Message Ordering - Is it cost effective?</a></p>
<p><a href="http://www.udidahan.com/2007/08/16/dont-eda-between-existing-systems/">Don&#8217;t EDA between existing systems</a></p>
<p><a href="http://www.udidahan.com/2007/05/31/podcast-handling-dependencies-between-subscribers-in-soa/">[Podcast] Handling dependencies between subscribers in SOA</a></p>
</blockquote>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=ikpKgFNrxPo:Gd_EA1kPRdY: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=ikpKgFNrxPo:Gd_EA1kPRdY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=ikpKgFNrxPo:Gd_EA1kPRdY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=ikpKgFNrxPo:Gd_EA1kPRdY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=ikpKgFNrxPo:Gd_EA1kPRdY:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/ikpKgFNrxPo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/12/13/self-contained-events-and-soa/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.udidahan.com/2008/12/13/self-contained-events-and-soa/</feedburner:origLink></item>
		<item>
		<title>Lost Notifications? No Problem.</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/-5be1RvaPf0/</link>
		<comments>http://www.udidahan.com/2008/12/07/lost-notifications-no-problem/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 09:46:05 +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[Pub/Sub]]></category>

		<category><![CDATA[Reliability]]></category>

		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/12/07/lost-notifications-no-problem/</guid>
		<description><![CDATA[ One of the most common questions I get on the topic of pub/sub messaging is what happens if a notification is lost. Interestingly enough, there are some who almost entirely write-off this pattern because of this issue, preferring the control of request/response-exception. So, what should be done about lost messages? The short answer is [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="148" alt="" src="http://www.udidahan.com/wp-content/uploads/image48.png" width="240" align="right" border="0"> One of the most common questions I get on the topic of pub/sub messaging is what happens if a notification is lost. Interestingly enough, there are some who almost entirely write-off this pattern because of this issue, preferring the control of request/response-exception. So, what should be done about lost messages? The short answer is durable messaging. The long answer is design.</p>
<h3>Durable Messaging</h3>
<p>In order to prevent a message from being lost when it is sent from a publisher to a subscriber, the message is written to disk on the publisher side, and then forwarded to the subscriber, where it is also written to disk. This store-and-forward mechanism enables our systems to gracefully recover from either side being temporarily unavailable.</p>
<p>In my <a href="http://msdn.microsoft.com/en-us/magazine/cc663023.aspx">MSDN article on this topic</a>, I outlined some problems with this approach. These problems are exacerbated for publishers. Imagine a publisher with 40 subscribers, publishing 10 messages a second, each containing 1MB of XML. If 10 of the subscribers are unavailable, that&#8217;s 100MB of data being written to the publisher&#8217;s disk every second, 6GB every minute. That&#8217;s liable to bring down a publisher before an administrator brews a cup of coffee.</p>
<p>Publishers have no choice but to throw away messages after a certain period of time.</p>
<h3>Publisher Contracts</h3>
<p>The whole issue of contracts and schema is considered one of the better understand parts of SOA. Unfortunately, the operational aspects of service contracts is hardly ever taken into account.</p>
<p>On top of the schema of the messages a service publishers, additional information is needed in the contract:</p>
<ol>
<li>How big will this message be?
<li>How often will it be published?
<li>How long will this message be stored if a subscriber is unavailable?</li>
</ol>
<p>This first two pieces of information are important for subscribers to do load and capacity planning. The last one is the most important as it dictates the required availability and fault-tolerance characteristic of subscribers.</p>
<h3>For Example</h3>
<p>In the canonical retail scenario, when our sales service accepts an order, it publishes an order accepted event. Other services subscribed to this event include shipping, billing, and business intelligence.</p>
<p>While shipping and billing are highly available and able to keep up with the rate at which orders are accepted, the business intelligence service is not. BI has two main parts to it - a nightly batch that does the number crunching, and a UI for reporting off of the results of that number crunching. Some even do the reporting in a semi-offline fashion, emailing reports back to the user when they&#8217;re ready.</p>
<p>Furthermore, nobody&#8217;s going to invest in servers for making BI highly available.</p>
<p>And wasn&#8217;t the whole point of this publish/subscribe messaging to keep our services autonomous? That not all services have to have the same level uptime?</p>
<p>Houston, do we have a problem.?</p>
<h3>Data Freshness</h3>
<p>There is a glimmer of light in all this doom and gloom.</p>
<p>Not all services have the same data freshness requirements.</p>
<p>The business intelligence service above doesn&#8217;t need to know about orders the second they&#8217;re accepted. A daily roll-up would be fine, and an hourly roll-up bring us that much closer to &#8220;real time business intelligence&#8221;.</p>
<p>So, while BI is ready to accept the sales message schema, it would like a slightly different contract around it - less messages per unit of time, more data in each message.</p>
<p>From the operational perspective of the sales service, it would be cost effective to have less &#8220;online&#8221; subscribers. It could even take things a few steps further. Instead of using the regular messaging backbone for transmitting these hourly messages, it could use FTP. The data could even be zipped to take up even less space. Since the total data size is less than the corresponding online stream, is stored on cheaper, large storage, and the number of subscribers for this zipped, hourly update is fairly small, these messages can be kept around far longer.</p>
<p>If you&#8217;ve heard about <a href="http://martinfowler.com/articles/consumerDrivenContracts.html">consumer-driven contracts</a>, this is it.</p>
<p>Note that we&#8217;re still talking about the same logical message schema.</p>
<h3>Summary</h3>
<p>It&#8217;s not that lost notifications aren&#8217;t a problem.</p>
<p><a href="http://en.wikipedia.org/wiki/Tesseract"><img style="margin: 0px 0px 10px 10px" src="http://upload.wikimedia.org/wikipedia/commons/5/55/Tesseract.gif" align="right"></a>
<p>It&#8217;s that they feed the design process in such a way that the resulting service ecosystem is set up in such a way that notifications won&#8217;t get lost. I know that that sounds kind of recursive, but that&#8217;s how it works. Either subscribers take care of their SLA allowing them to process the online stream of events, or they should subscribe to a different pipe (which will have different SLA requirements, but maybe they can deal with those).</p>
<p>It make sense to have multiple pipes for the same logical schema.</p>
<p>It&#8217;s practically a necessity to make pub/sub a feasible solution.</p>
<p>&nbsp;</p>
<hr size="1">
<h3>Related Content</h3>
<blockquote><p><a href="http://msdn.microsoft.com/en-us/magazine/cc663023.aspx">MSDN article on messaging and lost messages</a></p>
<p><a href="http://www.udidahan.com/2008/07/17/durable-messaging-dilemmas/">Durable messaging dilemmas</a></p>
<p><a href="http://www.udidahan.com/2008/10/22/additional-logic-required-for-service-autonomy/">Additional logic required for service autonomy</a></p>
<p><a href="http://www.udidahan.com/2008/11/01/soa-eda-and-cep-a-winning-combo/">More in depth example on events and pub/sub between services</a></p>
<p><a href="http://martinfowler.com/articles/consumerDrivenContracts.html">Consumer-Driven Contracts</a></p>
</blockquote>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=-5be1RvaPf0:qqye04GcjNc: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=-5be1RvaPf0:qqye04GcjNc:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=-5be1RvaPf0:qqye04GcjNc:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=-5be1RvaPf0:qqye04GcjNc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=-5be1RvaPf0:qqye04GcjNc:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/-5be1RvaPf0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/12/07/lost-notifications-no-problem/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.udidahan.com/2008/12/07/lost-notifications-no-problem/</feedburner:origLink></item>
		<item>
		<title>Intentions and Interfaces</title>
		<link>http://feedproxy.google.com/~r/UdiDahan-TheSoftwareSimplist/~3/QdNMiXIiujM/</link>
		<comments>http://www.udidahan.com/2008/11/17/intentions-and-interfaces/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 08:44:54 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
		
		<category><![CDATA[Presentations]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/11/17/intentions-and-interfaces/</guid>
		<description><![CDATA[I want to thank Lasse Eskildsen for the picture from my TechEd talk on Intentions and Interfaces capturing the main message in no uncertain terms.

You can download the presentation as a PDF here.
]]></description>
			<content:encoded><![CDATA[<p>I want to thank <a href="http://weblogs.asp.net/lasse/archive/2008/11/16/teched-2008-day-4-and-5.aspx">Lasse Eskildsen</a> for the picture from my TechEd talk on Intentions and Interfaces capturing the main message in no uncertain terms.</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 10px 10px; border-right-width: 0px" height="480" alt="image" src="http://www.udidahan.com/wp-content/uploads/image47.png" width="360" border="0"></p>
<p>You can download the presentation as a PDF <a href="http://cid-c8ad44874742a74d.skydrive.live.com/self.aspx/Blog/Intentions%20and%20Interfaces.pdf">here</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=QdNMiXIiujM:uMSp8AQAECY: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=QdNMiXIiujM:uMSp8AQAECY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=QdNMiXIiujM:uMSp8AQAECY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?a=QdNMiXIiujM:uMSp8AQAECY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/UdiDahan-TheSoftwareSimplist?i=QdNMiXIiujM:uMSp8AQAECY:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/UdiDahan-TheSoftwareSimplist/~4/QdNMiXIiujM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/11/17/intentions-and-interfaces/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.udidahan.com/2008/11/17/intentions-and-interfaces/</feedburner:origLink></item>
	</channel>
</rss>
