<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://codebetter.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Greg Young [MVP]</title><link>http://codebetter.com/blogs/gregyoung/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007 (Build: 20416.853)</generator><item><title>DDDD 11 [Basic Consistency]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/05/06/dddd-11-basic-consistency.aspx</link><pubDate>Tue, 06 May 2008 22:48:29 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:177756</guid><dc:creator>Greg</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=177756</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/05/06/dddd-11-basic-consistency.aspx#comments</comments><description>&lt;p&gt;So I was bad last week and didn&amp;#39;t get much written shame on me... hopefully I can get back on track. In DDDD 10 we looked at Command Query Seperation, now we are going to take that idea a little bit further as we really need to break it down a bit more than commands and queries.&lt;/p&gt;  &lt;p&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;Let&amp;#39;s say that I have a domain with a Customer Aggregate Root. When I want to update this Customer I need to issue a query to get its current state so I can validate whether my current changes would make the object en up in an invalid state. Generally for these types of reads one would want &lt;strong&gt;consistent&lt;/strong&gt; data. Why bother with all of my nice validation if it can be bypassed by inconsistency? One of the key concepts in domain driven design is that I know that certain things about my objects are true.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In that same domain I may also be able to search for customers with a first name of &amp;quot;Greg&amp;quot;. In this type of situation it is no longer a technical requirement (may still be a business requirement) that my search is completely consistent (for future reference I call any read such as this a &amp;quot;Report&amp;quot; even though they may not meet your personal criteria of a report). &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;If my search does not need to be completely consistent I can do many very neat things with my data such as having it come from a different model than my transactional model (i.e. query a reporting database). I can also do other things like cache the data with expirations. These types of operations can allow me to become much more scalable and they are a key concept we will be later exploiting in moving towards distributing our systems.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Because of how important our gains are; let&amp;#39;s make some rules.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Aggregate roots provide the boundary for where data is &lt;strong&gt;always &lt;/strong&gt;consistent. Anytime you deal with data that belongs to an aggregate from outside of an aggregate you should make your &lt;strong&gt;new &lt;/strong&gt;default to assume it to be eventually consistent.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Bounded Contexts are &lt;strong&gt;always &lt;/strong&gt;considered to be eventually consistent. If you have Bounded Contexts where this is not true there is probably something wrong with your model.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;When I say you should make it your new default; I mean that:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Unless it is explicitly stated otherwise and a &lt;strong&gt;business reason is provided with the story&lt;/strong&gt;; it will be eventually consistent.       &lt;br /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;One problem that you will run into with this transition is managing expectations. Domain Experts like most non-technical (and most technical for that matter) people have been trained to think that everything should be instantaneous with computers (&amp;quot;You mean when I click save and it says it saved it takes 45 seconds to show up in the search?&amp;quot;). They have &lt;a href="http://employees.csbsju.edu/tcreed/pb/pavcon.html"&gt;sucked from the evil tit of the RDBMS&lt;/a&gt; and its global consistency for far too long. The next post will be about getting over this hurdle.&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=177756" width="1" height="1"&gt;</description></item><item><title>I Want Spec#</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/28/i-want-spec.aspx</link><pubDate>Mon, 28 Apr 2008 22:23:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:177280</guid><dc:creator>Greg</dc:creator><slash:comments>35</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=177280</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/28/i-want-spec.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="244" alt="spec#" src="http://codebetter.com/blogs/gregyoung/WindowsLiveWriter/IWantSpec_D85E/spec__thumb_701b5ba8-3309-494c-8cdd-1d8d5725de28.jpg" width="181" align="left" border="0" /&gt; &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.hanselman.com/"&gt;Scott Hanselman&lt;/a&gt; has recently put up a new &lt;a href="http://hanselminutes.com/default.aspx?showID=128"&gt;hanselminutes of an interview with the spec# team&lt;/a&gt;. This interview was done at altdotnet; one of the key points of discussion both in general and in the interview was &amp;quot;how can we get spec#?&amp;quot;. The simple answer is we have to &lt;strong&gt;want &lt;/strong&gt;spec# and Microsoft &lt;strong&gt;needs to know&lt;/strong&gt; that we want it. The more people who want it the more likely we are to get it or to get it in pieces. The best way for them to know is for us to tell them!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let&amp;#39;s give Mike and the whole spec# team the best compliment possible on their research; asking for it to become integrated in products and/or to be available with a commercial license!&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;h1&gt;&lt;strong&gt;I want verifiable software...&lt;/strong&gt;&lt;/h1&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;h1&gt;&lt;strong&gt;I WANT SPEC#!&lt;/strong&gt;&lt;/h1&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Here is a reusable bumper sticker for your blog to make it easier to express how you feel.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://codebetter.com/blogs/gregyoung/WindowsLiveWriter/IWantSpec_D85E/spec_2.gif"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="52" alt="spec" src="http://codebetter.com/blogs/gregyoung/WindowsLiveWriter/IWantSpec_D85E/spec_thumb.gif" width="244" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Drop a note here so I can try to keep track of people!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;If you don&amp;#39;t know about spec# now may be a great time to watch the &lt;a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/26/altdotnet-spec-session.aspx"&gt;recent presentation from altdotnet&lt;/a&gt;&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=177280" width="1" height="1"&gt;</description></item><item><title>Knuth: Wow</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/28/knuth-wow.aspx</link><pubDate>Mon, 28 Apr 2008 20:35:43 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:177274</guid><dc:creator>Greg</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=177274</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/28/knuth-wow.aspx#comments</comments><description>&lt;p&gt;Donald Knuth is a mainstay of computer science from the last what 30 years? He recently did &lt;a href="http://www.informit.com/articles/article.aspx?p=1193856"&gt;an interview with InformIT&lt;/a&gt; talking about some more modern concepts.. Here are some quotes I found quite interesting!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;On the multi-core problem&lt;/h3&gt;  &lt;blockquote&gt;   &lt;p&gt;Donald: I don&amp;#8217;t want to duck your question entirely. I might as well flame a bit about my personal unhappiness with the current trend toward multicore architecture. To me, it looks more or less like the hardware designers have run out of ideas, and that they&amp;#8217;re trying to pass the blame for the future demise of Moore&amp;#8217;s Law to the software writers by giving us machines that work faster only on a few key benchmarks! I won&amp;#8217;t be surprised at all if the whole multithreading idea turns out to be a flop, worse than the &amp;quot;&lt;a href="http://en.wikipedia.org/wiki/Itanium"&gt;Titanium&lt;/a&gt;&amp;quot; approach that was supposed to be so terrific&amp;#8212;until it turned out that the wished-for compilers were basically impossible to write.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;later ...&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;They think a magic bullet will come along to make multicores speed up my kind of work; I think it&amp;#8217;s a pipe dream. (No&amp;#8212;that&amp;#8217;s the wrong metaphor!&lt;strong&gt; &amp;quot;Pipelines&amp;quot; actually work for me&lt;/strong&gt;, but threads don&amp;#8217;t. Maybe the word I want is &amp;quot;bubble.&amp;quot;)&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I agree; pipelines rock!&lt;/p&gt;  &lt;h3&gt;&amp;#160;&lt;/h3&gt;  &lt;h3&gt;On unit testing and mocking&lt;/h3&gt;  &lt;blockquote&gt;   &lt;p&gt;As to your real question, the idea of immediate compilation and &amp;quot;unit tests&amp;quot; appeals to me only rarely, when I&amp;#8217;m feeling my way in a totally unknown environment and need feedback about what works and what doesn&amp;#8217;t. Otherwise, lots of time is wasted on activities that I simply never need to perform or even think about. Nothing needs to be &amp;quot;mocked up.&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;On extreme programming&lt;/h3&gt;  &lt;blockquote&gt;   &lt;p&gt;Still, I hate to duck your questions even though I also hate to offend other people&amp;#8217;s sensibilities&amp;#8212;given that software methodology has always been akin to religion. With the caveat that there&amp;#8217;s no reason anybody should care about the opinions of a computer scientist/mathematician like me regarding software development, let me just say that almost everything I&amp;#8217;ve ever heard associated with the term &amp;quot;&lt;a href="http://en.wikipedia.org/wiki/Extreme_programming"&gt;extreme programming&lt;/a&gt;&amp;quot; sounds like exactly the wrong way to go...with one exception. The exception is the idea of working in teams and reading each other&amp;#8217;s code. That idea is crucial, and it might even mask out all the terrible aspects of extreme programming that alarm me.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;On reusable code&lt;/h3&gt;  &lt;blockquote&gt;   &lt;p&gt;I also must confess to a strong bias against the fashion for reusable code. To me, &amp;quot;re-editable code&amp;quot; is much, much better than an untouchable black box or toolkit. I could go on and on about this. If you&amp;#8217;re totally convinced that reusable code is wonderful, I probably won&amp;#8217;t be able to sway you anyway, but you&amp;#8217;ll never convince me that reusable code isn&amp;#8217;t mostly a menace.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;At the least these words should be stopped and thought of.&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=177274" width="1" height="1"&gt;</description></item><item><title>DDDD 10 [CQS]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/28/dddd-10-cqs.aspx</link><pubDate>Mon, 28 Apr 2008 07:55:18 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:177215</guid><dc:creator>Greg</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=177215</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/28/dddd-10-cqs.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Boring Sunday night so I figured I would get ahead on my posts for the week (I have not been good enough about hitting 5 but I am trying). This post is probably out of order but the concepts are simple, very important, and I will want to link to this later. Also this post and concept is valuable whether or not you are intending to use messaging.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;At altdotnet one thing I talked a bunch about that most people had not really dealt with before was &lt;a href="http://en.wikipedia.org/wiki/Command-Query_Separation"&gt;Command Query Separation&lt;/a&gt;( Martin Fowler also has a &lt;a href="http://martinfowler.com/bliki/CommandQuerySeparation.html"&gt;good post about it&lt;/a&gt; as well ). &lt;em&gt;This is still a running theme in these posts; what I am talking about is rarely if ever new.&lt;/em&gt; &lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I know its a really big surprise that Greg is into things that are based in DbC ... but later in our systems we will care *a lot* about what is mutating the state of the system and the consistency that is required for various actions. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Basically all things that change mutable state are commands and anything that only reads data is a query. Personally I like to add some further distinctions to this like &amp;quot;Only reads immutable state&amp;quot; vs &amp;quot;Reads mutable state&amp;quot; as with the latter I need to worry about the consistency of those reads but command and query are the roots.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;To help think about Commands and Queries let&amp;#39;s look at them in terms of SQL as it has good separation (usually)&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Command = Insert, Update, Delete   &lt;br /&gt;Query = Select (yeah surprisingly a query is a query :))&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Not surprisingly commands and queries have quite different interactions in a SQL database (in fact most of what your transaction isolation levels (Read Commited, etc) talk about is how commands and queries interact with each other). This concept has largely been lost from our code and thought (largely because we rely on databases too much) though some have pushed greatly for this separation. Most systems allow too many things to become commands and queries at once leading to difficult to understand code... &lt;em&gt;Patrick: this is a key metric ;-)&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This may seem quite weird to many people at first but one key point on commands vs queries is that &lt;strong&gt;commands should not return a value. &lt;/strong&gt;Only a query is allowed to return a value though a command can technically change state that is passed into it so ...&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;public void Foo(something s) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //modifies state&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; s.somevalue = &amp;quot;greg&amp;quot;;     &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;is ok but ..&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;public string Foo() {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //modifies state     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;greg&amp;quot;;     &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;is not!! this is a funny distinction eh?&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In general I would prefer you in your code to follow an even &lt;strong&gt;TIGHTER&lt;/strong&gt; distinction and assume that things passed into you are *also* immutable (The reason for this is that handling byref data in anything that crosses serialization boundaries is troublesome, but there are some cases where this can be ok (like setting something in a context)). It may feel really weird and unnatural at first but eventually it will become second nature; I promise. The only thing you are allowed to do is mutate state in some way (or add to an observable state &lt;em&gt;hint hint)&lt;/em&gt;. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;We will get more into this later but as a critical thought what does this do to the concept of request/response? Maybe tomorrow?&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;  &lt;h3&gt;&amp;#160;&lt;/h3&gt;  &lt;h3&gt;&amp;#160;&lt;/h3&gt;  &lt;h3&gt;Side Effect Free Functions and CQS&lt;/h3&gt;  &lt;p&gt;In Domain Driven Design, Eric Evans pushes Side Effect Free functions which are great and should be in your tool bag. What side effect free functions essentially do is change a command into a query. Instead of changing mutable state they create immutable state that is only visible to the caller. Since it is only visible to the caller it can be seen as a query as it is understood to be in a transient state and not visible to other queries.&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=177215" width="1" height="1"&gt;</description></item><item><title>Altdotnet Spec# Session</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/26/altdotnet-spec-session.aspx</link><pubDate>Sat, 26 Apr 2008 20:13:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:177168</guid><dc:creator>Greg</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=177168</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/26/altdotnet-spec-session.aspx#comments</comments><description>&lt;p&gt;Here is another video in the videos from altdotnet. We had Rustan and Mike of the spec# come out from their cave (j/k) at MSR to talk about spec#, boogie, and the future of compile time proving!&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Enjoy it and as always feel free to leave feedback!&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;i&gt;the video is not the best quality but hey ... it was taken on a phone!&lt;br /&gt;&lt;/i&gt;&lt;/p&gt;
&lt;object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="viddler_c19d1c94" height="370" width="437"&gt;&lt;param name="flashvars" value="autoplay=t"&gt;&lt;param name="movie" value="http://www.viddler.com/player/c19d1c94/"&gt;&lt;param name="allowScriptAccess" value="always"&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;embed src="http://www.viddler.com/player/c19d1c94/" type="application/x-shockwave-flash" allowscriptaccess="always" name="viddler_c19d1c94" height="370" width="437"&gt;&lt;/object&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=177168" width="1" height="1"&gt;</description></item><item><title>DDDD 9 [Producers and Consumers]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/25/dddd-9-producers-and-consumers.aspx</link><pubDate>Fri, 25 Apr 2008 05:58:36 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:177082</guid><dc:creator>Greg</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=177082</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/25/dddd-9-producers-and-consumers.aspx#comments</comments><description>&lt;p&gt;Not surprisingly the Producer/Consumer relationship is the single most important concept in messaging. Let&amp;#39;s take a look at those interfaces and how they work. The first we will look at is the interface that an object must meet in order to consume a message.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;public interface IConsume&amp;lt;T&amp;gt;
{
     void Consume(T message);     
}  &lt;/pre&gt;

