<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.thinkbeforecoding.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" xml:lang="en">
  
  <title type="html">Think Before Coding</title>
  <subtitle type="html" />
  
  <link href="http://thinkbeforecoding.com/" rel="alternate" type="text/html" title="" />
  <updated>2012-02-10T06:54:10+01:00</updated>
  <author>
    <name>Jérémie Chassaing</name>
  </author>
  <id>urn:md5:18477</id>
  <generator uri="http://www.dotclear.net/">Dotclear</generator>
  
    
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.thinkbeforecoding.com/ThinkBeforeCoding" /><feedburner:info uri="thinkbeforecoding" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare href="http://www.bloglines.com/sub/http://feeds.thinkbeforecoding.com/ThinkBeforeCoding" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare href="http://www.plusmo.com/add?url=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://plusmo.com/res/graphics/fbplusmo.gif">Subscribe with Plusmo</feedburner:feedFlare><feedburner:feedFlare href="http://www.thefreedictionary.com/_/hp/AddRSS.aspx?http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://img.tfd.com/hp/addToTheFreeDictionary.gif">Subscribe with The Free Dictionary</feedburner:feedFlare><feedburner:feedFlare href="http://www.bitty.com/manual/?contenttype=rssfeed&amp;contentvalue=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.bitty.com/img/bittychicklet_91x17.gif">Subscribe with Bitty Browser</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsalloy.com/?rss=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.newsalloy.com/subrss3.gif">Subscribe with NewsAlloy</feedburner:feedFlare><feedburner:feedFlare href="http://www.live.com/?add=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><feedburner:feedFlare href="http://mix.excite.eu/add?feedurl=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://image.excite.co.uk/mix/addtomix.gif">Subscribe with Excite MIX</feedburner:feedFlare><feedburner:feedFlare href="http://www.yourminis.com/subscribe.aspx?u=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.yourminis.com/images/addtoyourminisbadge.gif">Subscribe with Yourminis.com</feedburner:feedFlare><feedburner:feedFlare href="http://download.attensa.com/app/get_attensa.html?feedurl=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.attensa.com/blogs/attensa/WindowsLiveWriter/BadgeredintoBadges_10C02/attensa_feed_button5.gif">Subscribe with Attensa for Outlook</feedburner:feedFlare><feedburner:feedFlare href="http://www.webwag.com/wwgthis.php?url=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.webwag.com/images/wwgthis.gif">Subscribe with Webwag</feedburner:feedFlare><feedburner:feedFlare href="http://hub.netomat.net/account/account.autoSubscribe.jspa?urls=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.netomat.net/blogger/images/icon_netomat_feedbutton.gif">Subscribe with netomat Hub</feedburner:feedFlare><feedburner:feedFlare href="http://www.podcastready.com/oneclick_bookmark.php?url=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.podcastready.com/images/podcastready_button.gif">Subscribe with Podcast Ready</feedburner:feedFlare><feedburner:feedFlare href="http://www.flurry.com/pushRssFeed.do?r=fb&amp;url=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.flurry.com/images/flurry_rss_logo2.gif">Subscribe with Flurry</feedburner:feedFlare><feedburner:feedFlare href="http://www.wikio.com/subscribe?url=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.wikio.com/shared/img/add2wikio.gif">Subscribe with Wikio</feedburner:feedFlare><feedburner:feedFlare href="http://www.dailyrotation.com/index.php?feed=http%3A%2F%2Ffeeds.thinkbeforecoding.com%2FThinkBeforeCoding" src="http://www.dailyrotation.com/rss-dr2.gif">Subscribe with Daily Rotation</feedburner:feedFlare><entry>
    <title>I love SQL Server and cultures... NOT !</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/ngEPWF2hsDM/I-love-SQL-Server-and-cultures-NOT" rel="alternate" type="text/html" title="I love SQL Server and cultures... NOT !" />
    <id>urn:md5:95a4f226081be9d2825db2583fe70aad</id>
    <updated>2011-12-02T11:14:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>.Net Framework</dc:subject>
        <dc:subject>Globalization</dc:subject><dc:subject>SQL</dc:subject>    
    <content type="html">    &lt;p&gt;When developing a large system, all is not unicorns and rainbows.&lt;/p&gt;
&lt;p&gt;For now, everybody was working on a single SQL dev server and friction is
high.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;That’s why I’m working on SQL scripts management with mercurial and
powershell to the rescue, so that any developer can trash his own SQLExpress
instance, and rebuild everything needed in a single command. (I’ll maybe blog
about all that later).&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;We have loads of stored procs.. I know people don’t like it, but it acts as
a strong sanity layer when the database schema is so ugly your eyes bleed when
you look at it.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Yesterday, I run a stored proc, and I get the following error :&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The conversion of a varchar data type to a datetime data type
resulted in an out-of-range value.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;em&gt;Why the f**k.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The procedures is using a scalar function :&lt;/p&gt;
&lt;pre&gt;
ALTER FUNCTION [dbo].[DateMaxValue]()
RETURNS datetime
AS
BEGIN
RETURN '9999-12-31 23:59:59.998'
END
&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;It’s working on other servers.. why doesn’t it work here.&lt;/p&gt;
&lt;p&gt;After several tries, I try with the date ‘9999-12-01’ and I get the
following date:&lt;/p&gt;
&lt;p&gt;Year: 9999&lt;/p&gt;
&lt;p&gt;Month: 01&lt;/p&gt;
&lt;p&gt;Day: 12&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Yes.. the date is interpreted as YYYY-dd-MM on a French server.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Even when you use the YYYY-??-?? format, SQL Server still try to
mess with culture month/day ordering !&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You can use the SET DATEFORMAT dmy or SET DATEFORMAT mdy to change this, but
it will apply only in current session, and you cannot use it in a stored
proc.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You can change the server culture, but it wont change anything. The dmy/mdy
setting is ultimately in the Login culture.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You read it right :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For an English Login the function above works.&lt;/li&gt;
&lt;li&gt;For a French Login the function above fails miserably.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is no way to my knowledge to specify a strict date parsing in a stored
proc or function.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;So generates your logins with scripts, and enforce the same culture for all
logins.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;It’s just profoundly broken.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ngEPWF2hsDM:Nq83bD3dAOM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ngEPWF2hsDM:Nq83bD3dAOM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=ngEPWF2hsDM:Nq83bD3dAOM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ngEPWF2hsDM:Nq83bD3dAOM:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ngEPWF2hsDM:Nq83bD3dAOM:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=ngEPWF2hsDM:Nq83bD3dAOM:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ngEPWF2hsDM:Nq83bD3dAOM:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=ngEPWF2hsDM:Nq83bD3dAOM:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/ngEPWF2hsDM" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2011/12/02/I-love-SQL-Server-and-cultures-NOT#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/653120</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2011/12/02/I-love-SQL-Server-and-cultures-NOT</feedburner:origLink></entry>
    
  <entry>
    <title>Event Sourcing and CQRS, Dispatch options 2</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/ZnVFEpT1yFs/Event-Sourcing-and-CQRS%2C-Dispatch-options-2" rel="alternate" type="text/html" title="Event Sourcing and CQRS, Dispatch options 2" />
    <id>urn:md5:8f91e75b99b4020033d7a7debe704167</id>
    <updated>2011-06-21T10:38:00+02:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>Aggregate Root</dc:subject><dc:subject>Event Sourcing</dc:subject>    
    <content type="html">    &lt;p&gt;In the &lt;a href="http://thinkbeforecoding.com/post/2009/11/03/Event-Sourcing-and-CQRS-Dispatch-options" target="_blank"&gt;part one comments&lt;/a&gt;, Clement suggested a more efficient
solution than registering handler in constructor.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The proposed solution is to have a RegisterAllEvents virtual method in which
event handler registration would occur. This method is a method instance to
have access to this but will be called only once per class. The registration
use Expression&amp;lt;Action&amp;lt;T&amp;gt;&amp;gt; to access the expression tree and extract
the method info of the handler. This enables type checking, make R# happy – no
unused methods – and make reflection not too painful.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Good solution.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;I didn't go that far because with Event Sourcing, you usually keep
aggregates in memory, so aggregates are instantiated once per service
lifetime.&lt;br /&gt;
I just crafted a small performance test :&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;pre style="line-height: normal; width: auto; font-family: ; background: white; overflow: visible"&gt;

