<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Elegant Code &#187; CQRS</title>
	<atom:link href="http://elegantcode.com/category/ddd/cqrs/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Sun, 12 Feb 2012 04:40:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Is NoSQL Finally Going Mainstream?</title>
		<link>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=is-nosql-finally-going-mainstream</link>
		<comments>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:00:31 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CQRS]]></category>
		<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/</guid>
		<description><![CDATA[Its been a while since I enjoyed my adventures with CouchDB. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a while since I enjoyed <a href="http://elegantcode.com/category/couchdb/">my adventures with CouchDB</a>. I sure wish I could have some extra time to pick this up again, but getting some sleep at night is nice too once in a while. I noticed that OO databases and document/key-value stores are getting more and more traction lately and I must say that its about time.</p>
<p><a href="http://blog.wekeroad.com">Rob Conery</a> hits the nail right on the head in his post on <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql">Reporting in NoSQL</a>.</p>
<blockquote><p><em>Put as gently as I can – </em><a href="http://blogs.computerworld.com/15510/the_end_of_sql_and_relational_databases_part_1_of_3"><em>relational systems are an answer to a problem that we faced 30 years ago</em></a><em>. What you’re doing now is nothing other than compensating for a lack of imagination from the platform developers. Think about it – we code using Object Oriented approaches, we store those objects in a relational system.</em></p>
</blockquote>
<p>What we should all learn in this industry is to <strong>stop assuming that a relational database is the default option</strong> for storing the data of every solution we build. This is what we have been doing for a long time and its pure madness, plain and simple.</p>
<blockquote><p><em>RDBMS don’t fit for holding your application’s data, and they don’t fit for reporting. They’re a solution for a problem that doesn’t exist anymore. Time to kick them to the curb.</em></p>
</blockquote>
<p>The most typical setup you see is a single relational database that is used for both storing the data of an application as well as reporting from this data. The relational schema usually sits between normalized and denormalized tables, which means having a compromise for both needs. You can get away with this for small to medium-sized applications, but when you start working on mission-critical solutions with higher volumes, this compromise isn’t going to cut it anymore. This is why <a href="http://codebetter.com/blogs/gregyoung/">Greg Young</a>, <a href="http://www.udidahan.com/">Udi Dahan</a> and <a href="http://elegantcode.com/about/mark-nijhof/">Mark Nijhof</a> amongst others are advocating command query separation. For these kind of solutions, you want to have the best option for handling commands, which could be an OO database or a document/key-value store (with or without <a href="http://elegantcode.com/2010/02/05/cqrs-event-sourcing/">event-sourcing</a>) and for reporting you’d want the best option available as well like an OLAP system. What I’m describing here is just the elevator pitch, so if you want to learn more about this then do checkout the resources that these gentlemen mentioned above have already put on their blogs.</p>
<p>I hope that one day we realize that a relational database was just a means for optimizing file storage, which is hardly a need anymore these days. We shouldn’t be struggling with how to solve the impedance mismatch between relational databases and OO programming in any kind of application. The one thing we should care about is how to provide solid and clean solutions to our businesses without having to worry about tables and those zealots with their holy database schemas. Just store the objects you want and worry about other things like the so-called ‘<a href="http://www.codesqueeze.com/the-7-software-ilities-you-need-to-know/">ilities</a>’ and being able to respond to business needs in a timely manner.&#160; </p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/12/is-nosql-finally-going-mainstream/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>CQRS &#8211; Event Versioning</title>
		<link>http://elegantcode.com/2010/02/09/cqrs-event-versioning/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cqrs-event-versioning</link>
		<comments>http://elegantcode.com/2010/02/09/cqrs-event-versioning/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 23:37:28 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[CQRS]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Event Sourcing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/09/cqrs-event-versioning/</guid>
		<description><![CDATA[When using Event Sourcing you store your events in an Event Store. This Event Store can only insert new events and read historical events, nothing more nothing less. So when you change your domain logic and also the events belonging to this behavior, then you cannot go back into the Event Store and do a [...]]]></description>
			<content:encoded><![CDATA[<p>When using Event Sourcing you store your events in an Event Store. This Event Store can only insert new events and read historical events, nothing more nothing less. So when you change your domain logic and also the events belonging to this behavior, then you cannot go back into the Event Store and do a one time convert of all the historical events belonging to the same behavior. The Event Store needs to stay intact, that is one of its powers.</p>
<p>So you make a new version of the original event, this new version carries more or less information then the original one. Lets take a look at a very simple example:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Events<span style="color: aqua">.</span>Account</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Serializable</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">CashWithdrawnEvent</span> : <span style="color: #2b91af">DomainEvent</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">decimal</span> Balance { <span style="color: #0080c0">get</span>; <span style="color: #0080c0">private</span> <span style="color: #0080c0">set</span>; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">decimal</span> Amount { <span style="color: #0080c0">get</span>; <span style="color: #0080c0">private</span> <span style="color: #0080c0">set</span>; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> CashWithdrawnEvent(<span style="color: #0080c0">decimal</span> balance, <span style="color: #0080c0">decimal</span> amount)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Balance <span style="color: aqua">=</span> balance;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Amount <span style="color: aqua">=</span> amount;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Serializable</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">CashWithdrawnEvent_v2</span> : <span style="color: #2b91af">DomainEvent</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">decimal</span> Balance { <span style="color: #0080c0">get</span>; <span style="color: #0080c0">private</span> <span style="color: #0080c0">set</span>; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">decimal</span> Amount { <span style="color: #0080c0">get</span>; <span style="color: #0080c0">private</span> <span style="color: #0080c0">set</span>; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">Guid</span> AtmId { <span style="color: #0080c0">get</span>; <span style="color: #0080c0">private</span> <span style="color: #0080c0">set</span>; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> CashWithdrawnEvent_v2(<span style="color: #0080c0">decimal</span> balance, <span style="color: #0080c0">decimal</span> amount, <span style="color: #0080c0">Guid</span> atmId)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Balance <span style="color: aqua">=</span> balance;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Amount <span style="color: aqua">=</span> amount;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AtmId <span style="color: aqua">=</span> atmId;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span> }</pre>
</div>
<p>This to me looks like a natural evolution for this type of event, so how do you deal with this. Because after having used the system, before adding this extension there have been many cash withdrawals. So all these events are in the Event Store, they cannot be altered, and when you retrieve an Aggregate Root from the Event Store all these historical events need to be processed in order to restore the internal state.</p>
<p>Now what you don’t want is to maintain code in the Aggregate Root that knows how to handle these old event versions, sure one version is ok, but what about one hundred different versions? Also we are not just talking about just in the Aggregate Root, also the different event handlers need to be kept and maintained.</p>
<p>The better approach is to have a mechanism that you can hook-up with different event convertors. Then when an event is retrieved from the Event Store it first goes through this pipeline of convertors to be converted to the latest event version.</p>
<p>Now I wanted to do this properly and write some actual code for this, and then blog about it, but someone kept nagging me about it, so here is a very rough spike instead, first some tests:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Spike</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">Spike_test_1</span> : <span style="color: #2b91af">BaseTestFixture</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">object</span> ConvertedEvent;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> EventConvertor()<span style="color: aqua">.</span>Convert(<span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEvent</span>(<span style="color: yellow">10.0M</span>, <span style="color: yellow">20.0M</span>));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> The_converted_event_is_the_latest_version()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>WillBeOfType<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> The_converted_event_wil_contain_the_correct_data()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Balance<span style="color: aqua">.</span>WillBe(<span style="color: yellow">10.0M</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Amount<span style="color: aqua">.</span>WillBe(<span style="color: yellow">20.0M</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>AtmId<span style="color: aqua">.</span>WillBe(<span style="color: #0080c0">string</span><span style="color: aqua">.</span>Empty);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">Spike_test_2</span> : <span style="color: #2b91af">BaseTestFixture</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">object</span> ConvertedEvent;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> EventConvertor()<span style="color: aqua">.</span>Convert(<span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEvent_v2</span>(<span style="color: yellow">10.0M</span>, <span style="color: yellow">20.0M</span>, <span style="color: #a31515">&quot;12345&quot;</span>));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> The_converted_event_is_the_latest_version()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>WillBeOfType<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 41</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> The_converted_event_wil_contain_the_correct_data()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Balance<span style="color: aqua">.</span>WillBe(<span style="color: yellow">10.0M</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Amount<span style="color: aqua">.</span>WillBe(<span style="color: yellow">20.0M</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>AtmId<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;12345&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 49</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 50</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 51</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">Spike_test_3</span> : <span style="color: #2b91af">BaseTestFixture</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 52</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">object</span> ConvertedEvent;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 54</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> EventConvertor()<span style="color: aqua">.</span>Convert(<span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEvent_v3</span>(<span style="color: yellow">10.0M</span>, <span style="color: yellow">20.0M</span>, <span style="color: #a31515">&quot;12345&quot;</span>));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 59</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [Then]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> The_converted_event_is_the_latest_version()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>WillBeOfType<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 65</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [Then]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> The_converted_event_wil_contain_the_correct_data()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 68</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Balance<span style="color: aqua">.</span>WillBe(<span style="color: yellow">10.0M</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Amount<span style="color: aqua">.</span>WillBe(<span style="color: yellow">20.0M</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>AtmId<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;12345&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 73</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 74</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 75</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">Spike_test_4</span> : <span style="color: #2b91af">BaseTestFixture</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 76</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">object</span> ConvertedEvent;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 78</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 80</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 81</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> EventConvertor()<span style="color: aqua">.</span>Convert(<span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEvent_v4</span>(<span style="color: yellow">10.0M</span>, <span style="color: yellow">20.0M</span>, <span style="color: #a31515">&quot;12345&quot;</span>));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 82</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 83</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 84</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [Then]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 85</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> The_converted_event_is_the_latest_version()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 87</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>WillBeOfType<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 88</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 89</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 90</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 91</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> The_converted_event_wil_contain_the_correct_data()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 92</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 93</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Balance<span style="color: aqua">.</span>WillBe(<span style="color: yellow">10.0M</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 94</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Amount<span style="color: aqua">.</span>WillBe(<span style="color: yellow">20.0M</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 95</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConvertedEvent<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>AtmId<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;12345&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 96</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 97</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 98</span> }</pre>
</div>
<p>So basically some tests to confirm the correct conversion from one event to another event, now below here is the full implementation:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Spike</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">EventConvertor</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #2b91af">Func</span><span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;&gt;</span> _convertors;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> EventConvertor()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _convertors <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #2b91af">Func</span><span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RegisterEventConvertors();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> RegisterEventConvertors()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _convertors<span style="color: aqua">.</span>Add(<span style="color: #0080c0">typeof</span>(<span style="color: #2b91af">CashWithdrawnEvent</span>), x <span style="color: aqua">=&gt;</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEventConvertor</span>()<span style="color: aqua">.</span>Convert((<span style="color: #2b91af">CashWithdrawnEvent</span>)x));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _convertors<span style="color: aqua">.</span>Add(<span style="color: #0080c0">typeof</span>(<span style="color: #2b91af">CashWithdrawnEvent_v2</span>), x <span style="color: aqua">=&gt;</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEvent_v2Convertor</span>()<span style="color: aqua">.</span>Convert((<span style="color: #2b91af">CashWithdrawnEvent_v2</span>)x));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _convertors<span style="color: aqua">.</span>Add(<span style="color: #0080c0">typeof</span>(<span style="color: #2b91af">CashWithdrawnEvent_v3</span>), x <span style="color: aqua">=&gt;</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEvent_v3Convertor</span>()<span style="color: aqua">.</span>Convert((<span style="color: #2b91af">CashWithdrawnEvent_v3</span>)x));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">object</span> Convert(<span style="color: #0080c0">object</span> soureEvent)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Func</span><span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> convertor;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> _convertors<span style="color: aqua">.</span>TryGetValue(soureEvent<span style="color: aqua">.</span>GetType(), <span style="color: #0080c0">out</span> convertor) </pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">?</span> Convert(convertor(soureEvent)) </pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : soureEvent;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">interface</span> <span style="color: #2b91af">IEventConvertor</span><span style="color: aqua">&lt;</span>TSourceEvent, TTargetEvent<span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">where</span> TSourceEvent : <span style="color: #2b91af">IDomainEvent</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">where</span> TTargetEvent : <span style="color: #2b91af">IDomainEvent</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; TTargetEvent Convert(TSourceEvent sourceEvent);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">CashWithdrawnEventConvertor</span> : <span style="color: #2b91af">IEventConvertor</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent</span>, <span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">CashWithdrawnEvent_v4</span> Convert(<span style="color: #2b91af">CashWithdrawnEvent</span> sourceEvent)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> theEvent <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEvent_v4</span>(sourceEvent<span style="color: aqua">.</span>Balance, sourceEvent<span style="color: aqua">.</span>Amount, <span style="color: #0080c0">string</span><span style="color: aqua">.</span>Empty)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AggregateId <span style="color: aqua">=</span> sourceEvent<span style="color: aqua">.</span>AggregateId</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (theEvent <span style="color: #0080c0">as</span> <span style="color: #2b91af">IDomainEvent</span>)<span style="color: aqua">.</span>Version <span style="color: aqua">=</span> (sourceEvent <span style="color: #0080c0">as</span> <span style="color: #2b91af">IDomainEvent</span>)<span style="color: aqua">.</span>Version;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> theEvent;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 47</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 48</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 49</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">CashWithdrawnEvent_v2Convertor</span> : <span style="color: #2b91af">IEventConvertor</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v2</span>, <span style="color: #2b91af">CashWithdrawnEvent_v3</span><span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 50</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">CashWithdrawnEvent_v3</span> Convert(<span style="color: #2b91af">CashWithdrawnEvent_v2</span> sourceEvent)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> theEvent <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEvent_v3</span>(sourceEvent<span style="color: aqua">.</span>Balance, sourceEvent<span style="color: aqua">.</span>Amount, sourceEvent<span style="color: aqua">.</span>AtmId)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AggregateId <span style="color: aqua">=</span> sourceEvent<span style="color: aqua">.</span>AggregateId</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (theEvent <span style="color: #0080c0">as</span> <span style="color: #2b91af">IDomainEvent</span>)<span style="color: aqua">.</span>Version <span style="color: aqua">=</span> (sourceEvent <span style="color: #0080c0">as</span> <span style="color: #2b91af">IDomainEvent</span>)<span style="color: aqua">.</span>Version;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> theEvent;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 60</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 61</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 62</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">CashWithdrawnEvent_v3Convertor</span> : <span style="color: #2b91af">IEventConvertor</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">CashWithdrawnEvent_v3</span>, <span style="color: #2b91af">CashWithdrawnEvent_v4</span><span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 63</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">CashWithdrawnEvent_v4</span> Convert(<span style="color: #2b91af">CashWithdrawnEvent_v3</span> sourceEvent)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> theEvent <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEvent_v4</span>(sourceEvent<span style="color: aqua">.</span>Balance, sourceEvent<span style="color: aqua">.</span>Amount, sourceEvent<span style="color: aqua">.</span>AtmId)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 68</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AggregateId <span style="color: aqua">=</span> sourceEvent<span style="color: aqua">.</span>AggregateId</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (theEvent <span style="color: #0080c0">as</span> <span style="color: #2b91af">IDomainEvent</span>)<span style="color: aqua">.</span>Version <span style="color: aqua">=</span> (sourceEvent <span style="color: #0080c0">as</span> <span style="color: #2b91af">IDomainEvent</span>)<span style="color: aqua">.</span>Version;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> theEvent;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 73</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 74</span> }</pre>
</div>
<p>This implementation is definitely not very elegant (so it doesn’t really belong on this blog) but hey it does show you how a possible solution would work. When building this yourself you might want to use conventions to auto register the convertors and chain them together during configuration so there is no need for the recursive functionality.</p>
<p>Also look at the jump from version 1 to version 4, this is an optimization to speed up the conversion. You would do this after a few versions, not for each version.</p>
<p>I’ll be adding a proper solution to the example in the near future, something that you would just plug the convertors in and the system would figure out how to handle them itself.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/09/cqrs-event-versioning/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>CQRS &#8211; Scalability</title>
		<link>http://elegantcode.com/2010/02/09/cqrs-scalability/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cqrs-scalability</link>
		<comments>http://elegantcode.com/2010/02/09/cqrs-scalability/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 19:22:49 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[CQRS]]></category>
		<category><![CDATA[DDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/09/cqrs-scalability/</guid>
		<description><![CDATA[Scalability is one of the several different benefits you gain from applying CQRS and Event Sourcing to your application architecture. And that is what I wanted to take a closer look at in this post. One of the first obvious ways to increase the performance of your system is to split the Command from the [...]]]></description>
			<content:encoded><![CDATA[<p>Scalability is one of the several different benefits you gain from applying CQRS and Event Sourcing to your application architecture. And that is what I wanted to take a closer look at in this post.</p>
<p>One of the first obvious ways to increase the performance of your system is to split the Command from the Query side by using a service bus or just a simple queue. So instead of one machine being responsible for both responsibilities you now have 2 machines.</p>
<p>Then you also get the ability to measure more precisely what the actual bottleneck is, normally your application queries for data many times more then that it is executing behavior. And there are already many well known approaches for scaling your reporting database so that is not what I would like to talk about, but I guess it is clear that this side can now be scaled-out individually from the command side.</p>
<p>Now the command side; because even if this is used many times less then the query side, the actual behavior may take much more time and or processing power. </p>
<p>There are two natural ways of splitting up the domain, the first one is by Aggregate Root type, so you can choose to place a single (or multiple) Aggregate Root type on a different server. The second way is by splitting-up a single Aggregate Root type by the Identity each individual instance has. Think of for example about splitting them up depending on the last number of the Id, even versus un-even.</p>
<p>So who decides? The decision needs to be made by the process that accepts the commands and passes them on to the command handlers, these command handlers can then be running on different machines and each machine may even have it’s own Event Store.</p>
<p>This is a very flexible way of splitting-up your system into different components, and because of the Event Driven Architecture basically build-into this approach you will be able to trigger other behavior on different Aggregate Roots without added trouble.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/09/cqrs-scalability/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CQRS &#8211; Event Sourcing</title>
		<link>http://elegantcode.com/2010/02/05/cqrs-event-sourcing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cqrs-event-sourcing</link>
		<comments>http://elegantcode.com/2010/02/05/cqrs-event-sourcing/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 17:02:48 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[CQRS]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Event Sourcing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/05/cqrs-event-sourcing/</guid>
		<description><![CDATA[My new blog is at at http://cre8ivethought.com/blog/index come and follow me there. -Mark Nijhof So after reading this blog post by Rob Conery about Reporting In NoSQL where he explains very well what the problem is when using a RDBMS for persisting the state of your domain, or really anything that is written with Object [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>My new blog is at at <a href="http://cre8ivethought.com/blog/index">http://cre8ivethought.com/blog/index</a> come and follow me there.</p>
<p>-Mark Nijhof</em></p>
</blockquote>
<p>So after reading this blog post by Rob Conery about <a href="http://blog.wekeroad.com/2010/02/05/reporting-in-nosql" target="_blank">Reporting In NoSQL</a> where he explains very well what the problem is when using a RDBMS for persisting the state of your domain, or really anything that is written with Object Orientation in mind.</p>
<p>His solution to the problem is to use a object database or a document database for persisting the state of your object structure. And I do agree that this is a valuable approach to solve the problem.</p>
<p>But I would like to talk about a different approach, which is called <a href="http://martinfowler.com/eaaDev/EventSourcing.html" target="_blank">Event Sourcing</a>; a pattern thought of by Martin Fowler that, “<i>Captures all changes to an application state as a sequence of events.</i>”</p>
<p><img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="chipper" border="0" alt="chipper" align="right" src="http://elegantcode.com/wp-content/uploads/2010/02/chipper.jpg" width="240" height="159" />Hey that is interesting, so instead of trying to store the object tree in a whole, we just store all the individual state changes that the object tree encapsulates; meaning all the state changes that have happened during the complete lifetime of the object tree. These state changes are being represented in the form of events. And such an event is nothing more then a <a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object" target="_blank">Plain Old CLR Object</a>, so not an actual .Net event.</p>
<p>And the objects are also re-constructed from the same events by applying all the state changes that they represent, and thus coming back to the previous state in the identical way that it came there originally.</p>
<p>Now the interesting part with respect to persistence is that these events are being serialized using a technique you like (Binary, JSon or custom) and this serialized event (object) is persisted in an event store, and this event store will threat all serialized events equally. </p>
<p>This event store can be based on an object database, document database, file system or even a RDBMS, you basically need to have one collection that describes all the different objects that the event store has persisted this includes the Id and the Version. Then another collection will contain all the serialized events for each different object and they should be retrievable by the object Id ordered by their Version. So to simplify this, in a RDBMS this would mean 2 tables, in total.</p>
<p>So there is no impendence mismatch between the domain (object structure) and the persistence layer anymore. Which would mean that it is should pass Rob’s criteria&#8217;s as well.</p>
<p>But hey it doesn’t stop there, you get a real <a href="http://martinfowler.com/eaaDev/AuditLog.html" target="_blank">audit log</a> for free a well. And you have the ability to replay all the events to abstract new information about certain state changes. For example a web store is in business for 6 months and now they would like to know when and where an item is being removed from the shopping cart. With an event store you will have this information from the start of the system, and you didn’t have to think about it straight from the start.</p>
<p>And this also enables an easier swift to an Event Driven Architecture as well, because you can start publishing the events outside of the domain and have different behavior or processes react on them.</p>
<p>In my <a href="http://github.com/MarkNijhof/Fohjin/tree/master/Fohjin.DDD.Example/" target="_blank">example</a> I use a combination of CQRS and Event Sourcing and this makes a very powerful solution. I would recommend that when you are when applying CQRS you would do that in combination with Event Sourcing to get a very flexible system without much more complexity then just applying CQRS.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/05/cqrs-event-sourcing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>A web store using FubuMVC and CQRS</title>
		<link>http://elegantcode.com/2010/01/04/a-web-store-using-fubumvc-and-cqrs/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=a-web-store-using-fubumvc-and-cqrs</link>
		<comments>http://elegantcode.com/2010/01/04/a-web-store-using-fubumvc-and-cqrs/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 20:47:08 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[CQRS]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[FubuMVC]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/04/a-web-store-using-fubumvc-and-cqrs/</guid>
		<description><![CDATA[My new blog is at at http://cre8ivethought.com/blog/index come and follow me there. -Mark Nijhof My wife wants to start a web store selling all kinds of children products like clothing, toys and perhaps some small furniture. So there is a need for an internet store where she can list these products and try to sell [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>My new blog is at at <a href="http://cre8ivethought.com/blog/index">http://cre8ivethought.com/blog/index</a> come and follow me there.</p>
<p>-Mark Nijhof</em></p>
</blockquote>
<p>My wife wants to start a web store selling all kinds of children products like clothing, toys and perhaps some small furniture. So there is a need for an internet store where she can list these products and try to sell them. </p>
<p>I guess the normal procedure would be to try and find a service/product that enables her to do just that, but she married the wrong guy for that, at least until I have to declare defeat.</p>
<p>Anyway I thought that combining my wife’s needs and my own would be an ideal solution, again at least for me, we’ll see what she thinks about it after a while. So what are my needs?</p>
<p><em>Update: Ok to be more fair, I am using my wife’s ambition to open a web store to create a project for myself. It might be so that we will be using it for her store, but there is a very high likelihood that we will be renting an existing service instead. Even if it was just to not have it get between our marriage <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  but as I said I will build it as if it will be used by her.</em></p>
<h2>FubuMVC – v.Reboot</h2>
<p>As some of you (or most I actually don’t know my target audience good enough to make these statements) may already know Jeremy Miller has been putting a lot (read amazing amount) of work into FubuMVC replacing/rewriting parts of the original FubuMVC code base that Chad Myers initiated from lessons learned. I encourage you to read this <a href="http://codebetter.com/blogs/jeremy.miller/archive/2009/12/15/an-update-on-the-fubumvc-reboot.aspx" target="_blank">first</a> and <a href="http://codebetter.com/blogs/jeremy.miller/archive/2010/01/04/fubumvc-diagnostics-sneak-peek.aspx" target="_blank">second</a> post about this reboot from Jeremy to learn more about it.</p>
<p>But all this does mean that I have to dig deep into the FubuMVC code again and see what has changed and how it now works, sure most of the basic principles are still true, but especially on the configuration side many things have changed. Also the behavior chain as the controller is now an option that I am looking forward to be using. And hopefully I’ll be able to add my own pet peeve; behavior chains for partials even further separating responsibilities.</p>
<h2>DDD using CQRS and Event Sourcing</h2>
<p>And I want to incorporate CQRS using Event Sourcing into this as well, but not just that I’ll be using this to drive the development of my PI solution for this approach. You may read some initial thoughts about that <a href="http://elegantcode.com/2009/11/28/cqrs-trying-to-make-it-re-usable/" target="_blank">here</a>. Nothing beats developing something then driven by actual usage. I am also thinking about incorporating Udi Dahan’s <a href="http://www.udidahan.com/2009/12/09/clarified-cqrs/" target="_blank">thoughts</a> into this by not making my aggregate roots into huge tree structures, but by keeping it as flat as possible.</p>
<h2>Sharing by blogging and OSS</h2>
<p>I am building this for myself, for my wife really, anyway those are the specifications what I’ll be fulfilling (and yes I am going to try to make this as real as I can). Perhaps it is also time for me to start trying MSpec, but on the other hand I really like to try out <a href="http://gojko.net/2010/01/01/bdd-in-net-with-cucumber-cuke4nuke-and-teamcity/" target="_blank">Cucumber using Duke4Nuke and TeamCity</a>. It would be great if I could get a public build server for this (CodeBetter?).</p>
<p>Anyway I want to make this completely in the open by providing all the source code via <a href="http://github.com/MarkNijhof/Fohjin" target="_blank">my GitHub account</a> and also blog about the whole process. These posts would be covering everything, so from project setup with FubuMVC till some specifications and all the things that go into the creation of my PI CQRS solution (to early to call it a framework yet).</p>
<p>Now together with writing my book and many other things you should not expect a high speed project where this is build in no-time. So patience is a good thing here.</p>
<h2>So?</h2>
<p>Well I would like your feedback about this, what do you think about this approach? Also I would need a name for the project, both for the web store and for the PI CQRS solution. I hope you can help me with that, as I am very bad with names (we are currently looking into children names again and my wife will testify that I am indeed very bad at this).</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/04/a-web-store-using-fubumvc-and-cqrs/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Specifications</title>
		<link>http://elegantcode.com/2009/12/22/specifications/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=specifications</link>
		<comments>http://elegantcode.com/2009/12/22/specifications/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 03:30:50 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[Conventions]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/22/specifications/</guid>
		<description><![CDATA[My new blog is at at http://cre8ivethought.com/blog/index come and follow me there. -Mark Nijhof I received a couple questions about the Specification Framework that I use in the CQRS example and thought lets talk about that for a bit. The first thing that should be underlined is that this is _not_ a framework, they are [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>My new blog is at at <a href="http://cre8ivethought.com/blog/index">http://cre8ivethought.com/blog/index</a> come and follow me there.</p>
<p>-Mark Nijhof</em></p>
</blockquote>
<p>I received a couple questions about the Specification Framework that I use in the <a href="http://github.com/MarkNijhof/Fohjin/tree/f85a25181b4fa382bd3afbbbbcb08da891cc8e45/Fohjin.DDD.Example" target="_blank">CQRS example</a> and thought lets talk about that for a bit. The first thing that should be underlined is that this is _not_ a framework, they are a few <a href="http://github.com/MarkNijhof/Fohjin/tree/f85a25181b4fa382bd3afbbbbcb08da891cc8e45/Fohjin.DDD.Example/Test.Fohjin.DDD" target="_blank">classes and extension methods</a> that rely on <a href="http://www.nunit.org/index.php" target="_blank">NUnit</a> for the actual assertions and and <a href="http://code.google.com/p/moq/" target="_blank">Moq</a> for mocking of the dependencies. I got the initial bits from Greg Young at his DDD course which I extended a little bit for my specific needs. </p>
<p>I have the following base test fixture classes:</p>
<ul>
<li>BaseTestFixture </li>
<li>BaseTestFixture&lt;TSubjectUnderTest&gt; </li>
<li>AggregateRootTestFixture&lt;TAggregateRoot&gt; </li>
<li>CommandTestFixture&lt;TCommand, TCommandHandler, TAggregateRoot&gt; </li>
<li>EventTestFixture&lt;TEvent, TEventHandler&gt; </li>
<li>PresenterTestFixture&lt;TPresenter&gt; </li>
</ul>
<p>These different classes are all very specific towards a specific need, which is a direct opposite from what a framework usually provides. </p>
<h2>Black box</h2>
<p>I try my best to make my tests treat the subject under test (SUT) as a black box, meaning that in my tests I don’t directly interact with the actual class that I am testing. Instead I want to trigger the behavior by executing behavior that lies further outside. The behavior that triggers the behavior on the SUT may be an actual implementation, or it could be a fake. </p>
<p>The same applies to the result of the behavior that gets tested, instead of verifying some state in the SUT I want to verify what happens outside of the SUT. So what matters is that I test the behavior not the state of the subject under test.</p>
<p>I also try to get further away from the SUT then its immediate usage. Doing so makes the tests less brittle for change. This in itself is not always an easy task, but I recommend you try it anyway. </p>
<h2>The BaseTestFixture</h2>
<p>This is the simplest test fixture class that I have and I use this for and I actually don’t use this anywhere in the example code, but it serves a really good basic overview of the semantics that are shared among the other test fixture classes. </p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Specification</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">abstract</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">BaseTestFixture</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">Exception</span> CaughtException;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Given() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">abstract</span> <span style="color: #0080c0">void</span> When();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Finally() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Given</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Setup()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">ThereWasNoExceptionButOneWasExpectedException</span>();&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Given();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">try</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; When();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">catch</span> (<span style="color: #2b91af">Exception</span> exception)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> exception;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">finally</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Finally();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">GivenAttribute</span> : <span style="color: #2b91af">SetUpAttribute</span> { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">ThenAttribute</span> : <span style="color: #2b91af">TestAttribute</span> { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">SpecificationAttribute</span> : <span style="color: #2b91af">TestFixtureAttribute</span> { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span> }</pre>
</div>
<p></p>
<p>It is following the Given When Then (GWT) approach, and as you can see it is really simple. Also note that I introduced some other named attributes by simply inheriting from the default NUnit attributes, this was purely done to stay with the GWT approach.</p>
<p>Below here is an incredible KISS example of how you would use this BaseTestFixture, which I believe doesn’t need further explanation. (I know I am misusing the term KISS here, but I thought if was fitting anyway).</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">Very_adding_a_number_to_the_subject_under_test</span> : <span style="color: #2b91af">BaseTestFixture</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">int</span> SubjectUnderTest;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> Given()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SubjectUnderTest <span style="color: aqua">=</span> <span style="color: yellow">2</span>;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SubjectUnderTest <span style="color: aqua">+=</span> <span style="color: yellow">2</span>;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_the_new_value_of_the_subject_under_test_will_be_4()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SubjectUnderTest<span style="color: aqua">.</span>WillBe(<span style="color: yellow">4</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span> }</pre>
</div>
<h2>The BaseTestFixture&lt;TSubjectUnderTest&gt;</h2>
<p>Now we are getting into a more interesting case because now my subject under test is actually provided by the generic parameter of the base test fixture class. And to be honest this class is only used with 12 specifications out of the 122 specification classes. This is mostly because it is still a very generic solution, but again a nice way to ease into it.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Specification</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">abstract</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">BaseTestFixture</span><span style="color: aqua">&lt;</span>TSubjectUnderTest<span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> DoNotMock;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> TSubjectUnderTest SubjectUnderTest;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">Exception</span> CaughtException;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> SetupDependencies() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Given() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">abstract</span> <span style="color: #0080c0">void</span> When();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Finally() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Given</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Setup()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mocks <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DoNotMock <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">ThereWasNoExceptionButOneWasExpectedException</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; BuildMocks();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SetupDependencies();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SubjectUnderTest <span style="color: aqua">=</span> BuildSubjectUnderTest();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Given();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">try</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; When();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">catch</span> (<span style="color: #2b91af">Exception</span> exception)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> exception;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">finally</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Finally();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 42</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> OnDependency<span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>() <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 47</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> TSubjectUnderTest BuildSubjectUnderTest()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> constructorInfo <span style="color: aqua">=</span> <span style="color: #0080c0">typeof</span>(TSubjectUnderTest)<span style="color: aqua">.</span>GetConstructors()<span style="color: aqua">.</span>First();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 51</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> parameters <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">List</span><span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">foreach</span> (<span style="color: #0080c0">var</span> mock <span style="color: #0080c0">in</span> mocks)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">object</span> theObject;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>DoNotMock<span style="color: aqua">.</span>TryGetValue(mock<span style="color: aqua">.</span>Key, <span style="color: #0080c0">out</span> theObject))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; theObject <span style="color: aqua">=</span> ((<span style="color: #2b91af">Mock</span>) mock<span style="color: aqua">.</span>Value)<span style="color: aqua">.</span>Object;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 58</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; parameters<span style="color: aqua">.</span>Add(theObject);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 61</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> (TSubjectUnderTest)constructorInfo<span style="color: aqua">.</span>Invoke(parameters<span style="color: aqua">.</span>ToArray());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 64</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> BuildMocks()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> constructorInfo <span style="color: aqua">=</span> <span style="color: #0080c0">typeof</span>(TSubjectUnderTest)<span style="color: aqua">.</span>GetConstructors()<span style="color: aqua">.</span>First();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 68</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">foreach</span> (<span style="color: #0080c0">var</span> parameter <span style="color: #0080c0">in</span> constructorInfo<span style="color: aqua">.</span>GetParameters())</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mocks<span style="color: aqua">.</span>Add(parameter<span style="color: aqua">.</span>ParameterType, CreateMock(parameter<span style="color: aqua">.</span>ParameterType));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 73</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 74</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 75</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">static</span> <span style="color: #0080c0">object</span> CreateMock(<span style="color: #2b91af">Type</span> type)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> constructorInfo <span style="color: aqua">=</span> <span style="color: #0080c0">typeof</span>(<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;&gt;</span>)<span style="color: aqua">.</span>MakeGenericType(type)<span style="color: aqua">.</span>GetConstructors()<span style="color: aqua">.</span>First();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> constructorInfo<span style="color: aqua">.</span>Invoke(<span style="color: #0080c0">new</span> <span style="color: #0080c0">object</span>[] { });</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 80</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 81</span> }</pre>
</div>
<p></p>
<p>Wow there is _a lot_ more going on here! You are right, because here I make the base test fixture responsible for instantiating the subject under test, including providing mocks for any dependencies that it may have. So it is an auto mocker as well, but the interesting part here is that it puts a reference of the injected mocks in a collection that you can access inside your tests by using the OnDependency&lt;TType&gt; method that returns a Moq object.</p>
<p>Take a look at a specification using this base test fixture class:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Scenarios<span style="color: aqua">.</span>Receiving_money_transfer</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">When_receiving_a_money_transfer</span> : <span style="color: #2b91af">BaseTestFixture</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">MoneyReceiveService</span><span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> SetupDependencies()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OnDependency<span style="color: aqua">&lt;</span><span style="color: #2b91af">IReportingRepository</span><span style="color: aqua">&gt;</span>()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Setup(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>GetByExample<span style="color: aqua">&lt;</span><span style="color: #2b91af">AccountReport</span><span style="color: aqua">&gt;</span>(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>()))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Returns(<span style="color: #0080c0">new</span> <span style="color: #2b91af">List</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">AccountReport</span><span style="color: aqua">&gt;</span> { <span style="color: #0080c0">new</span> <span style="color: #2b91af">AccountReport</span>(<span style="color: #2b91af">Guid</span><span style="color: aqua">.</span>NewGuid(), <span style="color: #2b91af">Guid</span><span style="color: aqua">.</span>NewGuid(), <span style="color: #a31515">&quot;AccountName&quot;</span>, <span style="color: #a31515">&quot;target account number&quot;</span>) });</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SubjectUnderTest<span style="color: aqua">.</span>Receive(<span style="color: #0080c0">new</span> <span style="color: #2b91af">MoneyTransfer</span>(<span style="color: #a31515">&quot;source account number&quot;</span>, <span style="color: #a31515">&quot;target account number&quot;</span>, <span style="color: yellow">123.45M</span>));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_the_newly_created_account_will_be_saved()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OnDependency<span style="color: aqua">&lt;</span><span style="color: #2b91af">IBus</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Verify(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Publish(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span><span style="color: #2b91af">ReceiveMoneyTransferCommand</span><span style="color: aqua">&gt;</span>()));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span> }</pre>
</div>
<p></p>
<p>So the first thing you see is the method SetupDependencies that is requesting the mock object for injected type IReporintgRepository and it is using the Moq way of setting up the Mock object. This is only needed when in your specification you need the mocks to be setup in a specific way. I intentionally separated the SetupDependencies from the Given as they may be two different things. And in the actual test you see the usage of the OnDependency method again where its being used to verify that something was indeed triggered on the injected class.</p>
<p>Now indeed this is not really treating the subject under test as a black box, for example in the When I make a direct call to a method on the subject under test. So my test knows about this now, meaning when it changes I need to change this test as well. So lets take a look where I go a bit further into the black box mentality.</p>
<h2>The PresenterTestFixture&lt;TPresenter&gt;</h2>
<p>Here I am not going to show you the code of the PresenterTestFixture implementation as it is almost identical the the previous base test fixture. So lets go straight to an actual specification:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Scenarios<span style="color: aqua">.</span>Adding_a_new_client</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">When_in_the_GUI_the_phone_number_of_the_new_client_is_saved</span> : <span style="color: #2b91af">PresenterTestFixture</span><span style="color: aqua">&lt;</span>ClientDetailsPresenter<span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">object</span> CreateClientCommand;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> SetupDependencies()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OnDependency<span style="color: aqua">&lt;</span>IPopupPresenter<span style="color: aqua">&gt;</span>()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Setup(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>CatchPossibleException(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span><span style="color: #2b91af">Action</span><span style="color: aqua">&gt;</span>()))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Callback<span style="color: aqua">&lt;</span><span style="color: #2b91af">Action</span><span style="color: aqua">&gt;</span>(x <span style="color: aqua">=&gt;</span> x());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OnDependency<span style="color: aqua">&lt;</span>IBus<span style="color: aqua">&gt;</span>()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Setup(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Publish(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>()))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Callback<span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>(x <span style="color: aqua">=&gt;</span> CreateClientCommand <span style="color: aqua">=</span> x);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> Given()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Presenter<span style="color: aqua">.</span>SetClient(<span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Presenter<span style="color: aqua">.</span>Display();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>ClientName)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;New Client Name&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnFormElementGotChanged <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnSaveNewClientName <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Street)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;Street&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>StreetNumber)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;123&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>PostalCode)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;5000&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>City)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;Bergen&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnFormElementGotChanged <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnSaveNewAddress <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>PhoneNumber)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;1234567890&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnFormElementGotChanged <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnSaveNewPhoneNumber <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 41</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_the_save_button_will_be_disabled()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>VerifyThat<span style="color: aqua">.</span>Method(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>DisableSaveButton())<span style="color: aqua">.</span>WasCalled();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 47</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_a_create_client_command_with_all_collected_information_will_be_published()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IBus<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>VerifyThat<span style="color: aqua">.</span>Method(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Publish(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()))<span style="color: aqua">.</span>WasCalled();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 52</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ClientName<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;New Client Name&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Street<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;Street&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>StreetNumber<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;123&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>PostalCode<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;5000&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>City<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;Bergen&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>PhoneNumber<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;1234567890&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 60</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_overview_panel_will_be_shown()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>VerifyThat<span style="color: aqua">.</span>Method(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Close())<span style="color: aqua">.</span>WasCalled();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 66</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 67</span> }</pre>
</div>
<p></p>
<p>Again there is the setting up of a dependency in the beginning and then there is the Given method, Presenter in this case is the subject under test, so you can see that the specification still knows about the SUT in the Given, but you will notice that this is not used in either the When or the Then.</p>
<p>Hey what is that On thing in there? Well that is a very small DSL wrapping the Moq API. I did this to make it slightly more readable, and in this case it is very adapt towards working with a view and presenter. In the Given I am setting up my IClientDetailsView with the correct data, but I am also simulating that an event was triggered. This is not logic that the test is concerned about, all we do here is bring the view and the presenter in the correct state for this particular specification. So instead of setting these things directly on the presenter this will all be directed from the view.</p>
<p>Then in the When we again fire an event from the view, but in this case it is the going to trigger the behavior on the presenter that we want to verify. And in the Then methods we verify on the view again that the presenter actually did the correct things, but we also verify on other dependencies that the correct methods where called, in this case the IBus.</p>
<p>I am not completely happy with the mini DSL yet, but I think it is cleaner then the default Moq API. Just for those that are curious, here is the mini DSL which gets returned by the On method:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">MockDsl</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> _mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> MockDsl(<span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _mocks <span style="color: aqua">=</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">ValueSetter</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;</span> ValueFor<span style="color: aqua">&lt;</span>TProperty<span style="color: aqua">&gt;</span>(<span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Func</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;&gt;</span> selector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">ValueSetter</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;</span>(_mocks, selector);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> FireEvent(<span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> fieldSelector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>_mocks<span style="color: aqua">.</span>ContainsKey(<span style="color: #0080c0">typeof</span>(TType)))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">throw</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Exception</span>(<span style="color: #0080c0">string</span><span style="color: aqua">.</span>Format(<span style="color: #a31515">&quot;The requested dependency '{0}' is not specified in the constructor&quot;</span>, <span style="color: #0080c0">typeof</span>(TType)<span style="color: aqua">.</span>FullName));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> mock <span style="color: aqua">=</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)_mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mock<span style="color: aqua">.</span>Raise(fieldSelector);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">Verifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> VerifyThat { <span style="color: #0080c0">get</span> { <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Verifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>(_mocks); } }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">Verifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> _mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> Verifier(<span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _mocks <span style="color: aqua">=</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> ValueIsSetFor(<span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> selector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>_mocks<span style="color: aqua">.</span>ContainsKey(<span style="color: #0080c0">typeof</span>(TType)))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">throw</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Exception</span>(<span style="color: #0080c0">string</span><span style="color: aqua">.</span>Format(<span style="color: #a31515">&quot;The requested dependency '{0}' is not specified in the constructor&quot;</span>, <span style="color: #0080c0">typeof</span>(TType)<span style="color: aqua">.</span>FullName));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 42</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> mock <span style="color: aqua">=</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)_mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mock<span style="color: aqua">.</span>VerifySet(selector);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 46</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">MethodVerifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> Method(<span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;&gt;</span> selector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">MethodVerifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>(_mocks, selector);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 51</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 52</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 53</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">MethodVerifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 54</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> _mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;&gt;</span> _fieldSelector;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 57</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> MethodVerifier(<span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks, <span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;&gt;</span> fieldSelector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _mocks <span style="color: aqua">=</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _fieldSelector <span style="color: aqua">=</span> fieldSelector;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 63</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> WasCalled()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>_mocks<span style="color: aqua">.</span>ContainsKey(<span style="color: #0080c0">typeof</span>(TType)))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">throw</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Exception</span>(<span style="color: #0080c0">string</span><span style="color: aqua">.</span>Format(<span style="color: #a31515">&quot;The requested dependency '{0}' is not specified in the constructor&quot;</span>, <span style="color: #0080c0">typeof</span>(TType)<span style="color: aqua">.</span>FullName));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 68</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> mock <span style="color: aqua">=</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)_mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mock<span style="color: aqua">.</span>Verify(_fieldSelector);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 72</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 73</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 74</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">ValueSetter</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;</span> <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 75</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> _mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Func</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;&gt;</span> _fieldSelector;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 78</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> ValueSetter(<span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks, <span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Func</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;&gt;</span> fieldSelector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 80</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 81</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _mocks <span style="color: aqua">=</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 82</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _fieldSelector <span style="color: aqua">=</span> fieldSelector;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 83</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 84</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 85</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> IsSetTo(TProperty value)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 87</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>_mocks<span style="color: aqua">.</span>ContainsKey(<span style="color: #0080c0">typeof</span>(TType)))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 88</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">throw</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Exception</span>(<span style="color: #0080c0">string</span><span style="color: aqua">.</span>Format(<span style="color: #a31515">&quot;The requested dependency '{0}' is not specified in the constructor&quot;</span>, <span style="color: #0080c0">typeof</span>(TType)<span style="color: aqua">.</span>FullName));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 89</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 90</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> mock <span style="color: aqua">=</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)_mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 91</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mock<span style="color: aqua">.</span>SetupGet(_fieldSelector)<span style="color: aqua">.</span>Returns(value);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 92</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 93</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 94</span> }</pre>
</div>
<p></p>
<h2>CQRS and Event Sourcing</h2>
<p>By combining <a href="http://elegantcode.com/2009/11/11/cqrs-la-greg-young/" target="_blank">CQRS and Event Sourcing</a> we get an architecture that is very suitable for black box testing its behavior, which was a real eye opener when Greg demonstrated this to me. He says that the way to bring your aggregate root back into the desired state is to playback the events that are needed to do so. Then you can execute the behavior on the aggregate root, and finally to actually verify your behavior you retrieve the published events and verify that they are as expected. </p>
<p>Now the beauty of this is that the setup and the verification this will work on any aggregate root because we are using the IEventProvider interface there that they all implement. The only actual knowledge about the aggregate root that remains is the specific behavior that you trigger.</p>
<p>But I went a little bit further then what was shown during the course, I am saying that instead of executing the behavior on the aggregate root we could also just provide the command that should trigger this behavior to be executed. Because the command would be handled by a specific command handler which in turn would execute the domain behavior.</p>
<p>Now below here is the command test fixture that allows me to do just that, I need to provide the actual command, command handler and aggregate root types that are to be used in this specification.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Specification</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">abstract</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">CommandTestFixture</span><span style="color: aqua">&lt;</span>TCommand, TCommandHandler, TAggregateRoot<span style="color: aqua">&gt;</span> </pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">where</span> TCommand : <span style="color: #0080c0">class</span>, ICommand</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">where</span> TCommandHandler : <span style="color: #0080c0">class</span>, ICommandHandler<span style="color: aqua">&lt;</span>TCommand<span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">where</span> TAggregateRoot : <span style="color: #0080c0">class</span>, IOrginator, IEventProvider<span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span>, <span style="color: #0080c0">new</span>()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> TAggregateRoot AggregateRoot;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> ICommandHandler<span style="color: aqua">&lt;</span>TCommand<span style="color: aqua">&gt;</span> CommandHandler;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">Exception</span> CaughtException;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span> PublishedEvents;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> SetupDependencies() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span> Given() </pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">List</span><span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Finally() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">abstract</span> TCommand When();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Given</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Setup()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mocks <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">ThereWasNoExceptionButOneWasExpectedException</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AggregateRoot <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> TAggregateRoot();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AggregateRoot<span style="color: aqua">.</span>LoadFromHistory(Given());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CommandHandler <span style="color: aqua">=</span> BuildCommandHandler();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SetupDependencies();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">try</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CommandHandler<span style="color: aqua">.</span>Execute(When());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PublishedEvents <span style="color: aqua">=</span> AggregateRoot<span style="color: aqua">.</span>GetChanges();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">catch</span> (<span style="color: #2b91af">Exception</span> exception)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> exception;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">finally</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Finally();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 48</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> OnDependency<span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>() <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 53</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> ICommandHandler<span style="color: aqua">&lt;</span>TCommand<span style="color: aqua">&gt;</span> BuildCommandHandler()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> constructorInfo <span style="color: aqua">=</span> <span style="color: #0080c0">typeof</span>(TCommandHandler)<span style="color: aqua">.</span>GetConstructors()<span style="color: aqua">.</span>First();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 57</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">foreach</span> (<span style="color: #0080c0">var</span> parameter <span style="color: #0080c0">in</span> constructorInfo<span style="color: aqua">.</span>GetParameters())</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (parameter<span style="color: aqua">.</span>ParameterType <span style="color: aqua">==</span> <span style="color: #0080c0">typeof</span>(IDomainRepository<span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span>))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> repositoryMock <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>IDomainRepository<span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; repositoryMock<span style="color: aqua">.</span>Setup(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>GetById<span style="color: aqua">&lt;</span>TAggregateRoot<span style="color: aqua">&gt;</span>(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span><span style="color: #2b91af">Guid</span><span style="color: aqua">&gt;</span>()))<span style="color: aqua">.</span>Returns(AggregateRoot);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; repositoryMock<span style="color: aqua">.</span>Setup(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Add(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span>TAggregateRoot<span style="color: aqua">&gt;</span>()))<span style="color: aqua">.</span>Callback<span style="color: aqua">&lt;</span>TAggregateRoot<span style="color: aqua">&gt;</span>(x <span style="color: aqua">=&gt;</span> AggregateRoot <span style="color: aqua">=</span> x);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mocks<span style="color: aqua">.</span>Add(parameter<span style="color: aqua">.</span>ParameterType, repositoryMock);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">continue</span>;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 68</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mocks<span style="color: aqua">.</span>Add(parameter<span style="color: aqua">.</span>ParameterType, CreateMock(parameter<span style="color: aqua">.</span>ParameterType));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 71</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> (ICommandHandler<span style="color: aqua">&lt;</span>TCommand<span style="color: aqua">&gt;</span>)constructorInfo<span style="color: aqua">.</span>Invoke(mocks<span style="color: aqua">.</span>Values<span style="color: aqua">.</span>Select(x <span style="color: aqua">=&gt;</span> ((<span style="color: #2b91af">Mock</span>) x)<span style="color: aqua">.</span>Object)<span style="color: aqua">.</span>ToArray());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 73</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 74</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 75</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">static</span> <span style="color: #0080c0">object</span> CreateMock(<span style="color: #2b91af">Type</span> type)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> constructorInfo <span style="color: aqua">=</span> <span style="color: #0080c0">typeof</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;&gt;</span>)<span style="color: aqua">.</span>MakeGenericType(type)<span style="color: aqua">.</span>GetConstructors()<span style="color: aqua">.</span>First();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> constructorInfo<span style="color: aqua">.</span>Invoke(<span style="color: #0080c0">new</span> <span style="color: #0080c0">object</span>[]{});</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 80</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 81</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 82</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">ThereWasNoExceptionButOneWasExpectedException</span> : <span style="color: #2b91af">Exception</span> {}</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 83</span> }</pre>
</div>
<p></p>
<p>Please note that the Given method now returns an IEnumerable&lt;IDomainEvent&gt; this is to be used to provide the events that are needed to bring the aggregate root into the correct state for this specification. This is using the exact same mechanism as the actual code uses to make state changes in the aggregate root, so there cannot be a case that you are testing your aggregate root using a state that it cannot get into.</p>
<p>The When method returns the expected command, so all you do there is create the command with the correct information and return it to the specification.</p>
<p>Then in the try catch block you may have noticed that a command handler is executing the provided command and that after that the events are being retrieved from the aggregate root. These events are what you would verify to make sure your domain behavior is correct. </p>
<p>But this may all sound very abstract, lets look at a simple specification and see how clean and readable is really is:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Scenarios<span style="color: aqua">.</span>Withdrawing_cash</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">When_withdrawing_cash</span> : <span style="color: #2b91af">CommandTestFixture</span><span style="color: aqua">&lt;</span>WithdrawlCashCommand, WithdrawlCashCommandHandler, ActiveAccount<span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span> Given()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">yield</span> <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> AccountOpenedEvent(<span style="color: #2b91af">Guid</span><span style="color: aqua">.</span>NewGuid(), <span style="color: #2b91af">Guid</span><span style="color: aqua">.</span>NewGuid(), <span style="color: #a31515">&quot;AccountName&quot;</span>, <span style="color: #a31515">&quot;1234567890&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">yield</span> <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> CashDepositedEvent(<span style="color: yellow">20</span>, <span style="color: yellow">20</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> WithdrawlCashCommand When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> WithdrawlCashCommand(<span style="color: #2b91af">Guid</span><span style="color: aqua">.</span>NewGuid(), <span style="color: yellow">5</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_a_cash_withdrawn_event_will_be_published()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PublishedEvents<span style="color: aqua">.</span>Last()<span style="color: aqua">.</span>WillBeOfType<span style="color: aqua">&lt;</span>CashWithdrawnEvent<span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_the_published_event_will_contain_the_amount_and_new_account_balance()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PublishedEvents<span style="color: aqua">.</span>Last<span style="color: aqua">&lt;</span>CashWithdrawnEvent<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Balance<span style="color: aqua">.</span>WillBe(<span style="color: yellow">15</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PublishedEvents<span style="color: aqua">.</span>Last<span style="color: aqua">&lt;</span>CashWithdrawnEvent<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Amount<span style="color: aqua">.</span>WillBe(<span style="color: yellow">5</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span> }</pre>
</div>
<p></p>
<p>So you provide historical events to bring the aggregate root into the expected state, you fire of the command, then you verify the published events to ensure your domain behavior is correct. If you choose the correct naming for your events and commands, then a business person would be able to understand the specification. Especially if you parse the text and do a little bit of formatting.</p>
<h2>Where is Should?</h2>
<p> Hey what are all those WillBe and WillBeOfType things that I see in your specifications, should they not be ShouldBe and ShouldBeOfType? Well I used to think so as well, until I attended a presentation by <a href="http://arkiv.ndc2009.no/agenda.aspx?cat=1071&amp;id=1813&amp;day=3728" target="_blank">Kevlin Henney at NDC</a> where he explained that Should is not specific enough. Should indicates that it might not. I like to use the example; “I should really do the dishes, but I won’t”. By using Will Be and Must you are much more dictating what will or must happen, its not a question anymore. </p>
<h2>Finally</h2>
<p>I am completely taken by this approach and as you see you don’t need a big BDD framework for this. I think using something like this gives a good learning experience before going towards an actual BDD framework like <a href="http://codebetter.com/blogs/aaron.jensen/archive/2008/05/08/introducing-machine-specifications-or-mspec-for-short.aspx" target="_blank">MSpec</a>. Also Uncle Bob just wrote a good post about to <a href="http://blog.objectmentor.com/articles/2009/12/19/the-polyglot-tester" target="_blank">not abuse the Given When Then approach</a> and also take a look at the <a href="http://martinfowler.com/articles/mocksArentStubs.html" target="_blank">Mocks aren’t Stubs</a> article by Martin Fowler.</p>
<p>By now you must &lt;grin&gt; understand that I like to throw with code examples so yes the post is very long, but I hope it provides enough value instead of just confusion. </p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/22/specifications/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>CQRS &#8211; Domain state</title>
		<link>http://elegantcode.com/2009/12/08/cqrs-domain-state/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cqrs-domain-state</link>
		<comments>http://elegantcode.com/2009/12/08/cqrs-domain-state/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 21:34:32 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[DDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/08/cqrs-domain-state/</guid>
		<description><![CDATA[This morning Aaron Jensen asked a really interesting question on Twitter “Should Aggregate Roots en Entities always keep their state if it is not needed for business decisions? Is firing events and relying on the reporting store enough?”. He really made me think, Aaron thanks for that! So for example you have some behavior on [...]]]></description>
			<content:encoded><![CDATA[<p>This morning <a href="http://codebetter.com/blogs/aaron.jensen/default.aspx" target="_blank">Aaron Jensen</a> asked a really interesting <a href="http://twitter.com/aaronjensen/status/6454974718" target="_blank">question</a> on Twitter “Should Aggregate Roots en Entities always keep their state if it is not needed for business decisions? Is firing events and relying on the reporting store enough?”. He really made me think, Aaron thanks for that!</p>
<p>So for example you have some behavior on your domain that gets called when a customer moves, this behavior will publish a Customer Moved event containing the new address. If the domain does not use the address information for any decision making, does it then need to be persisted in the aggregate root?</p>
<p>I guess it would greatly depend on whether or not you are using <a href="http://martinfowler.com/eaaDev/EventSourcing.html" target="_blank">Event Sourcing</a> to persist your published events (this is what I would suggest). If you do then I don’t see a problem in skipping storing the address information in the aggregate root. Because if at a later time you need the address information to make some decisions then it is easy to retrieve this from the events when reloading the aggregate root from the event store. You just create the address property and an internal event handler to process the Customer Moved event. Just make sure you delete the snapshots first.</p>
<p>But if you do not store all the events that you publish, but instead store only a snapshot then I would not skip storing the new address information, because in the end the domain is responsible for the state, not the reporting side. In other words, you can rebuild the reporting side from the domain, but you cannot necessarily do the same in reverse. So if you don’t store the address information and you need to rebuild the reporting store then you cannot do this.</p>
<p>So my conclusion is that the information should be available on the domain side. And when using CQRS and Event Sourcing to store the events instead of the internal state of the domain then that makes it possible to skip having the information in the domain structure, else not.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/08/cqrs-domain-state/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>CQRS &#8211; Trying to make it re-usable</title>
		<link>http://elegantcode.com/2009/11/28/cqrs-trying-to-make-it-re-usable/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cqrs-trying-to-make-it-re-usable</link>
		<comments>http://elegantcode.com/2009/11/28/cqrs-trying-to-make-it-re-usable/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 23:52:10 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[DDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/28/cqrs-trying-to-make-it-re-usable/</guid>
		<description><![CDATA[My new blog is at at http://cre8ivethought.com/blog/index come and follow me there. -Mark Nijhof If you have been following the source code changes on GitHub you may have noticed that I renamed the folder Fohjin.DDD to Fohjin.DDD.Example, my intention is to not make anymore changes there. Instead I have created a new folder next to [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>My new blog is at at <a href="http://cre8ivethought.com/blog/index">http://cre8ivethought.com/blog/index</a> come and follow me there.</p>
<p>-Mark Nijhof</em></p>
</blockquote>
<p>If you have been following the source code changes on <a href="http://github.com/MarkNijhof/Fohjin" target="_blank">GitHub</a> you may have noticed that I renamed the folder Fohjin.DDD to Fohjin.DDD.Example, my intention is to not make anymore changes there. Instead I have created a new folder next to it and in there I am rebuilding the same components but now with re-use and ease-of-use in mind. </p>
<p>The first thing that is very obvious in the example code is that the domain is not very persistence ignorant, something that is valued a lot in Domain-Driven Design. So this is something that I wanted to try to address first. I really like the way NHibernate makes our code persistence ignorant and am attempting to solve this in a similar manner, using <a href="http://www.castleproject.org/dynamicproxy/index.html" target="_blank">Castle DynamicProxy</a>. </p>
<p>Since I am a complete Castle DynamicProxy noob I asked the help of <a href="http://twitter.com/Kkozmic" target="_blank">Krzysztof Ko&#378;mic</a> who is an active committer on the Castle project. Krzysztof was gracias enough to get me through the basics in .Net proxy-ing. For those interested in this technology I would also recommend reading his excellent <a href="http://kozmic.pl/archive/2009/04/27/castle-dynamic-proxy-tutorial.aspx" target="_blank">blog series</a> about the same topic.</p>
<p>What I want to achieve is that you get back your <a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object" target="_blank">poco</a> and I am going to show you one suggestion that I have working now.</p>
<p>For comparison I’ll show you here how an aggregate root looks like in my initial example code (it is stripped down to only contain one behavioral method):</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Domain<span style="color: aqua">.</span>Client</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">Client</span> : <span style="color: #2b91af">BaseAggregateRoot</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">IDomainEvent</span><span style="color: aqua">&gt;</span>, <span style="color: #2b91af">IOrginator</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #2b91af">Address</span> _address;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> Client()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; registerEvents();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> ClientMoved(<span style="color: #2b91af">Address</span> newAddress)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Apply(<span style="color: #0080c0">new</span> <span style="color: #2b91af">ClientMovedEvent</span>(newAddress<span style="color: aqua">.</span>Street, newAddress<span style="color: aqua">.</span>StreetNumber, newAddress<span style="color: aqua">.</span>PostalCode, newAddress<span style="color: aqua">.</span>City));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IMemento</span> <span style="color: #2b91af">IOrginator</span><span style="color: aqua">.</span>CreateMemento()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">ClientMemento</span>(Id, Version, _address<span style="color: aqua">.</span>Street, _address<span style="color: aqua">.</span>StreetNumber, _address<span style="color: aqua">.</span>PostalCode, _address<span style="color: aqua">.</span>City);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">void</span> <span style="color: #2b91af">IOrginator</span><span style="color: aqua">.</span>SetMemento(<span style="color: #2b91af">IMemento</span> memento)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> clientMemento <span style="color: aqua">=</span> (<span style="color: #2b91af">ClientMemento</span>) memento;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Id <span style="color: aqua">=</span> clientMemento<span style="color: aqua">.</span>Id;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Version <span style="color: aqua">=</span> clientMemento<span style="color: aqua">.</span>Version;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _address <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Address</span>(clientMemento<span style="color: aqua">.</span>Street, clientMemento<span style="color: aqua">.</span>StreetNumber, clientMemento<span style="color: aqua">.</span>PostalCode, clientMemento<span style="color: aqua">.</span>City);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> registerEvents()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RegisterEvent<span style="color: aqua">&lt;</span><span style="color: #2b91af">ClientMovedEvent</span><span style="color: aqua">&gt;</span>(onNewClientMoved);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> onNewClientMoved(<span style="color: #2b91af">ClientMovedEvent</span> clientMovedEvent)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _address <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Address</span>(clientMovedEvent<span style="color: aqua">.</span>Street, clientMovedEvent<span style="color: aqua">.</span>StreetNumber, clientMovedEvent<span style="color: aqua">.</span>PostalCode, clientMovedEvent<span style="color: aqua">.</span>City);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span> }</pre>
</div>
<p></p>
<p>And here is the same aggregate root but now _more_ persistence ignorant:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Domain<span style="color: aqua">.</span>Client</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">Client</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Apply(<span style="color: #0080c0">object</span> @event) { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span><span style="color: aqua">&gt;</span> RegisteredEvents()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">yield</span> <span style="color: #0080c0">return</span> <span style="color: #0080c0">typeof</span>(<span style="color: #2b91af">ClientMovedEvent</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #2b91af">Address</span> Address { <span style="color: #0080c0">get</span>; <span style="color: #0080c0">set</span>; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> ClientMoved(<span style="color: #2b91af">Address</span> newAddress)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Apply(<span style="color: #0080c0">new</span> <span style="color: #2b91af">ClientMovedEvent</span>(newAddress));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span> }</pre>
</div>
<p></p>
<p>Ok this may seem a bit strange at first, I mean we are sending an event into nothing and we don’t set our state anymore. Let me try to explain <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Convention over Configuration</h2>
<p>Well in order to make this work I am going to insist on some conventions, in fact it is going to be very opinionated. If you want to learn more about convention over configuration then I would suggest reading <a href="http://codebetter.com/blogs/jeremy.miller/" target="_blank">Jeremy Millers</a> article on <a href="http://msdn.microsoft.com/en-us/magazine/dd419655.aspx" target="_blank">MSDN</a>. Personally I am all in favor for some strong guidelines so lets just go over them and see if they make sense to you as well.</p>
<h5></h5>
<h5>Mandatory methods</h5>
<p>The protected virtual method Apply and the protected method RegisteredEvents are both mandatory if you want to instantiate the aggregate root using this approach. </p>
<p>In the RegisteredEvents you return all the events that the aggregate root can publish, this is to let the persistence code know what it can expect. So when you add some new behavior that will publish a new event then you just add it to the list here. I think that you can sort of think about this as the mapping classes in NHibernate.</p>
<p>The Apply method is used to publish the events from the domain behavior, but as you can see it has an empty method body, so nothing will happen, right? Here we use Castle DynamicProxy to intercept calls to this method and replace the behavior (non-existing) with our persistence logic. Where this logic comes from I’ll explain in a minute.</p>
<h5>Internal state needs to be protected virtual properties</h5>
<p>One other difference with this approach versus the example code is that instead of private fields or private properties for the internal state, it now needs to be in the form of protected virtual properties. The reason for this is because state will not be managed by the aggregate root anymore, but instead by something that we add using Castle DynamicProxy and we will be using interception to map the properties to the internal representation of the state. The code even throws an exception if the aggregate root tries to set the internal state directly.</p>
<h5>The event store</h5>
<p>I haven’t done anything with the event store yet, I don’t think the functionality will change much from the code in the example project. One thing that will remain the same is the dependency on interfaces, and not on specific implementations. This is obviously because it is good design practices. And it will enable us to mix this new approach and the approach using the BaseAggregateRoot class together with the same event store. The BaseAggregateRoot is discussed in detail <a href="http://elegantcode.com/2009/11/20/cqrs-the-domain-events/" target="_blank">here</a>.</p>
<h5>Mix-in magic</h5>
<p>So after reading the previous chapter you would probably say, but this aggregate root doesn’t implement anything? This is true, but when our repository is instantiating the aggregate root it is also adding or injecting two more classes into it. One class that implements the IEventProvider interface and an other class that implements the IOrginator interface. This is called mixing-in classes and by doing so the aggregate root will implement both these two interfaces and it will re-direct calls to it to the mix-in classes. So as far as our event store knows it just is a class that implements the needed interfaces.</p>
<h2>Why have everything protected?</h2>
<p>Well in order to be able to intercept members using Castle DynamicProxy these members need to be visible to the interceptor. The most private way that is available to us is protected, and the reason why I want this to be as private as possible is because this is not domain logic, and thus should not be visible to the users of the domain. For the same reason you may notice that for example the IEventProvider implementation has all the members implement the interface explicitly thus hiding them when the domain is used and only showing them when the domain is explicitly casted to the IEventProvider interface.</p>
<p>I have been thinking about providing a basic interface that dictates that the Apply and RegisterEvents methods needs to be present, but I like this approach better as from the outside these implementation details would not be known, but I could imagine being able to support both approaches.</p>
<h2>Reflect only once</h2>
<p>There is quit a bit of reflection going on to discover and the properties of both the domain events and the aggregate root internal state. But once this is done once the results will be cached and re-used for each sequential need. But I am sure this can improve quit a bit <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conventional limits</h2>
<p>So what if I run into the limits of this conventional approach? Well the Event Store only cares about that the different interfaces are implemented, so you will be able to switch back to the BaseAggregateRoot solution at any time. This does mean that you will have to do some more coding, but you will have all the freedom to do what ever you need to do. And the different approaches can be used side by side.</p>
<p>One other limit is that if you instantiate the aggregate root yourself nothing will be intercepted and mixed-in so no state will change nor will the events be published.</p>
<h2>Current state</h2>
<p>Currently the code only supports very basic usages and there are some challenges ahead of it being truly useable. Think for example about other entities in the same aggregate managed by the aggregate root and collecting their events as well. Also think about adding and removing entities from a list which may need some configurable conventions so you can decide when something needs to be added or removed.</p>
<p>I am thinking about the ability to provide adapters to be able to handle certain state changes differently from the conventional way. One that I see a need for already is the ability to set the aggregate root Id, since this is not managed by the aggregate root but by the event provider mix-in. </p>
<h2>Finally</h2>
<p>Currently the functionality is very limited but I have tests passing proving that this is working <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  so what I would like is to get some feedback on this approach. And yes I realize that this is very opinionated, and will not fit everybody&#8217;s approach. Having said that, keep the suggestions coming anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/28/cqrs-trying-to-make-it-re-usable/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>CQRS &#8211; The Domain Events</title>
		<link>http://elegantcode.com/2009/11/20/cqrs-the-domain-events/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cqrs-the-domain-events</link>
		<comments>http://elegantcode.com/2009/11/20/cqrs-the-domain-events/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 01:17:39 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[DDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/20/cqrs-the-domain-events/</guid>
		<description><![CDATA[My new blog is at at http://cre8ivethought.com/blog/index come and follow me there. -Mark Nijhof As you may have seen in my previous post “CQRS à la Greg Young” now our domain aggregate root is responsible for publishing domain events indicating that some internal state has changed. In fact state changes within our aggregate root are [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>My new blog is at at <a href="http://cre8ivethought.com/blog/index">http://cre8ivethought.com/blog/index</a> come and follow me there.</p>
<p>-Mark Nijhof</em></p>
</blockquote>
<p>As you may have seen in my previous post “<a href="http://elegantcode.com/2009/11/11/cqrs-la-greg-young/" target="_blank">CQRS à la Greg Young</a>” now our domain aggregate root is responsible for publishing domain events indicating that some internal state has changed. In fact state changes within our aggregate root are _only_ allowed through such domain events. Secondly the internal event handlers are _not_ allowed to have any sort of business logic in them, they are _only_ supposed to set or update the internal state of the aggregate root directly from the data the event carries. Using these rules you completely separate the business logic from the state changes. This separation enables us to replay historical domain events without any business logic being triggered bringing it back to the same state as the original aggregate root.</p>
<p>Why is this important? Well now we can use these same domain events for our persistence using an Event Store, this pattern by Martin Fowler is called “<a href="http://martinfowler.com/eaaDev/EventSourcing.html" target="_blank">Event Sourcing</a>”. Obviously you don’t want to process a credit card or send an e-mail every time you load an aggregate root from the event store. Also from the time the original domain event was recorded and when the aggregate root is loaded from the event store the business logic that decided a state change was needed could have changed, this should not affect the actual historical state change. So this separation is to be taken seriously.</p>
<p>Domain events can also be used to signal something to the outside world (taken from the aggregate roots view point) that something has happened without having an actual state change. When persisting our domain events we would not differentiate between those two different domain events. </p>
<p>All domain events should be named with the ubiquitous language in mind, meaning that they should closely represent what the user intended to do in the same language as the user would use to explain it to you. By keeping all these domain events we gain a huge amount of knowledge about what happened and why it happened.</p>
<p>This means that our aggregate root gets the added responsibility of tracking these domain events, but I don’t see this as being any different then for example the proxy that NHibernate generates for you, except perhaps that it is not a proxy and that you have more control over what happens. But it is true your aggregate root has these added responsibilities.</p>
<p>So let us take a look at how the aggregate root provides this functionality, for obvious reasons we use a base class for this, but really all that is needed is that the aggregate root implements the following two interfaces:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>EventStore<span style="color: aqua">.</span>Storage<span style="color: aqua">.</span>Memento</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">interface</span> <span style="color: #2b91af">IOrginator</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IMemento</span> CreateMemento();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">void</span> SetMemento(<span style="color: #2b91af">IMemento</span> memento);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span> }</pre>
</div>
<p></p>
<p>The IOrginator interface is for the snapshot functionality which is an optimization technique for speeding up loading aggregate roots from the Event Store. As you can see it is using the “Memento” patter from the <a href="http://c2.com/cgi/wiki?GangOfFour" target="_blank">Gang Of Four</a> book. I wanted to get this interface out of the way first; it is not needed, but does provide a good optimization for loading aggregate roots.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>EventStore</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">interface</span> <span style="color: #2b91af">IEventProvider</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">void</span> Clear();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">void</span> LoadFromHistory(<span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">IDomainEvent</span><span style="color: aqua">&gt;</span> domainEvents);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">void</span> UpdateVersion(<span style="color: #0080c0">int</span> version);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Guid</span> Id { <span style="color: #0080c0">get</span>; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">int</span> Version { <span style="color: #0080c0">get</span>; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">IDomainEvent</span><span style="color: aqua">&gt;</span> GetChanges();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span> }</pre>
</div>
<p></p>
<p>The IEventProvider interface is the most interesting interface of the two, this one defines how domain events can be retrieved from the aggregate root and how historical domain events can be loaded back. It also defines that each aggregate root must have an Id and a Version, both of these are used by the event store, the Id is obvious so I won’t go into that, the version on the other hand may not be that obvious. The version is used to detect concurrency violations, meaning this is used to prevent conflicts that occur because between the time the command was send and the aggregate root was saved an other user or process has updated the same aggregate root. In this case we would throw an Concurrency Violation Exception which currently results in a rollback. In a future post I plan to look into how you could try to deal with these concurrency violations automatically.</p>
<h2>Using domain events for state change</h2>
<p>Now we will take a look at how these interfaces are implemented and how the implementation is used. The way I am going to go through the code is as if you where using R# going from method to method. So in order to get a more overall impression&#160; I would encourage you to look at the source code. The source code can be found here: <a href="http://github.com/MarkNijhof/Fohjin">http://github.com/MarkNijhof/Fohjin</a></p>
<p>Each aggregate root has to register the domain events and the internal event handlers with the base class. I am working on getting this as static information for the type since this will not change between different instances of the same type. As you can see I am registering the domain event type and an action to handle the specific type. As you can see the actions have the specific domain event as an input parameter.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> registerEvents()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; RegisterEvent<span style="color: aqua">&lt;</span><span style="color: #2b91af">ClientCreatedEvent</span><span style="color: aqua">&gt;</span>(onNewClientCreated);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; RegisterEvent<span style="color: aqua">&lt;</span><span style="color: #2b91af">ClientMovedEvent</span><span style="color: aqua">&gt;</span>(onNewClientMoved);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span> }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;<span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> onNewClientCreated(<span style="color: #2b91af">ClientCreatedEvent</span> clientCreatedEvent)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160; Id <span style="color: aqua">=</span> clientCreatedEvent<span style="color: aqua">.</span>ClientId;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160; _clientName <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">ClientName</span>(clientCreatedEvent<span style="color: aqua">.</span>ClientName);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160; _address <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Address</span>(clientCreatedEvent<span style="color: aqua">.</span>Street, clientCreatedEvent<span style="color: aqua">.</span>StreetNumber, clientCreatedEvent<span style="color: aqua">.</span>PostalCode, clientCreatedEvent<span style="color: aqua">.</span>City);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160; _phoneNumber <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">PhoneNumber</span>(clientCreatedEvent<span style="color: aqua">.</span>PhoneNumber);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span> }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;<span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> onNewClientMoved(<span style="color: #2b91af">ClientMovedEvent</span> clientMovedEvent)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160; _address <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Address</span>(clientMovedEvent<span style="color: aqua">.</span>Street, clientMovedEvent<span style="color: aqua">.</span>StreetNumber, clientMovedEvent<span style="color: aqua">.</span>PostalCode, clientMovedEvent<span style="color: aqua">.</span>City);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span> }</pre>
</div>
<p></p>
<p>The RegisterEvent method is defined in the BaseAggregateRoot class, here is a little bit of interesting logic going on where basically a new action is defined that has an IDomainEvent as an input parameter, that is how they can all be stored in the same Dictionary. Then inside this action the provided action is invoked where the input value is cast from an IDomainEvent to the actual expected type TEvent.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">IDomainEvent</span><span style="color: aqua">&gt;&gt;</span> _registeredEvents;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span>&#160;<span style="color: #0080c0">protected</span> <span style="color: #0080c0">void</span> RegisterEvent<span style="color: aqua">&lt;</span>TEvent<span style="color: aqua">&gt;</span>(<span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TEvent<span style="color: aqua">&gt;</span> eventHandler) <span style="color: #0080c0">where</span> TEvent : <span style="color: #0080c0">class</span>, <span style="color: #2b91af">IDomainEvent</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; _registeredEvents<span style="color: aqua">.</span>Add(<span style="color: #0080c0">typeof</span>(TEvent), theEvent <span style="color: aqua">=&gt;</span> eventHandler(theEvent <span style="color: #0080c0">as</span> TEvent));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span> }</pre>
</div>
<p></p>
<p>So if we would write the example out of what is actually happening for the Client Created Event then it would look like this example below, and this is what is being stored in the _registeredEvents.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Delegate(<span style="color: #2b91af">IDomainEvent</span> domainEvent)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; onNewClientCreated(domainEvent <span style="color: #0080c0">as</span> <span style="color: #2b91af">ClientCreatedEvent</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span> }</pre>
</div>
<p></p>
<p>And below here is the private apply method that is being called from two different methods in the aggregate root base class. The method retrieves the action that is registered for the provided domain event, and than it invokes the action with the domain event as the input parameter. The apply method takes an IDomainEvent instead of the specific domain event and because of that we have the above mentioned logic.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> apply(<span style="color: #2b91af">Type</span> eventType, <span style="color: #2b91af">IDomainEvent</span> domainEvent)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">IDomainEvent</span><span style="color: aqua">&gt;</span> handler;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>_registeredEvents<span style="color: aqua">.</span>TryGetValue(eventType, <span style="color: #0080c0">out</span> handler))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">throw</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">UnregisteredDomainEventException</span>(<span style="color: #0080c0">string</span><span style="color: aqua">.</span>Format(<span style="color: #a31515">&quot;The requested domain event '{0}' is not registered in '{1}'&quot;</span>, eventType<span style="color: aqua">.</span>FullName, GetType()<span style="color: aqua">.</span>FullName));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160; handler(domainEvent);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span> }</pre>
</div>
<p>&#160;</p>
<p>Let us take a look from where this apply method is being called, first we will look at some actual domain behavior in the aggregate root.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Withdrawl(<span style="color: #2b91af">Amount</span> amount)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; Guard();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; IsBalanceHighEnough(amount);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> newBalance <span style="color: aqua">=</span> _balance<span style="color: aqua">.</span>Withdrawl(amount);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160; Apply(<span style="color: #0080c0">new</span> <span style="color: #2b91af">CashWithdrawnEvent</span>(newBalance, amount));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span> }</pre>
</div>
<p></p>
<p>First we execute all our valuable domain logic, the business behavior. Then when we are satisfied that everything is ok and we know what type of state change we need to execute and we Apply a new domain event with the new internal state. The Apply method used here is a protected method on the BaseAgregateRoot. Btw there is nothing stating that there can only be one outcome, i.e. only one type of domain event being Applied.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">protected</span> <span style="color: #0080c0">void</span> Apply<span style="color: aqua">&lt;</span>TEvent<span style="color: aqua">&gt;</span>(TEvent domainEvent) <span style="color: #0080c0">where</span> TEvent : <span style="color: #0080c0">class</span>, <span style="color: #2b91af">IDomainEvent</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; domainEvent<span style="color: aqua">.</span>AggregateId <span style="color: aqua">=</span> Id;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; domainEvent<span style="color: aqua">.</span>Version <span style="color: aqua">=</span> GetNewEventVersion();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; apply(domainEvent<span style="color: aqua">.</span>GetType(), domainEvent);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160; _appliedEvents<span style="color: aqua">.</span>Add(domainEvent);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span> }</pre>
</div>
<p></p>
<p>When we Apply a domain event we will first assign the aggregate root Id to the event so that we can keep track to which aggregate root this event belongs to. Secondly we get a new version and assign this to the event, this is to maintain the correct order of the events. Then we call the apply method which will make the state change to the aggregate root. And finally we will add this domain event to the internal list of applied events. This is very similar with the dirty check of NHibernate (the idea, not the actual implementation).</p>
<p>This is all what what is needed to execute domain behavior and keep track of the domain events that have been used to update internal state of an aggregate root. </p>
<h2>Getting the state changes</h2>
<p>So now we have build up some internal state changes and we want to persist them to some sort of medium. I am not going to discuss how to actually persist these state changes, that is for a later post, all I want to show you now is how to get them out of the aggregate root. How about we start with the method GetChanges <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span> IEventProvider<span style="color: aqua">.</span>GetChanges()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> _appliedEvents</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Concat(GetEntityEvents())</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>OrderBy(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Version)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>ToList();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span> }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;<span style="color: #0080c0">private</span> <span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span> GetEntityEvents()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> _entityEventProviders</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>SelectMany(entity <span style="color: aqua">=&gt;</span> entity<span style="color: aqua">.</span>GetChanges());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span> }</pre>
</div>
<p></p>
<p>Here we simply return all the applied domain events, by tracking these domain events we in effect track all the state changes that have happened since the aggregate root was instantiated. Here we also request all the applied domain events from all entities. Than new order the domain events by version. After having received and processed all the applied domain events we should Clear the aggregate root from all applied domain events so they won’t be persisted again.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">void</span> <span style="color: #2b91af">IEventProvider</span><span style="color: aqua">.</span>Clear()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; _entityEventProviders<span style="color: aqua">.</span>ForEach(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Clear());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; _appliedEvents<span style="color: aqua">.</span>Clear();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span> }</pre>
</div>
<p></p>
<p>Just a quick word about the entity event providers, in some cases you want to have domain behavior inside entities that are part of the aggregate but are not the aggregate root. Well in this case you want to have those entities generate domain events as well, and you want to get those as well when getting all changes. Same when clearing the domain also the changes inside each entity event provider should be cleared.</p>
<p>Before saving the changes in the aggregate root is finalized we also need to update the version of the aggregate root. This version will match the version of the last applied domain event.</p>
<h2>Loading historical domain events</h2>
<p>In a next post I’ll dig deeper into the event store but for now lets just assume you have a very nice way of storing these domain events and have the ability to retrieve them again. </p>
<p>As the IEventProvider interface nicely dictates there is a LoadFromHistory method that takes an IEnumerable&lt;IDomainEvent&gt; below here is the implementation of this method.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">void</span> <span style="color: #2b91af">IEventProvider</span><span style="color: aqua">.</span>LoadFromHistory(<span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">IDomainEvent</span><span style="color: aqua">&gt;</span> domainEvents)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (domainEvents<span style="color: aqua">.</span>Count() <span style="color: aqua">==</span> <span style="color: yellow">0</span>)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span>;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">foreach</span> (<span style="color: #0080c0">var</span> domainEvent <span style="color: #0080c0">in</span> domainEvents)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; apply(domainEvent<span style="color: aqua">.</span>GetType(), domainEvent);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160; Version <span style="color: aqua">=</span> domainEvents<span style="color: aqua">.</span>Last()<span style="color: aqua">.</span>Version;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160; EventVersion <span style="color: aqua">=</span> Version;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span> }</pre>
</div>
<p></p>
<p>When you take a look in the for each loop you will notice that here we are calling apply again, please note that this is the private variant which is responsible for updating the internal state of the aggregate root and that these events are not added to the _appliedEvents collection, nor is the Id or Version updated. We don’t want to save these events again. After applying all the historical domain events we update the aggregate root version to the version of the last event.</p>
<h2>The base class</h2>
<p>I am using a base class to provide this functionality to all the different aggregate roots using inheritance and I know there are different opinions about this. So I wanted to highlight that you can achieve the same results by using composition. Your aggregate roots still need to implement the interfaces, but the implementation can be provided by using composition.</p>
<p>An other thing is that all the public methods in the BaseAggregateRoot are explicate interface implementation. I do this because I want to hide these details for any piece of code that is using the aggregate root as is, only when dealing with persistence we use the interface and get access to the explicit interface methods. Nice and clean.</p>
<h2>Aggregate entities</h2>
<p>Ok I have already mentioned the term entities and entity event providers, and I would like to focus on them for a bit. An entity is an domain object that is part of the same aggregate as the aggregate root, but is not the aggregate root it self, is however it is managed by the aggregate root. We manage state changes in these entities in the exact same way as we do this in the aggregate root, so each entity is also an event provider. There is a different interface for the entities then for the aggregate root so they can not be confused among each other.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>EventStore</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">interface</span> <span style="color: #2b91af">IEntityEventProvider</span> </pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">void</span> Clear();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">void</span> LoadFromHistory(<span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">IDomainEvent</span><span style="color: aqua">&gt;</span> domainEvents);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">void</span> HookUpVersionProvider(<span style="color: #2b91af">Func</span><span style="color: aqua">&lt;</span><span style="color: #0080c0">int</span><span style="color: aqua">&gt;</span> versionProvider);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">IDomainEvent</span><span style="color: aqua">&gt;</span> GetChanges();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Guid</span> Id { <span style="color: #0080c0">get</span>; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span> }</pre>
</div>
<p></p>
<p>You may have also noticed the hookup version provider method, remember it <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The problem is that we should deal with the whole aggregate as a whole so all state changes need to be persisted within the same transaction. So we want to have a single point to access the internal state of the whole aggregate as well as a single point to load the history back into the whole aggregate. In order to achieve this the aggregate root needs to register all the entity event providers so it can track them. To enable that we have an additional interface for our aggregate root to implement.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>EventStore</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">interface</span> <span style="color: #2b91af">IRegisterEntities</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">void</span> RegisterEntityEventProvider(<span style="color: #2b91af">IEntityEventProvider</span> entityEventProvider);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span> }</pre>
</div>
<p></p>
<p>Lets look at the implementation right away</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">void</span> IRegisterEntities<span style="color: aqua">.</span>RegisterEntityEventProvider(IEntityEventProvider entityEventProvider)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; entityEventProvider<span style="color: aqua">.</span>HookUpVersionProvider(GetNewEventVersion);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; _entityEventProviders<span style="color: aqua">.</span>Add(entityEventProvider);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span> }</pre>
</div>
<p></p>
<p>Now this is only a very simple collection that hold references to IEntityEventProviders which is used in the previous shown GetEntityEvents method. So getting out the changes is relatively simple this way. I also created a special collection that will automatically register entities when they are added to the collection.</p>
<p>Remember I mentioned the HookUpVersionProvider method, well this is used to get a reference to the method from the aggregate root that deals with assigning a new version to each event. We want a reference to the same version generator that the aggregate root uses because then all the versions of each domain event will be in sequence independently is the aggregate root or an entity created it.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Domain</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">EntityList</span><span style="color: aqua">&lt;</span>TEntity<span style="color: aqua">&gt;</span> : <span style="color: #2b91af">List</span><span style="color: aqua">&lt;</span>TEntity<span style="color: aqua">&gt;</span> <span style="color: #0080c0">where</span> TEntity : IEntityEventProvider</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> IRegisterEntities _aggregateRoot;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> EntityList(IRegisterEntities aggregateRoot)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _aggregateRoot <span style="color: aqua">=</span> aggregateRoot;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">new</span> <span style="color: #0080c0">void</span> Add(TEntity entity)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _aggregateRoot<span style="color: aqua">.</span>RegisterEntityEventProvider(entity);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">base</span><span style="color: aqua">.</span>Add(entity);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span> }</pre>
</div>
<p></p>
<p>The collection takes a reference to the aggregate root it is part of in the constructor to be able to add each added entity event provider to the collection in the aggregate root.</p>
<p>Currently there is some duplication between the BaseAggregateRoot and the BaseEntity because they are inherited from different interfaces, I am sure there is some optimization possible there <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>There is finally one more thing to cover about this and that is how historical domain events are being passed into the correct entity, currently this is a bit ugly but I am working on a more elegant solution. Take a look at the code.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> registerEvents()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: green">// Registration of Aggregate Root event handlers</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; RegisterEvent<span style="color: aqua">&lt;</span>BankCardWasReportedStolenEvent<span style="color: aqua">&gt;</span>(onAnyEventForABankCard);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160; RegisterEvent<span style="color: aqua">&lt;</span>BankCardWasCanceledByCLientEvent<span style="color: aqua">&gt;</span>(onAnyEventForABankCard);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span> }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;<span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> onAnyEventForABankCard(IDomainEvent domainEvent)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160; IEntityEventProvider bankCard;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>_bankCards<span style="color: aqua">.</span>TryGetValueById(domainEvent<span style="color: aqua">.</span>AggregateId, <span style="color: #0080c0">out</span> bankCard))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">throw</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">NonExistingBankCardException</span>(<span style="color: #a31515">&quot;The requested bank card does not exist!&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160; bankCard<span style="color: aqua">.</span>LoadFromHistory(<span style="color: #0080c0">new</span>[] { domainEvent });</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span> }</pre>
</div>
<p></p>
<p>So each entity domain event will be registered here as well and are all passed to one specific event handler (one specific event handler for each different entity type). What happens is that we check if the specific event provider is present in the collection by looking for the Id, when the requested event provider is found the historical domain event will be loaded using the load from history method on the event provider. </p>
<p>The problem here is that the information which events the entity can produce is already registered, and that is in the entities them self&#8217;s. So by making that information statically available I should be able to auto register the entity event handlers.</p>
<h2>Finally</h2>
<p>I hope that this was a useful explanation of how the aggregate root works with the internal domain events, and that you would agree with me that it really is not very difficult. Next time I want to discuss the event store so we can take a look at persisting the domain events. I will also post a blog about making the entity registration more elegant and less manual work.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/20/cqrs-the-domain-events/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Recording of my E-VAN presentation about CQRS</title>
		<link>http://elegantcode.com/2009/11/19/recording-of-my-e-van-presentation-about-cqrs/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=recording-of-my-e-van-presentation-about-cqrs</link>
		<comments>http://elegantcode.com/2009/11/19/recording-of-my-e-van-presentation-about-cqrs/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 20:11:37 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[CQRS]]></category>
		<category><![CDATA[DDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/19/recording-of-my-e-van-presentation-about-cqrs/</guid>
		<description><![CDATA[My new blog is at at http://cre8ivethought.com/blog/index come and follow me there. -Mark Nijhof Jan van Ryswyck put again a great deal of effort into getting the E-VAN recording of my CQRS presentation uploaded so quickly. We are only talking about one day, almost seems like he has nothing else to do Thanks Jan! The [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>My new blog is at at <a href="http://cre8ivethought.com/blog/index">http://cre8ivethought.com/blog/index</a> come and follow me there.</p>
<p>-Mark Nijhof</em></p>
</blockquote>
<p>Jan van Ryswyck put again a great deal of effort into getting the E-VAN recording of my CQRS presentation uploaded so quickly. We are only talking about one day, almost seems like he has nothing else to do <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Thanks Jan!</p>
<p>The recording can be found here: <a href="http://www.vimeo.com/7838858">http://www.vimeo.com/7838858</a></p>
<p>When you create an account at Vimeo (free) then you can also download the recordings in even better quality.</p>
<p>The code that the presentation covers can be found here: <a href="http://github.com/MarkNijhof/Fohjin">http://github.com/MarkNijhof/Fohjin</a></p>
<p>I hope that many will provide their feedback, both good and bad as this is how I evolve. </p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/19/recording-of-my-e-van-presentation-about-cqrs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