&lt;p&gt;Next we will need something that can produce a message so we can hook up the consumer to a producer (generally used in a pipeline not for domain objects, domain objects tend to call a message publisher to act for them).&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public interface IPublisher   
{
      void Publish&amp;lt;T&amp;gt;(T message);   
}&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public interface IProduce&amp;lt;T&amp;gt;  
{
     void AttachConsumer(IConsume&amp;lt;T&amp;gt; consumer); 
}  &lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;One important fact to note is that most objects who are producers will only ever have one consumer. So AttachConsumer for&lt;strong&gt; most &lt;/strong&gt;objects will throw an exception (or replace) any time that it already has a consumer and we connect another consumer to that producer (there are some notable exceptions to this rule in the framework that we will discuss later). The reason that we do this is separation of concerns. The producer does not need to know whether there is one or many consumers connected to it.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I think now is a great time to mention that in terms of bringing my domain to messaging these interfaces are the main leak of messaging into the domain. All of the infrastructure etc outside of my domain stays &lt;strong&gt;completely &lt;/strong&gt;outside of my domain. This non-leakage of the infrastructure into our domain is a key concept as we will see shortly when we Remove Infrastructure Out Of Domain and rid ourselves of those pesky nouns inserted by our infrastructure. &lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;One of the great parts of alt.net seattle was the chance to talk with many developers and bounce ideas off of them (ask Dave Laribee I am an &amp;quot;Idea Bouncer&amp;quot;). One of these idea bouncing sessions was with Chris Patterson and Dru Sellers who are working on (and doing a great job I should add) a light weight ESB they call MassTransit. I discussed with them my Producer/Consumer relationships that were quite different that what they were using.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Last night Chris came back to me with a question of whether it was explicit enough in the contract that in a request/response style message which request matched up with which response. While my &amp;quot;first step&amp;quot; in teaching someone messaging is to say &amp;quot;stop thinking request response&amp;quot; these messages can happen fairly often (though not in a synchronous fashion). What is even more likely to have happen is you have an object in a state who when it receives a message &amp;quot;Responds with&amp;quot; no a reply to the client but another routed message itself (see controller example in Mocks are a Code Smell) ... In going through this conversation I started thinking back to DDD and &amp;quot;Intent Revealing Interfaces&amp;quot;.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;What if I could create an interface that was essentially the same thing but did a better job at revealing my intent of using both of these messages in a &amp;quot;triggered&amp;quot; way.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;    class RespondsTo&amp;lt;T&amp;gt;
    {
        public interface With&amp;lt;V&amp;gt; : IConsume&amp;lt;T&amp;gt;, IProduce&amp;lt;V&amp;gt;
        {
            
        }
    }&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;important note: I would &lt;strong&gt;NOT &lt;/strong&gt;use this interface for request/response style messaging ... there are better ways of handling this... the domain object still dispatches!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;This allows me as a user of this class to realize not only that it produces and consumes a message but that the two messages are in fact linked (when/if they are). Consider the following two examples.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;class FooHandler : IConsume&amp;lt;Foo&amp;gt;, IProduce&amp;lt;Bar&amp;gt; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;class FooHandler : RespondsTo&amp;lt;Foo&amp;gt;.With&amp;lt;Bar&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The second example here although it simply implements the same two interfaces gives me a lot more easily discoverable information about how that relationship interacts. A more &amp;quot;appropriate&amp;quot; example might be from the controller example&lt;/p&gt;