&lt;span style="color:"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span style="color:"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;br /&gt;&lt;span style="color:"&gt;using&lt;/span&gt; System.Diagnostics;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:"&gt;namespace&lt;/span&gt; AggregatePerfTest&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:"&gt;class&lt;/span&gt; &lt;span style="color:"&gt;Program&lt;/span&gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:"&gt;static&lt;/span&gt; &lt;span style="color:"&gt;void&lt;/span&gt; Main(&lt;span style="color:"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:"&gt;var&lt;/span&gt; watch = &lt;span style="color:"&gt;new&lt;/span&gt; &lt;span style="color:"&gt;Stopwatch&lt;/span&gt;();&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:"&gt;const&lt;/span&gt; &lt;span style="color:"&gt;int&lt;/span&gt; count = 10000000;&lt;br /&gt;            &lt;span style="color:"&gt;Guid&lt;/span&gt; id = &lt;span style="color:"&gt;Guid&lt;/span&gt;.NewGuid();&lt;br /&gt; &lt;br /&gt;            watch.Start();&lt;br /&gt;            &lt;span style="color:"&gt;for&lt;/span&gt; (&lt;span style="color:"&gt;int&lt;/span&gt; i = 0; i &amp;lt; count; i++)&lt;br /&gt;                &lt;span style="color:"&gt;new&lt;/span&gt; &lt;span style="color:"&gt;AggregateRegisteredOncePerInstance&lt;/span&gt;(id);&lt;br /&gt; &lt;br /&gt;            watch.Stop();&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:"&gt;Console&lt;/span&gt;.WriteLine(watch.Elapsed.TotalMilliseconds);&lt;br /&gt; &lt;br /&gt;            watch.Reset();&lt;br /&gt;            watch.Start();&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:"&gt;for&lt;/span&gt; (&lt;span style="color:"&gt;int&lt;/span&gt; i = 0; i &amp;lt; count; i++)&lt;br /&gt;                &lt;span style="color:"&gt;new&lt;/span&gt; &lt;span style="color:"&gt;AggregateRegisteredOncePerClass&lt;/span&gt;(id);&lt;br /&gt; &lt;br /&gt;            watch.Stop();&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:"&gt;Console&lt;/span&gt;.WriteLine(watch.Elapsed.TotalMilliseconds);&lt;br /&gt; &lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    &lt;span style="color:"&gt;public&lt;/span&gt; &lt;span style="color:"&gt;class&lt;/span&gt; &lt;span style="color:"&gt;AggregateRegisteredOncePerClass&lt;/span&gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:"&gt;private&lt;/span&gt; &lt;span style="color:"&gt;readonly&lt;/span&gt; &lt;span style="color:"&gt;Guid&lt;/span&gt; id;&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:"&gt;private&lt;/span&gt; &lt;span style="color:"&gt;static&lt;/span&gt; &lt;span style="color:"&gt;readonly&lt;/span&gt; &lt;span style="color:"&gt;object&lt;/span&gt; ClassInitLock = &lt;span style="color:"&gt;new&lt;/span&gt; &lt;span style="color:"&gt;object&lt;/span&gt;();&lt;br /&gt;        &lt;span style="color:"&gt;private&lt;/span&gt; &lt;span style="color:"&gt;static&lt;/span&gt; &lt;span style="color:"&gt;bool&lt;/span&gt; initialized;&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;        &lt;span style="color:"&gt;public&lt;/span&gt; AggregateRegisteredOncePerClass(&lt;span style="color:"&gt;Guid&lt;/span&gt; id)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:"&gt;this&lt;/span&gt;.id = id;&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:"&gt;lock&lt;/span&gt; (ClassInitLock)&lt;br /&gt;            {&lt;br /&gt;                &lt;span style="color:"&gt;if&lt;/span&gt; (!initialized)&lt;br /&gt;                {&lt;br /&gt;                    initialized = &lt;span style="color:"&gt;true&lt;/span&gt;;&lt;br /&gt;                    &lt;span style="color:"&gt;// registration happens only once here&lt;/span&gt;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:"&gt;public&lt;/span&gt; &lt;span style="color:"&gt;Guid&lt;/span&gt; Id&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:"&gt;get&lt;/span&gt; { &lt;span style="color:"&gt;return&lt;/span&gt; id; }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt; &lt;br /&gt;    &lt;span style="color:"&gt;public&lt;/span&gt; &lt;span style="color:"&gt;class&lt;/span&gt; &lt;span style="color:"&gt;AggregateRegisteredOncePerInstance&lt;/span&gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:"&gt;private&lt;/span&gt; &lt;span style="color:"&gt;readonly&lt;/span&gt; &lt;span style="color:"&gt;Guid&lt;/span&gt; id;&lt;br /&gt;        &lt;span style="color:"&gt;private&lt;/span&gt; &lt;span style="color:"&gt;readonly&lt;/span&gt; &lt;span style="color:"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:"&gt;Type&lt;/span&gt;, &lt;span style="color:"&gt;dynamic&lt;/span&gt;&amp;gt; handlers = &lt;br /&gt;&lt;span style="color:"&gt;                         new&lt;/span&gt; &lt;span style="color:"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:"&gt;Type&lt;/span&gt;, &lt;span style="color:"&gt;dynamic&lt;/span&gt;&amp;gt;(5);&lt;br /&gt;        &lt;span style="color:"&gt;public&lt;/span&gt; AggregateRegisteredOncePerInstance(&lt;span style="color:"&gt;Guid&lt;/span&gt; id)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:"&gt;this&lt;/span&gt;.id = id;&lt;br /&gt;            Register&amp;lt;&lt;span style="color:"&gt;int&lt;/span&gt;&amp;gt;(OnSomethingHappened);&lt;br /&gt;            Register&amp;lt;&lt;span style="color:"&gt;double&lt;/span&gt;&amp;gt;(OnSomethingHappened);&lt;br /&gt;            Register&amp;lt;&lt;span style="color:"&gt;float&lt;/span&gt;&amp;gt;(OnSomethingHappened);&lt;br /&gt;            Register&amp;lt;&lt;span style="color:"&gt;long&lt;/span&gt;&amp;gt;(OnSomethingHappened);&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:"&gt;public&lt;/span&gt; &lt;span style="color:"&gt;Guid&lt;/span&gt; Id&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:"&gt;get&lt;/span&gt; { &lt;span style="color:"&gt;return&lt;/span&gt; id; }&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:"&gt;public&lt;/span&gt; &lt;span style="color:"&gt;void&lt;/span&gt; DoSomething()&lt;br /&gt;        {&lt;br /&gt;            Apply(1);&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:"&gt;private&lt;/span&gt; &lt;span style="color:"&gt;void&lt;/span&gt; OnSomethingHappened(&lt;span style="color:"&gt;int&lt;/span&gt; message) { }&lt;br /&gt;        &lt;span style="color:"&gt;private&lt;/span&gt; &lt;span style="color:"&gt;void&lt;/span&gt; OnSomethingHappened(&lt;span style="color:"&gt;double&lt;/span&gt; message){ }&lt;br /&gt;        &lt;span style="color:"&gt;private&lt;/span&gt; &lt;span style="color:"&gt;void&lt;/span&gt; OnSomethingHappened(&lt;span style="color:"&gt;float&lt;/span&gt; message) { }&lt;br /&gt;        &lt;span style="color:"&gt;private&lt;/span&gt; &lt;span style="color:"&gt;void&lt;/span&gt; OnSomethingHappened(&lt;span style="color:"&gt;long&lt;/span&gt; message) { }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:"&gt;protected&lt;/span&gt; &lt;span style="color:"&gt;void&lt;/span&gt; Apply&amp;lt;T&amp;gt;(T @event)&lt;br /&gt;        {&lt;br /&gt;            handlers[&lt;span style="color:"&gt;typeof&lt;/span&gt; (T)](@event);&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:"&gt;protected&lt;/span&gt; &lt;span style="color:"&gt;void&lt;/span&gt; Register&amp;lt;T&amp;gt;(&lt;span style="color:"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt; handler)&lt;br /&gt;        {&lt;br /&gt;            handlers.Add(&lt;span style="color:"&gt;typeof&lt;/span&gt;(T), handler);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}
&lt;/pre&gt;
&lt;p&gt;The code is straight forward, I just created two aggregate classes :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;one with registration in .ctor based on this post code&lt;/li&gt;
&lt;li&gt;one without any registration at all, considering that doing it once is the
same as not doing it for large numbers, but I added a lock section with a
boolean check to simulate what will done on each instance creation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I created 10.000.000 instances for each, and you get:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;3978ms for the one with .ctor registrations,&lt;/li&gt;
&lt;li&gt;377 ms for the one without.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It's true that it makes a difference. But how many aggregates do you have in
your system ?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;With 10.000 aggregate you're still under 8ms. I think you can afford
that.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;This is then a trade-off between performance and simplicity :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you have very large numbers, go for expression tree parsing, class lock
management etc.&lt;/li&gt;
&lt;li&gt;In any other situation I recommend using registration in .ctor that makes
the code easy to implement in approximately 5min.&lt;/li&gt;
&lt;/ul&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ZnVFEpT1yFs:ooaqG30sPig:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ZnVFEpT1yFs:ooaqG30sPig:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=ZnVFEpT1yFs:ooaqG30sPig:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ZnVFEpT1yFs:ooaqG30sPig:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ZnVFEpT1yFs:ooaqG30sPig:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=ZnVFEpT1yFs:ooaqG30sPig:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ZnVFEpT1yFs:ooaqG30sPig:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=ZnVFEpT1yFs:ooaqG30sPig:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/ZnVFEpT1yFs" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2011/06/21/Event-Sourcing-and-CQRS%2C-Dispatch-options-2#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/615066</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2011/06/21/Event-Sourcing-and-CQRS%2C-Dispatch-options-2</feedburner:origLink></entry>
    
  <entry>
    <title>DDDx 2011</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/HLiuoCjqmfc/DDDx-2011" rel="alternate" type="text/html" title="DDDx 2011" />
    <id>urn:md5:6785fe91b2fd1a7a413c6f3e9dbddd27</id>
    <updated>2011-06-14T10:30:00+02:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>DDD Exchange</dc:subject><dc:subject>Domain Driven Design</dc:subject><dc:subject>Evans</dc:subject><dc:subject>Strategic Design</dc:subject><dc:subject>TDD</dc:subject>    
    <content type="html">    &lt;p&gt;I’m just back from DDDx 2011, and it was great !&lt;/p&gt;
&lt;p&gt;The event happened Friday 10 at Skills Matter in London, with great
speakers, coffee and food.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You can see all the talks on &lt;a href="http://skillsmatter.com/event/design-architecture/ddd-exchange-2011/js-2046" target="_blank"&gt;Skills Matter website&lt;/a&gt;. Congratulation to the team that
released the videos on the web in less that an hour.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;It was also the occasion to meet IRL DDD practitioners I usually find on
twitter.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You can also &lt;a href="http://bit.ly/mse2Ko" target="_blank"&gt;register for
next year now&lt;/a&gt; for only 50£.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;So Hurry up !&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=HLiuoCjqmfc:WlyzElz7Se8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=HLiuoCjqmfc:WlyzElz7Se8:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=HLiuoCjqmfc:WlyzElz7Se8:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=HLiuoCjqmfc:WlyzElz7Se8:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=HLiuoCjqmfc:WlyzElz7Se8:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=HLiuoCjqmfc:WlyzElz7Se8:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=HLiuoCjqmfc:WlyzElz7Se8:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=HLiuoCjqmfc:WlyzElz7Se8:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/HLiuoCjqmfc" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2011/06/14/DDDx-2011#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/613679</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2011/06/14/DDDx-2011</feedburner:origLink></entry>
    
  <entry>
    <title>Time</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/9xoIORb3dYQ/Time" rel="alternate" type="text/html" title="Time" />
    <id>urn:md5:d26b5613758836721c89d22c42a1ca4b</id>
    <updated>2011-06-09T00:54:00+02:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>CQRS</dc:subject><dc:subject>Domain Driven Design</dc:subject><dc:subject>Domain Events</dc:subject><dc:subject>Event Sourcing</dc:subject><dc:subject>Time</dc:subject>    
    <content type="html">    &lt;a href="http://thinkbeforecoding.com/public/WindowsLiveWriter/Time_14D45/556656621_ba9e8c870f_1__2.jpg"&gt;&lt;img style="border-bottom: 0; border-left: 0; margin: 0 8px 0 15px; display: inline; border-top: 0; border-right: 0" title="556656621_ba9e8c870f[1]" border="0" alt="556656621_ba9e8c870f[1]" align="right" src="http://thinkbeforecoding.com/public/WindowsLiveWriter/Time_14D45/556656621_ba9e8c870f_1__thumb.jpg" width="134" height="134" /&gt;&lt;/a&gt;
&lt;h3&gt;&lt;strong&gt;How do we usually manage time in applications ?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Timers, threads, concurrency locks…&lt;/p&gt;
&lt;p&gt;If we want to practice Domain Driven Design, we’re surely at the wrong level
of abstraction.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;What is time, btw ?&lt;/h3&gt;
&lt;p&gt;Tricky question. We know what time is, but… giving a definition is not that
easy.&lt;/p&gt;
&lt;p&gt;What defines time ? The second ?&lt;/p&gt;
&lt;p&gt;Not really. It is used as a measure of time, but it doesn’t seem
sufficient.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Let’s have a look at &lt;a href="http://en.wikipedia.org/wiki/Time" target="_blank"&gt;Wikipedia’s definition of time&lt;/a&gt;  :&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Time&lt;/strong&gt; is a part of the measuring system used to sequence
events, to compare the durations of events and the intervals between them, and
to quantify rates of change such as the motions of objects. […]&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now we have something interesting : Time is what happens between events.&lt;/p&gt;
&lt;p&gt;But what is this thing between events.&lt;/p&gt;
&lt;p&gt;The definition of the measure unit surely can give us further insight.&lt;/p&gt;
&lt;p&gt;Lest have a look at &lt;a href="http://en.wikipedia.org/wiki/Second" target="_blank"&gt;Wikipedia’s definition of the second&lt;/a&gt; :&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;[…] Since 1967, the second has been defined to be&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;the duration of 9,192,631,770 periods of the radiation corresponding to the
transition between the two hyperfine levels of the ground state of the
caesium-133 atom.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;p&gt;The second is defined as a count of transitions between states of an atom of
cesium.&lt;/p&gt;
&lt;p&gt;We measure time by considering that the ~time~ between those state
transitions is constant.&lt;/p&gt;
&lt;p&gt;What if it’s not ?&lt;/p&gt;
&lt;p&gt;…&lt;/p&gt;
&lt;p&gt;It’s not important that important if other events seem synchronized with
those events. Will come back to this later.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;Events&lt;/h3&gt;
&lt;p&gt;Let’s step back a bit.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;How do you feel time passing ?&lt;/p&gt;
&lt;p&gt;By looking at your watch ?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Maybe, but how could you explain that an hour sometimes seems so long, and
sometimes passes in a flash ?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Time seems slow and empty when you’re bored.&lt;/p&gt;
&lt;p&gt;Times seems fast and full when you’re busy with interesting things.&lt;/p&gt;
&lt;p&gt;When you’re bored, it’s because few interesting things happen.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You can deduce from this that your personal state transitions are those
interesting things that happen.&lt;/p&gt;
&lt;p&gt;This are &lt;strong&gt;meaningful events&lt;/strong&gt;. Things that happens and change
you deeply.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Of course a lot of things happen between those meaningful events, you’re
moving, thinking. Your blood flows through your body, but it is just
maintenance move. You don’t change deeply.&lt;/p&gt;
&lt;p&gt;Maybe some things happen between state transition of a cesium atom, but
since we cannot notice it and give it a meaning for now, it has no
influence.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;But when when a meaningful event happens, you change. You’re not the same
before and after.&lt;/p&gt;
&lt;p&gt;This is what time is about, and this is why it’s one way.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Before –&amp;gt; Event –&amp;gt; After&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Events define time by causality&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This perception of meaningful events is surely a reason why people say the
time pass faster when old. In your 6 first years any event around you is
meaningful. Any event make you change since you have no previous knowledge.
Then has time goes by, you integrate knowledge and filter things you already
know, you’ve already seen. When old, a year can more easily seem the same than
the year before.&lt;/p&gt;
&lt;p&gt;But some people continue to enjoy and learn as much as they can to still
have a &lt;em&gt;long now&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;When do your system change ?&lt;/h3&gt;
&lt;p&gt;Your system never change for now reason.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;It’s always because a meaningful event happened.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This event can be a user interaction, a call from an external system, a
sensor trigger…&lt;/p&gt;
&lt;p&gt;And when thing change because it’s midnight ?&lt;/p&gt;
&lt;p&gt;It simply means that midnight is a meaningful event in your system.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Where are those meaningful events in your code ? Hidden in infrastructure
code ?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;I hear Greg Young say :&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Make the implicit explicit !&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And it’s simple :&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use Domain Events&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Once you’ve introduced Domain Events in your domain model, you have made
Events and so Time explicit in your domain.&lt;/p&gt;
&lt;p&gt;There is no change in the domain that is not due to an Event.&lt;/p&gt;
&lt;p&gt;The events appear everywhere in the Ubiquitous Language :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When the client HasMoved to a new location, send him a welcome kit.&lt;/li&gt;
&lt;li&gt;When a RoomHasBeenOverbooked try to relocate the customer&lt;/li&gt;
&lt;li&gt;Every day at midnight = MidnightOccured, change last minute prices.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I’m sure you can find examples in your own domain. If your domain is
business related, it has to deal with time because business is about time and
money.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Time is now part of your Ubiquitous language and you have an implementation
for it.&lt;/p&gt;
&lt;p&gt;And that’s huge.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=9xoIORb3dYQ:8VP2fWYreBY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=9xoIORb3dYQ:8VP2fWYreBY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=9xoIORb3dYQ:8VP2fWYreBY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=9xoIORb3dYQ:8VP2fWYreBY:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=9xoIORb3dYQ:8VP2fWYreBY:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=9xoIORb3dYQ:8VP2fWYreBY:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=9xoIORb3dYQ:8VP2fWYreBY:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=9xoIORb3dYQ:8VP2fWYreBY:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/9xoIORb3dYQ" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2011/06/09/Time#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/612637</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2011/06/09/Time</feedburner:origLink></entry>
    
  <entry>
    <title>Switching keyboard language in WP7</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/ZkujKi2MWVg/Switching-keyboard-language-in-WP7" rel="alternate" type="text/html" title="Switching keyboard language in WP7" />
    <id>urn:md5:3078ea391e15942e429cedf8d5900f1f</id>
    <updated>2011-01-24T11:36:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>WP7</dc:subject>
            
    <content type="html">    &lt;p&gt;It was bugging me that I could not switch WP7 keyboard language.&lt;/p&gt;
&lt;p&gt;I write most of my emails in French, but my blog and &lt;a href="http://twitter.com/thinkb4coding" target="_blank"&gt;tweets&lt;/a&gt; are in
English.&lt;/p&gt;
&lt;p&gt;I’ve seen that some people were also asking it for the soon to come
update.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;But you can actually already do it easily.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Here’s a short how to.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Go to Settings/Keyboard. Then tap on Keyboard language.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;You can select multiple languages here with the checkboxes
!&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;That’s all.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Then when you open any application with a keyboard you can notice the
language selector near the space bar :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://thinkbeforecoding.com/public/Windows-Live-Writer/WP7_963A/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://thinkbeforecoding.com/public/Windows-Live-Writer/WP7_963A/image_thumb.png" width="210" height="381" /&gt;&lt;/a&gt;&lt;a href="http://thinkbeforecoding.com/public/Windows-Live-Writer/WP7_963A/image_4.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://thinkbeforecoding.com/public/Windows-Live-Writer/WP7_963A/image_thumb_1.png" width="210" height="382" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You an also view all selectable languages with tap&amp;amp;hold :&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://thinkbeforecoding.com/public/Windows-Live-Writer/WP7_963A/image_8.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://thinkbeforecoding.com/public/Windows-Live-Writer/WP7_963A/image_thumb_3.png" width="210" height="382" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;That’s it.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ZkujKi2MWVg:JSIdNGO52zE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ZkujKi2MWVg:JSIdNGO52zE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=ZkujKi2MWVg:JSIdNGO52zE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ZkujKi2MWVg:JSIdNGO52zE:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ZkujKi2MWVg:JSIdNGO52zE:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=ZkujKi2MWVg:JSIdNGO52zE:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=ZkujKi2MWVg:JSIdNGO52zE:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=ZkujKi2MWVg:JSIdNGO52zE:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/ZkujKi2MWVg" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2011/01/24/Switching-keyboard-language-in-WP7#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/579982</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2011/01/24/Switching-keyboard-language-in-WP7</feedburner:origLink></entry>
    
  <entry>
    <title>SmtpListener</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/04x_e9rz7C8/SmtpListener" rel="alternate" type="text/html" title="SmtpListener" />
    <id>urn:md5:2afcca511c9fe329aceafc0c1ca7194b</id>
    <updated>2011-01-21T14:45:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>.Net Framework</dc:subject>
            
    <content type="html">    &lt;p&gt;I’ve been posting a &lt;a href="http://code.thinkbeforecoding.com/smtplistener" target="_blank"&gt;sample SmtpListener on my repository&lt;/a&gt; a few days ago.&lt;/p&gt;
&lt;p&gt;It’s infrastructure stuff.. what does it have to do with what I’m usually
talking about here ?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;It’s about Reactive Programming.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Sometimes you have to integrate with legacy systems that’ll send you things
through email.&lt;/p&gt;
&lt;p&gt;Ok, I know, it kind of sucks as an integration mechanism, but still… you
have to do this way.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The usual way to receive email in an application is to set a mailbox on a
mail server (think Exchange), and pull mailboxes periodically to see if there’s
something new there.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;There are two bad things here :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;People tend to use their enterprise mail server for this. The mail sever is
often vital for your business and screwing it up with a bug can have a big
impact on your organization.&lt;/li&gt;
&lt;li&gt;Email are pushed to you.. why would you pull it. You can push it directly
on your service bus !&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So, I prototyped a small smtp listener that could easily be integrated with
whatever you want.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You need to add a MX entry in your dns zone configuration so that other
servers find it, and get a valid certificate if you want to use secured TLS
connections (I’ve disabled certificated verification for demo purpose).&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;But as you can see, the code is very simple since the .Net framework has
already what’s needed.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The TcpListener is used to receive connections that’ll provide a TcpClient
with underlying streams.&lt;/p&gt;
&lt;p&gt;The SslStream class is used to encapsulate tcp streams to add Ssl
encryption.&lt;/p&gt;
&lt;p&gt;I’m using the reactive framework to convert the Begin/EndAcceptTcpClient
methods to an Observable to avoid writing the accept loop myself.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Then implementation of the protocol is very easy, you can find &lt;a href="http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol" target="_blank"&gt;an
overview and sample on wikipedia&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The RFCs can easily be found :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tools.ietf.org/rfc/rfc5321.txt"&gt;RFC 5321&lt;/a&gt;: Simple Mail
Transfer Protocol&lt;/p&gt;
&lt;p&gt;&lt;a href="http://tools.ietf.org/rfc/rfc3207.txt"&gt;RFC 3207&lt;/a&gt;: SMTP Service
Extension for Secure SMTP over Transport Layer Security&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Of course you can use it freely, and propose changes to make it better since
it’s not attack proof.&lt;/p&gt;
&lt;p&gt;This code is not resistant to known potential SMTP attacks, including
dangling connections, long lines etc..&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=04x_e9rz7C8:XlH8BCgksyY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=04x_e9rz7C8:XlH8BCgksyY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=04x_e9rz7C8:XlH8BCgksyY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=04x_e9rz7C8:XlH8BCgksyY:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=04x_e9rz7C8:XlH8BCgksyY:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=04x_e9rz7C8:XlH8BCgksyY:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=04x_e9rz7C8:XlH8BCgksyY:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=04x_e9rz7C8:XlH8BCgksyY:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/04x_e9rz7C8" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2011/01/21/SmtpListener#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/579381</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2011/01/21/SmtpListener</feedburner:origLink></entry>
    
  <entry>
    <title>Code code code</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/FrIt3ceSNqE/Code-code-code" rel="alternate" type="text/html" title="Code code code" />
    <id>urn:md5:75874b5421286b661b9ff44823f046f5</id>
    <updated>2011-01-20T12:07:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
            
    <content type="html">    &lt;p&gt;There are some sample/proof-of-concept codes on &lt;a href="http://code.thinkbeforecoding.com/" target="_blank"&gt;code.thinkbeforecoding.com&lt;/a&gt; (hosted by bitbucket.org)&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You can have a look at it and contribute if you want.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Enjoy !&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=FrIt3ceSNqE:LgAb62SN0sE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=FrIt3ceSNqE:LgAb62SN0sE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=FrIt3ceSNqE:LgAb62SN0sE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=FrIt3ceSNqE:LgAb62SN0sE:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=FrIt3ceSNqE:LgAb62SN0sE:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=FrIt3ceSNqE:LgAb62SN0sE:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=FrIt3ceSNqE:LgAb62SN0sE:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=FrIt3ceSNqE:LgAb62SN0sE:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/FrIt3ceSNqE" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2011/01/20/Code-code-code#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/579053</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2011/01/20/Code-code-code</feedburner:origLink></entry>
    
  <entry>
    <title>Duck : Delete Update Create Killer</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/fTjNevYd7As/Duck-Delete-Update-Create-Killer" rel="alternate" type="text/html" title="Duck : Delete Update Create Killer" />
    <id>urn:md5:dcd6ead6822353fb6a2610476a416f85</id>
    <updated>2010-10-19T01:49:00+02:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Duck</dc:subject>
        <dc:subject>Duck</dc:subject><dc:subject>Event Sourcing</dc:subject>    
    <content type="html">    &lt;p&gt;&lt;img style="display: inline; margin-left: 0; margin-right: 0" title="Duck sign, Stockbridge High Street" alt="Duck sign, Stockbridge High Street" align="right" src="http://s0.geograph.org.uk/photos/21/35/213574_3470e842.jpg" width="188" height="251" /&gt;I recently had a remark from Frederic Fadel from
Aspectize, telling me about Event Sourcing something like:&lt;/p&gt;
&lt;p&gt;Why would you like to write SQL to write data to your read model when our
product can do it for you ?&lt;/p&gt;
&lt;p&gt;I acknowledge that their product is fancy. You simply declare your
&lt;span style="text-decoration: line-through"&gt;db&lt;/span&gt; data schema, your UI
and services and bind it all together.&lt;/p&gt;
&lt;p&gt;But it doesn’t fit well with CQRS and Event Sourcing. And I want to do Event
Sourcing for domain and business reasons, not technical reasons.&lt;/p&gt;
&lt;p&gt;But he was write on this point :&lt;/p&gt;
&lt;p&gt;I don’t want to write SQL to denormalize my events to my queryable
storage.&lt;/p&gt;
&lt;p&gt;What are my options ?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Writing SQL by hand, but testability is low, and you’ll get a mix of C# to
get data from the events, and SQL for the Update queries.&lt;/li&gt;
&lt;li&gt;Using an ORM. When doing NHibernate you don’t really write SQL. Testability
wont be great anyway.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;The problem with ORMs&lt;/h2&gt;
&lt;p&gt;ORM are usually better at getting data than at changing it. You can do it,
but let’s look at what happens.&lt;/p&gt;
&lt;p&gt;The ORM loads data from your Db into entities that will be tracked by an
identity tracker. Then you change the values in memory . Then the ORM will find
what needs to be sent back to the server and make a query to the Db so that the
change happens.&lt;/p&gt;
&lt;p&gt;But what I need to do is a lot simpler. Just emit some INSERT, UPDATE or
DELETE based on current table values and event data.&lt;/p&gt;
&lt;p&gt;With an ORM, what happens if the data is changed between loading and saving
? I’ll have to manage some versioning and/or transaction. And I’ll make two
roundtrips to the server needlessly.&lt;/p&gt;
&lt;h2&gt;Here comes Duck&lt;/h2&gt;
&lt;p&gt;Duck is a kind of ORM oriented toward Delete Update Create.&lt;/p&gt;
&lt;p&gt;Don’t ask Duck to load data in memory, it simply can’t.&lt;/p&gt;
&lt;p&gt;You simply express how data should change based on current row content and
values that you’ll pass.&lt;/p&gt;
&lt;p&gt;It avoids the first roundtrip to the database, and make shorter code to
express the change.&lt;/p&gt;
&lt;h2&gt;Let’s see how to use it&lt;/h2&gt;
&lt;p&gt;First, you should declare a class that has the structure of your table with
public get/set properties, and marked with a Table attribute :&lt;/p&gt;
&lt;pre style="font-family: consolas"&gt;
     [&lt;span style="color: #2b91af"&gt;Table&lt;/span&gt;]&lt;br /&gt;       &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Species&lt;/span&gt;&lt;br /&gt;   {&lt;br /&gt;         &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt; Id { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;br /&gt;          &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;br /&gt;         &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; BinomialName { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;br /&gt;         &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsEndangered { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;br /&gt;           &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt; Population { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;br /&gt;      }
&lt;/pre&gt;
&lt;p&gt;It contains current observed species at an observatory.&lt;/p&gt;
&lt;p&gt;Then a simple new statement, let’s say that a new species has be registered
at the observatory :&lt;/p&gt;
&lt;pre style="font-family: consolas"&gt;
     &lt;span style="color: blue"&gt;var&lt;/span&gt; duck = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DuckContext'(connectionString)&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: blue"&gt;var&lt;/span&gt; speciesId = &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid();&lt;br /&gt;      duck.In&amp;lt;&lt;span style="color: #2b91af"&gt;Species&lt;/span&gt;&amp;gt;()&lt;br /&gt;          .Insert(() =&amp;gt; &lt;br /&gt;            &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Species&lt;/span&gt;&lt;br /&gt;             {&lt;br /&gt;          Id = speciesId,&lt;br /&gt;            Name = &lt;span style="color: #a31515"&gt;&amp;quot;Mallard&amp;quot;&lt;/span&gt;,&lt;br /&gt;            BinomialName = &lt;span style="color: #a31515"&gt;&amp;quot;Anas platyrhynchos&amp;quot;&lt;/span&gt;,&lt;br /&gt;          IsEndangered = &lt;span style="color: blue"&gt;false&lt;/span&gt;,&lt;br /&gt;            Population = 50&lt;br /&gt;          });
&lt;/pre&gt;
&lt;p&gt;Nothing tricky here..&lt;/p&gt;
&lt;p&gt;The observatory noticed a population decay, the species is endangered :&lt;/p&gt;
&lt;pre style="font-family: consolas"&gt;
     duck.In&amp;lt;&lt;span style="color: #2b91af"&gt;Species&lt;/span&gt;&amp;gt;()&lt;br /&gt;          .Where(r =&amp;gt; r.Id == speciesId)&lt;br /&gt;         .Update(r =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Species&lt;/span&gt; {&lt;br /&gt;              Population = r.Population - 40,&lt;br /&gt;              IsEndangered = &lt;span style="color: blue"&gt;true&lt;/span&gt;});
&lt;/pre&gt;
&lt;p&gt;Here, the use of the current value of Population will not load current
value. It will the following statement :&lt;/p&gt;
&lt;p&gt;UPDATE Species&lt;br /&gt;
SET&lt;br /&gt;
    Population = Population - 40,&lt;br /&gt;
    IsEndangered = 1&lt;br /&gt;
WHERE&lt;br /&gt;
    Id = @p0&lt;/p&gt;
&lt;p&gt;I chose  to create a new Row from the old one rather than change the
original one for two reasons :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It makes rows immutable and you don’t have to think about execution order
between fields assignments. It’s the way SQL works&lt;/li&gt;
&lt;li&gt;Linq Expressions cannot represent statement blocks and assignments in C#3,
Duck would have been .Net only…&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The –40 is directly in the query here because we used a constant. I we where
using a variable, query would contain a parameter&lt;/p&gt;
&lt;p&gt;Now the species has disappeared, it should be removed from the observed
species (though it could be just an update somewhere else) :&lt;/p&gt;
&lt;pre style="font-family: consolas"&gt;
     duck.In&amp;lt;&lt;span style="color: #2b91af"&gt;Species&lt;/span&gt;&amp;gt;()&lt;br /&gt;          .Where(r =&amp;gt; r.Id == speciesId)&lt;br /&gt;         .Delete();
&lt;/pre&gt;
&lt;h2&gt;Testability&lt;/h2&gt;
&lt;p&gt;To run your test you just have to use the InMemoryDuckContext… you have then
access to Table&amp;lt;T&amp;gt;() that’ll enable you to set up your data and verify
after execution that things changed as expected. I’ll talk a bit more about it
soon.&lt;/p&gt;
&lt;h2&gt;Try it now, it’s OSS&lt;/h2&gt;
&lt;p&gt;You can grab the code at bitbucket and try it now :&lt;/p&gt;
&lt;p&gt;&lt;a title="http://bitbucket.org/thinkbeforecoding/duck" href="http://bitbucket.org/thinkbeforecoding/duck" target="_blank"&gt;http://bitbucket.org/thinkbeforecoding/duck&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It’s in F# ! Writing a AST analyzer in F# is far more easy, concise and
expressive than in C#. You’ll just have to reference Duck in you project,
there’s no direct F# dependency.&lt;/p&gt;
&lt;p&gt;Next episode will be about how to mix it with Rx (Reactive Framework) to
declare your event handling logic.&lt;/p&gt;
&lt;p&gt;Hope you like it, and don’t hesitate to give feedback and suggestions.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=fTjNevYd7As:jvpwJD4oVGA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=fTjNevYd7As:jvpwJD4oVGA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=fTjNevYd7As:jvpwJD4oVGA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=fTjNevYd7As:jvpwJD4oVGA:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=fTjNevYd7As:jvpwJD4oVGA:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=fTjNevYd7As:jvpwJD4oVGA:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=fTjNevYd7As:jvpwJD4oVGA:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=fTjNevYd7As:jvpwJD4oVGA:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/fTjNevYd7As" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2010/10/19/Duck-Delete-Update-Create-Killer#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/555499</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2010/10/19/Duck-Delete-Update-Create-Killer</feedburner:origLink></entry>
    
  <entry>
    <title>DDD Exchange 2010</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/A3UntyhJdEA/DDD-Exchange-2010" rel="alternate" type="text/html" title="DDD Exchange 2010" />
    <id>urn:md5:022c0a1bb08d0f8fea5c89428744a542</id>
    <updated>2010-06-14T10:58:00+02:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>CQRS</dc:subject><dc:subject>DDD Exchange</dc:subject><dc:subject>Distributed Domain Driven Design</dc:subject><dc:subject>Domain Driven Design</dc:subject><dc:subject>Evans</dc:subject><dc:subject>Event Sourcing</dc:subject>    
    <content type="html">    &lt;p&gt;I could not attend to this year’s edition that seemed really great with Eric
Evans, Greg Young, Udi Dahan, Ian Cooper and Gojko Adzic.&lt;/p&gt;
&lt;p&gt;The videos from the events should be soon somewhere around &lt;a href="http://skillsmatter.com/event/design-architecture/ddd-exchange-2010/wd-23"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;And you can already find transcripts of the talks on Gojko ‘I post faster
than people talk’s blog :&lt;/p&gt;
&lt;p&gt;&lt;a href="http://gojko.net/2010/06/11/eric-evans-domain-driven-design-redefined/" target="_blank"&gt;Eric Evans: Domain driven design redefined&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://gojko.net/2010/06/11/udi-dahan-the-biggest-mistakes-teams-make-when-applying-ddd/" target="_blank"&gt;Udi Dahan: the biggest mistakes teams make when applying
DDD&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://gojko.net/2010/06/11/evolution-of-ddd-cqrs-and-event-sourcing/" target="_blank"&gt;Greg Young :Evolution of DDD: CQRS and Event Sourcing&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;If you also missed it, don’t make the same mistake next year, and &lt;a href="http://skillsmatter.com/event/design-architecture/ddd-exchange-2011/wd-23" target="_blank"&gt;register now for £50.00&lt;/a&gt; (instead of £250.00) until the end
of the week.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=A3UntyhJdEA:vJdiBRqItfs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=A3UntyhJdEA:vJdiBRqItfs:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=A3UntyhJdEA:vJdiBRqItfs:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=A3UntyhJdEA:vJdiBRqItfs:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=A3UntyhJdEA:vJdiBRqItfs:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=A3UntyhJdEA:vJdiBRqItfs:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=A3UntyhJdEA:vJdiBRqItfs:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=A3UntyhJdEA:vJdiBRqItfs:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/A3UntyhJdEA" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2010/06/14/DDD-Exchange-2010#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/526729</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2010/06/14/DDD-Exchange-2010</feedburner:origLink></entry>
    
  <entry>
    <title>Event Sourcing and CQRS, Events Deserialization</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/cos0jYx4Rfw/Event-Sourcing-and-CQRS-Events-Deserialization" rel="alternate" type="text/html" title="Event Sourcing and CQRS, Events Deserialization" />
    <id>urn:md5:370b2696a1ad39fb0a07238652438004</id>
    <updated>2010-04-25T22:51:00+02:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>CQRS</dc:subject><dc:subject>Distributed Domain Driven Design</dc:subject><dc:subject>Domain Driven Design</dc:subject><dc:subject>Event Sourcing</dc:subject>    
    <content type="html">    &lt;p&gt;So we have our events serialized in our event store. Deserializing events is
not an issue, until we start to make them evolve and need to manage several
versions.&lt;/p&gt;
&lt;p&gt;Since we never modify what has been log, we’ll have to deal with old
versions anyway.&lt;/p&gt;
&lt;p&gt;A simple way to do it is to &lt;strong&gt;maintain every versions of the
events&lt;/strong&gt; in the projects, and make the aggregate root accept all of
them. But it will soon charge the aggregate root with a lot of code and will
make it bloated rapidly.&lt;/p&gt;
&lt;p&gt;This is why you can usually introduce a &lt;strong&gt;converter&lt;/strong&gt; that will
convert any version of the event to the last one (usually you provide methods
to update to next version, and iterate until last version so that this part of
the code is incremental). This is a convenient way to address the problem, but
you still have classes v1, v2 … vn that you keep in your project only for
versioning purpose even if you don’t use it anymore in your production
code.&lt;/p&gt;
&lt;h3&gt;Events as documents&lt;/h3&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/serkel/519485340"&gt;&lt;img style="border-bottom: 0; border-left: 0; margin: 0 0 0 25px; border-top: 0; border-right: 0" title="519485340_1a83117720_o[1]" border="0" alt="519485340_1a83117720_o[1]" align="right" src="http://thinkbeforecoding.com/public/WindowsLiveWriter/EventSourcingandCQRSEventsDeserializatio_13AEE/519485340_1a83117720_o_1__3.jpg" width="184" height="244" /&gt;&lt;/a&gt;It is easy do deserialize an event as an object
&lt;strong&gt;or&lt;/strong&gt; a document, you only need to split two responsibilities in
you deserialization process :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Stream reading&lt;/li&gt;
&lt;li&gt;Object building&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The deserializer will be in charge of reading the data, it reads the bits,
and get the meaning from context, it will tell the Object Builder about objects
types, fields names and value.&lt;/p&gt;
&lt;p&gt;On its side, the ob ject builder will instantiate the objects, set fields
values depending on names.&lt;/p&gt;
&lt;p&gt;You can provides two distinct Object Builders. The strongly typed one will
instantiate concrete .net types and set fields using reflection. The document
builder one, will instantiate objects that will only be property bags.&lt;/p&gt;
&lt;p&gt;When deserializing an event in its last version, you can use directly the
strongly typed one, but when reading an previous version of the event, you can
deserialize it as a document and give it to the converter.&lt;/p&gt;
&lt;p&gt;The converter will then add/remove properties from the document to make it
up to date, and the document will be used to create a concrete .net type of the
last event version.&lt;/p&gt;
&lt;p&gt;Here the process is quite the same, you should provide a document reader
that will use the strongly typed object builder to instantiate the event.&lt;/p&gt;
&lt;p&gt;There’s no need to keep every version of you Event Classes now since you can
manipulate old versions as documents.&lt;/p&gt;
&lt;h3&gt;Using dynamic in C#4&lt;/h3&gt;
&lt;p&gt;Document manipulation can make things a bit messy since it can be hard to
understand the original structure of the object. This is where you can use the
DLR DynamicObject class to make the property bag (the document) a dynamic
object that you’ll be able to use as any standard .net object.&lt;/p&gt;
&lt;p&gt;This way, in the converter you can manipulate old versions of the events as
.net objects without having to keep all those old classes that won’t be used
anymore.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=cos0jYx4Rfw:s1gR0RAON_s:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=cos0jYx4Rfw:s1gR0RAON_s:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=cos0jYx4Rfw:s1gR0RAON_s:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=cos0jYx4Rfw:s1gR0RAON_s:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=cos0jYx4Rfw:s1gR0RAON_s:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=cos0jYx4Rfw:s1gR0RAON_s:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=cos0jYx4Rfw:s1gR0RAON_s:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=cos0jYx4Rfw:s1gR0RAON_s:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/cos0jYx4Rfw" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2010/04/25/Event-Sourcing-and-CQRS-Events-Deserialization#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/511514</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2010/04/25/Event-Sourcing-and-CQRS-Events-Deserialization</feedburner:origLink></entry>
    
  <entry>
    <title>Event Sourcing and CQRS, Bounded Contexts</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/XtrA3WTzzb4/Event-Sourcing-and-CQRS-Bounded-Contexts" rel="alternate" type="text/html" title="Event Sourcing and CQRS, Bounded Contexts" />
    <id>urn:md5:85ee33eb8ae4e47b3485a05e331d45a0</id>
    <updated>2010-04-17T15:21:00+02:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>CQRS</dc:subject><dc:subject>Domain Driven Design</dc:subject><dc:subject>Event Sourcing</dc:subject>    
    <content type="html">    &lt;p&gt;Once again, I prefer a new post that a long comment reply. This one is about
a important concept of Domain Driven Design, Bounded Contexts.&lt;/p&gt;
&lt;p&gt;Hendry Luk asked :&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Just 1 question, you represent borrower in events as a simple full-name
string.&lt;br /&gt;
Is there any reason or just for sake of simplicity for example?&lt;br /&gt;
Supposed I'm using borrowerId, how would that work in other BC, say&lt;br /&gt;
LateBookNotifier (let's assume its a separate BC). How does this BC shows
the&lt;br /&gt;
name of the borrower? Does it communicate directly with command BC using
ACL?&lt;br /&gt;
Or does it also subscribe to BorrowerRegistered event as well (hence every
BC&lt;br /&gt;
would have duplicate data of each of the borrowers, just like they do each
of&lt;br /&gt;
the books)?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The short answer is ‘Yes, it was just for sake of simplicity’. In a real
world scenario, borrowers would probably be entities, and thus would have an
identity. I would even probably be an Aggregate Root.&lt;/p&gt;
&lt;p&gt;The Borrower Aggregate Root would encapsulate state needed to perform
commands on this Aggregate.&lt;/p&gt;
&lt;h2&gt;Bounded Contexts Communications&lt;img style="margin: 25px 0 0 15px" title="Books" border="0" alt="Books" align="right" src="http://thinkbeforecoding.com/public/WindowsLiveWriter/EventSourcingandCQRSBoundedContexts_C8DA/Books_3.jpg" width="278" height="228" /&gt;&lt;/h2&gt;
&lt;p&gt;I can see the following contexts here :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Inventory : Manage books availability and state (the book has been damaged,
there a notes written on it etc..)&lt;/li&gt;
&lt;li&gt;Relationships : Manage contact by email, phone with borrowers, and tracks
the care they take to your books, if they return it on schedule.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Since we are using CQRS (and even more, Event Sourcing), aggregates in these
context don’t need more state that what’s needed to take decisions,&lt;/p&gt;
&lt;p&gt;So a Book in the Inventory Context will probably not need more that the Id
of the borrower and the date a witch it was borrowed.&lt;br /&gt;
We can then call the ReturnToShelf command on the Book that will publish a
ReturnedLateToShelf { Book : bookId, By : borrowerId, After : 20 days, LateBy :
6 days  }.&lt;/p&gt;
&lt;p&gt;A Handler at the Relationships Boundary will catch the event, and call a
CheckExcuseForLateReturn on the Borrower Aggregate Root (based on its id). The
command will check the borrower’ss record to see if its acceptable. It will
simply publish a LateReturnGentlyAccepted if the borrower is usually in time,
but will publish a KindnessLimitReached in the other case.&lt;br /&gt;
&lt;br /&gt;
Another handler will catch it, and call SendAngryMessage on the Messaging
Service. The role of the Messaging Service is to tweet borrowers to let them
know they should not forget to return your books. How does this service know
the twitter account of the borrower ? When the handler (the one that call
SendAngryMessage) catches a BorrowerRegistered event or a
BorrowerTwitterAccountChanged message, it says so to the service that can
maintain a list of accounts in any desired storage (SLQ, NoSql, in memory.. ?).
The SendAngryMessage can now tweet ‘Hey you filthy @borrower, you better return
my book today or I shall share all the pics from your last party…’&lt;/p&gt;
&lt;p&gt;Done.&lt;/p&gt;
&lt;h2&gt;Where does data live ?&lt;/h2&gt;
&lt;p&gt;There’s usually a huge concern about data duplication in all contexts. Is
the info duplicated in so many places ?&lt;/p&gt;
&lt;p&gt;There will be two main places :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The Persistent View Model used to see and edit borrower’s details&lt;/li&gt;
&lt;li&gt;The Persistent View Model used by the messaging service to Query borrower’s
twitter accounts. Here, no other borrower’s data is needed except its id and
account name.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The Borrower Aggregate Root and Book Aggregate Root in the two main Domain
Bounded Contexts will not need to keep track of this kind of data. They won’t
need it in their decision process.&lt;/p&gt;
&lt;p&gt;If you pursue this idea, to answer further to Leonardo, you’ll notice that
strings will probably never been used as state inside Domain Bounded Context.
They can appear as identity key, or just pass through a command and be
republished in the following event. But since strings are rarely – if never – a
good way to represent information on which you’ll have to take a decision, it
should almost never be stored in an aggregate root current state. This is
another reason why most domain models can fit in memory, because names,
descriptions and other documents usually represent the biggest part of the data
in a system, the remaining data is usually small. These documents and names are
useless to run domain  internal logic (except validation rules, but not
state change rules) so they can simply be logged in events and persisted in the
Query’s View Models. Only state needed to take state change decisions will stay
in memory.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=XtrA3WTzzb4:skJdnAUKMDY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=XtrA3WTzzb4:skJdnAUKMDY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=XtrA3WTzzb4:skJdnAUKMDY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=XtrA3WTzzb4:skJdnAUKMDY:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=XtrA3WTzzb4:skJdnAUKMDY:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=XtrA3WTzzb4:skJdnAUKMDY:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=XtrA3WTzzb4:skJdnAUKMDY:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=XtrA3WTzzb4:skJdnAUKMDY:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/XtrA3WTzzb4" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2010/04/17/Event-Sourcing-and-CQRS-Bounded-Contexts#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/509715</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2010/04/17/Event-Sourcing-and-CQRS-Bounded-Contexts</feedburner:origLink></entry>
    
  <entry>
    <title>Event Sourcing and CQRS, Snapshots !</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/8I6uO6EcmkU/Event-Sourcing-and-CQRS-Snapshots" rel="alternate" type="text/html" title="Event Sourcing and CQRS, Snapshots !" />
    <id>urn:md5:efe26f1f63b0b2be0bc518cb35f4f5b2</id>
    <updated>2010-02-25T10:38:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>CQRS</dc:subject><dc:subject>Distributed Domain Driven Design</dc:subject><dc:subject>Domain Driven Design</dc:subject>    
    <content type="html">    &lt;p&gt;Leonardo had a question about reloading huge amounts of events.&lt;/p&gt;
&lt;p&gt;It’s true that some Aggregate Roots have very long lifetimes with lots of
events, and it can become a problem.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;There are two things involved to resolve this problem :&lt;/p&gt;
&lt;h3&gt;Snapshots&lt;/h3&gt;
&lt;p&gt;Ok, the philosophy of event sourcing is to store changes instead of state,
but we’ll still need state in our Aggregate Roots, and getting it from scratch
can be long.&lt;/p&gt;
&lt;p&gt;Take a snapshot every n events (you’ll see that n can be quite high), and
store it alongside events, with the version of the aggregate root.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;To reload the Aggregate Root, simply find the snapshot, give it to the
Aggregate root, the replay events that happened after the snapshot.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You only need the last snapshot for each Aggregate Root, no need to log all
passed snapshots.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;When you want to change stored state in an Aggregate Root, you won’t be able
to used last snapshot since it will not contains expected state. But you can
still replay events from scratch when it happens, so you have no loss, and
simply take a new snapshot with the new state.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;In memory domain&lt;/h3&gt;
&lt;p&gt;Usually with an ORM, you reload entities from the storage on every unit of
work.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;But in the case of Event Sourcing, your Aggregate Roots only need to retain
state that will be used to take business decisions. You’ll never query state
from Aggregate Roots. A large part of the entity state and especially the part
that has the biggest memory footprint is usually stored only for queries, like
names, descriptions and things like that.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;In an Aggregate Root in an Event Sourcing environment, a name or description
can simply be checked for validity, put in an event, but don’t need to be
kipped in the in memory entity state – the Aggregate Root fields.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You’ll notice that your big domain state can fit in memory once you’ve
trimmed it this way.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Now that your model is in memory, no need to reload every events on each
unit of work. It happens only once when the Aggregate Root is needed the first
time.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Well see soon how you can use this to make your event serialization even
faster to have very high business peak throughput.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=8I6uO6EcmkU:kWbR97fIfUs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=8I6uO6EcmkU:kWbR97fIfUs:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=8I6uO6EcmkU:kWbR97fIfUs:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=8I6uO6EcmkU:kWbR97fIfUs:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=8I6uO6EcmkU:kWbR97fIfUs:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=8I6uO6EcmkU:kWbR97fIfUs:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=8I6uO6EcmkU:kWbR97fIfUs:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=8I6uO6EcmkU:kWbR97fIfUs:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/8I6uO6EcmkU" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2010/02/25/Event-Sourcing-and-CQRS-Snapshots#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/489754</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2010/02/25/Event-Sourcing-and-CQRS-Snapshots</feedburner:origLink></entry>
    
  <entry>
    <title>Business Errors are Just Ordinary Events</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/oAjAObrAsG0/Business-Errors-are-Just-Ordinary-Events" rel="alternate" type="text/html" title="Business Errors are Just Ordinary Events" />
    <id>urn:md5:145125b298e5943f8397dad3338e6532</id>
    <updated>2009-12-10T14:31:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
            
    <content type="html">    &lt;p&gt;Error handling has always been something quite difficult to grasp in
software design and still is.&lt;/p&gt;
&lt;p&gt;Exceptions are now widespread in languages, and it helps a lot to manage
corner case where something fails badly.&lt;/p&gt;
&lt;p&gt;But should we use Exceptions to manage business errors ?&lt;/p&gt;
&lt;h2&gt;The business errors&lt;/h2&gt;
&lt;p&gt;What do we call business errors actually ?&lt;/p&gt;
&lt;h3&gt;Broken Invariants&lt;/h3&gt;
&lt;p&gt;What if an invariance rule is broken ?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The situation should never happen&lt;/strong&gt; : There is a bug. A bug
is not a business error, correct it and deploy.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The situation can happens sometimes&lt;/strong&gt; : This is not an
invariant, but a rare state. It should be handled as any other state
change.&lt;/p&gt;
&lt;h3&gt;Invalid commands&lt;/h3&gt;
&lt;p&gt;What if we receive an invalid command ?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The command data is meaningless&lt;/strong&gt; : There’s a bug, you should
always validate that command data is not just garbage.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The command leads to an invalid state&lt;/strong&gt; : The user
nonetheless requested to perform the command.&lt;/p&gt;
&lt;p&gt;In this case the event will be ‘the request was rejected’. The event can be
handled by sending an email back to the customer, or a support request can be
started so that the support can call the customer and manage the problem.
&lt;em&gt;All this is part of the business process anyway&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Corner cases create business opportunities&lt;/h2&gt;
&lt;p&gt;I can often see discussions around account validation for credit, to make
the transaction fail when your account goes below zero.&lt;/p&gt;
&lt;p&gt;But it’s not what’s happening in real life. Transaction is accepted, then
the bank charges you because your account is in the red zone.&lt;/p&gt;
&lt;p&gt;I’m currently working in the hotel business. When a booking is received
and  there’s no room left, should I reject the booking ? Another client
can cancel soon, or I can move the customer to another hotel nearby, but just
saying ‘there’s no room left’ is not a good business answer ! Overbooking
management has even become a strategic practice in the business.&lt;/p&gt;
&lt;p&gt;To fully manage your customers you should embrace the whole business
lifecycle in your system. This includes support and corner case management.
Part of it will be done by hand, other part automatically, but you should not
just report an exception is a trace log.&lt;/p&gt;
&lt;p&gt;These critical situations are usually the one in which you customer needs
you more than in any other case, you should design your fault handling strategy
with care and make it a full concern of you business.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=oAjAObrAsG0:4ioDweNRu3o:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=oAjAObrAsG0:4ioDweNRu3o:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=oAjAObrAsG0:4ioDweNRu3o:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=oAjAObrAsG0:4ioDweNRu3o:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=oAjAObrAsG0:4ioDweNRu3o:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=oAjAObrAsG0:4ioDweNRu3o:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=oAjAObrAsG0:4ioDweNRu3o:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=oAjAObrAsG0:4ioDweNRu3o:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/oAjAObrAsG0" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2009/12/10/Business-Errors-are-Just-Ordinary-Events#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/466830</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2009/12/10/Business-Errors-are-Just-Ordinary-Events</feedburner:origLink></entry>
    
  <entry>
    <title>Udi Dahan's post on CQRS</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/PXxyk0b37xw/Udi-Dahan-post-on-CQRS" rel="alternate" type="text/html" title="Udi Dahan's post on CQRS" />
    <id>urn:md5:b62067d37689e6893975eff0823421b5</id>
    <updated>2009-12-10T10:15:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
            
    <content type="html">    &lt;p&gt;Udi Dahan wrote a new post on CQRS today : &lt;a href="http://www.udidahan.com/2009/12/09/clarified-cqrs/" target="_blank"&gt;Clarified
CQRS&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It is essentially the content of the presentation he gave here in Paris and
in other places.&lt;/p&gt;
&lt;p&gt;You should read it I you want to understand the deep reasons to use CQRS and
see how to change your mind to use it.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=PXxyk0b37xw:hG64q76qPuU:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=PXxyk0b37xw:hG64q76qPuU:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=PXxyk0b37xw:hG64q76qPuU:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=PXxyk0b37xw:hG64q76qPuU:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=PXxyk0b37xw:hG64q76qPuU:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=PXxyk0b37xw:hG64q76qPuU:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=PXxyk0b37xw:hG64q76qPuU:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=PXxyk0b37xw:hG64q76qPuU:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/PXxyk0b37xw" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2009/12/10/Udi-Dahan-post-on-CQRS#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/466789</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2009/12/10/Udi-Dahan-post-on-CQRS</feedburner:origLink></entry>
    
  <entry>
    <title>Udi Dahan talks on CQRS in Paris</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/obzDZMemFIo/Udi-Dahan-talks-on-CQRS-in-Paris" rel="alternate" type="text/html" title="Udi Dahan talks on CQRS in Paris" />
    <id>urn:md5:4d9b270c91aa510fb7d678a4b83b8eca</id>
    <updated>2009-11-17T16:37:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
            
    <content type="html">    &lt;p&gt;&lt;a href="http://www.udidahan.com/" target="_blank"&gt;Udi Dahan&lt;/a&gt; gave a very
good talk yesterday evening at Zenika, there was only few attendees… perhaps
because it was on a Monday evening. Whatever, there was barely not enough place
already in the Italian restaurant where we moved after.&lt;/p&gt;
&lt;p&gt;I won’t make a full report, just talk about some interesting points.&lt;/p&gt;
&lt;p&gt;First of all, the session focused mainly on why you should do CQRS and not
how. Second point, the talk was not about event sourcing, but you already now
that you can do CQRS without event sourcing.&lt;/p&gt;
&lt;h2&gt;Something we should accept : Stale Data&lt;/h2&gt;
&lt;p&gt;The paradigm of usual architecture’s best practice has a serious flow : when
you show data to your users, it’s already stale.&lt;/p&gt;
&lt;p&gt;Is it important ? Yes.&lt;/p&gt;
&lt;p&gt;Is it a problem ? Not really.&lt;/p&gt;
&lt;p&gt;The world have worked with stale data for years, and it was handled rather
gracefully until now. Computers have reduced the time span, but when the data
appear on the screen, it’s stale.&lt;/p&gt;
&lt;p&gt;Tel it to your users, they will accept it. Find with them what is
acceptable. 1 second, 10 seconds, 1 minute, 1 hour, 1 day ? The users are used
to it in there own business. Do it too.&lt;/p&gt;
&lt;h2&gt;Queries&lt;/h2&gt;
&lt;p&gt;What’s the purpose of queries ? To show data. Not objects.&lt;/p&gt;
&lt;p&gt;So why should the data from the database come across 5 layers through 3
model transformations ? It’s a bit overkill to display data.&lt;/p&gt;
&lt;p&gt;Why not just this : The UI read data from the database and displays it ?&lt;/p&gt;
&lt;p&gt;No DTOs, no ORM, not business rules executed on each query.&lt;/p&gt;
&lt;p&gt;You simply define a &lt;strong&gt;Persistent ViewModel&lt;/strong&gt; (thank’s Udi, I
like this description of the Q side), and display it directly to screen. It
should be as simple as one database table per UI view.&lt;/p&gt;
&lt;p&gt;Of course you need a way to keep the Persistent ViewModel up to date, but
we’ll see that later.&lt;/p&gt;
&lt;h2&gt;Commands&lt;/h2&gt;
&lt;p&gt;On the other side, there are commands.&lt;/p&gt;
&lt;p&gt;It should be done in 3 phases :&lt;/p&gt;
&lt;h3&gt;Validation&lt;/h3&gt;
&lt;p&gt;Is the input potentially good ? Structured correctly, no missing field,
everything fit in ranges ?&lt;/p&gt;
&lt;p&gt;This can be done without knowing current state, and be done outside of
entities command handling.&lt;/p&gt;
&lt;h3&gt;Rules&lt;/h3&gt;
&lt;p&gt;Should we do this ?&lt;/p&gt;
&lt;p&gt;Here, the decision is taken using current state.&lt;/p&gt;
&lt;p&gt;It leads to a discussion about UI design. In order to handle the user
command as well as you can, you have to capture the user intent in the
command.&lt;/p&gt;
&lt;p&gt;In CRUD applications, the new data is sent by the UI layer. You have to
extract the user intent from that data to know if you can process the data.&lt;/p&gt;
&lt;p&gt;There is a huge difference between UserMovesToNewAddress and
CorrectTheMisspellingInUserAddress from a business point, but in a CRUD
application you would probably end with the same Update data…&lt;/p&gt;
&lt;h3&gt;State change&lt;/h3&gt;
&lt;p&gt;What’s the new state ?&lt;/p&gt;
&lt;p&gt;It’s the easy part once the rules are applied.&lt;/p&gt;
&lt;h2&gt;Domain Model&lt;/h2&gt;
&lt;h3&gt;What aren’t they for ?&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Validation&lt;/strong&gt; : commands are validated before the model is
called. Do not bloat your domain model with this.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Queries&lt;/strong&gt; : entity relationships for reading are unnecessary.
You can do eager loading on your Aggregate Roots safely, they’ll never be used
for queries that need only partial information.&lt;/p&gt;
&lt;h3&gt;What are they for ?&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Answer to the question&lt;/strong&gt; : should we do what this valid
command is asking ?&lt;/p&gt;
&lt;p&gt;If the answer is yes, &lt;strong&gt;change the state&lt;/strong&gt; !&lt;/p&gt;
&lt;h2&gt;Maintain the query model up to date&lt;/h2&gt;
&lt;p&gt;There are two main ways to maintain query model up to date.&lt;/p&gt;
&lt;p&gt;You can use something like views or ETL to transform data from the domain
data to the shape required by the query side.&lt;/p&gt;
&lt;p&gt;If you prefer, or when your domain persistence is not compatible with this
option (OODB, Event Storage..), you can publish events from you command side,
and provides handler’s on the query side that will maintain the views state in
the relational database (or a cube… or whatever). A denormalization will happen
here.&lt;/p&gt;
&lt;h2&gt;What do we gain from this ?&lt;/h2&gt;
&lt;h3&gt;Asynchronous model&lt;/h3&gt;
&lt;p&gt;The model is deeply asynchronous, it’s not a matter of tweaking things with
threads. It’s asynchronous from the ground up, at domain level.&lt;/p&gt;
&lt;p&gt;Your user sends a command, and your design is good if you can answer :
“thank you, we will come back to you soon…”. Take the time needed to fulfill
your user wish, he will be happy !&lt;/p&gt;
&lt;h3&gt;Scalability&lt;/h3&gt;
&lt;p&gt;By relaxing the rules, the system becomes more scalable.&lt;/p&gt;
&lt;h3&gt;Domain persistence choice&lt;/h3&gt;
&lt;p&gt;The domain is accessed only to process rules and state changes. There is no
need to join tables, filter rows. So you can easily use an non relational
database.&lt;/p&gt;
&lt;p&gt;Possible options are a OODB or an Event Storage (for event sourcing).&lt;/p&gt;
&lt;p&gt;You can still use a RDBMS with or without an ORM if you’re more familiar
with these technologies.&lt;/p&gt;
&lt;p&gt;But the persistence mechanism becomes an implementation detail from from
Command side that will not interfere with your queries.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Ooops, I said it was not a complete report… but it actually is. Every point
was interesting ?&lt;/p&gt;
&lt;p&gt;After the talk we had a discussion about forecasting and other interesting
subjects. Perhaps more on this later.&lt;/p&gt;
&lt;p&gt;There was a video camera in the room, so I think the guys from Zenika will
try to put it on the internet when they have time. I’ll add the link when
available.&lt;/p&gt;
&lt;p&gt;If you was here and have a picture of the event, I would be glad to put it
in the blog :D&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=obzDZMemFIo:U5nXozVLcMU:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=obzDZMemFIo:U5nXozVLcMU:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=obzDZMemFIo:U5nXozVLcMU:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=obzDZMemFIo:U5nXozVLcMU:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=obzDZMemFIo:U5nXozVLcMU:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=obzDZMemFIo:U5nXozVLcMU:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=obzDZMemFIo:U5nXozVLcMU:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=obzDZMemFIo:U5nXozVLcMU:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/obzDZMemFIo" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2009/11/17/Udi-Dahan-talks-on-CQRS-in-Paris#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/460508</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2009/11/17/Udi-Dahan-talks-on-CQRS-in-Paris</feedburner:origLink></entry>
    
  <entry>
    <title>Udi Dahan talks on CQRS at Zenika</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/P_DOL6L5T5s/Udi-Dahan-talks-on-CQRS-at-Zenika" rel="alternate" type="text/html" title="Udi Dahan talks on CQRS at Zenika" />
    <id>urn:md5:af932606313d2692d12af103cea76e54</id>
    <updated>2009-11-16T11:08:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>CQRS</dc:subject><dc:subject>Distributed Domain Driven Design</dc:subject><dc:subject>Domain Driven Design</dc:subject>    
    <content type="html">    &lt;p&gt;I’ll be at &lt;a href="http://www.udidahan.com/" target="_blank"&gt;Udi
Dahan&lt;/a&gt;’s talk this evening (19h) at Zenika in Paris.&lt;/p&gt;
&lt;p&gt;Tell me if you’re planning to be there too !&lt;/p&gt;
&lt;p&gt;I’ll surely post about it in the following days.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=P_DOL6L5T5s:BLCIgbDO2z4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=P_DOL6L5T5s:BLCIgbDO2z4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=P_DOL6L5T5s:BLCIgbDO2z4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=P_DOL6L5T5s:BLCIgbDO2z4:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=P_DOL6L5T5s:BLCIgbDO2z4:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=P_DOL6L5T5s:BLCIgbDO2z4:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=P_DOL6L5T5s:BLCIgbDO2z4:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=P_DOL6L5T5s:BLCIgbDO2z4:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/P_DOL6L5T5s" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2009/11/16/Udi-Dahan-talks-on-CQRS-at-Zenika#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/460191</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2009/11/16/Udi-Dahan-talks-on-CQRS-at-Zenika</feedburner:origLink></entry>
    
  <entry>
    <title>Event Sourcing and CQRS, Serialization</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/eqhi1doOIUY/Event-Sourcing-and-CQRS-Serialization" rel="alternate" type="text/html" title="Event Sourcing and CQRS, Serialization" />
    <id>urn:md5:c55a1bc2959d5eaa2c2ca830df99f2d9</id>
    <updated>2009-11-05T14:00:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>CQRS</dc:subject><dc:subject>Design</dc:subject><dc:subject>Distributed Domain Driven Design</dc:subject><dc:subject>Domain Driven Design</dc:subject>    
    <content type="html">    &lt;p&gt;Be sure to read the three preceding parts of the series:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://thinkbeforecoding.com/post/2009/10/30/Event-Sourcing-and-CQRS-Now" target="_blank"&gt;Event Sourcing and CQRS, Now !&lt;/a&gt;  &lt;br /&gt;
&lt;a href="http://thinkbeforecoding.com/post/2009/11/02/Event-Sourcing-and-CQRS-Lets-use-it" target="_blank"&gt;Event Sourcing and CQRS, Let’s use it&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://thinkbeforecoding.com/post/2009/11/03/Event-Sourcing-and-CQRS-Dispatch-options" target="_blank"&gt;Event Sourcing and CQRS; Dispatch-options&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Today, we’ll study to a required part of the event storage :
Serialization/Deserialization&lt;/p&gt;
&lt;h2&gt;The easy way&lt;/h2&gt;
&lt;p&gt;The .Net framework as several serialization technologies that can be used
here, Binary serialization, XML serialization or even DataContract
serialization introduced with WCF.&lt;/p&gt;
&lt;h2&gt;The penalty&lt;/h2&gt;
&lt;p&gt;The particularity of Event Sourcing is that we will never delete or update
stored events. They’ll be logged, insert only, once and forever.&lt;/p&gt;
&lt;p&gt;So the log grows. grows. grows.&lt;/p&gt;
&lt;p&gt;Event storage size will influence greatly the growth rate of the log.&lt;/p&gt;
&lt;h3&gt;Xml Serialization&lt;/h3&gt;
&lt;p&gt;If your system processes frequently lots of events, forget about XML. Far to
verbose, you’ll pay the &lt;a href="http://www.codinghorror.com/blog/archives/001114.html" target="_blank"&gt;Angle
Bracket Tax&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Binary Serialization&lt;/h3&gt;
&lt;p&gt;But the binary serialization still cost much, even if compact, it will
contain type names and field names…&lt;/p&gt;
&lt;h3&gt;Raw Serialization&lt;/h3&gt;
&lt;p&gt;You could write serialization/deserialization code into your type.&lt;/p&gt;
&lt;p&gt;The type can chose a format, so no extra type/field name is needed. This
kind of serialization is very compact – it contains only required bits – but
you cannot read data back without the deserialization code.&lt;/p&gt;
&lt;p&gt;It can be ok if you plan to have a definite small number of well documented
events. Unmanageable if your event type count will grow with time and
versions.&lt;/p&gt;
&lt;h2&gt;Avoid it&lt;/h2&gt;
&lt;p&gt;Let’s consider how data are stored in a database.&lt;/p&gt;
&lt;p&gt;A database contains tables. Tables have a schema. When storing a row, no
need to repeat column names on each cell. The data layout is defined by the
table schema and will be the same on each row.&lt;/p&gt;
&lt;p&gt;We cannot do the same since events have different schemas, but we work with
a limited set of events that will occur many times.&lt;/p&gt;
&lt;h2&gt;Split schema and data&lt;/h2&gt;
&lt;p&gt;We can thus store schemas aside, and specify the row data schema on each
row. The event data will the be stored as raw bits corresponding to specified
schema.&lt;/p&gt;
&lt;p&gt;This way you can design tools to explore your log file with complete event
representation without needing the original event class, and you got a very
compact serialization. Have your cake and eat it too !&lt;/p&gt;
&lt;p&gt;Stay tuned, the code comes tomorrow…&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=eqhi1doOIUY:kLKgK1oQQj0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=eqhi1doOIUY:kLKgK1oQQj0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=eqhi1doOIUY:kLKgK1oQQj0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=eqhi1doOIUY:kLKgK1oQQj0:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=eqhi1doOIUY:kLKgK1oQQj0:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=eqhi1doOIUY:kLKgK1oQQj0:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=eqhi1doOIUY:kLKgK1oQQj0:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=eqhi1doOIUY:kLKgK1oQQj0:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/eqhi1doOIUY" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2009/11/05/Event-Sourcing-and-CQRS-Serialization#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/457791</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2009/11/05/Event-Sourcing-and-CQRS-Serialization</feedburner:origLink></entry>
    
  <entry>
    <title>Event Sourcing and CQRS, Dispatch options.</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/hzMSb2CMLmA/Event-Sourcing-and-CQRS-Dispatch-options" rel="alternate" type="text/html" title="Event Sourcing and CQRS, Dispatch options." />
    <id>urn:md5:07e43fc62650cd848784b9a430a56c74</id>
    <updated>2009-11-03T15:41:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>CQRS</dc:subject><dc:subject>Design</dc:subject><dc:subject>Distributed Domain Driven Design</dc:subject><dc:subject>Domain Driven Design</dc:subject>    
    <content type="html">    &lt;p&gt;As seen in &lt;a href="http://thinkbeforecoding.com/post/2009/11/02/Event-Sourcing-and-CQRS%2C-Letrsquo%3Bs-use-it." target="_blank"&gt;previous post&lt;/a&gt;, I used dynamic to replay events.&lt;/p&gt;
&lt;p&gt;The main reason to use it was to avoid long code using reflection in the
infrastructure that would have made it hard to read.&lt;/p&gt;
&lt;p&gt;I’ll show several ways to do this dispatch with pros and cons in each
cases.&lt;/p&gt;
&lt;h2&gt;Dynamic&lt;/h2&gt;
&lt;p&gt;The proposed solution was using dynamic.&lt;/p&gt;
&lt;p&gt;+ Pros : there is no reflection code involved, code is very simple.&lt;br /&gt;
- Cons : all state change (Apply) methods must have the same name.&lt;/p&gt;
&lt;p&gt;I made no performance test, so I cannot judge if perf is better or not. It
seems that the DLR has a rather good cache when the same type is encountered
several time, but only measures can tell.&lt;/p&gt;
&lt;h2&gt;Handlers registration&lt;/h2&gt;
&lt;p&gt;This is the current implementation in &lt;a href="http://github.com/MarkNijhof/Fohjin/blob/9ae1f925c4a71aaaa7d2f1a5c24dcb63aabe7b54/Fohjin.DDD/Fohjin.DDD.Domain/BaseAggregateRoot.cs" target="_blank"&gt;Mark Nijhof’s sample&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The base class maintains a dictionary of Type/Action&amp;lt;T&amp;gt; association to
dispatch events based on type.&lt;/p&gt;
&lt;p&gt;Since an Action&amp;lt;T&amp;gt; delegate must have a target instance, the delegate
must be constructed from within the instance, in the .ctor.&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;AggregateRoot&lt;/span&gt;&amp;lt;TId&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt;,
&lt;span style="COLOR: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; handlers =&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
             
&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt;,
&lt;span style="COLOR: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Register&amp;lt;T&amp;gt;(&lt;span style="COLOR: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt;
handler)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
handlers.Add(&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(T),e =&amp;gt;
handler((T)e));&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Replay(&lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; @event
&lt;span style="COLOR: blue"&gt;in&lt;/span&gt; events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
handlers[@event.GetType()](@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: green"&gt;// rest of the aggregate root class&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Here is code that use it :&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Book&lt;/span&gt;
: &lt;span style="COLOR: #2b91af"&gt;AggregateRoot&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; Book(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;
id,&lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events) : &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;(id)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
Replay(events);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; Book(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;
id,&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; title, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; isbn) : &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;(id)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; @event = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookRegistered&lt;/span&gt;(id, title, isbn);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
OnBookRegistered(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
Append(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; Book(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;
id)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.id = id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
Register&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookRegistered&lt;/span&gt;&amp;gt;(OnBookRegistered);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
Register&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookLent&lt;/span&gt;&amp;gt;(OnBookLent);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
Register&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookReturned&lt;/span&gt;&amp;gt;(OnBookReturned);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
OnBookRegistered(&lt;span style="COLOR: #2b91af"&gt;BookRegistered&lt;/span&gt; @event) {
&lt;span style="COLOR: green"&gt;/**/&lt;/span&gt; }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
OnBookLent(&lt;span style="COLOR: #2b91af"&gt;BookLent&lt;/span&gt; @event) { &lt;span style="COLOR: green"&gt;/**/&lt;/span&gt; }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
OnBookReturned(&lt;span style="COLOR: #2b91af"&gt;BookReturned&lt;/span&gt; @event) {
&lt;span style="COLOR: green"&gt;/**/&lt;/span&gt; }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;+Pros : Still no reflection,&lt;br /&gt;
            Meaningful
method names&lt;br /&gt;
-Cons : Additional plumbing code, &lt;br /&gt;
            Private
constructor to avoid repetition&lt;br /&gt;
            Registration
occurs at each instantiation&lt;/p&gt;
&lt;h2&gt;Convention Based Method Naming&lt;/h2&gt;
&lt;p&gt;This is the way &lt;a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/15743" target="_blank"&gt;advocated by Greg Young&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If your event is called BookRegistered, assume the method will be called
OnBookRegistered, and find it by reflection. You can implement a cache at class
level to avoid reflection on each dispatch.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;abstract&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;AggregateRoot&lt;/span&gt;&amp;lt;TId&amp;gt; : &lt;span style="COLOR: #2b91af"&gt;IAggregateRoot&lt;/span&gt;&amp;lt;TId&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt;,
&lt;span style="COLOR: #2b91af"&gt;IEventDispatcher&lt;/span&gt;&amp;gt; Handlers =&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
              
&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt;,
&lt;span style="COLOR: #2b91af"&gt;IEventDispatcher&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt; &lt;span style="COLOR: blue"&gt;object&lt;/span&gt; HandlersLock = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Replay(&lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; dispatcher = GetDispatcher();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
dispatcher.Dispatch(&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;, @events);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IEventDispatcher&lt;/span&gt; GetDispatcher()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: #2b91af"&gt;IEventDispatcher&lt;/span&gt; handlers;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; type = GetType();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;lock&lt;/span&gt; (HandlersLock)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (!Handlers.TryGetValue(type, &lt;span style="COLOR: blue"&gt;out&lt;/span&gt; handlers))&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
{&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
handlers = &lt;span style="COLOR: #2b91af"&gt;EventDispatcher&lt;/span&gt;.Create(type);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
Handlers.Add(type, handlers);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; handlers;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        ... rest of the
code here&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The dispatcher code :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; &lt;span style="COLOR: blue"&gt;interface&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;IEventDispatcher&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Dispatch(&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;
target, &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;events);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;EventDispatcher&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="COLOR: #2b91af"&gt;IEventDispatcher&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="COLOR: #2b91af"&gt;IEventHandler&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; handlers;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; EventDispatcher()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; h = &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; m
&lt;span style="COLOR: blue"&gt;in&lt;/span&gt; &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(T)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
             
.GetMethods(&lt;span style="COLOR: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance |
&lt;span style="COLOR: #2b91af"&gt;BindingFlags&lt;/span&gt;.NonPublic)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
&lt;span style="COLOR: blue"&gt;let&lt;/span&gt; parameters = m.GetParameters()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
&lt;span style="COLOR: blue"&gt;where&lt;/span&gt; parameters.Length ==1&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
&amp;amp;&amp;amp; m.Name == &lt;span style="COLOR: #a31515"&gt;&amp;quot;On&amp;quot;&lt;/span&gt; +
parameters[0].ParameterType.Name&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
&lt;span style="COLOR: blue"&gt;select&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;EventHandler&lt;/span&gt;.Create&amp;lt;T&amp;gt;(m);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            handlers =
h.ToDictionary(i =&amp;gt; i.EventType);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Dispatch(&lt;span style="COLOR: blue"&gt;object&lt;/span&gt; target, &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; typedTarget = (T)target;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; @event
&lt;span style="COLOR: blue"&gt;in&lt;/span&gt; events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; handler = handlers[@event.GetType()];&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
handler.Call(typedTarget, @event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;EventDispatcher&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;IEventDispatcher&lt;/span&gt; Create(&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt; type)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; (&lt;span style="COLOR: #2b91af"&gt;IEventDispatcher&lt;/span&gt;)&lt;span style="COLOR: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
              
&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;EventDispatcher&lt;/span&gt;&amp;lt;&amp;gt;).MakeGenericType(type));&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;and the event handler :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; &lt;span style="COLOR: blue"&gt;interface&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;IEventHandler&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Call(T target, &lt;span style="COLOR: blue"&gt;object&lt;/span&gt; argument);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt; EventType { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;;
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;EventHandler&lt;/span&gt;&amp;lt;TEntity, TEvent&amp;gt; :
&lt;span style="COLOR: #2b91af"&gt;IEventHandler&lt;/span&gt;&amp;lt;TEntity&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;TEntity, TEvent&amp;gt; handler;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; EventHandler(&lt;span style="COLOR: #2b91af"&gt;MethodInfo&lt;/span&gt; methodInfo)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            handler =
(&lt;span style="COLOR: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;TEntity, TEvent&amp;gt;)&lt;span style="COLOR: #2b91af"&gt;Delegate&lt;/span&gt;.CreateDelegate(&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                 
&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;TEntity, TEvent&amp;gt;), methodInfo,
&lt;span style="COLOR: blue"&gt;true&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Call(TEntity
target, &lt;span style="COLOR: blue"&gt;object&lt;/span&gt; argument)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
handler(target, (TEvent)argument);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt;
EventType&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(TEvent); }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;EventHandler&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;IEventHandler&lt;/span&gt;&amp;lt;T&amp;gt;
Create&amp;lt;T&amp;gt;(&lt;span style="COLOR: #2b91af"&gt;MethodInfo&lt;/span&gt; methodInfo)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; eventType =
methodInfo.GetParameters()[0].ParameterType;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; (&lt;span style="COLOR: #2b91af"&gt;IEventHandler&lt;/span&gt;&amp;lt;T&amp;gt;)&lt;span style="COLOR: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                 
&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;EventHandler&lt;/span&gt;&amp;lt;,&amp;gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                 
.MakeGenericType(&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(T), eventType),&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                 
methodInfo&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                 
);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The trick here is to create a static delegate with two parameters from an
instance method info that take one parameter (and one implicit this
target).&lt;/p&gt;
&lt;p&gt;This way, the delegate is not tied to a specific instance and can be used on
any target.&lt;/p&gt;
&lt;p&gt;As you can see, this option requires more code ! I did not want to start
with that.&lt;/p&gt;
&lt;p&gt;+Pros : Convention base names mean no manual mapping, mapping is
implicit&lt;br /&gt;
            Binding is
made a class level instead of instance level&lt;/p&gt;
&lt;p&gt;-Cons : Only unit tests can tell when you mess with names&lt;br /&gt;
            Not immune
to event name change, should have good unit tests !&lt;/p&gt;
&lt;h2&gt;Apply then Append&lt;/h2&gt;
&lt;p&gt;I also had a remark that if I forget Append after Apply, I’ll get in
trouble.&lt;/p&gt;
&lt;p&gt;In Handler Registration option and Convention base method naming, the
dispatch can be done by the base class, so I could tell the base class to
dispatch then Append then event to UncommittedEvents.&lt;/p&gt;
&lt;p&gt;This way you end with something like :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; @event = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
BookLent(&lt;span style="COLOR: green"&gt;/**/&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
Play(@event);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;where play dispatches the event to the right method and appends.&lt;/p&gt;
&lt;p&gt;This way you cannot forget.&lt;/p&gt;
&lt;p&gt;My problem with this, especially in the Convention base method naming
scenario is that nobody references the event application methods anymore.
Resharper will report them as unused methods, and you won’t know unless you run
unit tests.&lt;/p&gt;
&lt;p&gt;Moreover, you pay the cost of a dynamic dispatch when you know your event
type.&lt;/p&gt;
&lt;p&gt;Perhaps something like this could be better :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; @event = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
BookLent(&lt;span style="COLOR: green"&gt;/**/&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
Play(@event).With(OnBookLent);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;the implementation is not very complicated :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;AggregateRoot&lt;/span&gt;&amp;lt;TId&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;UncommittedEvents&lt;/span&gt; uncommittedEents;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;EventPlayer&lt;/span&gt;&amp;lt;TEvent&amp;gt; Play&amp;lt;TEvent&amp;gt;(TEvent
@event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;EventPlayer&lt;/span&gt;&amp;lt;TEvent&amp;gt;(@event,
uncommitedEvents);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        ... rest of the
code here&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;struct&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;EventPlayer&lt;/span&gt;&amp;lt;TEvent&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt; TEvent
@event;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;UncommittedEvents&lt;/span&gt; uncommittedEvents;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; EventPlayer(TEvent @event, &lt;span style="COLOR: #2b91af"&gt;UncommittedEvents&lt;/span&gt; uncommittedEvents)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.@event = @event;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.uncommittedEvents = uncommittedEvents;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
With(&lt;span style="COLOR: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;TEvent&amp;gt; handler)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
handler(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
uncommittedEvents.Append(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;This way, methods are referenced at least once with type check.&lt;/p&gt;
&lt;p&gt;My mind is still not set… What do you prefer ?&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=hzMSb2CMLmA:k4iiD3PErQ0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=hzMSb2CMLmA:k4iiD3PErQ0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=hzMSb2CMLmA:k4iiD3PErQ0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=hzMSb2CMLmA:k4iiD3PErQ0:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=hzMSb2CMLmA:k4iiD3PErQ0:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=hzMSb2CMLmA:k4iiD3PErQ0:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=hzMSb2CMLmA:k4iiD3PErQ0:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=hzMSb2CMLmA:k4iiD3PErQ0:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/hzMSb2CMLmA" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2009/11/03/Event-Sourcing-and-CQRS-Dispatch-options#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/457356</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2009/11/03/Event-Sourcing-and-CQRS-Dispatch-options</feedburner:origLink></entry>
    
  <entry>
    <title>Event Sourcing and CQRS, Let's use it.</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/CYfRAHe1pv4/Event-Sourcing-and-CQRS-Lets-use-it" rel="alternate" type="text/html" title="Event Sourcing and CQRS, Let's use it." />
    <id>urn:md5:eafbc0de36bd89e388aa5565768037e9</id>
    <updated>2009-11-02T15:45:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>CQRS</dc:subject><dc:subject>Design</dc:subject><dc:subject>Distributed Domain Driven Design</dc:subject><dc:subject>Domain Driven Design</dc:subject><dc:subject>Reporting</dc:subject><dc:subject>Repository</dc:subject><dc:subject>Service Bus</dc:subject>    
    <content type="html">    &lt;p&gt;&lt;a href="http://thinkbeforecoding.com/post/2009/10/30/Event-Sourcing-and-CQRS%2C-Now-%21" target="_blank"&gt;Last time&lt;/a&gt;, we started a very basic Event Sourcing/Domain
Events/CQRS framework. &lt;em&gt;Be careful, I made an edit in the nested
DomainEvents+Handler&amp;lt;T&amp;gt;.Handles&amp;lt;E&amp;gt;() method, the
AggregateRoot.Replay method will not work as is, but we won’t need it.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We’ll build an equally simplistic application for personal library
management.&lt;/p&gt;
&lt;p&gt;The Ubiquitous Language will be minimal.&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;Book&lt;/strong&gt; can be &lt;strong&gt;Registered&lt;/strong&gt; with a
&lt;strong&gt;Title&lt;/strong&gt; and an &lt;strong&gt;ISBN&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;Book&lt;/strong&gt; can be &lt;strong&gt;Lent&lt;/strong&gt; to a
&lt;strong&gt;Borrower&lt;/strong&gt; at some &lt;strong&gt;Date&lt;/strong&gt; for an &lt;strong&gt;Expected
Time Span&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;Book&lt;/strong&gt; can then be &lt;strong&gt;Returned&lt;/strong&gt;. If it is
&lt;strong&gt;Returned&lt;/strong&gt; after &lt;strong&gt;Expected Time Span&lt;/strong&gt;, the return
is &lt;strong&gt;Late&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;That’s enough for our first try.&lt;/p&gt;
&lt;h2&gt;The Command Context&lt;/h2&gt;
&lt;h3&gt;The State Change Events&lt;/h3&gt;
&lt;p&gt;Here is the code for the three events that we found in the Ubiquitous
language:&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookRegistered&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; Id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; Title;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; Isbn;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; BookRegistered(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;
title, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; isbn)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            Id = id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            Title =
title;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            Isbn =
isbn;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookLent&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; Id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; Borrower;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;DateTime&lt;/span&gt; Date;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;TimeSpan&lt;/span&gt; ExpectedDuration;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; BookLent(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;
id, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; borrower, &lt;span style="COLOR: #2b91af"&gt;DateTime&lt;/span&gt; date,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
              
&lt;span style="COLOR: #2b91af"&gt;TimeSpan&lt;/span&gt; expectedDuration)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            Id = id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            Borrower =
borrower;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            Date =
date;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
ExpectedDuration = expectedDuration;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookReturned&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; Id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; By;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;TimeSpan&lt;/span&gt; After;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; Late;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; BookReturned(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; @by,
&lt;span style="COLOR: #2b91af"&gt;TimeSpan&lt;/span&gt; after,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            
&lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; late)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            Id = id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            By = by;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            After =
after;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            Late =
late;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;These events will usually be serialized to the event storage and on a
service bus, but here everything runs in memory.&lt;/p&gt;
&lt;h3&gt;The Book Aggregate Root&lt;/h3&gt;
&lt;p&gt;The book will need to be referenced by an identity in our system. We’ll hide
a Guid behind a BookId struct :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;struct&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;IEquatable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Guid&lt;/span&gt; id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; BookId(&lt;span style="COLOR: #2b91af"&gt;Guid&lt;/span&gt;
id) { &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.id = id; }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; NewBookId() { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid()); }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt;
Equals(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; other) { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; other.id.Equals(id); }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; Equals(&lt;span style="COLOR: blue"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (ReferenceEquals(&lt;span style="COLOR: blue"&gt;null&lt;/span&gt;,
obj)) &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: blue"&gt;false&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (obj.GetType() != &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;))
&lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: blue"&gt;false&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; Equals((&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;)obj);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;int&lt;/span&gt; GetHashCode() { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; id.GetHashCode(); }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now, the Book class itself :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;&lt;span style="COLOR: blue"&gt;  public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Book&lt;/span&gt;
: AggregateRoot&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;  {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; title;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; isbn;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;
borrower;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;DateTime&lt;/span&gt;
date;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;TimeSpan&lt;/span&gt;
expectedDuration;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; Book(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id,
&lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.id = id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
&lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt; @event &lt;span style="COLOR: blue"&gt;in&lt;/span&gt;
events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
             
Apply(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; Book(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id,
&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; title, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; isbn)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.id = id;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; @event = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; BookRegistered(id, title, isbn);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
Apply(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
Append(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; Id { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; id; } }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Lend(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; borrower, &lt;span style="COLOR: #2b91af"&gt;DateTime&lt;/span&gt; date,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                             
&lt;span style="COLOR: #2b91af"&gt;TimeSpan&lt;/span&gt; expectedDuration)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
&lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.borrower != &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
             
&lt;span style="COLOR: blue"&gt;throw&lt;/span&gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/span&gt;(&lt;span style="COLOR: #a31515"&gt;&amp;quot;The book is already lent.&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; @event =&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; BookLent(id, borrower, date,
expectedDuration);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
Apply(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
Append(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Return(&lt;span style="COLOR: #2b91af"&gt;DateTime&lt;/span&gt; returnDate)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
&lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (borrower == &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
             
&lt;span style="COLOR: blue"&gt;throw&lt;/span&gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/span&gt;(&lt;span style="COLOR: #a31515"&gt;&amp;quot;The book has not been lent.&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
&lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (returnDate &amp;lt; date)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
             
&lt;span style="COLOR: blue"&gt;throw&lt;/span&gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: #a31515"&gt;&amp;quot;The book cannot be returned before being
lent.&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; actualDuration = returnDate - date;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; @event = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; BookReturned(&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                        
id,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                        
borrower,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                        
actualDuration,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                        
actualDuration &amp;gt; expectedDuration);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
Apply(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
Append(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Apply(BookRegistered @event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
title = @event.Title;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
isbn = @event.Isbn;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Apply(BookLent @event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
borrower = @event.Borrower;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
date = @event.Date;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
expectedDuration = @event.ExpectedDuration;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Apply(BookReturned @event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;         
borrower = &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;      }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;  }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The class implements AggregateRoot&amp;lt;BookId&amp;gt; and so provides an
explicitly implemented UncommittedEvents property.&lt;/p&gt;
&lt;p&gt;The first .ctor is used to load the Aggregate Root, the second one is used
to build a new Aggregate Root.&lt;/p&gt;
&lt;p&gt;The public methods (Lend and Return) are the commands on the Aggregate Root
as defined in the Ubiquitous Language.&lt;/p&gt;
&lt;p&gt;The structure is always the same :&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Validate arguments and state&lt;/li&gt;
&lt;li&gt;Prepare state transition using domain logic&lt;/li&gt;
&lt;li&gt;Apply state transition (no domain logic should happen here)&lt;/li&gt;
&lt;li&gt;Append state transition to uncommitted events&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first .ctor uses dynamic to dispatch each event object on the
corresponding specific Apply method. In case you implement the pattern is
previous C# version, it is advised to provide a Replay method in the base class
that will perform the dynamic dispatch based on reflection.&lt;/p&gt;
&lt;p&gt;That’s all for the entity. No ORM, no mapping… easy.&lt;/p&gt;
&lt;h3&gt;The Repository&lt;/h3&gt;
&lt;p&gt;It is often clearer to provide a specific repository interface that exposes
only available methods. With event sourcing, it’s not that useful… we’ll write
it anyway in case you’d like to use dependency injection. The interface is part
of the domain and should be in the same assembly as the entity and the
events.&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;interface&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IBookRepository&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Add(&lt;span style="COLOR: #2b91af"&gt;Book&lt;/span&gt;
book);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;Book&lt;/span&gt; &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;[&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id] { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;;
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The implementation will simply derive from the Repository base class, it can
be in the application assembly.&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookRepository&lt;/span&gt; :&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;       
Repository&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;, &lt;span style="COLOR: #2b91af"&gt;Book&lt;/span&gt;&amp;gt;,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;IBookRepository&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Book&lt;/span&gt; CreateInstance(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Book&lt;/span&gt;(id, events);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Add and the indexer are implemented by the base class. The only thing to
provide is a way to instantiate the class with expected parameters.&lt;/p&gt;
&lt;p&gt;We could use Activator.CreateInstance or reflection to provide a generic
implementation. I choose to make it simpler to read.&lt;/p&gt;
&lt;h2&gt;The Query context&lt;/h2&gt;
&lt;h3&gt;The Report Database&lt;/h3&gt;
&lt;p&gt;We’ll mimic a reporting table of book lent state :&lt;/p&gt;
&lt;p&gt;This would be the data returned from table rows :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookState&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; Id {
&lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;;
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; Title {
&lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;;
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; Lent {
&lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; &lt;span style="COLOR: blue"&gt;set&lt;/span&gt;;
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;And this will hide the data table implementation :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;interface&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IBookStateQuery&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookState&lt;/span&gt;&amp;gt; GetBookStates();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;BookState&lt;/span&gt; GetBookState(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookState&lt;/span&gt;&amp;gt; GetLentBooks();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; AddBookState(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;
title);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; SetLent(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;
id, &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; lent);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;We can simply query data to report in the UI, and update data state.&lt;/p&gt;
&lt;p&gt;Implementation will be in memory for now :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;&lt;span style="COLOR: blue"&gt;    class&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookStateQuery&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;IBookStateQuery&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;, &lt;span style="COLOR: #2b91af"&gt;BookState&lt;/span&gt;&amp;gt; states =&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                    
&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;, &lt;span style="COLOR: #2b91af"&gt;BookState&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookState&lt;/span&gt;&amp;gt; GetBookStates()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; states.Values;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookState&lt;/span&gt;
GetBookState(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; states[id];&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookState&lt;/span&gt;&amp;gt; GetLentBooks()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; states.Values.Where(b =&amp;gt; b.Lent);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
AddBookState(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; title)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; state = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookState&lt;/span&gt; { Id = id, Title = title };&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
states.Add(id, state);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
SetLent(&lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt; id, &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; lent)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
states[id].Lent = lent;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The important point here is that no domain logic occurs.&lt;/p&gt;
&lt;p&gt;A RDBMS implementation could use an ORM or simply build DTOs from a
DataReader.&lt;/p&gt;
&lt;h2&gt;The event handlers&lt;/h2&gt;
&lt;p&gt;We can now denormalize domain states to the reporting database using an
event handler :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;&lt;span style="COLOR: blue"&gt;    class&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookStateHandler&lt;/span&gt; :&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    Handles&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookRegistered&lt;/span&gt;&amp;gt;,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    Handles&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookLent&lt;/span&gt;&amp;gt;,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    Handles&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookReturned&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;IBookStateQuery&lt;/span&gt; stateQuery;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; BookStateHandler(&lt;span style="COLOR: #2b91af"&gt;IBookStateQuery&lt;/span&gt; stateQuery)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.stateQuery = stateQuery;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Handle(&lt;span style="COLOR: #2b91af"&gt;BookRegistered&lt;/span&gt; @event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
stateQuery.AddBookState(@event.Id, @event.Title);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Handle(&lt;span style="COLOR: #2b91af"&gt;BookLent&lt;/span&gt; @event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="COLOR: #a31515"&gt;&amp;quot;Book
lent to {0}&amp;quot;&lt;/span&gt;, @event.Borrower);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
stateQuery.SetLent(@event.Id, &lt;span style="COLOR: blue"&gt;true&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Handle(&lt;span style="COLOR: #2b91af"&gt;BookReturned&lt;/span&gt; @event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="COLOR: #a31515"&gt;&amp;quot;Book
returned by {0}&amp;quot;&lt;/span&gt;, @event.By);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
stateQuery.SetLent(@event.Id, &lt;span style="COLOR: blue"&gt;false&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The Console.WriteLine are here to view when things happen, you would usually
not use it in your production code. Logging this would not provide much
benefits since all the events are already stored in the EventStorage.&lt;/p&gt;
&lt;p&gt;Using this handler, the IBookStateQuery will be up to date with current
Command Context state. In an asynchronous environment, this is where eventual
consistency is introduced.&lt;/p&gt;
&lt;p&gt;We will also add a service that will notify when a user returned a book too
late :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;class&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;LateReturnNotifier&lt;/span&gt; :&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    Handles&amp;lt;&lt;span style="COLOR: #2b91af"&gt;BookReturned&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Handle(&lt;span style="COLOR: #2b91af"&gt;BookReturned&lt;/span&gt; @event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (@event.Late)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="COLOR: #a31515"&gt;&amp;quot;{0} was late&amp;quot;&lt;/span&gt;, @event.By);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Here again, no domain logic, we just do the infrastructure stuff, usually
sending an email or a SMS.&lt;/p&gt;
&lt;h2&gt;View it in Action&lt;/h2&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;&lt;span style="COLOR: blue"&gt;    class&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Program&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Main(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
ISessionFactory factory = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
SessionFactory(&lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
EventStorage());               
&lt;span style="COLOR: #2b91af"&gt;IBookStateQuery&lt;/span&gt; query = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookStateQuery&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
DomainEvents.RegisterHanlder(() =&amp;gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;BookStateHandler&lt;/span&gt;(query));&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
DomainEvents.RegisterHanlder(() =&amp;gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;LateReturnNotifier&lt;/span&gt;());&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; bookId = &lt;span style="COLOR: #2b91af"&gt;BookId&lt;/span&gt;.NewBookId();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;using&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; session =
factory.OpenSession())&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; books = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookRepository&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
books.Add(&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Book&lt;/span&gt;(bookId,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                  
&lt;span style="COLOR: #a31515"&gt;&amp;quot;The Lord of the Rings&amp;quot;&lt;/span&gt;,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                  
&lt;span style="COLOR: #a31515"&gt;&amp;quot;0-618-15396-9&amp;quot;&lt;/span&gt;));&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
session.SubmitChanges();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
ShowBooks(query);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;using&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; session =
factory.OpenSession())&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; books = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookRepository&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; book = books[bookId];&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
book.Lend(&lt;span style="COLOR: #a31515"&gt;&amp;quot;Alice&amp;quot;&lt;/span&gt;,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                    
&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;DateTime&lt;/span&gt;(2009, 11, 2),&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                    
&lt;span style="COLOR: #2b91af"&gt;TimeSpan&lt;/span&gt;.FromDays(14));&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
session.SubmitChanges();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
ShowBooks(query);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;using&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; session =
factory.OpenSession())&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; books = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookRepository&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; book = books[bookId];&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
book.Return(&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;DateTime&lt;/span&gt;(2009, 11, 8));&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
session.SubmitChanges();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
ShowBooks(query);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;using&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; session =
factory.OpenSession())&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; books = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookRepository&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; book = books[bookId];&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
book.Lend(&lt;span style="COLOR: #a31515"&gt;&amp;quot;Bob&amp;quot;&lt;/span&gt;,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                     
&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;DateTime&lt;/span&gt;(2009, 11, 9),&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                     
&lt;span style="COLOR: #2b91af"&gt;TimeSpan&lt;/span&gt;.FromDays(14));&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
session.SubmitChanges();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
ShowBooks(query);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;using&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; session =
factory.OpenSession())&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; books = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;BookRepository&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; book = books[bookId];&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
book.Return(&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;DateTime&lt;/span&gt;(2010, 03, 1));&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
session.SubmitChanges();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
ShowBooks(query);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;void&lt;/span&gt; ShowBooks(&lt;span style="COLOR: #2b91af"&gt;IBookStateQuery&lt;/span&gt; query)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; state
&lt;span style="COLOR: blue"&gt;in&lt;/span&gt; query.GetBookStates())&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="COLOR: #a31515"&gt;&amp;quot;{0} is {1}.&amp;quot;&lt;/span&gt;,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                      
state.Title,&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                      
state.Lent ? &lt;span style="COLOR: #a31515"&gt;&amp;quot;lent&amp;quot;&lt;/span&gt; : &lt;span style="COLOR: #a31515"&gt;&amp;quot;home&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;We start by instantiating storage for the command context (the
ISessionFactory) and the query context (the &lt;span style="COLOR: #2b91af"&gt;IBookStateQuery&lt;/span&gt;). In production you’ll use persistent
storages (a persistent event storage and a RDBMS). I highly recommend using a
Dependency Injection Container for real size projects.&lt;/p&gt;
&lt;p&gt;Then we wire the handlers on domain events.&lt;/p&gt;
&lt;p&gt;The application can start.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We register a book in the library.&lt;/li&gt;
&lt;li&gt;We lend it to Alice on 2009-11-02 for 14 days&lt;/li&gt;
&lt;li&gt;She returns it on 2009-11-08, she’s on time&lt;/li&gt;
&lt;li&gt;We lend it to Bob on 2009-11-09 for 14 days,&lt;/li&gt;
&lt;li&gt;He returns it on 2010-03-01, he’s late&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The output is the following :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;The Lord of the Rings &lt;span style="COLOR: blue"&gt;is&lt;/span&gt;
home.    &lt;span style="COLOR: green"&gt;// written from
state&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;Book lent to
Alice               
&lt;span style="COLOR: green"&gt;// written by the book state handler&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;The Lord of the Rings &lt;span style="COLOR: blue"&gt;is&lt;/span&gt;
lent.    &lt;span style="COLOR: green"&gt;// written from
state&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;Book returned by
Alice           
&lt;span style="COLOR: green"&gt;// written by the book state handler&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;The Lord of the Rings &lt;span style="COLOR: blue"&gt;is&lt;/span&gt;
home.    &lt;span style="COLOR: green"&gt;// written from
state&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;Book lent to
Bob                 
&lt;span style="COLOR: green"&gt;// written by the book state handler&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;The Lord of the Rings &lt;span style="COLOR: blue"&gt;is&lt;/span&gt;
lent.    &lt;span style="COLOR: green"&gt;// written from
state&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;Book returned by
Bob             
&lt;span style="COLOR: green"&gt;// written by the book state handler&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;Bob was
late                     
&lt;span style="COLOR: green"&gt;// written by the late return notifier&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;The Lord of the Rings &lt;span style="COLOR: blue"&gt;is&lt;/span&gt;
home.    &lt;span style="COLOR: green"&gt;// written from
state&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;We have here a clear separation between Command that handles the domain
logic and Query that handles presentation logic.&lt;/p&gt;
&lt;p&gt;Have fun. Questions and remarks expected !&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=CYfRAHe1pv4:0-8iycDB6VM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=CYfRAHe1pv4:0-8iycDB6VM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=CYfRAHe1pv4:0-8iycDB6VM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=CYfRAHe1pv4:0-8iycDB6VM:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=CYfRAHe1pv4:0-8iycDB6VM:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=CYfRAHe1pv4:0-8iycDB6VM:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=CYfRAHe1pv4:0-8iycDB6VM:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=CYfRAHe1pv4:0-8iycDB6VM:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/CYfRAHe1pv4" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2009/11/02/Event-Sourcing-and-CQRS-Lets-use-it#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/457100</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2009/11/02/Event-Sourcing-and-CQRS-Lets-use-it</feedburner:origLink></entry>
    
  <entry>
    <title>Event Sourcing and CQRS, Now !</title>
    <link href="http://feeds.thinkbeforecoding.com/~r/ThinkBeforeCoding/~3/zeQtwGPdAK0/Event-Sourcing-and-CQRS-Now" rel="alternate" type="text/html" title="Event Sourcing and CQRS, Now !" />
    <id>urn:md5:e543def22b2926eeb3ba9146b58c31e0</id>
    <updated>2009-10-30T15:53:00+01:00</updated>
    <author><name>Jérémie Chassaing</name></author>
        <dc:subject>Domain Driven Design</dc:subject>
        <dc:subject>CQRS</dc:subject><dc:subject>Distributed Domain Driven Design</dc:subject><dc:subject>Domain Driven Design</dc:subject><dc:subject>Repository</dc:subject>    
    <content type="html">    &lt;p&gt;Enough talking, Action !&lt;/p&gt;
&lt;p&gt;Today, we will build a basic event sourcing infrastructure. Get the beta 2
of Visual Studio 2010, we’ll be using C# dynamic features to go straight to our
goal.&lt;/p&gt;
&lt;h2&gt;Then Event Storage&lt;/h2&gt;
&lt;p&gt;Let’s hide the ugly details of the event storage behind two simple
interfaces :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;&lt;span style="COLOR: blue"&gt;    public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;interface&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IEventStorage&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;IDisposable&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;IAggregateRootStorage&lt;/span&gt;&amp;lt;TId&amp;gt;
GetAggregateRootStore&amp;lt;TAggregateRoot, TId&amp;gt;()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;where&lt;/span&gt; TAggregateRoot : AggregateRoot&amp;lt;TId&amp;gt;;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;interface&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IAggregateRootStorage&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;in&lt;/span&gt; TId&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Append(TId id, &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;[TId id] {
&lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;And we start with a minimal in memory implementation, the event storage
first :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;&lt;span style="COLOR: blue"&gt;    public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;EventStorage&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;IEventStorage&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt;&amp;gt;
stores = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt;,
&lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IAggregateRootStorage&lt;/span&gt;&amp;lt;TId&amp;gt;
GetAggregateRootStorage&amp;lt;TAggregateRoot, TId&amp;gt;()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;&lt;span style="COLOR: blue"&gt;          
where&lt;/span&gt; TAggregateRoot : AggregateRoot&amp;lt;TId&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;dynamic&lt;/span&gt; store;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (!stores.TryGetValue(&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(TAggregateRoot), &lt;span style="COLOR: blue"&gt;out&lt;/span&gt; store))&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
store = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
AggregateRootStorage&amp;lt;TId&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
stores.Add(&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt; (TAggregateRoot),
store);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; store;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Dispose()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
stores.Clear();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Here I could replace dynamic by object, and cast to requested type on
return. I use dynamic because this kind of code is not compile time safe
anyway. There’s a specific storage for each Aggregate Root type, especially
depending on identifier type, for type safety.&lt;/p&gt;
&lt;p&gt;Then the AggregateRootStorage :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;&lt;span style="COLOR: blue"&gt;    class&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;AggregateRootStorage&lt;/span&gt;&amp;lt;TId&amp;gt; :
&lt;span style="COLOR: #2b91af"&gt;IAggregateRootStorage&lt;/span&gt;&amp;lt;TId&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;TId, &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;
store = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;TId, &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Append(TId
id, &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;
aggregateRootEvents;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (!store.TryGetValue(id, &lt;span style="COLOR: blue"&gt;out&lt;/span&gt; aggregateRootEvents))&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
aggregateRootEvents = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
store.Add(id, aggregateRootEvents);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
aggregateRootEvents.AddRange(events);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;[TId
id]&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; store[id];
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;It simply stores list of events associated with aggregate root
identifier.&lt;/p&gt;
&lt;h2&gt;The Aggregate Root&lt;/h2&gt;
&lt;p&gt;Aggregate roots manage uncommitted events :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;interface&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IUncommittedEvents&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; HasEvents { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;;
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Commit();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The interface can indicates whether there are events, returns the events,
and clears the uncommitted events by committing.&lt;/p&gt;
&lt;p&gt;Quick implementation :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;UncommittedEvents&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;IUncommittedEvents&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Append(&lt;span style="COLOR: blue"&gt;object&lt;/span&gt; @event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
events.Add(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;IEnumerator&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt;.GetEnumerator()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; events.GetEnumerator();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; HasEvents&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; events.Count
!= 0; }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IUncommittedEvents&lt;/span&gt;.Commit()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
events.Clear();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;IEnumerator&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;.GetEnumerator()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; events.GetEnumerator();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Nothing tricky here neither.&lt;/p&gt;
&lt;p&gt;Now, the IAggregateRoot interface used by the repository gives access to the
uncommitted events:&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;&lt;span style="COLOR: blue"&gt;    public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;interface&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IAggregateRoot&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;out&lt;/span&gt;
TId&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        TId Id {
&lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;IUncommittedEvents&lt;/span&gt; UncommittedEvents { &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The AggregateRoot class will maintain the uncommitted events :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;abstract&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;AggregateRoot&lt;/span&gt;&amp;lt;TId&amp;gt; : &lt;span style="COLOR: #2b91af"&gt;IAggregateRoot&lt;/span&gt;&amp;lt;TId&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;UncommittedEvents&lt;/span&gt; uncommittedEvents =
&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;UncommittedEvents&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Replay(&lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            dynamic me =
&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; @event
&lt;span style="COLOR: blue"&gt;in&lt;/span&gt; events)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
me.Apply(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Append(&lt;span style="COLOR: blue"&gt;object&lt;/span&gt; @event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
uncommittedEvents.Append(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;abstract&lt;/span&gt; TId Id {
&lt;span style="COLOR: blue"&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;IUncommittedEvents&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;IAggregateRoot&lt;/span&gt;&amp;lt;TId&amp;gt;.UncommittedEvents&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt;
uncommittedEvents; }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The Append method will be use by child class to append events after they are
applied.&lt;/p&gt;
&lt;p&gt;The Replay method is used in the child class constructor to rebuild the
Aggregate Root state from events.&lt;/p&gt;
&lt;p&gt;Here I use a dynamic me variable to dispatch events on specific child class
Apply methods. A .Net 2 or 3.5 implementation would use reflection to dispatch
events on Apply methods.&lt;/p&gt;
&lt;p&gt;The UncommittedEvents property is implemented explicitly so that it does not
appear in standard class use.&lt;/p&gt;
&lt;h2&gt;The Repository&lt;/h2&gt;
&lt;p&gt;The repository is just very slightly longer. I added a session concept so
that several repositories can submit changes in a single transaction :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; &lt;span style="COLOR: blue"&gt;interface&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;ISessionItem&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; SubmitChanges();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;     &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;abstract&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Repository&lt;/span&gt;&amp;lt;TId, TAggregateRoot&amp;gt; :
ISessionItem&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;where&lt;/span&gt; TAggregateRoot : &lt;span style="COLOR: #2b91af"&gt;AggregateRoot&lt;/span&gt;&amp;lt;TId&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;TId, TAggregateRoot&amp;gt; users
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;TId, TAggregateRoot&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;IAggregateRootStorage&lt;/span&gt;&amp;lt;TId&amp;gt;
aggregateRootStorage;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; Repository()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
aggregateRootStorage = Session.Enlist(&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
Add(TAggregateRoot user)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
users.Add(user.Id, user);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; TAggregateRoot &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;[TId id]&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; Find(id) ??
Load(id); }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; TAggregateRoot Find(TId id)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
TAggregateRoot user;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; users.TryGetValue(id, &lt;span style="COLOR: blue"&gt;out&lt;/span&gt; user) ? user : &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; TAggregateRoot Load(TId id)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; events = aggregateRootStorage[id];&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; user = CreateInstance(id, events);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
users.Add(id, user);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; user;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; &lt;span style="COLOR: blue"&gt;abstract&lt;/span&gt;
TAggregateRoot CreateInstance(TId id, &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;object&lt;/span&gt;&amp;gt; events);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
SubmitChanges()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: #2b91af"&gt;IAggregateRoot&lt;/span&gt;&amp;lt;TId&amp;gt; user &lt;span style="COLOR: blue"&gt;in&lt;/span&gt; users.Values)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; uncomitedEvents =
user.UncommittedEvents;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (uncomitedEvents.HasEvents)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
{&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
aggregateRootStorage.Append(user.Id, uncomitedEvents);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
PublishEvents(uncomitedEvents);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
uncomitedEvents.Commit();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
users.Clear();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
PublishEvents(&lt;span style="COLOR: #2b91af"&gt;IUncommittedEvents&lt;/span&gt;
uncommittedEvents)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (dynamic @event &lt;span style="COLOR: blue"&gt;in&lt;/span&gt; uncommittedEvents)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
DomainEvents.Raise(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The constructor enlist the repository in current session.&lt;/p&gt;
&lt;p&gt;The Add method registers the aggregate root in the repository, its events
will be persisted in SubmitChanges()&lt;/p&gt;
&lt;p&gt;The indexer finds an entity already in memory or loads it from the event
store. The abstract CreateInstance method implementation will be responsible
for instantiation.&lt;/p&gt;
&lt;p&gt;Submit changes does what is expected, and also publish committed events.
Will see the trick with dynamic @events when we analyze domain events.&lt;/p&gt;
&lt;h2&gt;The Session and its Factory&lt;/h2&gt;
&lt;p&gt;Just to group the SubmitChanges on several repositories :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;interface&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;ISessionFactory&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;IDisposable&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: #2b91af"&gt;ISession&lt;/span&gt; OpenSession();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;SessionFactory&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;ISessionFactory&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;IEventStorage&lt;/span&gt; eventStorage;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; SessionFactory(&lt;span style="COLOR: #2b91af"&gt;IEventStorage&lt;/span&gt; eventStorage)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.eventStorage = eventStorage;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;ISession&lt;/span&gt;
OpenSession()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Session&lt;/span&gt;(eventStorage);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Dispose()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
eventStorage.Dispose();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;interface&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;ISession&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;IDisposable&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; SubmitChanges();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Session&lt;/span&gt; : &lt;span style="COLOR: #2b91af"&gt;ISession&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;IEventStorage&lt;/span&gt; eventStorage;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;HashSet&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ISessionItem&lt;/span&gt;&amp;gt; enlistedItems = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;HashSet&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ISessionItem&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        [&lt;span style="COLOR: #2b91af"&gt;ThreadStatic&lt;/span&gt;] &lt;span style="COLOR: blue"&gt;private&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Session&lt;/span&gt; current;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; Session(&lt;span style="COLOR: #2b91af"&gt;IEventStorage&lt;/span&gt; eventStorage)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.eventStorage = eventStorage;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (current != &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;throw&lt;/span&gt; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/span&gt;(&lt;span style="COLOR: #a31515"&gt;&amp;quot;Cannot nest unit of work&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            current =
&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Session&lt;/span&gt; Current&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;get&lt;/span&gt; { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; current;
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt;
SubmitChanges()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; enlisted
&lt;span style="COLOR: blue"&gt;in&lt;/span&gt; enlistedItems)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
enlisted.SubmitChanges();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
enlistedItems.Clear();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Dispose()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            current =
&lt;span style="COLOR: blue"&gt;null&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;internal&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;IAggregateRootStorage&lt;/span&gt;&amp;lt;TId&amp;gt;
Enlist&amp;lt;TId, TAggregateRoot&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                       
(&lt;span style="COLOR: #2b91af"&gt;Repository&lt;/span&gt;&amp;lt;TId, TAggregateRoot&amp;gt;
repository)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;where&lt;/span&gt; TAggregateRoot : &lt;span style="COLOR: #2b91af"&gt;AggregateRoot&lt;/span&gt;&amp;lt;TId&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; unitOfWork = Current;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
unitOfWork.enlistedItems.Add(repository);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;return&lt;/span&gt;
unitOfWork.eventStorage.GetAggregateRootStorage&amp;lt;TAggregateRoot,
TId&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Ok almost everything is here. The last part, for events to be used by other
parts of the system, needed to go CQRS.&lt;/p&gt;
&lt;h2&gt;Domain Events&lt;/h2&gt;
&lt;p&gt;Here, I made a minor variation on &lt;a href="http://www.udidahan.com/2009/06/14/domain-events-salvation/" target="_blank"&gt;Udi Dahan’s DomainEvents&lt;/a&gt; implementation :&lt;/p&gt;
&lt;div style="BORDER-BOTTOM: #a0a0a0 1px solid; BORDER-LEFT: #a0a0a0 1px solid; FONT-FAMILY: courier new; BACKGROUND: #f8f8f8; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: #a0a0a0 1px solid; BORDER-RIGHT: #a0a0a0 1px solid"&gt;
&lt;p style="MARGIN: 0"&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;DomainEvents&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        [&lt;span style="COLOR: #2b91af"&gt;ThreadStatic&lt;/span&gt;] &lt;span style="COLOR: blue"&gt;private&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Delegate&lt;/span&gt;&amp;gt; actions;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Handler&lt;/span&gt;&amp;gt; handlers;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Register&amp;lt;T&amp;gt;(&lt;span style="COLOR: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt; callback)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (actions == &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;)
actions = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Delegate&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
actions.Add(callback);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;void&lt;/span&gt; RegisterHanlder&amp;lt;T&amp;gt;(&lt;span style="COLOR: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T&amp;gt; factory)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (handlers == &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;)
handlers = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Handler&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
           
handlers.Add(&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Handler&lt;/span&gt;&amp;lt;T&amp;gt;(factory));&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: green"&gt;//Raises the given domain
event      &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Raise&amp;lt;T&amp;gt;(T @event)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (actions != &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: #2b91af"&gt;Delegate&lt;/span&gt; action &lt;span style="COLOR: blue"&gt;in&lt;/span&gt;
actions)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
&lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (action &lt;span style="COLOR: blue"&gt;is&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                       
((&lt;span style="COLOR: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt;) action)(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (handlers != &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;var&lt;/span&gt;
h &lt;span style="COLOR: blue"&gt;in&lt;/span&gt; handlers)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
{&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
&lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (h.Handles&amp;lt;T&amp;gt;())&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
{&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                       
&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; handler= h.CreateInstance&amp;lt;T&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                       
handler.Handle(@event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                   
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
}&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;abstract&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Handler&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;abstract&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; Handles&amp;lt;E&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;abstract&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Handles&lt;/span&gt;&amp;lt;E&amp;gt;
CreateInstance&amp;lt;E&amp;gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Handler&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="COLOR: #2b91af"&gt;Handler&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;private&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T&amp;gt; factory;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; Handler(&lt;span style="COLOR: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T&amp;gt; factory)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.factory = factory;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; Handles&amp;lt;E&amp;gt;()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;
(Handles&amp;lt;E&amp;gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
                  
.IsAssignableFrom(&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt; (T));&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt;
&lt;span style="COLOR: #2b91af"&gt;Handles&lt;/span&gt;&amp;lt;E&amp;gt;
CreateInstance&amp;lt;E&amp;gt;()&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
               
&lt;span style="COLOR: blue"&gt;return&lt;/span&gt; (&lt;span style="COLOR: #2b91af"&gt;Handles&lt;/span&gt;&amp;lt;E&amp;gt;)factory();&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;
            }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt;
&lt;span style="COLOR: blue"&gt;interface&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Handles&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;in&lt;/span&gt;
T&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;        &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Handle(T @event);&lt;/p&gt;
&lt;p style="MARGIN: 0"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;em&gt;Edit : Changed the Handles&amp;lt;E&amp;gt;, T should be casted as
Handles&amp;lt;E&amp;gt;, not as E, of course.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Event handlers can be registerd as Action&amp;lt;T&amp;gt; delegates or as class
that implements once or more Handles&amp;lt;T&amp;gt;.&lt;/p&gt;
&lt;p&gt;The private Handler and Handler&amp;lt;T&amp;gt; classes are used to find handlers
that handles a specific message and dispatch it, without using a Dependency
Injection Container like Udi’s implementation.&lt;/p&gt;
&lt;p&gt;The simple dynamic-fu in the repository was to call
DomainEvents.Raise&amp;lt;T&amp;gt; using a dynamic dispatch. This way, Raise is always
called with the actual event type in T. No tricky reflection is needed for the
dispatch. inside Raise&amp;lt;T&amp;gt;, we can the rely on T as being the actual event
type. Funky !&lt;/p&gt;
&lt;h2&gt;Next Time…&lt;/h2&gt;
&lt;p&gt;There’s already a lot of code for a single post, every thing is in place at
infrastructure level. You can already try it for yourself if you can figure how
to.&lt;/p&gt;
&lt;p&gt;The sample will come in the next post, stay tuned.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=zeQtwGPdAK0:7BScoAL78ws:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=zeQtwGPdAK0:7BScoAL78ws:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=zeQtwGPdAK0:7BScoAL78ws:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=zeQtwGPdAK0:7BScoAL78ws:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=zeQtwGPdAK0:7BScoAL78ws:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=zeQtwGPdAK0:7BScoAL78ws:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.thinkbeforecoding.com/~ff/ThinkBeforeCoding?a=zeQtwGPdAK0:7BScoAL78ws:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/ThinkBeforeCoding?i=zeQtwGPdAK0:7BScoAL78ws:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ThinkBeforeCoding/~4/zeQtwGPdAK0" height="1" width="1"/&gt;</content>
    
    

    
          <wfw:comment>http://thinkbeforecoding.com/post/2009/10/30/Event-Sourcing-and-CQRS-Now#comment-form</wfw:comment>
      <wfw:commentRss>http://thinkbeforecoding.com/feed/atom/comments/456396</wfw:commentRss>
      <feedburner:origLink>http://thinkbeforecoding.com/post/2009/10/30/Event-Sourcing-and-CQRS-Now</feedburner:origLink></entry>
  
</feed>