&lt;p&gt;class ATMController : RespondsTo&amp;lt;CardInsertedMessage&amp;gt;.With&amp;lt;RequestAuthenticationMessage&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Take this new interface with a grain of salt as I have not spent a good deal of time playing with it yet (I prefer to re-dispatch generally and not have the producer interface on the object itself) but it would seem to be at the outset quite useful (as well as more intention revealing). &lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=177082" width="1" height="1"&gt;</description><category domain="http://codebetter.com/blogs/gregyoung/archive/tags/DDD/default.aspx">DDD</category><category domain="http://codebetter.com/blogs/gregyoung/archive/tags/Featured/default.aspx">Featured</category></item><item><title>altdotnet seattle session video</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/25/altdotnet-seattle-session-video.aspx</link><pubDate>Fri, 25 Apr 2008 05:32:11 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:177081</guid><dc:creator>Greg</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=177081</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/25/altdotnet-seattle-session-video.aspx#comments</comments><description>&lt;p&gt;Here is the video from the altdotnet talks... There are many good points brought up through out this fishbowl; the one that I really wish was answered better was the question from Adam on introducing stuff to a team who is not familiar with messaging... What are your thoughts?&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;object id="viddler_e8529fc5" height="451" width="545" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"&gt;&lt;param name="_cx" value="14420"&gt;&lt;param name="_cy" value="11933"&gt;&lt;param name="FlashVars" value=""&gt;&lt;param name="Movie" value="http://www.viddler.com/player/e8529fc5/"&gt;&lt;param name="Src" value="http://www.viddler.com/player/e8529fc5/"&gt;&lt;param name="WMode" value="Window"&gt;&lt;param name="Play" value="0"&gt;&lt;param name="Loop" value="-1"&gt;&lt;param name="Quality" value="High"&gt;&lt;param name="SAlign" value="LT"&gt;&lt;param name="Menu" value="0"&gt;&lt;param name="Base" value=""&gt;&lt;param name="AllowScriptAccess" value="always"&gt;&lt;param name="Scale" value="NoScale"&gt;&lt;param name="DeviceFont" value="0"&gt;&lt;param name="EmbedMovie" value="0"&gt;&lt;param name="BGColor" value=""&gt;&lt;param name="SWRemote" value=""&gt;&lt;param name="MovieData" value=""&gt;&lt;param name="SeamlessTabbing" value="1"&gt;&lt;param name="Profile" value="0"&gt;&lt;param name="ProfileAddress" value=""&gt;&lt;param name="ProfilePort" value="0"&gt;&lt;param name="AllowNetworking" value="all"&gt;&lt;param name="AllowFullScreen" value="true"&gt; &lt;embed src="http://www.viddler.com/player/e8529fc5/" width="545" height="451" type="application/x-shockwave-flash" allowscriptaccess="always" name="viddler_e8529fc5"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=177081" width="1" height="1"&gt;</description></item><item><title>DDDD 8 [Fluent Builders and Tests]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx</link><pubDate>Tue, 22 Apr 2008 06:43:21 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:176869</guid><dc:creator>Greg</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=176869</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-8-fluent-builders-and-tests.aspx#comments</comments><description>&lt;p&gt;Ok this will not be the only post I wrote on this subject as its a pretty big one but I had some good discussions with Udi about this over the weekend. I find that my present way of doing this is pretty cool and of great value to people already using messaging systems without having a big discussion about all of the aspects of testing in messaging based systems (I imagine that would be about 50-60 pages for a cursory overview).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In commenting on &lt;a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/14/dddd-4-messages-are-value-objects.aspx"&gt;Messages are Value Objects&lt;/a&gt; korkless hit the nail on the head as to why equality comparisons for messages should be overridden to operate with value semantics. When I am testing I can use my fluent interface to build up the message I expect to receive and compare the two of them in a single Assert. I should have been more clear when I said &amp;quot;I can&amp;#39;t see why anyone would use this in their &lt;em&gt;domain&amp;quot;; &lt;/em&gt;there is a use for it ... just not so much in your domain as in your tests.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Since I am fried still but code speaks 1000 words; I am taking a simple test from &lt;a href="http://codebetter.com/blogs/gregyoung/archive/2008/02/12/mocks-are-a-code-smell.aspx"&gt;Mocks are a Code Smell&lt;/a&gt; and implementing it here with a fluent builder to illustrate.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;public void Controller_ShouldRequestAuthentication_When_CardInsertedMessageReceived()  
{
     ATMController Controller = New.ATMController.InState(ATMController.States.AwaitingCard);
     CardInsertedMessage InsertedMessage = New.CardInserted.ForCard(&amp;quot;1111111111111111&amp;quot;).WithAccountHolderOf(&amp;quot;Greg Young&amp;quot;);
     controller.ConsumeMessage(InsertedMessage);
     List messages = new List(FakeMessageDispatcher.AllMessages());
     RequestAuthenticationMessage Expected = New.RequestForAuthentication.ByPinNumberOnly.From(Controller);
     Assert.AreEqual(Expected, messages[0]);
}   &lt;/pre&gt;

&lt;p&gt;This allows me to write a very fluent test ... as we will see later, we can even refactor this test to be much smaller and more expressive : imagine ...&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public void Controller_ShouldRequestAuthentication_When_CardInsertedMessageReceived()  
{
     When.A(New.ATMController.InState(ATMController.States.AwaitingCard))
	.Consumes(
		New.CardInserted.ForCard(&amp;quot;1111111111111111&amp;quot;).WithAccountHolderOf(&amp;quot;Greg Young&amp;quot;)
        )
        .Expect.AMessageOf(New.RequestForAuthentication.ByPinNumberOnly.From(Controller));
}   &lt;/pre&gt;

&lt;p&gt;The fact that I am using a fluent builder to define my expectation helps to make my expectations a lot clearer and to avoid having multiple asserts. If later I decide to add a field to the message and the object sets that field away from its default I will also now get a test failure since I am allowing the message to do its own comparison.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;This is an important pattern to keep in mind not only for messages but also for Value Objects within your domain since as I said before .... Messages are Value Objects.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;One thing I have been playing a lot with lately is changing tests like this to a context based format ... just a passing thought.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=176869" width="1" height="1"&gt;</description></item><item><title>DDDD 7 [Wow]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-7-wow.aspx</link><pubDate>Tue, 22 Apr 2008 06:14:02 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:176868</guid><dc:creator>Greg</dc:creator><slash:comments>16</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=176868</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/22/dddd-7-wow.aspx#comments</comments><description>&lt;p&gt;I have to say Alt.Net Seattle and the MVP summit were one of the best developer community experiences I have ever have. Aside from all the awesome side discussions over beers or just hanging out in the halls I was quite surprised to find this during the opening of the open spaces.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://codebetter.com/blogs/gregyoung/WindowsLiveWriter/DDDD7Wow_146B7/DDDD2.jpg"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="184" alt="DDDD2" src="http://codebetter.com/blogs/gregyoung/WindowsLiveWriter/DDDD7Wow_146B7/DDDD2_thumb.jpg" width="244" align="left" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This was a huge surprise to me to say the least, not even that there was one ... but there were THREE on the same subject! At first I was a bit hesitant as there are still many things not completely cohesive in my head but I am really glad that this session happened. It was amazing to have not only Udi Dahan in the fish bowl but also Martin Fowler and quite a bit of Dru. Let&amp;#39;s just say that the discussions were great!&lt;/p&gt;  &lt;p&gt;&lt;em&gt;I do wonder though ... who decided the first D was Distributed? :)&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;I have to say that I was a bit disappointed that many of those discussions focused primarily on infrastructure but it is an important part of the topic that I will go into quite a bit of later.&lt;/p&gt;  &lt;p&gt;I think my camera got most of the session so I will try to post a video of this and the Next Generation Architectures fishbowl (I was expecting this one to be more focused on infrastructures)&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;There was a question asked in the DDDD fishbowl that was never answered very well that I hope to address in the near future:&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;My team has never used messaging before; what would be the best way for us to get up to speed and to learn to do things right.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;If you are already doing messaging and have thoughts on this drop me an email offline at &lt;a href="mailto:gregorzyzounzgz1@gzmail.com"&gt;gregorzyzounzgz1@gzmail.com&lt;/a&gt; (remove the zs)&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Aside from those two great talks the rest of altdotnet was also great. It is just crazy what happens when you get so many smart people into one place with high bandwith communications. Aside from conversations with Udi (which were great btw because they were mainly about the 5% of things we disagree on) I also had great conversations with many others including Chris and Dru who are working on MassTransit which is shaping up to be a useful and lightweight tool for enterprise integrations. I am looking forward to seeing this project progress.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In the Next Generation Architectures talk I got to meet Addy S for the first time as well as some other smart gents. &lt;em&gt;If the guy with the beard who was in the fishbowl a bunch is reading this please leave a comment or drop me an email, I never got your name.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;I am still in shell shock from all of last week and in need of sleep. Before tomorrow though I wanted to ask people reading this whether they would prefer the next cluster of posts to be implementing the easiest messaging scenario (in process infrastructure abstraction) or whether they would prefer to get more into the underlying infrastructure and concepts of a next generation default architecture ...&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=176868" width="1" height="1"&gt;</description></item><item><title>DDDD 6 [Fluent Builders, Alternate Ending]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx</link><pubDate>Wed, 16 Apr 2008 22:22:32 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:176740</guid><dc:creator>Greg</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=176740</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/16/dddd-6-fluent-builders-alternate-ending.aspx#comments</comments><description>&lt;p&gt;Sitting with an awesome view of Seattle ... Last night I spent some time over at Eleutian with Aaron and during our discussions we talked a bit about Fluent Builders. He brought up an interesting idea to me of whether or not builders should be immutable and I think I agree with his mutable version so I am going to provide another way of implementing the Fluent Builders from DDDD 5 [Messages have Fluent Builders].&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The basics of the discussion revolve around the methods used for setting fields in the builder. It is best seen in code comparing the builder in the last post of ...&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;	public class EquityQuoteMessageBuilder {
		internal readonly ExchangeGateways m_Gateway;
		internal readonly string m_Security;
		internal readonly DateTime m_Time;
		internal readonly decimal m_BidPrice;
		internal readonly decimal m_AskPrice;


		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder OnGateway(ExchangeGateways _Gateway) {
			return new InsertEquityQuoteCommandBuilder(_Gateway,m_Security,m_Time,m_BidPrice,m_AskPrice);
		}

		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder ForSecurity(string _Symbol) {
			return new InsertEquityQuoteCommandBuilder(m_Gateway,_Symbol,m_Time,m_BidPrice,m_AskPrice);
		}

		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder At(DateTime _Time) {
			return new InsertEquityQuoteCommandBuilder(m_Gateway,m_Security,_Time,m_BidPrice,m_AskPrice);
		}

		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder WithSpreadOf(decimal _BidPrice, decimal _AskPrice) {
			return new InsertEquityQuoteCommandBuilder(m_Gateway,m_Security,m_Time,_BidPrice,_AskPrice);
		}

		private EquityQuoteMessageBuilder(ExchangeGateways _Gateway,string _Symbol,DateTime _Time,decimal _BidPrice,decimal _AskPrice) {
			m_Gateway = _Gateway;
			m_Security = _Symbol;
			m_Time = _Time;
			m_BidPrice = _BidPrice;
			m_AskPrice = _AskPrice;
		}

        	public EquityQuoteMessageBuilder {}
		
        	public static implicit operator EquityQuoteMessage(EquityQuoteMessageBuilder _Builder) {
				return new InsertEquityQuoteCommand(_Builder.m_Gateway, _Builder.m_Security, _Builder.m_Time, _Builder.m_BidPrice, _Builder.m_AskPrice);
		}
	}&lt;/pre&gt;


&lt;p&gt;With the modified version being ....&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;	public class EquityQuoteMessageBuilder {
		internal ExchangeGateways m_Gateway;
		internal string m_Security;
		internal DateTime m_Time;
		internal decimal m_BidPrice;
		internal decimal m_AskPrice;


		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder OnGateway(ExchangeGateways _Gateway) {
			m_Gateway = _Gateway;
			return this;
		}

		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder ForSecurity(string _Symbol) {
			m_Symbol = _Symbol;
                        return this;
		}

		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder At(DateTime _Time) {
			m_Time = _Time;
			return this;
		}

		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder WithSpreadOf(decimal _BidPrice, decimal _AskPrice) {
			m_BidPrice = _BidPrice;
			m_AskPrice = _AskPrice;
			return this;
		}

        	public EquityQuoteMessageBuilder {}
		
        	public static implicit operator EquityQuoteMessage(EquityQuoteMessageBuilder _Builder) {
			return new InsertEquityQuoteCommand(_Builder.m_Gateway, _Builder.m_Security, _Builder.m_Time, _Builder.m_BidPrice, _Builder.m_AskPrice);
		}
	}&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The main difference between the two implementations of the Fluent Builder being whether or not the builder itself is mutable. This version is much more terse but the question remains for me of ... Do I care? I am generating them ... The one place there is quite a bit of benefit is in terms of performance if this is being called often in production code as it will create many less objects and the JIT could be more intelligent about inlining.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;My original reason for making the builders immutable was for some odd edge conditions like&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;InsertOrderMessageBuilder AlreadyHasSecurityAndBroker = New.InsertOrder.ForSecurity(&amp;quot;RIM&amp;quot;).FromBroker(0);&lt;/p&gt;

&lt;p&gt;InsertOrderMessage FirstMessage = AlreadyHasSecurityAndBroker.ToBuy(1000).AtPrice(15.00).AcceptingOnlyCash;&lt;/p&gt;

&lt;p&gt;InsertOrderMessage SecondMessage = AlreadyHasSecurityAndBroker.ToSell(1000).AtPrice(15.50);&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;But after thinking about this a bit I could do something similar using a mutable builder ... I could extract a method that returned me the AlreadyHasSecurityAndBroker version of the builder to something similar to an object mother. I will have to think a bit more about this but as of now I am considering moving my immutable builders to their mutable brethren.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Another difference that we found in implementation was my use of the DebuggerStepThrough attribute. For those who are not familiar with it; it tells the debugger to not step into this method ... so if I have a long line like ..&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;InsertOrderMessage message = New.InsertOrder.ForSecurity(&amp;quot;RIM&amp;quot;).FromBroker(12).ToBuy(2000).AtPrice(15.00);&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;and I set a break point on it ... then hit F11 to step into the code, it will bypass all of the short &amp;quot;setter&amp;quot; calls and take me directly to the implicit cast which is generally where my problem would be happening as the builder doesn&amp;#39;t really care about its state. &lt;em&gt;I really like the way this works! &lt;/em&gt;It is a good tool to remember for scenarios like this&lt;em&gt;&amp;#160;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;A side discussion came up with someone else in regard to why I define the internal state of the builder as internal and not private. The reason why I do this is because I write (read: generate, &lt;em&gt;the reason why I generate tests is that these classes are generated then manually edited from that point forward&lt;/em&gt;) tests for the builders and want to check the fields one by one when issuing a set ... As an example of this kind of test consider:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;		[Test]
		public void Should_Set_Gateway() {
			EquityQuoteMessageBuilder builder = new EquityQuoteMessageBuilder().OnGateway(ExchangeGateways.CNQToronto);
			Assert.AreEqual(ExchangeGateways.CNQToronto,builder.m_Gateway);
		}&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;I could just as easily put properties on the builder to expose its internal state or could make the fields public but when a builder is being used outside of these tests I don&amp;#39;t want code to be able to access the internal state of the builder (I like to consider them write only). The need to have these things working properly kind of fits in with the want to have the debugger step through the code (since the code is simple and I have tests to make sure the simple code is working, I can safely guess that I wouldn&amp;#39;t need to be debugging the code).&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=176740" width="1" height="1"&gt;</description><category domain="http://codebetter.com/blogs/gregyoung/archive/tags/Featured/default.aspx">Featured</category></item><item><title>DDDD 5 [Messages have Fluent Builders]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx</link><pubDate>Wed, 16 Apr 2008 03:11:42 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:176696</guid><dc:creator>Greg</dc:creator><slash:comments>20</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=176696</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/15/dddd-5-messages-have-fluent-builders.aspx#comments</comments><description>&lt;p&gt;In DDDD 4 I was lucky enough to get some commentary from an astute reader &lt;a href="http://lostechies.com/blogs/jimmy_bogard/default.aspx"&gt;Jimmy Bogard&lt;/a&gt;, he wrote in his comment:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;I think I&amp;#39;d have to respectfully disagree here. Valid state can be enforced in more ways than an immutable object. Immutability leads to everything supplied in the constructor. If I have coarse-grained services, messages can get fairly large. Suppose instead of Email, you had something like Order. Order contains LineItems (flattened out), maybe Addresses, ShippingOptions, etc etc. This is quite a lot to satisfy in an immutable fashion. For simple message, I like to make them immutable to satisfy invariants. But if I&amp;#39;m using things like XML Serialization, it&amp;#39;s much simpler to have a plain DTO and a Validate method. I leave it to my Factory to satisfy invariants. As for other Value Object semantics like equality, I never got much use out of those. I never compared DTOs, as they were strictly used at the service layer boundary. Nothing interesting was ever done with them, as they are immediately used with a Mapper to get back to real domain objects. When I tried this concept (DTOs are Value Objects) a couple of years back, it started out nicely but broke down in our real-world scenarios of coarse-grained services. I chalked it up to my misplaced desire to model everything in our system in DDD concepts (entity, value object, service). Perhaps you&amp;#39;ve had a different experience.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The builder helps alleviate a lot of this pain while at the same time making our code more readable. I have talked about fluent builders in the past in &lt;a href="http://codebetter.com/blogs/gregyoung/archive/2007/12/05/a-use-for-extension-methods.aspx"&gt;A Use for Extension Methods&lt;/a&gt; but I will briefly go through how they work again. The basic parts of the pattern are to create a class that is immutable and has internally the same members that will be required to call the constructor of the target object. Properties and methods are exposed with fluent names to help build the target object. The target object&amp;#39;s constructor is eventually called through an implicit cast operation. As is &lt;strong&gt;always &lt;/strong&gt;the case code speaks 1000 words.&lt;/p&gt;  &lt;p&gt;Let&amp;#39;s take a simple message. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;InsertOrderCommand ioc = new InsertOrderCommand(null, MarketSide.Buy, &amp;quot;B12345678&amp;quot;, 0.12m, &amp;quot;NT&amp;quot;,    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TestDate, 1000, false, 100, 1000, false, TestDate, OrderDuration.Day, ExchangeGateways.CNQToronto, false, false);&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;YUCK!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let&amp;#39;s try rewriting that with a builder.&lt;/p&gt;  &lt;p&gt;InsertOrderCommand ioc = New.InsertDayOrder&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .On(ExchangeGateways.CNQToronto)&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .FromBroker(10)&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .ToBuy(1000)&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .OfSecurity(&amp;quot;NT&amp;quot;)&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .At(0.12m)&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .WithUniqueOrderNumberOf(&amp;quot;B12345678&amp;quot;);&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;note that the builder does not always match up 1-1 with the object ... in this example ToBuy(Volume) sets that the order type is of type buy, it sets another field, and it sets the volume.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Phew that&amp;#39;s a whole lot easier to read! Let&amp;#39;s look at how to have this happen. First let&amp;#39;s look at a message.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;    [Serializable]
    public class EquityQuoteMessage : ILevelOneCommand, IHasSecurityInformation
    {
        public readonly DateTime Time;
        public readonly string Symbol;
        public readonly decimal BidPrice;
        public readonly decimal AskPrice;
        public readonly ExchangeGateways Gateway;

        public EquityQuoteMessage(ExchangeGateways _Gateway, string _Symbol, DateTime _Time, decimal _BidPrice, decimal _AskPrice)
        {
            Gateway = _Gateway;
            Symbol = _Symbol;
            Time = _Time;
            BidPrice = _BidPrice;
            AskPrice = _AskPrice;
        }

        public string GetSummary()
        {
            return &amp;quot;InsertEquityQuote: &amp;quot; + Gateway.ToString() + &amp;quot; &amp;quot; + Symbol + &amp;quot;  &amp;quot; + BidPrice + &amp;quot;-&amp;quot; + AskPrice;
        }

        ExchangeId IHasSecurityInformation.ExchangeId
        {
            get { return ExchangeGatewayIdTranslator.TranslateGatewayToExchangeId(this.Gateway); }
        }

        string IHasSecurityInformation.Symbol
        {
            get { return this.Symbol; }
        }
    }&lt;/pre&gt;

&lt;p&gt;Now ... we need a builder for this message.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;	public class EquityQuoteMessageBuilder {
		internal readonly ExchangeGateways m_Gateway;
		internal readonly string m_Security;
		internal readonly DateTime m_Time;
		internal readonly decimal m_BidPrice;
		internal readonly decimal m_AskPrice;


		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder OnGateway(ExchangeGateways _Gateway) {
			return new InsertEquityQuoteCommandBuilder(_Gateway,m_Security,m_Time,m_BidPrice,m_AskPrice);
		}

		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder ForSecurity(string _Symbol) {
			return new InsertEquityQuoteCommandBuilder(m_Gateway,_Symbol,m_Time,m_BidPrice,m_AskPrice);
		}

		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder At(DateTime _Time) {
			return new InsertEquityQuoteCommandBuilder(m_Gateway,m_Security,_Time,m_BidPrice,m_AskPrice);
		}

		[DebuggerStepThroughAttribute]
		public EquityQuoteMessageBuilder WithSpreadOf(decimal _BidPrice, decimal _AskPrice) {
			return new InsertEquityQuoteCommandBuilder(m_Gateway,m_Security,m_Time,_BidPrice,_AskPrice);
		}

		private EquityQuoteMessageBuilder(ExchangeGateways _Gateway,string _Symbol,DateTime _Time,decimal _BidPrice,decimal _AskPrice) {
			m_Gateway = _Gateway;
			m_Security = _Symbol;
			m_Time = _Time;
			m_BidPrice = _BidPrice;
			m_AskPrice = _AskPrice;
		}

        	public EquityQuoteMessageBuilder {}
		
        	public static implicit operator EquityQuoteMessage(EquityQuoteMessageBuilder _Builder) {
				return new InsertEquityQuoteCommand(_Builder.m_Gateway, _Builder.m_Security, _Builder.m_Time, _Builder.m_BidPrice, _Builder.m_AskPrice);
		}
	}&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I have written a generator to make skeletons of these builder classes that I may post which simplifies this process greatly (especially if you use resharper ctrl+rr or f2 everything else is done) but I will only release it if people promise not to make fun of the code as it was written in an afternoon just to save time when writing a few and looks like it was written by a second year student :-)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blog.eleutian.com/"&gt;Aaron over at elutian&lt;/a&gt; uses a structurally identical pattern (except for the additional use of extension methods in &lt;a href="http://codebetter.com/blogs/gregyoung/archive/2007/12/05/a-use-for-extension-methods.aspx"&gt;A Use for Extension Methods&lt;/a&gt;) in his post &lt;a href="http://blog.eleutian.com/2007/09/29/FluentFixtures.aspx"&gt;Fluent Fixtures&lt;/a&gt; the key difference between what I call a Fluent Builder and what he calls a Fluent Fixture is in the &lt;strong&gt;intent. &lt;/strong&gt;Aaron uses his fixtures only for the initialization of setup data for tests which they are EXTREMELY valuable for (especially when dealing with messages as it produces a soluble method of expressing what is in the method) ... My fluent builders are used not only for the initialization of test data but also for the creation of real objects (I might even go so far as to say that the construtors for messages should be made internal and you should only expose builders though in some extremely high performance conditions you might want to actually use constructors). &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Jimmy Bogard hit the nail on the head in that complex messages can be a nightmare to build immutably because they get really big. What we do to get around that is we use an mutable builder that we then cast to an immutable message. This gives us the best of both worlds although the builders can be annoying to type, since essentially we have made explicit a transient mutable version of the message and a fixed immutable version of the message.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;To be more clear, let&amp;#39;s try a bigger example like the one that Jimmy mentions and see how bad it is to create a bigger message. I will use a DTO example since they tend to be the offenders&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;OrderDTO order = New.OrderDTO 
  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .WithId(SomeGuid) 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .ShippingTo(New.Address .....) 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .AddLineItem(New.LineItem.For(12).Of(&amp;quot;Product 1&amp;quot;).At(15.99) 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .AddLineItem(New.LineItem.For(1).Of(&amp;quot;Product 2&amp;quot;).At(22.97) 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .AddLineItem(New.LineItem.For(15).Or(&amp;quot;Product 3&amp;quot;).At(15.22)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;naturally you could use a loop etc when building the message (and sometimes in terms of test fixtures you may find your self actually referencing the Builder then later adding more items to it similar to how an object mother would work but this is another post)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Of course we can go a different route than this. You will notice that a natural break is occurring in the setup of our message between the root and the pieces under the root. What if instead of trying to create one really big message we created a few smaller ones just like how our actual setup is working. In other words what if instead of having an OrderDTO as one big message we introduced a container to hold smaller messages and relate them as being one something like:&lt;/p&gt;

&lt;p&gt;SnapShot&lt;/p&gt;

&lt;p&gt;&amp;#160;&amp;#160;&amp;#160; CreateOrderMessage&lt;/p&gt;

&lt;p&gt;&amp;#160;&amp;#160;&amp;#160; AddLineItemMessage&lt;/p&gt;

&lt;p&gt;&amp;#160;&amp;#160;&amp;#160; AddLineItemMessage&lt;/p&gt;

&lt;p&gt;&amp;#160;&amp;#160;&amp;#160; AddLineItemMessage&lt;/p&gt;

&lt;p&gt;EndSnapShot&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;This would help prevent us from having to deal with the larger items but each method has their own benefits. The main benefit of the first method is that its schema exists within the message. Especially when dealing with external clients or other types of integration points this can be extremely beneficial (there is no need of logic to figure out how to build the data). When combined with something like XML this can be extremely powerful.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The second method can also be extremely powerful in a closed environment for a few reasons. In many circumstances it is actually simpler to send over these smaller pieces, the big problem with this methodology is that you need to have domain logic existing on the client in order to know how to process these smaller messages to get the client to a state matching what the originator of the message intended. As we will see later on though, in some circumstances (especially when we want disconnected access) some of this logic will be distributed for other reasons anyways so it may become a viable option.&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=176696" width="1" height="1"&gt;</description></item><item><title>DDDD 4 [Messages are Value Objects]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/14/dddd-4-messages-are-value-objects.aspx</link><pubDate>Tue, 15 Apr 2008 00:51:19 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:176673</guid><dc:creator>Greg</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=176673</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/14/dddd-4-messages-are-value-objects.aspx#comments</comments><description>&lt;p&gt;This week I may take a break from these posts for a few days due to the Summit and Alt.Net but I should still push a few posts... &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Messages as we will see are an interesting breed of objects as the same object will sometimes have differing connotations in different sections of code. In the domain messages are to be treated as value objects even though they may be treated differently later by certain types of infrastructure. There a a few aspects of being a value object that are imperative to a successful implementation of a messaging system within the domain. By looking at the general rules for value objects we can derive many of the rules for messages within our domain.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Messages &lt;strong&gt;ARE&lt;/strong&gt; immutable &lt;/li&gt;    &lt;li&gt;Messages should vary in equality based upon the data they hold &lt;/li&gt;    &lt;li&gt;Messages should have fluent builders &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;&lt;em&gt;Messages ARE immutable...&lt;/em&gt;&lt;/h4&gt;  &lt;p&gt;I don&amp;#39;t think that I can stress this point enough. &lt;strong&gt;Mutable messages are an anti-pattern. &lt;/strong&gt;They are the path to a system that is held together with duct tape and bubble gum. There are many reasons why this is the case but the simplest reason is that you suddenly end up with different semantics when you cross a serialization boundary. &lt;/p&gt;  &lt;p&gt;Let&amp;#39;s imagine for a moment the ridiculous case of a simple service that receives a message, alters a field in it then sends an ack that it completed its work (the caller keeping a copy of the message checks for the changed field when it receives the ack). Even worse it does something to the message that causes the message to say raise an event back to the caller. These sound like ridiculous cases that no one would ever do but I have seen both done. Either will work just fine so long as you are in the same process but will break once you introduce a serialization boundary.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let&amp;#39;s make an additional rule to get help this issue (please,never need this rule): You should never hold a reference to a message after you send it. As far as you are concerned, once you dispatch it, its gone. We will learn later that if at all possible we also want to limit our exposure to any shared state between the message sender and receiver as it is this shared state that limits scalability.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Another reason that mutable messages are evil is that you never actually&lt;strong&gt; know &lt;/strong&gt;that the message is in a valid state. The use of immutable messages introduces a stronger contract for your messaging as you know that if a message exists that it is also in a valid state. You will never have a SendEmailMessage that is missing a To and From unless you specify that that is a valid way of sending an email in your system. Because you know that your messages are in a valid state you can in the case of using a shared library for messaging fail early on your requests i.e. instead of whatever processes the SendEmailMessage saying &amp;quot;Hey guy you need to actually send me someone who this email is to!&amp;quot;, the thing sending the message fails to create a valid message. This is hugely beneficial because what if the thing processing the SendEmailMessage is actually on another system? Not only have we transported an invalid request across the wire but we now also have to route an error back to the sender!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;What we have done by making our messages in an assumed valid state is allowed other code to operate upon them while making assumptions that the message is meeting its minimum contract. In other words, the thing processing the SendEmailMessage does not need to actually check that the fields in the SendEmailMessage are what it needs them to be unless it is requiring something that is above the specified contract of the message (let&amp;#39;s say that it requires something the message defines as optional). The SendEmailMessage has been given the responsibility of insuring that it is always in a valid state.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;A side benefit of this is that when dealing with messaging to infrastructure services the invariants you maintain to them are part of your messages which are part of your ubiquitous language as opposed to being implemented in the code handling the messages (infrastructure).&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I like to think about using validated messages in this way in relation to design by contract. My code that sends the email doesn&amp;#39;t just work on a message that defines data layouts, the message also defines invariants about my data! These can be quite simple (To is not null) or they can be extremely complex like To must be a valid email address that is not at gmail.com. This becomes extremely valuable later when we go to deal with the consumption of messages; one of the biggest problems with interfaces and testing with .NET is that you cannot specify enough information to narrow your contract. You should therefore to be precise test the entire surface of the interface for every implementation (yuck, these tests add up). It is far easier to introduce the message that defines the invariants and then assume them to be true in your code.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Quite quickly we end up thinking about a contract first method of development for these types of things. In other words that we create our message objects before we create the code that sends or processes them. I personally like this method of development a lot for messaging based systems.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Serialization can cause issues with your messaging if you deal with un-trusted clients (versionization problems can cause this as well) as if you use default serialization it can (and will) deserialize messages that are not valid according to say the logic you apply in their constructor! If you are running with untrusted clients you should implement ISerializable on your messages to take control of the serialization process for them so you can insure they are only created in a valid state. For a large system you may want to consider moving this behavior to a &amp;quot;message&amp;quot; layer super type or consider mixing in an interface/implementor pair for ISerializable if you are using compile time AOP &lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;We will look later why but never use runtime AOP on your messages or on the handling functions, its slow, redundant, and way too complicated.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;&lt;em&gt;&lt;/em&gt;&lt;/h4&gt;  &lt;h4&gt;&lt;em&gt;Messages should vary in equality based upon the data they hold&lt;/em&gt;&lt;/h4&gt;  &lt;p&gt;Why someone would feel the need to compare messages for equality in a domain context is completely beyond but .... If you do, do consider them to be varying based upon the data that they hold not upon their reference. Beyond that if you can come up with a reason to do it I would love to hear it :-)&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt; &lt;em&gt;&lt;/em&gt;  &lt;h4&gt;&lt;em&gt;Messages should have fluent builders&lt;/em&gt;&lt;/h4&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt; This is something I believe may be controversial and this post is getting a bit long so I will finish this part as DDDD 5 [Messages have FluentBuilders] on the bus to the summit today.&lt;img src="http://codebetter.com/aggbug.aspx?PostID=176673" width="1" height="1"&gt;</description></item><item><title>DDDD 3 [What/Why this]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/11/dddd-3-what-why-this.aspx</link><pubDate>Fri, 11 Apr 2008 22:00:25 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:176616</guid><dc:creator>Greg</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=176616</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/11/dddd-3-what-why-this.aspx#comments</comments><description>&lt;p&gt;I jumped right into DDDD 1 without really explaining what this is or why I am doing it. I am going to break a day to explain my reasons for posting this information and what I intend to go through.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Those who have had a beer with me at a summit/conference/etc may know that I have been dealing with the unusual combination of messaging and domain driven design together for probably about four years now though there are bits and pieces that are even older than that. &lt;em&gt;Its a favorite discussion topic of mine over a beer...&lt;/em&gt; Over the time I have tried many different schemes of getting this working (many of which were drastic failures in my mind) that eventually became workable solutions, each with its own strengths and weaknesses. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I presented some of what has been come up with at QCon last year but to be honest I don&amp;#39;t think it was fleshed out enough yet at that point. I wanted to drastically change my slides only a week before the presentation! I have been trying since QCon (about 6 months) to get all of these thoughts put together into a cohesive paper; the problem is that paper quickly becomes more like a book and I find myself rewriting it constantly in search of cohesion. I have probably typed up about 1000 pages of information yet only have about 45 pages to show for it and right now I am still extremely frustrated with those 45 pages.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;To me a passing thought style delivery is much easier for me to deal with as I can reasonably maintain cohesion over a few pages of information. As such I have decided to just begin throwing up my thoughts / realizations on the subject into blog posts. Hopefully at the end I will have learned a lot by posting all of the material as I find trying to get things written down often forces me to distill them in my own mind to simpler versions. I warn you they may be scattered and/or difficult to follow and may not necessarily be in what &amp;quot;most&amp;quot; would be deemed a correct chronological order. I am unfortunately not a person who can easily sit down and write what is probably a books worth of information in any sort of cohesive manner, nor to I particularly want the stress of trying to do so.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Beyond that because much of what I am doing has been done in environments that I have been in I worry about the generality of some of the things I have come across with. These posts also serve as a great way for me to get feedback on those types of issues. I would imagine someone who is reading such a post set probably has at a minimum an exposure to DDD or to messaging; I encourage feedback if you think something that I said is &amp;#39;full of it&amp;#39; because frankly; I may be!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;When I am done with all of these thoughts I may attempt to seek someone to help me make them more coherent and linear to unify them into one piece of material that can be more easily consumed ...&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;That said I will do my best to keep cohesion, this is much easier to do when the code starts coming into play ... Although I do not have an exact outline for what everything will be here I will be releasing the associated code into a SVN repository that is built upon with each post and that by no way means I will be using vertical slices ... I am now looking more at growing that code slowly and organically by attempting to solve new situations brought in through these posts. At the end it should be at the least an interesting bit of code for exploration of the concepts.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Some have asked that I start pushing up large amounts of code and discussion early, things like how the messages get dispatched and processed. Over my experiences the largest thing I have learned is that how these things happen is very much dependent upon the needs of the application (especially things like levels/regions of trust, disconnected data access, networking boundaries, and the need for persistent subscriptions). Throughout these posts I will be giving all of the building blocks as well as guidance for how you may want to use them in different scenarios but there is no one size fits all solution (no matter what BizTalk people tell you ;-)) As I said last night to someone ... messaging is like legos, it only requires a bit of creativity to make something not in the picture.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;As for what I intend to post, let me start by saying; there will be &lt;strong&gt;ALOT &lt;/strong&gt;of posts, months worth. I would not be surprised to see DDDD with a three digit number next to it. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;For the first month or so of posts I think most things will be conceptual but it will include the introduction of a simple in-process messaging framework. I want to look in particular at things like using messages and a fluent interface instead of using DI for domain/infrastructure services. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;After that I want to continue into some other more advanced areas of distributed meshing, some things I want to cover include basic client server interactions, dealing with data warehouses, peer meshing, load balancing, distributed locking, consistency, versionization of clusters, pure pubsub environments, and much more. To be honest I have no idea how long these will take but I expect a pretty large number with frequent posts.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Thrown in throughout these will be other posts that pop into my mind like Application Sagas vs Application Services, Aggregates as Sentries, or my next post Messages are Value Objects. These tend to be more so on the DDD side where the messaging side is deliberately removed. One of our goals in moving our domain to using messages is to make it care about LESS infrastructure, while the messages themselves may be first class citizens in our domain the infrastructure supporting them is not (nor is it really referenced in the domain).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I apologize in advance for the lack of cohesion in these posts but hopefully after writing them all I will have a better view of how to make this all cohesive (I hear putting all of the titles on index cards and moving them around on a wall works pretty well but would love to hear any successes people have had with things like this).&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=176616" width="1" height="1"&gt;</description></item><item><title>DDDD 2 [Messaging and the Ubiquitous Language]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/11/dddd-2-messaging-and-the-ubiquitous-language.aspx</link><pubDate>Fri, 11 Apr 2008 04:33:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:176586</guid><dc:creator>Greg</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=176586</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/11/dddd-2-messaging-and-the-ubiquitous-language.aspx#comments</comments><description>&lt;p&gt;Yay!! 2 days of blogging at lunch (then at home, time crunch today so excuse the hurried format) in a row, keeping on track. In DDDD 1 we looked at boundaries where messaging is applicable / good. We will in the future look more at why we would want to message on those boundaries but first it is time to see how messaging may impact our ubiquitous language which is imho the most important part of Domain Driven Design.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;The largest place that our ubiquitous language is changed is in dealing with infrastructure services within our domain. As has been written about &lt;a href="http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html"&gt;Java, C# tends to be the land of the nouns&lt;/a&gt;. Unfortunately this creeps into our domain and we end often with many nouns in our language that only represent gateways to verbs. An example may make this more clear ...&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;Do you have an EmailGateway, IEmailGteway : EmailProvider, IEmailer, DomainContext.Current.EmailProvider.SendEmail(email), etc in your domain? Is it part of your ubiquitous language? How do you discuss the action of &lt;b&gt;SENDING &lt;/b&gt;an email? Why is it that this verb has become an interaction with a noun? &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;I have seen some go much further than this as well under the guise of loose coupling (read testability). I have even seen people going to the point of injecting things like email providers directly into domain objects. This is a great example of technical details leaking into the domain (even with dependency injection).&lt;/i&gt;&lt;/p&gt;    &lt;p&gt;&lt;i&gt;&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Domain Driven Design is not all about nouns; it is more so about verbs (and some very important adjectives/adverbs ;-)). Verbs are the most important parts of our ubiquitous language as they define interactions which in turn defines our nouns, &lt;i&gt;consider this in comparison with active record where the process tends to be reversed&lt;/i&gt;. The introduction of nouns such as EmailProvider to support our verbs such as SendEmail not only is artificial but it also distracts our attention from the domain verb of sending an email and focuses it on the noun that is doing it. There is no need for this noun to be in our ubiquitous language therefor there is also no reason for it to exist in our model.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;By replacing the interaction with a Message we gain in terms of our Ubiquitous Language. Let&amp;#39;s imagine a contrived example of printing a purchase order to a file at the completion of it being created. Instead of introducing a FileDocumentPrinter that has a method Print&amp;lt;PurchaseOrder&amp;gt; on it that we would then have to deal with the injecting of ... We would introduce a first class domain concept of a PrintPurchaseOrderDocumentMessage ... &lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;&lt;i&gt;One valid argument here is that PrintPurchaseOrderDocumentMessage is still a noun but in conversation it loses its suffix and becomes PrintPurchaseOrderDocument. We have quite a few of these in our domain InsertTrade, CancelOrder, MarkOrderAsKnownTrader. One could make the argument that they should be renamed without the suffix but I have found that it can be confusing for developers to not have the suffix. Another idea I have been working with is to instead give a prefix such as Do for these style messages or to use a fluent builder ala &lt;a href="http://codebetter.com/blogs/gregyoung/archive/2007/12/05/a-use-for-extension-methods.aspx"&gt;A use for extension methods&lt;/a&gt; and prefix it with Actions. then have the creations work with the verb.&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;By replacing with a message we have removed the nouns from our discussion, and at the same time lowered our coupling to the actual services involved. We can at runtime bind up our infrastructure and our domain (and ubiquitous language) are blissfully unaware of the mechanism that we use to hook it up.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;Another interesting note here is a subtle difference between the Ubiquitous Language and the model implementation is the synchronous vs asynchronous behaviors of any method based approach. When do I consider it ok to continue in terms of the printing being done? Is it when the document is done or when I told it to do it? This can lead to bugs in the implementation as well as a much less fleshed out domain model. Messaging makes this interaction specific in the Ubiquitous Language, if it is not waiting on a reply the action is considered to be &lt;b&gt;asynchronous.&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Later ... I will come back to this topic in terms of Repositories and making the implicit explicit but there is alot to cover first. &lt;br /&gt;&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=176586" width="1" height="1"&gt;</description></item><item><title>DDDD 1 [When to Message]</title><link>http://codebetter.com/blogs/gregyoung/archive/2008/04/09/dddd-1-when-to-message.aspx</link><pubDate>Wed, 09 Apr 2008 21:49:00 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:176548</guid><dc:creator>Greg</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://codebetter.com/blogs/gregyoung/rsscomments.aspx?PostID=176548</wfw:commentRss><comments>http://codebetter.com/blogs/gregyoung/archive/2008/04/09/dddd-1-when-to-message.aspx#comments</comments><description>&lt;p&gt;Just as a bit of a preamble, today is the first of what will hopefully be many days of me knocking out my blog backlog by actually taking an hour lunch and bringing my laptop to write. Unfortunately I currently have a backlog of over 40 posts ... Hopefully in a few weeks that number will come down a bit. This is the first in many posts about DDDD (the D is for Distributed), oddly when talking about it here the extra D seems to make sense just about everywhere so I will leave it to you to figure out which one it is...&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;I recently posted about using messaging in the domain in Mocks are a Code Smell. One item that I left out of that post was &lt;b&gt;when&lt;/b&gt; one should be using a message or a direct call. I have over time found boundaries that work very well for me at least ;-)&lt;/p&gt;  &lt;h2&gt;&amp;nbsp;&lt;/h2&gt;  &lt;h2&gt;Services&lt;/h2&gt;  &lt;p&gt;&lt;b&gt;DO &lt;/b&gt;use a message any time you call a service from any where.&lt;/p&gt;  &lt;p&gt;Services are the most natural (and easiest implemented) boundary for messaging. Services generally have a clear interface that is easily changed to become messages instead. It is also quite easy to route these service calls as you can usually just route the messages based on the type of message. Ex:&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;When I have a SendEmailService I can route all SendEmailRequest messages to it, in terms of dispatching; this is extremely simple and has about the same overhead as using DI but I have greatly reduced my coupling. As has been seen in Mocks are a Code Smell it can also make my tests more explicit.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;When moving to DDDD this allows you to load balance and/or distribute services in a transparent manner.&lt;/p&gt;  &lt;h2&gt;&amp;nbsp;&lt;/h2&gt;  &lt;h2&gt;Repositories&lt;/h2&gt;  &lt;p&gt;&lt;b&gt;DON&amp;#39;T &lt;/b&gt;message to your repositories ... &lt;/p&gt;  &lt;p&gt;Not a lot of explanation here ... Just don&amp;#39;t do it. I will go into more later (in another post) about how repositories and messaging can interact.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;h2&gt;Aggregate Roots&lt;/h2&gt;  &lt;p&gt;&lt;b&gt;DO &lt;/b&gt;use a message anytime you are in an aggregate root and make a call out of the aggregate root&lt;/p&gt;  &lt;p&gt;&lt;b&gt;DO &lt;/b&gt;use a message to access functionality in or under an aggregate root.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;NEVER &lt;/b&gt;use a message when you are within the same aggregate&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;Aggregate boundaries are one of the most important messaging boundaries when introducing messaging to a domain. Aggregates should &lt;b&gt;not &lt;/b&gt;be considered groups of related objects but in terms of messaging should be treated as a single functional unit (update: this also applies to little helper functions etc that the aggregate calls). &lt;/p&gt;  &lt;p&gt;This becomes especially true when you move towards DDDD. Aggregates exist within the mesh in their entirety, server 4 will either have the entirety of the Customer aggregate for customer 17 or it will have none of it, it will never hold &amp;quot;parts&amp;quot;, this does not mean that the entirety of the aggregate will be loaded (it may still support lazy loading). Not only does this greatly simplify the routing of messages within the mesh it also helps to prevent chatty interfaces.&lt;/p&gt;  &lt;p&gt;All messages coming into an aggregate should be destined to&lt;b&gt; &lt;/b&gt;the &lt;b&gt;aggregate&lt;/b&gt;, never to a piece of the aggregate. The aggregate should be responsible for coordinating the messages it receives to its contained parts. By dispatching messages to aggregates we have less information to store for routing purposes, locate by aggregate. This also fits directly into the concept of an aggregate in DDD in that it should be controlling access to all objects within its boundary.&lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;h2&gt;Bounded Contexts&lt;/h2&gt;  &lt;p&gt;&lt;b&gt;DO &lt;/b&gt;use a message anytime you cross a context boundary&lt;/p&gt;  &lt;p&gt;&lt;b&gt;DO &lt;/b&gt;make messages between context boundaries a format that is specific to neither Bounded Context&lt;/p&gt;&lt;p&gt;Consider messaging between Bounded Contexts to be a key Integration point.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;Context Boundaries are one of the places that messaging shines in any system although this could be included in Domain Service in many cases because interactions generally sits behind a service. Messaging introduces what is essentially a very thin and loosely coupled Shared Kernel (or perhaps its more of a Core Domain) between the Bounded Contexts. It is key that when you develop these messages you treat them as the global interface to the originating Bounded Context, any other Bounded Context will be accessing the originating Bounded Context through this interface.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;h3&gt;Anti-Corruption&lt;/h3&gt;  &lt;p&gt;The anti-corruption layer when introducing messaging across Bounded Contexts is especially interesting, there are two common ways of implementation. In either implementation the originating Bounded Context translates its own concept of data to the shared messaging contract. For many cases with small amounts of interaction this is actually enough to keep the contexts decoupled and the receiving Bounded Context can deal directly with the messages. &lt;/p&gt;  &lt;p&gt;If this becomes a bit burdensome it can be beneficial to have the receiving bounded context translate the shared messages it receives into its own concept of the messages.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;h3&gt;&lt;/h3&gt;  &lt;h2&gt;Application Services&lt;/h2&gt;  &lt;p&gt;You &lt;b&gt;may &lt;/b&gt;use messages for talking to your application services. &lt;/p&gt;  &lt;p&gt;Since if you follow the rules above your application services will use messages to talk to the domain there may or may not be a gain in using messaging to talk to your application services. One place that it can be a gain is if you are sharing your API with others as your application services would generally match up better with a SOA type API. &lt;/p&gt;  &lt;p&gt;There is another side of this discussion and that is you message &lt;b&gt;TO&lt;/b&gt; your application services and &lt;b&gt;ONLY &lt;/b&gt;to your application services (i.e. each instance of the application services is its own unique instance of the domain). This can work very well if you want your domain to be primarily Request/Response but it begins to fail very quickly if you want to do more pub/sub style interactions with the domain objects that live behind your application service facades.&lt;/p&gt;  &lt;p&gt;I have actually been liking lately the idea of distributing application services to all clients and having those application services generate messages to the other pieces of the system. This can at times be chatty but later we will discuss ways to work around that.&lt;/p&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;to be continued ...&lt;/p&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=176548" width="1" height="1"&gt;</description><category domain="http://codebetter.com/blogs/gregyoung/archive/tags/DDD/default.aspx">DDD</category><category domain="http://codebetter.com/blogs/gregyoung/archive/tags/Featured/default.aspx">Featured</category></item></channel></rss>