<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The journeylism of @yreynhout</title>
	<atom:link href="http://seabites.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://seabites.wordpress.com</link>
	<description>On CQRS, DDD(D), ES, ...</description>
	<lastBuildDate>Sun, 21 Apr 2013 15:09:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='seabites.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The journeylism of @yreynhout</title>
		<link>http://seabites.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://seabites.wordpress.com/osd.xml" title="The journeylism of @yreynhout" />
	<atom:link rel='hub' href='http://seabites.wordpress.com/?pushpress=hub'/>
		<item>
		<title>A role to play</title>
		<link>http://seabites.wordpress.com/2013/02/24/a-role-to-play/</link>
		<comments>http://seabites.wordpress.com/2013/02/24/a-role-to-play/#comments</comments>
		<pubDate>Sun, 24 Feb 2013 13:30:54 +0000</pubDate>
		<dc:creator>yreynhout</dc:creator>
				<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Event sourcing]]></category>

		<guid isPermaLink="false">http://seabites.wordpress.com/?p=607</guid>
		<description><![CDATA[Every so often someone new arrives at the DDD/CQRS list (*) and topics such as set based validation rear their head, resulting in near-endless threads of discussion and coming to a common understanding. A topic that isn&#8217;t as often discussed is one of roles in the domain model and how that would work in combination [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=607&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Every so often someone new arrives at the DDD/CQRS list (*) and topics such as set based validation rear their head, resulting in near-endless threads of discussion and coming to a common understanding. A topic that isn&#8217;t as often discussed is one of roles in the domain model and how that would work in combination with event sourcing. If you want to read up on roles, Mark Seemann has <a title="About roles" href="http://blog.ploeh.dk/2013/01/07/RoleHints.aspx" target="_blank">some great posts on that topic</a> on his blog, albeit in a slightly different context. There&#8217;s also this <a title="Making roles explicit video" href="http://www.infoq.com/presentations/Making-Roles-Explicit-Udi-Dahan" target="_blank">video</a> by <a title="The software simplist" href="http://www.udidahan.com/" target="_blank">Udi Dahan</a> about making roles explicit, which is more akin to what I&#8217;ll be touching upon here. Fanatics of whitepapers, might get their brain washed by papers like &#8220;<a href="http://martinfowler.com/bliki/RoleInterface.html" title="Role interfaces" target="_blank">Role Interfaces</a>&#8220;, &#8220;<a title="The Role Object Pattern" href="http://hillside.net/plop/plop97/Proceedings/riehle.pdf" target="_blank">The Role Object Pattern</a>&#8220;, &#8220;<a title="Modeling Roles" href="http://objectdiscovery.com/solutions/publications/roles/index.html" target="_blank">Modeling Roles</a>&#8221; or &#8220;<a title="Mock Roles, not Objects" href="http://jmock.org/oopsla2004.pdf" target="_blank">Mock Roles, not Objects</a>&#8220;.</p>
<h3>Let&#8217;s make it practical</h3>
<p>Suppose I&#8217;m building a Realtor app that has a finite number of real estate property types (think apartment, villa, house, warehouse, etc&#8230;). I could model each type of property as a class/type (**) putting specific behavior on each of them. I&#8217;d end up with Apartment, Villa, House and Warehouse as aggregate root entity types (and hence as aggregates). What if I had some common behavior that applies to all of them, where the calling code has a desire to be ignorant of the specific type, i.e. it is interested in the role that a property plays, not the specific type of property it represents. Let&#8217;s call that role &#8216;Property&#8217; for lack of inspiration. I could implement it using a base class, an interface or even a totally separate class as we&#8217;ll see in a moment. The calling code could be about adding a set of properties to a listing:</p>
<pre class="brush: csharp; title: ; notranslate">
public class AddPropertiesToListingService {
  readonly IListingRepository _listingRepository;
  readonly IPropertyRepository _propertyRepository;

  public AddPropertiesToListingService(IListingRepository listingRepository, IPropertyRepository propertyRepository) {
    _listingRepository = listingRepository;
    _propertyRepository = propertyRepository;
  }

  public void AddPropertiesToListing(ListingId listingId, PropertyId[] propertyIds) {
    var listing = _listingRepository.Get(listingId);
    var properties = propertyIds.Select(propertyId =&gt; _propertyRepository.Get(propertyId));
    listing.AddProperties(properties);
  }
}
</pre>
<p>If this was modeled using either an interface or base class to denote the role, the repository code (***) would look a bit like this:</p>
<pre class="brush: csharp; title: ; notranslate">
public interface IPropertyRepository {
  Property Get(Guid id);
}

public class PropertyRepository : IPropertyRepository {
  readonly IPropertyFactory _factory;
  readonly IEventStreamReader _reader;

  public PropertyRepository(IPropertyFactory factory, IEventStreamReader reader) {
    _factory = factory;
    _reader = reader;
  }

  public Property Get(Guid id) {
    var result = _reader.Read(id);
    if(!result.HasValue) {
      throw new PropertyNotFoundException(id);
    }
    var root = _factory.Create(result.Value);
    root.Initialize(result.Value.Events);
    return root;
  }
}

//This could also be a Func&lt;EventStream, Property&gt;
public interface IPropertyFactory {
  Property Create(EventStream eventStream);
}

public class EventStreamAnalyzingPropertyFactory : IPropertyFactory {
  readonly Dictionary&lt;Type, Func&lt;Property&gt;&gt; _propertyFactories;

  public EventStreamAnalyzingPropertyFactory() {
    _propertyFactories = new Dictionary&lt;Type, Func&lt;Property&gt;&gt;();
    //Assume that each of the aggregate root entities
    //has a static Factory method that creates a new instance.
    _propertyFactories.Add(typeof(ApartmentRegistered), () =&gt; Apartment.Factory());
    _propertyFactories.Add(typeof(VillaRegistered), () =&gt; Villa.Factory());
    _propertyFactories.Add(typeof(HouseRegistered), () =&gt; House.Factory());
    _propertyFactories.Add(typeof(WarehouseRegistered), () =&gt; Warehouse.Factory());
    //Remark: Yes, this is an OCP violation.
  }

  public Property Create(EventStream eventStream) {
    Func&lt;Property&gt; propertyFactory;
    if(!_propertyFactories.TryGet(eventStream[0].GetType(), out propertyFactory))
      throw new PropertyUnknownException(eventStream.Id);
    return propertyFactory();
  }
}

public interface IEventStreamReader {
  Optional&lt;EventStream&gt; Read(Guid id);
}

public interface Optional&lt;T&gt; {
  bool HasValue { get; }
  T Value { get; }
}

public interface EventStream {
  Guid Id { get; }
  Int32 ExpectedVersion { get; }
  //In a real world implementation this would be streaming,
  //i.e. IEnumerable&lt;object&gt;
  object[] Events { get; }
}

public class PropertyNotFoundException : Exception {
  public PropertyNotFoundException(Guid id) { }
}

public class PropertyUnknownException : Exception {
  public PropertyUnknownException(Guid id) { }
}

//As an aside, Property could also be 
//turned into an interface to describe
//the role behavior.
public abstract class Property : AggregateRootEntity { 
  /* Common behavior can be put here */
}

//Similar for the other property types.
public class Villa : Property {
  public static readonly Func&lt;Villa&gt; Factory = () =&gt; new Villa();

  Villa() { /* ... */}

  /* Specific behavior can be put here */
}
</pre>
<p>Notice how the factory is made responsible for analyzing the event stream and deciding which Property type to instantiate based on the type of the first event. It should be obvious that &#8220;the type of the first event&#8221; is just one of the ways you could come to decision of which Property type to instantiate.</p>
<p>A slightly different scenario is one where you load the stream into a dedicated class, instead of relying on a base class or an interface to fulfill the role.</p>
<pre class="brush: csharp; title: ; notranslate">
public class Property {
  public static readonly Func&lt;Property&gt; Factory = () =&gt; new Property();

  Property() { 
    /* Streams from each of the Property types can 
       be loaded into this Role class  */
    Register&lt;ApartmentRegistered&gt;(When);
    Register&lt;VillaRegistered&gt;(When);
    Register&lt;HouseRegistered&gt;(When);
    Register&lt;WarehouseRegistered&gt;(When);
    /* Notice how I haven't even brought up what 
       you could do if these were polymorphic
       messages */
  } 

  /* Common role behavior goes here */
}

public class PropertyRepository : IPropertyRepository {
  readonly IEventStreamReader _reader;

  public PropertyRepository(IEventStreamReader reader) {
    _reader = reader;
  }

  public Property Get(Guid id) {
    var result = _reader.Read(id);
    if(!result.HasValue) {
      throw new PropertyNotFoundException(id);
    }
    var root = Property.Factory();
    root.Initialize(result.Value.Events);
    return root;
  }
}
</pre>
<p>This is exactly why you shouldn&#8217;t use the type name of an aggregate as a form of stream identification (at least if you want to support this kind of scenario). The repository will just load up the stream, being totally ignorant of what class was used to produce the events in the stream in the first place and happily feed it to the Property class.</p>
<p>Another scenario where this technique could prove to be useful is when your entity goes through a life cycle where each state has very different behavior or behavior needs to be limited as of a certain stage in its life cycle. Of course, this shouldn&#8217;t be used as an excuse to NOT model things explicitly.</p>
<h3>Conclusion</h3>
<p>The most important takeaway is that a stream of events does not need to be loaded into the same class all the time and that roles remain useful within a model backed by event sourcing. Like anything, this should be used with moderation and only if applicable.</p>
<p>(*) I&#8217;m reliving a scene with Arnold in Total Recall (1990) as I&#8217;m writing this (<a href="http://www.youtube.com/watch?feature=player_detailpage&#038;v=WFMLGEHdIjE#t=86s" rel="nofollow">http://www.youtube.com/watch?feature=player_detailpage&#038;v=WFMLGEHdIjE#t=86s</a>).<br />
(**) Having worked in the Realtor business, I can tell you right off the bat that having a class per real estate property type is going to hurt in the long run, but who am I to judge about the usefulness of this particular model.<br />
(***) Don&#8217;t complain if the code doesn&#8217;t compile out of the box. I used my C# brain compiler.</p>
<br />Filed under: <a href='http://seabites.wordpress.com/category/domain-driven-design/'>Domain Driven Design</a>, <a href='http://seabites.wordpress.com/category/event-sourcing/'>Event sourcing</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/seabites.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/seabites.wordpress.com/607/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=607&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://seabites.wordpress.com/2013/02/24/a-role-to-play/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39e267e07e5b6ada467e7e2c559f41b9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yreynhout</media:title>
		</media:content>
	</item>
		<item>
		<title>Object Inheritance</title>
		<link>http://seabites.wordpress.com/2012/10/12/object-inheritance/</link>
		<comments>http://seabites.wordpress.com/2012/10/12/object-inheritance/#comments</comments>
		<pubDate>Fri, 12 Oct 2012 21:15:46 +0000</pubDate>
		<dc:creator>yreynhout</dc:creator>
				<category><![CDATA[Command & Query Responsibility Segregation]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Event sourcing]]></category>
		<category><![CDATA[Messaging]]></category>

		<guid isPermaLink="false">http://seabites.wordpress.com/?p=546</guid>
		<description><![CDATA[When mentioning &#8220;object inheritance&#8221; most people immediately think of &#8220;class inheritance&#8220;. Alas, that&#8217;s not what it is. Quoting from Streamlined Object Modeling*: Object inheritance allows two objects representing a single entity to be treated as one object by the rest of the system. Put a different way, where class inheritance allows for code reuse, object [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=546&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>When mentioning &#8220;object inheritance&#8221; most people immediately think of &#8220;<a href="http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)" title="Class inheritance" target="_blank">class inheritance</a>&#8220;. Alas, that&#8217;s not what it is. Quoting from <a href="http://www.amazon.com/Streamlined-Object-Modeling-Patterns-Implementation/dp/0130668397" title="Streamlined object modeling" target="_blank">Streamlined Object Modeling</a>*: </p>
<blockquote><p>Object inheritance allows two objects representing a single entity to be treated as one object by the rest of the system.</p></blockquote>
<p>Put a different way, where class inheritance allows for code reuse, object inheritance allows for data reuse.</p>
<h3>Application</h3>
<p>Where could this be useful? Depending on the domain you are in, I&#8217;d say quite a few places. Whenever you replicate data from one object to another object, you should stop and consider if &#8220;object inheritance&#8221; could be applicable. While object inheritance does impose some constraints, it also saves you from writing reconciliation code to synchronize two or more objects (especially in systems that heavily rely on messaging).<br />
A common example I like to use is the one of a video title and a video tape (**). From a <em>real world</em> point of view, a video tape has both the attributes of the tape itself and the title. Yet, if I modeled this as two separate objects, I run into trouble (a.k.a. synchronization woes). If I copy the title information to the tape upon <em>creation</em> of the tape, and I made an error in say the release date of the title, I now have to replicate that information to the &#8220;affected&#8221; tapes. Don&#8217;t get me wrong, sometimes this kind of behavior is desirable, i.e. it makes sense from a business perspective. But what if it doesn&#8217;t? That&#8217;s where &#8220;object inheritance&#8221; comes in. To a large extent, it can cover the scenarios that a synchronization based solution can, <strong>IF</strong> constraints are met.</p>
<h3>Constraints</h3>
<ul>
<li>Localized data: &#8220;Object inheritance&#8221; assumes that the data of both objects lives close together. That might be a deal breaker for larger systems, or at least an indication that you&#8217;d have to consider co-locating the data of &#8220;both&#8221; objects.</li>
<li>Immutable data: One of the objects, the &#8220;parent&#8221;, its data is immutable during object inheritance. From an <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" title="Object oriented programming" target="_blank">OOP</a> perspective that means you can only invoke <a href="http://domaindrivendesign.org/resources/ddd_terms" title="Side effect free function" target="_blank">side-effect free functions</a> on a &#8220;parent&#8221;.</li>
<li>&#8220;Parent&#8221; object responsibilities: the parent object contains information and behaviors that are valid across multiple contexts, multiple interactions, and multiple variations of an object.</li>
<li>&#8220;Child&#8221; object responsibilities: the child object represents the parent in a specialized context, in a particular interaction, or as a distinct variation.</li>
</ul>
<p>The aforementioned book also states other constraints such as the child object exhibiting the parent object&#8217;s &#8220;profile&#8221; (think interface), but I find those less desirable in an environment that uses CQRS. For more in-depth information, I strongly suggest you read it. It has a wealth of information on this very topic.</p>
<h3>Code</h3>
<p>In its simplest form &#8220;object inheritance&#8221; looks and feels like <a href="http://en.wikipedia.org/wiki/Object_composition" title="Object composition" target="_blank">object composition</a>.<br />
<script src="https://gist.github.com/3880987.js"></script></p>
<p>The composition can be hidden from <em>child object consuming code</em> behind a repository&#8217;s facade as shown below. </p>
<script src="https://gist.github.com/3880929.js"></script>
<p>The gist of &#8220;object inheritance&#8221; is that the child object (the video tape) asks the parent (the video) for data or invokes side-effect free functions on the parent to accomplish its task.</p>
<h3>Lunch -&gt; Not Free</h3>
<p>Whether you go down the route of &#8220;object inheritance&#8221; or (message based) &#8220;synchronization&#8221;, you will have to <em>think</em> about object life-cycles (both parent and child). Sometimes it&#8217;s desirable for the child to have a still view of the parent, a snapshot if you will. Other times you may want the child to always see the parent in its most current form. Children can even change parent during their life-cycle. Other children may want a more &#8220;controlled&#8221; view of the parent, rolling forward or backward based on their needs or context. You can get pretty sophisticated with this technique, especially in an event sourced domain model since it&#8217;s very well suited to roll up to a certain point in time or a certain revision of a parent. In the same breath, I should also say that you can very well shoot yourself in the foot with this technique, especially if the composition is to be taking place in the user interface, in which case there&#8217;s no point in using this. It&#8217;s also very easy to bury yourself in the pit of abstraction when talking about &#8220;child&#8221; and &#8220;parent&#8221;, so do replace those words with nouns from your domain, and get your <a href="http://www.petercoad.com/download/bookpdfs/jmcuch01.pdf" title="Archetypes" target="_blank">archetypes</a> right.</p>
<p>All in all, I&#8217;ve found this a good tool in the box, that plays nicely with aggregates, event-sourcing, even state-based models that track the concept of time. It&#8217;s not something I use everyday, but whenever I did, I ended up with less code. YMMV.</p>
<p>(*) <a href="http://www.amazon.com/Streamlined-Object-Modeling-Patterns-Implementation/dp/0130668397" title="Streamlined object modeling" target="_blank">Streamlined object modeling</a> is a successor to Peter Coad&#8217;s &#8220;<a href="http://www.amazon.com/Java-Modeling-Color-With-UML/dp/013011510X" title="Modeling In Color" target="_blank">Modeling In Color</a>&#8220;. A Kindle version of the former is available.<br />
(**) I&#8217;m from the &#8220;cassette &amp; video&#8221; ages.</p>
<br />Filed under: <a href='http://seabites.wordpress.com/category/command-query-responsibility-segregation/'>Command &amp; Query Responsibility Segregation</a>, <a href='http://seabites.wordpress.com/category/domain-driven-design/'>Domain Driven Design</a>, <a href='http://seabites.wordpress.com/category/event-sourcing/'>Event sourcing</a>, <a href='http://seabites.wordpress.com/category/messaging/'>Messaging</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/seabites.wordpress.com/546/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/seabites.wordpress.com/546/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=546&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://seabites.wordpress.com/2012/10/12/object-inheritance/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39e267e07e5b6ada467e7e2c559f41b9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yreynhout</media:title>
		</media:content>
	</item>
		<item>
		<title>The Money Box</title>
		<link>http://seabites.wordpress.com/2012/08/29/the-money-box/</link>
		<comments>http://seabites.wordpress.com/2012/08/29/the-money-box/#comments</comments>
		<pubDate>Wed, 29 Aug 2012 18:23:43 +0000</pubDate>
		<dc:creator>yreynhout</dc:creator>
				<category><![CDATA[Command & Query Responsibility Segregation]]></category>
		<category><![CDATA[Event sourcing]]></category>
		<category><![CDATA[Messaging]]></category>

		<guid isPermaLink="false">http://seabites.wordpress.com/?p=529</guid>
		<description><![CDATA[Once in a while I hear/read people struggle with projection performance. The root causes of their performance issues are diverse: using the same persistence behavior during projection rebuild as during live/production build use of ill fit technology (here&#8217;s looking at you EF) or usage of said technology in the wrong way too wide a persistence [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=529&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Once in a while I hear/read people struggle with <a href="http://cqrsguide.com/doc:projection" title="Projection" target="_blank">projection</a> performance. The root causes of their performance issues are diverse:</p>
<ul>
<li>using the same persistence behavior during projection rebuild as during live/production build</li>
<li>use of ill fit technology (here&#8217;s looking at you <a href="http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx" title="The Entity Framework" target="_blank">EF</a>) or usage of said technology in the wrong way</li>
<li>too wide a persistence interface which makes it difficult to optimize for performance</li>
<li>no batching support or batching as an afterthought</li>
<li>not thinking about the implication of performing reads during projection</li>
<li>&#8230;</li>
</ul>
<p>Ever heard of the book <em><a href="http://www.amazon.com/Refactoring-Patterns-Joshua-Kerievsky/dp/0321213351/" title="Refactoring to patterns - the book" target="_blank">Refactoring to Patterns</a></em>? It has a nice refactoring in there, called <em><a href="http://www.industriallogic.com/xp/refactoring/accumulationToCollection.html" title="Move accumulation to collecting parameter" target="_blank">Move Accumulation to Collecting Parameter</a></em> that refers to the <em><a href="http://c2.com/cgi/wiki?CollectingParameter" title="Collecting parameter" target="_blank">Collecting Parameter</a></em> pattern on <a href="http://c2.com" title="C2 website" target="_blank">C2</a>. How would this help with thy projections? Well, what if you could decouple the act of performing an action from collecting what is required to be able to perform an action? Put another way, what if you decouple the act of executing SQL DML statements from collecting those statements during projection (a.k.a. event handling)? So, instead of &#8230;</p>
<p><script src="https://gist.github.com/3515704.js"></script></p>
<p>&#8230; we add another level of indirection &#8230;</p>
<p><script src="https://gist.github.com/3515757.js"></script></p>
<p>The most noticeable differences are the decoupling from persistence technology(*), no reads, and no promises with regard to when the requested operations will be executed/flushed to storage. Usually, the IProjectionSqlOperations interface will have a very small surface (i.e. low member count), covering INSERT, UPDATE and DELETE. During live/production projection building you could have an implementation of this interface (a.k.a. <a href="http://en.wikipedia.org/wiki/Strategy_pattern" title="The Strategy Pattern" target="_blank">strategy</a>) that flushes as soon as an operation is requested.</p>
<p>However, the more interesting implementations are the ones that are used during rebuild.</p>
<p><script src="https://gist.github.com/3516020.js"></script></p>
<p>This implementation translates the requested operations into sql statement objects (abstracted by ISqlStatement) and pushes them onto something that observes these sql statements. The observer couldn&#8217;t care less what the actual sql statements are (that happened in the projection handler above). The simplest observer implementation could look something like this &#8230;</p>
<p><script src="https://gist.github.com/3516121.js"></script></p>
<p>Of course, collecting in and by itself is not all that useful. You have to do something with what you&#8217;ve collected (&#8220;the money in the box&#8221;). Let&#8217;s look at another observer that takes a slightly different approach.</p>
<p><script src="https://gist.github.com/3516291.js"></script></p>
<p>Without diving too much into the details, this observer flushes statements to the database as soon as a hard-coded threshold is reached. It does so in a batch-like fashion to minimize the number of roundtrips, but still adhering to the limitations that come with this particular ADO.NET data provider. Other implementations use SqlBulkCopy to maximize performance (but come with their own limitations). Depending on what memory resources a server has you could get pretty creative as to which strategy you choose to rebuild a large projection.</p>
<h3>Conclusion</h3>
<p>I&#8217;ve shown you SQL centric projections, but please, do step out of the box. Nothing is stopping you from producing and collecting &#8220;HttpStatements&#8221; for your favorite key-value store or &#8220;FileOperations&#8221; for your file-based projections. Nothing is stopping you from making different choices for the producing and consuming side. Nothing is stopping you from doing the &#8220;statement&#8221; execution in an asynchronous and/or parallel fashion. It&#8217;s just a matter of exploring your options and use what works in your environment. Next time I&#8217;ll show you how reading fits into all this &#8230;</p>
<p>(*): Yeah, yeah, I know, too much abstraction kills kittens &#8230;</p>
<br />Filed under: <a href='http://seabites.wordpress.com/category/command-query-responsibility-segregation/'>Command &amp; Query Responsibility Segregation</a>, <a href='http://seabites.wordpress.com/category/event-sourcing/'>Event sourcing</a>, <a href='http://seabites.wordpress.com/category/messaging/'>Messaging</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/seabites.wordpress.com/529/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/seabites.wordpress.com/529/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=529&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://seabites.wordpress.com/2012/08/29/the-money-box/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39e267e07e5b6ada467e7e2c559f41b9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yreynhout</media:title>
		</media:content>
	</item>
		<item>
		<title>Viewmodels.js</title>
		<link>http://seabites.wordpress.com/2012/08/05/viewmodels-js/</link>
		<comments>http://seabites.wordpress.com/2012/08/05/viewmodels-js/#comments</comments>
		<pubDate>Sun, 05 Aug 2012 11:53:36 +0000</pubDate>
		<dc:creator>yreynhout</dc:creator>
				<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://seabites.wordpress.com/?p=416</guid>
		<description><![CDATA[Over the past couple of weeks I&#8217;ve been entrenched in UI development, learning &#8211; the good parts of &#8211; javascript and using libs like Knockout.js, jQuery and Twitter BootStrap. My relationship with javascript, which started when Netscape Navigator was still cool, has been on and off over the past years. Given its current rise, it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=416&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Over the past couple of weeks I&#8217;ve been entrenched in UI development, learning &#8211; the good parts of &#8211; javascript and using libs like Knockout.js, jQuery and Twitter BootStrap. My relationship with javascript, which started when Netscape Navigator was still cool, has been on and off over the past years. Given its current rise, it seemed like a good idea to get intimate again. Tasked with building the administrative part of an app that has a fair bit of client-side behavior, we set out to build the first <del>version</del> euh &#8230; failure. It was monolithic, taking on way too many responsibilities in places you didn&#8217;t expect, it had hardly any extensibility points (let alone figuring out where to find them), it was overloaded with technical &#8220;look at my neat trick to make it bark&#8221; concerns, and &#8211; to top it off &#8211; it was borderline unreadable. It&#8217;s safe to say we weren&#8217;t very happy with the result (which would haunt us in the months and years to come). Although, in all fairness, there were some nice gems in there.</p>
<p>So, what were we todo? What any agile team should do &#8230; be honest about it and tackle it head on. I started hacking on what I thought &#8220;it should be more like&#8221; and so did another team member. We sat down as a team, did a code review of the current approach to better understand what the actual problems were, compared it with two more approaches, sollicited feedback, bounced off ideas on how to improve the situation, and settled on a path forward.</p>
<p>It&#8217;s this path forward I want to show you a glimpse of. This is a more in depth exploration of what I touched upon in <a href="http://seabites.wordpress.com/2011/12/31/viewmodels-like-you-meant-it/" title="Viewmodels like you meant it" target="_blank"><strong>viewmodels like you meant it</strong></a>, but in a slightly different context.</p>
<h3>Building blocks</h3>
<p>If you want to prevent logic from creeping into your view, if you want to clearly define how visual state relates to a piece of data or to a trigger, you should refrain from using primitive types in your viewmodel, or you&#8217;ll end up with something similar to the sample below.</p>
<p><script src="https://gist.github.com/3256325.js"></script></p>
<p>Preventing conditions from creeping into views is easy to solve. You just move the condition into the viewmodel, giving it a descriptive name along the way, maybe using a computed observable if it happens to be dynamic. Easy enough and nothing wrong with doing so (in fact I encourage you to do so). What may be less obvious is how to get rid of the primitives. The answer is simple: building the right abstractions.</p>
<p><script src="https://gist.github.com/3256615.js"></script></p>
<p>Being valid or invalid, having focus, visibility, being enabled or disabled, having a value, change tracking and &#8211; in the case of data-binding &#8211; being able to pause and dispose subscriptions are all concerns that revolve around that one value most people seem to put as a primitive in their viewmodel. Don&#8217;t get me wrong, I&#8217;m not suggesting that each and every primitive value requires all these concerns, but I&#8217;d rather put them in my abstraction than polluting my viewmodel when I do need them.<br />
Next to TextInput and NumericInput you can imagine DateInput, TimeInput, SelectOneInput, SelectManyInput, but also more domain specific inputs like PercentageInput (constrained numeric input), MinDefaultMaxDurationsInput (composite input), or even HtmlColorInput (formatted text input).</p>
<p><script src="https://gist.github.com/3256733.js"></script></p>
<p>What applies to inputs also applies to triggers, which are most commonly visualized as buttons and links. A trigger could be used to replace the CanSave observable and Save function in the first example.</p>
<p>It&#8217;s nice to build and have these abstractions, it&#8217;s even better to actually use them. Do realize that, like any abstraction, they emerged from actual viewmodel implementations. For those that think <em>&#8220;this is a controls library&#8221;</em>, I can only feel pitty, because this has little to do with view specific controls.</p>
<h3>Composition is the name of the game</h3>
<p>This was one of the most fundamental insights we had during this journey: the ability to compose viewmodels using the above building blocks and other viewmodels (that in turn have been composed using those building blocks) made them easier to define and reason about. It also helps to keep them at the same level of abstraction.</p>
<p><script src="https://gist.github.com/3256867.js"></script></p>
<p>This line of thought can also be applied at other levels. It has a very recursive and composite feel to it.</p>
<p><script src="https://gist.github.com/3263501.js"></script></p>
<h3>Behavior and low-carb viewmodels</h3>
<p>Something that&#8217;s been missing from the previous snippets is any form of actual behavior. How is one going to introduce logic/flow and interact with dialogs and server side resources? I tend to associate that coordination task with a controller. <em>&#8220;Viewmodels as controllers&#8221;</em> starts to crumble really fast when you&#8217;ve applied the composition above. You&#8217;ll end up with big fat viewmodels that feel like a blatant <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" title="Single responsibility principle" target="_blank">SRP</a> violation.<br />
Therefor making both the controller and how it&#8217;s bound to viewmodels explicit, pays off in readability, testability, and keeping the viewmodels thin. Don&#8217;t get too hung up on the details below, there are many ways to implement these concerns.</p>
<p><script src="https://gist.github.com/3264054.js"></script></p>
<p>Below is a glimpse of what the final binding could look like.</p>
<p><script src="https://gist.github.com/3265543.js"></script></p>
<h3>Conclusion</h3>
<p>This is still very much a <a href="http://en.wikipedia.org/wiki/Work_in_process" title="Work in progress" target="_blank">WIP</a>, but with what little abstractions we&#8217;ve come up with, we&#8217;ve been able to untangle most of the mess we were in before. Dependencies are now easy to replace (adhering to SRP pays off). Composition has simplified the amount of detail we have to worry about at each level of abstraction. Making the controller explicit has kept our viewmodels free of unnecessary dependencies. All in all we&#8217;ve been much more productive using this approach.</p>
<br />Filed under: <a href='http://seabites.wordpress.com/category/ui/'>UI</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/seabites.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/seabites.wordpress.com/416/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=416&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://seabites.wordpress.com/2012/08/05/viewmodels-js/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39e267e07e5b6ada467e7e2c559f41b9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yreynhout</media:title>
		</media:content>
	</item>
		<item>
		<title>Value objects in an event sourced domain model</title>
		<link>http://seabites.wordpress.com/2012/06/18/value-objects-in-an-eventsourced-domain-model/</link>
		<comments>http://seabites.wordpress.com/2012/06/18/value-objects-in-an-eventsourced-domain-model/#comments</comments>
		<pubDate>Mon, 18 Jun 2012 18:56:32 +0000</pubDate>
		<dc:creator>yreynhout</dc:creator>
				<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Event sourcing]]></category>

		<guid isPermaLink="false">http://seabites.wordpress.com/?p=449</guid>
		<description><![CDATA[A question that comes up from time to time is what role value objects can play in an event sourced domain model and how they contribute to and interact with the events produced by said domain model. Value objects come in many shapes and sizes, but obvious ones like money (amount and currency) and period [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=449&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>A question that comes up from time to time is what role <a href="http://domaindrivendesign.org/node/135" title="Value Objects" target="_blank">value objects</a> can play in an <a href="http://martinfowler.com/eaaDev/EventSourcing.html" title="Event sourcing" target="_blank">event sourced</a> <a href="http://domaindrivendesign.org/node/108" title="domain layer" target="_blank">domain model</a> and how they contribute to and interact with the <a href="http://en.wikipedia.org/wiki/Event" title="Event" target="_blank">events</a> produced by said domain model. Value objects come in many shapes and sizes, but obvious ones like money (amount and currency) and period (range of time values) come to mind. Fields that change together or are used together in the <a href="http://domaindrivendesign.org/node/132" title="Ubiquitous language" target="_blank">ubiquitous language</a> are prime suspects of information &#8220;clustering&#8221; and can usually be represented as value objects when they clearly lack any form of identity. They are an ideal place to <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself" title="Don't repeat yourself" target="_blank">DRY</a> up some of the <em>validation</em>, <em>security</em> and <em>contextual capturing</em> code that surrounds these fields.<br />
Below you&#8217;ll find a stripped version of a <a href="http://c2.com/cgi/wiki?ValueObject" title="Value object" target="_blank">value object</a> from my own domain. Things you can&#8217;t easily spot from the code below is the clusivity of the lower and upper boundary of this range-like value type, partially because I omitted the arithmetic operations. The finer details, such as which methods are required and who to collaborate with, surface as you discuss them with domain experts.</p>
<pre class="brush: csharp; title: ; notranslate">
namespace Domain {
  public class RolloutPeriod {
    readonly DateTime _from;
    readonly DateTime _to;

    public RolloutPeriod(DateTime from, DateTime to) {
      if(from &gt; to)
        throw new ArgumentException(&quot;The from value of the rollout period must be less than or equal to the to value.&quot;, &quot;from&quot;);
      _from = from;
      _to = to;
    }

    //Arithmetic members omitted for brevity

    public bool Equals(RolloutPeriod other) {
      if (ReferenceEquals(null, other)) return false;
      if (ReferenceEquals(this, other)) return true;
      return other._from.Equals(_from) &amp;&amp; other._to.Equals(_to);
    }

    public override bool Equals(object obj) {
      if (ReferenceEquals(null, obj)) return false;
      if (ReferenceEquals(this, obj)) return true;
      if (obj.GetType() != typeof (RolloutPeriod)) return false;
      return Equals((RolloutPeriod) obj);
    }

    public override int GetHashCode() {
      return _from.GetHashCode() ^ _to.GetHashCode();
    }
  }
}
</pre>
<p>Something you do notice in the above code is that there are no getters nor setters. This is deliberate. On one hand, it allows me to redefine how I represent values on the inside (i.e. the from and to value). On the other hand, I decouple any consuming code from the internals and gently steer that code into a <a href="http://pragprog.com/articles/tell-dont-ask" title="Tell Don't Ask" target="_blank">Tell Don&#8217;t Ask</a> style of interaction. It makes you think about the question: &#8220;Why do you want access to my internals? What is it that you want to achieve with them? Just tell me what you want to do.&#8221;.<br />
Once you get that right, you bump into the next hurdle: using value objects in an event sourced domain model. </p>
<pre class="brush: csharp; title: ; notranslate">
namespace Domain {
  public class Schedule : AggregateRootEntity {
    public void Rollout(RolloutPeriod period) {
      //Guards ommitted for brevity

      ApplyEvent(Build.RolledoutSchedule.ForPeriod(period));
    }
  }

  public abstract class AggregateRootEntity {
    Guid _id;
    long _version;

    protected void ApplyEvent(IEventBuilder&lt;IEvent&gt; builder) {
      var @event = builder.Build(_id, _version++);
      //The usual stuff as found below goes here
      //https://github.com/gregoryyoung/m-r/blob/master/SimpleCQRS/Domain.cs
    }
  }
}

namespace Messaging {
  public static class Build {
    public static RolledoutScheduleBuilder RolledoutSchedule { get { return new RolledoutScheduleBuilder(); } }
  }

  public class RolledoutScheduleBuilder : IEventBuilder&lt;RolledoutScheduleEvent&gt; {
    RolloutPeriod _rolloutPeriod;

    public RolledoutScheduleEvent Build(Guid id, long version) {
      return new RolledoutScheduleEvent(id, version, _rolloutPeriod);
    }

    public RolledoutScheduleBuilder ForPeriod(RolloutPeriodBuilder builder) {
      _rolloutPeriod = builder.Build();
      return this;
    }
  }

  public interface IEventBuilder&lt;out TEvent&gt; where TEvent : IEvent {
    TEvent Build(Guid id, long version);
  }

  public class RolledoutScheduleEvent : IEvent {
    public RolledoutScheduleEvent(Guid id, long version, RolloutPeriod period) {
      Id = id;
      Version = version;
      Period = period;
    }

    public RolloutPeriod Period { get; private set; }

    public long Version { get; private set; }

    public Guid Id { get; private set; }
  }

  public interface IEvent { }

  public class RolloutPeriod {
    public DateTime From { get; private set; }
    public DateTime To { get; private set; }

    public RolloutPeriod(DateTime from, DateTime to) {
      From = from;
      To = to;
    }
  }

  public class RolloutPeriodBuilder {
    DateTime _from;
    DateTime _to;

    public RolloutPeriodBuilder From(DateTime value) {
      _from = value;
      return this;
    }

    public RolloutPeriodBuilder To(DateTime value) {
      _to = value;
      return this;
    }

    public RolloutPeriod Build() {
      return new RolloutPeriod(_from, _to);
    }
  }
}
</pre>
<p>The rollout period passed into the Schedule rollout method, is a value object part of the domain model. The rollout period of the event &#8211; a bit obfuscated by the syntax &#8216;Build.RolledoutSchedule.ForPeriod&#8217; above &#8211; is part of the messaging bits. How do these two meet without exposing internals?</p>
<blockquote><p>One thing I&#8217;d like to make clear: the domain value object IS NOT the data structure used inside the event. It&#8217;s a different type.</p></blockquote>
<p>By applying double dispatch to the domain rollout period and adding a builder extension method &#8211; inside the domain model &#8211; that is domain rollout period aware, we get a nice translation going.</p>
<pre class="brush: csharp; title: ; notranslate">
namespace Domain {
  public class RolloutPeriod {
    //... see above for other members

    internal void BuildValue(RolloutPeriodBuilder builder) {
      builder.From(_from).To(_to);
    }
  }

  public static class BuildExtensions {
    public static RolledoutScheduleBuilder ForPeriod(this RolledoutScheduleBuilder builder, RolloutPeriod period) {
      var valueBuilder = new RolloutPeriodBuilder();
      period.BuildValue(valueBuilder);
      builder.ForPeriod(valueBuilder);
      return builder;
    }
  }
}
</pre>
<p>The reverse, going from an event (or data structure within that event) to a value object, is just as easy.</p>
<pre class="brush: csharp; title: ; notranslate">
namespace Domain {
  public class RolloutPeriod {
    //... see above for other members

    internal static RolloutPeriod FromEvent(Messaging.RolloutPeriod period) {
      return new RolloutPeriod(period.From, period.To);
    }
  }

  public class Schedule : AggregateRootEntity {
    void Apply(RolledOutScheduleEvent @event) {
      _rolloutPeriod = RolloutPeriod.FromEvent(@event.RolloutPeriod);
      //...
    }
  }
}
</pre>
<p>So, really, there&#8217;s no excuse to NOT use value objects inside an event sourced domain model nor to expose the internal state of those value objects for event sourcing purposes. Happy coding &#8230;</p>
<p>P.S. More information on event builders can be found <a href="http://seabites.wordpress.com/2011/07/27/eventbuilders/" title="Event builders" target="_blank">here</a>.</p>
<br />Filed under: <a href='http://seabites.wordpress.com/category/domain-driven-design/'>Domain Driven Design</a>, <a href='http://seabites.wordpress.com/category/event-sourcing/'>Event sourcing</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/seabites.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/seabites.wordpress.com/449/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=449&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://seabites.wordpress.com/2012/06/18/value-objects-in-an-eventsourced-domain-model/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39e267e07e5b6ada467e7e2c559f41b9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yreynhout</media:title>
		</media:content>
	</item>
		<item>
		<title>CSharp Temporal Method Analysis</title>
		<link>http://seabites.wordpress.com/2012/04/22/csharp-temporal-method-analysis/</link>
		<comments>http://seabites.wordpress.com/2012/04/22/csharp-temporal-method-analysis/#comments</comments>
		<pubDate>Sun, 22 Apr 2012 16:59:20 +0000</pubDate>
		<dc:creator>yreynhout</dc:creator>
				<category><![CDATA[VCS]]></category>

		<guid isPermaLink="false">http://seabites.wordpress.com/?p=447</guid>
		<description><![CDATA[I promised to give you a taste of temporal C# method analysis. You can find a starting point here for git repositories: https://github.com/yreynhout/sharptemporalgitalyzer. Filed under: VCS<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=447&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I promised to give you a taste of temporal C# method analysis. You can find a starting point here for git repositories: <a href="https://github.com/yreynhout/sharptemporalgitalyzer">https://github.com/yreynhout/sharptemporalgitalyzer</a>.</p>
<br />Filed under: <a href='http://seabites.wordpress.com/category/vcs/'>VCS</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/seabites.wordpress.com/447/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/seabites.wordpress.com/447/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=447&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://seabites.wordpress.com/2012/04/22/csharp-temporal-method-analysis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39e267e07e5b6ada467e7e2c559f41b9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yreynhout</media:title>
		</media:content>
	</item>
		<item>
		<title>Your VCS &#8211; the forgotten feedback loop</title>
		<link>http://seabites.wordpress.com/2012/04/19/your-vcs-the-forgotten-feedback-loop/</link>
		<comments>http://seabites.wordpress.com/2012/04/19/your-vcs-the-forgotten-feedback-loop/#comments</comments>
		<pubDate>Thu, 19 Apr 2012 20:58:07 +0000</pubDate>
		<dc:creator>yreynhout</dc:creator>
				<category><![CDATA[VCS]]></category>

		<guid isPermaLink="false">http://seabites.wordpress.com/?p=417</guid>
		<description><![CDATA[Most agile processes are about (shortening) feedback loops. They come in many shapes and sizes: red/green/refactor, burn down charts, continuous integration, ui mockups, quality assurance &#8230; to name a few. One I recently rediscovered is the power of queries on a version control system. If history has taught me anything, it&#8217;s that learning from the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=417&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Most agile processes are about (shortening) feedback loops. They come in many shapes and sizes: red/green/refactor, burn down charts, continuous integration, ui mockups, quality assurance &#8230; to name a few. One I recently rediscovered is the power of queries on a version control system.</p>
<blockquote><p>If history has taught me anything, it&#8217;s that learning from the past is essential to understand the future better. A <a href="http://www.youtube.com/watch?v=4dWE_ag9W6o" title="The GodFather" target="_blank">GodFather</a> inspired quote.</p></blockquote>
<p>While VCS (version control system) statistics are not a new thing and countless tools exist to query the lines of code in a file, in general I find them to be just that &#8230; too general. Nobody knows the specifics and structure of your working copy as well as you do (or maybe you don&#8217;t if you got thrown into it, but that&#8217;s another story).</p>
<blockquote><p>I&#8217;m using the term <em>working copy layout</em> to refer to the structure of code within a specific branch. The term <em>repository layout</em> usually refers to how your branches are structured. Keep this in mind while reading further.</p></blockquote>
<p>Perhaps you&#8217;ve aligned your working copy layout &#8211; like so many have done before us &#8211; along the lines of layers or tiers of a project.<br />
<div id="attachment_420" class="wp-caption aligncenter" style="width: 346px"><a href="http://seabites.files.wordpress.com/2012/03/workingcopylayout.png"><img src="http://seabites.files.wordpress.com/2012/03/workingcopylayout.png?w=630" alt="Picture of a working copy with a layered layout" title="Working Copy - Layered Layout"   class="size-full wp-image-420" /></a><p class="wp-caption-text">Working Copy - Layered Layout</p></div><br />
Eventhough this is not the most flattering example in terms of working copy structure, it&#8217;s already triggering a few questions I might want to ask.</p>
<ul>
<li>Which parts are we working the most on (change the most)?</li>
<li>Any infrastructure we&#8217;re changing all the time (due to bugs)?</li>
<li>How much time/effort are we spending on domain code vs infrastructure code?</li>
<li>Any big files in there we should be worried about (changing all the time)?</li>
<li>Any files in there that have a tendency to change together but cross boundaries (project/tier/layer)?</li>
<li>How are we committing (big/small changesets, frequency of commits)?</li>
<li>Are we committing changes all over the code base or in certain parts of it?</li>
<li>Does each commit bring a set of test files along with it?</li>
<li>&#8230;</li>
</ul>
<p>Needless to say, this list is endless. Both your working copy and VCS are a goldmine waiting to be exploited. The good news is that you don&#8217;t need a whole lot of tooling to get started. Pretty much any language has an API that let&#8217;s you deal with files and folders, so that should cover the working copy part. Most modern version control systems have several language bindings that allow you to automate them. And even if they don&#8217;t, there&#8217;s probably a command line tool you can invoke to get the data you want. So far I tried this out on Git, Mercurial and Subversion without any noticeable problems.</p>
<p>If you want to do any meaningful analysis, you should &#8211; and I can&#8217;t stress this enough &#8211; <strong>start by figuring out which question(s) you want answered</strong>. Some might be answered by merely exporting the log out of the VCS, others by scanning the files and folders in your working copy. The difficult ones &#8211; the ones that go beyond the file name/size into the file content &#8211; might require some script, regular expressions or language parsers to be able to tell you things such as how your code is changing (Michael Feathers has some <a href="http://michaelfeathers.typepad.com/michael_feathers_blog/2011/09/temporal-correlation-of-class-changes.html" title="Temporal Correlation of Class Changes" target="_blank">nice things</a> to say about that). Still others might be answered by correlating log messages to your bug/issue/story tracking tool to certain clusters of code. For simple log related questions I found that <a href="http://en.wikipedia.org/wiki/MapReduce" title="What is map reduce?" target="_blank">map/reduce</a> over a <a href="http://en.wikipedia.org/wiki/Canonical_Model" title="What is a canonical model?" target="_blank">canonical log model</a> can do wonders.</p>
<blockquote><p>I&#8217;ve been able to perform the method-level analysis mentioned by <a href="https://twitter.com/#!/mfeathers" title="Michael Feathers" target="_blank">Michael Feathers</a> &#8211; using <a href="http://wiki.sharpdevelop.net/NRefactory.ashx" title="NRefactory" target="_blank">NRefactory</a> &#8211; to get an idea of how methods evolved in a C# project over time. I&#8217;ll explain that approach in an upcoming post.</p></blockquote>
<p>I&#8217;ll show you a brief example of how you can plot a correlation between changes to files and large files. The first query gives me an idea of the most changed files. It&#8217;s using <a href="http://sharpsvn.open.collab.net/" title="SharpSvn project" target="_blank">SharpSvn</a> to get at each revision. You could use <a href="http://mercurialnet.codeplex.com/" title="Mercurial.NET project" target="_blank">Mercurial.NET</a> or <a href="https://github.com/libgit2/libgit2sharp" title="LibGit2Sharp repository" target="_blank">libgit2sharp</a> to obtain similar results in Mercurial or Git.</p>
<pre class="brush: csharp; title: ; notranslate">
   from mapped in
     (from log in logs
      from path in log.ChangedPaths
      where
        path.Action == SvnChangeAction.Add ||
          path.Action == SvnChangeAction.Modify ||
            path.Action == SvnChangeAction.Replace
      select new {
        Path = path.RepositoryPath,
        CommitCount = 1
      })
  group mapped by mapped.Path
  into reduced
  select new MostChangedPath {
    RepositoryPath = reduced.Key,
    CommitCount = reduced.Sum(i =&gt; i.CommitCount) 
  };
</pre>
<p>Next up is a query that gives me an idea of the line count on a file by file basis. The GetFiles function recursively iterates all files in a working copy that match a given search pattern regular expression (to match multiple files).</p>
<pre class="brush: csharp; title: ; notranslate">
  GetFiles(workingCopyPath, GetSearchPatternExpression(), SearchOption.AllDirectories).
    Select(filePath =&gt; new FileLineCount {
                         FilePath = filePath,
                         LineCount = File.ReadAllLines(filePath).Length
                       });
</pre>
<p>Combined with some charting and unleashed on the .cs files of the popular <a href="http://json.codeplex.com/" title="Json.NET">Json.NET library</a>, you&#8217;ll get something that resembles the diagram below. Yeah, it&#8217;s gonna take some zooming &#8230;<br />
<a href="http://seabites.files.wordpress.com/2012/04/filelinecountandcommit1.png"><img src="http://seabites.files.wordpress.com/2012/04/filelinecountandcommit1.png?w=630&#038;h=630" alt="" title="File line count and commit count" width="630" height="630" class="aligncenter size-full wp-image-438" /></a></p>
<p>Files you don&#8217;t have control over should be excluded (or replaced by something lighter if relevant) from the analysis. Big files that change slowly, only become a priority when you&#8217;ve dealt with big and smaller files that change frequently (in that order).</p>
<h4>Conclusion</h4>
<p>Basically all this log and repository analysis revolves around very simple recipes. Get the log entries, run some map/reduce queries over them, evaluate the results. If the metric doesn&#8217;t buy you much, try another query. Some VCS are slow with regard to feeding your code the log entries, so you might benefit from caching them in a canonical format on disk or in some map/reduce optimized <em>database </em> system. If you&#8217;re using a VCS that is &#8220;slow&#8221; or &#8220;difficult&#8221; to work with in this setting, you may want to convert your repository &#8211; for analysis purposes only (<a href="http://en.wikipedia.org/wiki/Team_Foundation_Server" title="Team Foundation Systems" target="_blank">TFS</a> comes to mind) &#8211; to a VCS that is more suitable.<br />
As usual, I&#8217;ve only scratched the surface of what is possible. People working with legacy code will surely appreciate this <em>new</em> tool in their box and put it to good use. So, please, do try this at home and at work <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . I&#8217;m looking forward to hearing back from you. Without wanting to sound patronizing, I do have a few remarks:</p>
<ul>
<li>Don&#8217;t use this to blame, the why &amp; what are far more important than the who.</li>
<li>Do watch out for false positives, digg for the reasons, and if they&#8217;re just, leave it at that.</li>
<li>This is not a &#8220;silver bullet&#8221; and is best mixed with other static code analysis tools and forms of feedback.</li>
</ul>
<br />Filed under: <a href='http://seabites.wordpress.com/category/vcs/'>VCS</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/seabites.wordpress.com/417/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/seabites.wordpress.com/417/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=417&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://seabites.wordpress.com/2012/04/19/your-vcs-the-forgotten-feedback-loop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39e267e07e5b6ada467e7e2c559f41b9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yreynhout</media:title>
		</media:content>

		<media:content url="http://seabites.files.wordpress.com/2012/03/workingcopylayout.png" medium="image">
			<media:title type="html">Working Copy - Layered Layout</media:title>
		</media:content>

		<media:content url="http://seabites.files.wordpress.com/2012/04/filelinecountandcommit1.png" medium="image">
			<media:title type="html">File line count and commit count</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Windows Azure to build a simple EventStore</title>
		<link>http://seabites.wordpress.com/2012/02/16/using-windows-azure-to-build-a-simple-eventstore/</link>
		<comments>http://seabites.wordpress.com/2012/02/16/using-windows-azure-to-build-a-simple-eventstore/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 19:32:39 +0000</pubDate>
		<dc:creator>yreynhout</dc:creator>
				<category><![CDATA[Command & Query Responsibility Segregation]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Event sourcing]]></category>
		<category><![CDATA[Messaging]]></category>

		<guid isPermaLink="false">http://seabites.wordpress.com/?p=403</guid>
		<description><![CDATA[Continuing my journey, I figured it shouldn&#8217;t be that hard to apply “Your EventStream is a linked list” using Windows Azure Storage Services. Disclaimer: Again, this is just a proof of concept, a way to get started, not production code … obviously. I’m assuming you know your way around Windows Azure (how to set up [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=403&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Continuing my journey, I figured it shouldn&#8217;t be that hard to apply <a href="http://seabites.wordpress.com/2011/12/07/your-eventstream-is-a-linked-list/" title="Your EventStream is a linked list" target="_blank">“Your EventStream is a linked list”</a> using <a href="http://www.windowsazure.com" title="Windows Azure" target="_blank">Windows Azure Storage Services</a>.</p>
<blockquote><p>Disclaimer: Again, this is just a proof of concept, a way to get started, not production code … obviously. I’m assuming you know your way around Windows Azure (how to set up an account, stuff like that) and how the <a href="http://www.microsoft.com/download/en/details.aspx?id=28045" title="Windows Azure .NET SDK" target="_blank">.NET SDK</a> works.</p></blockquote>
<p>Just like with <a href="http://aws.amazon.com" title="Amazon Web Services" target="_blank">Amazon Web Services</a>, the first thing to create are the containers that will harbor the changesets and aggregate heads. Unlike the <a href="http://seabites.wordpress.com/2012/02/11/using-amazon-web-services-to-build-a-simple-event-store/" title="Using Amazon Web Services to build a simple Event Store" target="_blank">AWS example</a>, I&#8217;ve chosen to store both using the same service, <a href="http://www.windowsazure.com/home/tour/storage/" title="Windows Azure Blob Storage" target="_blank">Windows Azure Blob Storage</a>. Why? Because it offers optimistic concurrency by leveraging <a href="http://en.wikipedia.org/wiki/HTTP_ETag" title="HTTP ETag" target="_blank">ETags</a>. The only unfortunate side-effect is that it seems to force me to break CQS, but I can live with that for now. The Windows Azure Blob Storage provides convenience methods to only create the containers when they don&#8217;t exist.</p>
<pre class="brush: csharp; title: ; notranslate">
//Setting up the Windows Azure Blob Storage container to store changesets and aggregate heads

const string PrimaryAccessKey = &quot;your-key-here-for-testing-purposes-only-ofcourse-;-)&quot;;
const string AccountName = &quot;youraccountname&quot;;
const string ChangesetContainerName = &quot;changesets&quot;;
const string AggregateContainerName = &quot;aggregates&quot;;

var storageAccount =
  new CloudStorageAccount(
      new StorageCredentialsAccountAndKey(
            AccountName,
            PrimaryAccessKey),
      false);

var blobClient = storageAccount.CreateCloudBlobClient();

var changesetContainer = blobClient.GetContainerReference(ChangesetContainerName);
changesetContainer.CreateIfNotExist();

var aggregateContainer = blobClient.GetContainerReference(AggregateContainerName);
aggregateContainer.CreateIfNotExist();
</pre>
<p>Now that we’ve set up the infrastructure, let’s tackle the scenario of storing a changeset. The flow is pretty simple. First we try to store the changeset in the changeset container as a blob.</p>
<pre class="brush: csharp; title: ; notranslate">
public class ChangesetDocument {
      public const long InitialVersion = 0;

      //Exposing internals to make the sample easier
      public Guid ChangesetId { get; set; }
      public Guid? ParentChangesetId { get; set; }
      public Guid AggregateId { get; set; }
      public long AggregateVersion { get; set; }
      public string AggregateETag { get; set; }

      public byte[] Content { get; set; }
}

//Assuming there's a changeset document we want to store,
//going by the variable name 'document'.

var changesetBlob = changesetContainer.GetBlobReference(document.ChangesetId.ToString());
var changesetUploadOptions = new BlobRequestOptions {
      AccessCondition = AccessCondition.None,
      BlobListingDetails = BlobListingDetails.None,
      CopySourceAccessCondition = AccessCondition.None,
      DeleteSnapshotsOption = DeleteSnapshotsOption.None,
      RetryPolicy = RetryPolicies.NoRetry(),
      Timeout = TimeSpan.FromSeconds(90),
      UseFlatBlobListing = false
};
changesetBlob.UploadByteArray(document.Content, changesetUploadOptions);

const string AggregateIdMetaName = &quot;aggregateid&quot;;
const string AggregateVersionMetaName = &quot;aggregateversion&quot;;
const string ChangesetIdMetaName = &quot;changesetid&quot;;
const string ParentChangesetIdMetaName = &quot;parentchangesetid&quot;;

//Set the meta-data of the changeset
//Notice how this doesn't need to be transactional
changesetBlob.Metadata[AggregateIdMetaName] = document.AggregateId.ToString();
changesetBlob.Metadata[AggregateVersionMetaName] = document.AggregateVersion.ToString();
changesetBlob.Metadata[ChangesetIdMetaName] = document.ChangesetId.ToString();
if(document.ParentChangesetId.HasValue)
      changesetBlob.Metadata[ParentChangesetIdMetaName] = document.ParentChangesetId.Value.ToString();
changesetBlob.SetMetadata();
</pre>
<p>If that goes well, we try to upsert the head of the aggregate to get it to point to this changeset. Below, we&#8217;re using the ETag of the aggregate head blob as a way of doing optimistic concurrency checking. It caters for both the initial (competing inserters) and update (competing updaters) concurrency.</p>
<pre class="brush: csharp; title: ; notranslate">
public static class ExtensionsForChangeDocument {
      public static AccessCondition ToAccessCondition(this ChangesetDocument document) {
            if(document.AggregateVersion == ChangesetDocument.InitialVersion) {
                  return AccessCondition.IfNoneMatch(&quot;*&quot;);
            }
            return AccessCondition.IfMatch(document.AggregateETag);
      }
}

//Upsert the aggregate
var aggregateBlob = aggregateContainer.GetBlobReference(document.AggregateId.ToString());
var aggregateUploadOptions = new BlobRequestOptions {
      AccessCondition = document.ToAccessCondition(),
      BlobListingDetails = BlobListingDetails.None,
      CopySourceAccessCondition = AccessCondition.None,
      DeleteSnapshotsOption = DeleteSnapshotsOption.None,
      RetryPolicy = RetryPolicies.NoRetry(),
      Timeout = TimeSpan.FromSeconds(90),
      UseFlatBlobListing = false
};
aggregateBlob.UploadByteArray(document.ChangesetId.ToByteArray(), aggregateUploadOptions);
//Here's where we are breaking CQS if we'd like to cache the aggregate.
//This won't be a problem if we're re-reading the aggregate upon each behavior.
var eTag = aggregateBlob.Properties.ETag;
</pre>
<p>If the UploadByteArray operation throws a StorageClientException indicating that “The condition specified using HTTP conditional header(s) is not met.”, we know there was some form of optimistic concurrency. In such a case, it’s best to repeat the entire operation.<br />
Now that we&#8217;ve dealt with writing, let&#8217;s take a look at reading. First we need to fetch the pointer to the last stored and approved changeset identifier.</p>
<pre class="brush: csharp; title: ; notranslate">
var aggregateBlob = aggregateContainer.GetBlobReference(aggregateId.ToString());
var aggregateDownloadOptions = new BlobRequestOptions {
      AccessCondition = AccessCondition.None,
      BlobListingDetails = BlobListingDetails.None,
      CopySourceAccessCondition = AccessCondition.None,
      DeleteSnapshotsOption = DeleteSnapshotsOption.None,
      RetryPolicy = RetryPolicies.NoRetry(),
      Timeout = TimeSpan.FromSeconds(90),
      UseFlatBlobListing = false
};
var changesetId = new Guid?(new Guid(aggregateBlob.DownloadByteArray(aggregateDownloadOptions)));
var eTag = aggregateBlob.Properties.ETag;
</pre>
<p>Now that we’ve bootstrapped the reading process, we keep reading each changeset, until there’s no more changeset to read. Each approved changeset contains metadata that points to the previous approved changeset. It’s the responsibility of the calling code todo something useful with the read changesets (e.g. deserialize the content and replay each embedded event into the corresponding aggregate).</p>
<pre class="brush: csharp; title: ; notranslate">
while(changesetId.HasValue) {
      var changesetBlob = changesetContainer.GetBlobReference(changesetId.Value.ToString());
      var changesetDownloadOptions = new BlobRequestOptions {
            AccessCondition = AccessCondition.None,
            BlobListingDetails = BlobListingDetails.None,
            CopySourceAccessCondition = AccessCondition.None,
            DeleteSnapshotsOption = DeleteSnapshotsOption.None,
            RetryPolicy = RetryPolicies.NoRetry(),
            Timeout = TimeSpan.FromSeconds(90),
            UseFlatBlobListing = false
      };
      var content = changesetBlob.DownloadByteArray(changesetDownloadOptions);
      changesetBlob.FetchAttributes();
      var document = new ChangesetDocument {
            AggregateETag = eTag,
            AggregateId = new Guid(changesetBlob.Metadata[AggregateIdMetaName]),
            AggregateVersion = Convert.ToInt64(changesetBlob.Metadata[AggregateVersionMetaName]),
            ChangesetId = new Guid(changesetBlob.Metadata[ChangesetIdMetaName]),
            Content = content,
      };
      if (changesetBlob.Metadata[ParentChangesetIdMetaName] != null)
            document.ParentChangesetId = new Guid(changesetBlob.Metadata[ParentChangesetIdMetaName]);

      yield return document;

      changesetId = document.ParentChangesetId;
}
</pre>
<p>The only *weird* thing with this code is that I&#8217;m propagating the aggregate&#8217;s head ETag using the changesets. It&#8217;s a modeling issue I&#8217;ll have to revisit <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .<br />
But that’s basically all there’s to it. In reality, you’ll need a lot more metadata and error handling to make this a success. I should point out that the performance of this service consumed on premise was better than what I experienced with AWS.</p>
<h1>Conclusion</h1>
<p>Using Windows Azure Storage Services is not so different from the Amazon Web Services in this case. However, this overall technique suffers from a few drawbacks. As mentioned before, upon concurrency, you might be wasting some storage space. Another drawback is the fact that you need to read all changesets that make up the history of an aggregate (or eventsource if want to decouple it from DDD terminology) before being able to apply the first event. There&#8217;s ways around this, such as storing all the changeset identifiers in the aggregate head if the total number of behaviors in an aggregate is low on average. You could even partition the aggregate head into multiple documents using nothing but its version number or count of applied behaviors to partition, but that&#8217;s the subject of a future exploration. I apologize if this post is a somewhat copy-paste of its AWS counterpart, but given the goal and similarities that was to be expected <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<br />Filed under: <a href='http://seabites.wordpress.com/category/command-query-responsibility-segregation/'>Command &amp; Query Responsibility Segregation</a>, <a href='http://seabites.wordpress.com/category/domain-driven-design/'>Domain Driven Design</a>, <a href='http://seabites.wordpress.com/category/event-sourcing/'>Event sourcing</a>, <a href='http://seabites.wordpress.com/category/messaging/'>Messaging</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/seabites.wordpress.com/403/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/seabites.wordpress.com/403/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=403&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://seabites.wordpress.com/2012/02/16/using-windows-azure-to-build-a-simple-eventstore/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39e267e07e5b6ada467e7e2c559f41b9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yreynhout</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Amazon Web Services to build a simple Event Store</title>
		<link>http://seabites.wordpress.com/2012/02/11/using-amazon-web-services-to-build-a-simple-event-store/</link>
		<comments>http://seabites.wordpress.com/2012/02/11/using-amazon-web-services-to-build-a-simple-event-store/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 19:48:42 +0000</pubDate>
		<dc:creator>yreynhout</dc:creator>
				<category><![CDATA[Command & Query Responsibility Segregation]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Event sourcing]]></category>
		<category><![CDATA[Messaging]]></category>

		<guid isPermaLink="false">http://seabites.wordpress.com/?p=376</guid>
		<description><![CDATA[My post on &#8220;Your EventStream is a linked list&#8221; might have been somewhat abstract (a more prosaic version can be found here). By way of testing my theory, I set out to apply it using Amazon&#8217;s Web Services (AWS) stack. I used AWS DynamoDB as the transactional medium that would handle the optimistic concurrency, and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=376&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>My post on &#8220;<a href="http://seabites.wordpress.com/2011/12/07/your-eventstream-is-a-linked-list/" title="Your EventStream is a linked list" target="_blank">Your EventStream is a linked list</a>&#8221; might have been somewhat abstract (a more prosaic version can be found <a href="http://stackoverflow.com/questions/9083972/is-it-possible-to-make-conditional-inserts-with-azure-table-storage/9085397#9085397" title="Prosaic version of Your EventStream is a linked list" target="_blank">here</a>). By way of <em>testing</em> my <em>theory</em>, I set out to apply it using Amazon&#8217;s Web Services (AWS) stack. I used AWS DynamoDB as the transactional medium that would handle the optimistic concurrency, and AWS S3 to store the changesets.</p>
<blockquote><p>Disclaimer: This is just a proof of concept, a way to get started. It&#8217;s not production code &#8230; obviously <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  I&#8217;m assuming you know your way around AWS (how to set up an account, stuff like that) and how the <a href="http://aws.amazon.com/net/" title="Windows &amp; .NET SDK home" target="_blank">.NET SDK</a> works.</p></blockquote>
<p>The first thing todo is to create the container for the <em>changesets</em>. In AWS S3 those things are called <em>buckets</em>. This is a one time operation. You might want to make this conditional at the startup of your application/worker role/what-have-you. The AWS S3 API provides mechanisms to query for buckets, so you should be able to pull that off. On the other hand, when creating buckets using the REST API the HTTP PUT verb is used, and we all know that PUT is supposed to be idempotent. You might want to read up on the <a href="http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUT.html" title="AWS S3 PUT Bucket API" target="_blank">PUT Bucket API</a>. One last thing: bucket names are unique across all of S3.</p>
<pre class="brush: csharp; title: ; notranslate">
//Setting up the Amazon S3 bucket to store changesets in
const string ChangesetBucketName = &quot;yourorganization_yourboundedcontextname_changesets&quot;;

var s3Client = AWSClientFactory.CreateAmazonS3Client();

s3Client.PutBucket(
  new PutBucketRequest().
    WithBucketName(ChangesetBucketName).
    WithBucketRegion(S3Region.EU)); //Your region might vary
</pre>
<p>The same thing needs to happen in AWS DynamoDB. There, the containers are called <em>tables</em>. Again, this is a one time, conditional operation. You might wanna <a href="http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/API_CreateTable.html" title="AWS DynamoDB CreateTable API docs" target="_blank">read up</a> on the specifics of table naming and uniqueness. The <a href="http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/ProvisionedThroughputIntro.html" title="AWS DynamoDB throughput provisioning" target="_blank">throughput provisioning</a> is not something you want to be static. Monitor the load on your system (they have notification events/alerts for that), both in terms of number of reads/writes and bytes used for storage, and use that information to tune the table throughput settings.</p>
<pre class="brush: csharp; title: ; notranslate">
//Setting up the Amazon DynamoDB table to store aggregates in
const string AggregateTableName = &quot;yourboundedcontextname_aggregates&quot;;
const string AggregateIdName = &quot;aggregate-id&quot;;

//At the time of writing, DynamoDBClient didn't make it yet
//into the AWSClientFactory.
var dynamoDbClient = new AmazonDynamoDBClient();

dynamoDbClient.CreateTable(
  new CreateTableRequest().
    WithTableName(AggregateTableName).
    WithKeySchema(
      new KeySchema().
        WithHashKeyElement(
          new KeySchemaElement().
            WithAttributeName(AggregateIdName).
            WithAttributeType(&quot;S&quot;)
        ).
        WithRangeKeyElement(null)
    ).
    WithProvisionedThroughput(
      new ProvisionedThroughput().
        WithReadCapacityUnits(300).
        WithWriteCapacityUnits(500)
    )
  );
</pre>
<p>Now that we&#8217;ve set up the <em>infrastructure</em>, let&#8217;s tackle the scenario of storing a changeset. The flow is pretty simple. First we try to store the changeset in AWS S3 as an object in the configured bucket.</p>
<pre class="brush: csharp; title: ; notranslate">
//An internal representation of the changeset
public class ChangesetDocument {
  public const long InitialValue = 0;

  //Exposing internal state here to
  //simplify the example.
  public Guid AggregateId { get; set; }
  public long AggregateVersion { get; set; }
  public Guid ChangesetId { get; set; }
  public Guid? ParentChangesetId { get; set; }

  public byte[] Content { get; set; }

  public Stream GetContentStream() {
    return new MemoryStream(Content, writable: false);
  }
}

//Assuming there's a changeset document we want to store,
//going by the variable name 'document'.

const string AggregateIdMetaName = &quot;x-amz-meta-aggregate-id&quot;;
const string AggregateVersionMetaName = &quot;x-amz-meta-aggregate-version&quot;;
const string ChangesetIdMetaName = &quot;x-amz-meta-changeset-id&quot;;
const string ParentChangesetIdMetaName = &quot;x-amz-meta-parent-changeset-id&quot;;

var putObjectRequest = new PutObjectRequest().
  WithBucketName(ChangesetBucketName).
  WithGenerateChecksum(true).
  WithKey(document.ChangesetId.ToString()).
  WithMetaData(ChangesetIdMetaName, document.ChangesetId.ToString()).
  WithMetaData(AggregateIdMetaName, document.AggregateId.ToString()).
  WithMetaData(AggregateVersionMetaName, Convert.ToString(document.AggregateVersion));

if (document.ParentChangesetId.HasValue) {
  putObjectRequest.WithMetaData(ParentChangesetIdMetaName, document.ParentChangesetId.Value.ToString());
}

putObjectRequest.WithInputStream(document.GetContentStream());

s3Client.PutObject(putObjectRequest);
</pre>
<p>If that goes well, we try to create an item in the configured AWS DynamoDB table. The expected values below are the AWS DynamoDB way of doing optimistic concurrency checking. They cater for both the initial (competing inserters) and update (competing updaters) concurrency.</p>
<blockquote><p>In this example I&#8217;m using an incrementing version number to do that. Strictly speaking, I could be using the changeset identifier for that, but an incrementing version number is easier to relate to when you come from an ORM/database background.
</p></blockquote>
<pre class="brush: csharp; title: ; notranslate">
const string AggregateVersionName = &quot;aggregate-version&quot;;
const string ChangesetIdName = &quot;changeset-id&quot;;

public static class ExtensionsForChangesetDocument {
  public static KeyValuePair&lt;string, ExpectedAttributeValue&gt;[] ToExpectedValues(this ChangesetDocument document) {
    var dictionary = new Dictionary&lt;string, ExpectedAttributeValue&gt;();
    if(document.AggregateVersion == ChangesetDocument.InitialValue) {
      //Make sure we're the first to create the aggregate
      dictionary.Add(AggregateIdName,
        new ExpectedAttributeValue().
          WithExists(false)
      );
    } else {
      //Make sure nobody changed the aggregate behind our back 
      dictionary.Add(AggregateIdName,
        new ExpectedAttributeValue().
          WithExists(true).
          WithValue(
            new AttributeValue().
              WithS(document.AggregateId.ToString()))
      );
      dictionary.Add(AggregateVersionName,
        new ExpectedAttributeValue().
          WithValue(
            new AttributeValue().
              WithN(Convert.ToString(document.AggregateVersion - 1)))
      );
    }
    return dictionary.ToArray();
  }

  public static KeyValuePair&lt;string, AttributeValue&gt;[] ToItemValues(this ChangesetDocument document) {
    //The relevant values to store in the item.
    var dictionary = new Dictionary&lt;string, AttributeValue&gt; {
      {AggregateIdName, new AttributeValue().WithS(document.AggregateId.ToString())},
      {AggregateVersionName, new AttributeValue().WithN(Convert.ToString(document.AggregateVersion))},
      {ChangesetIdName, new AttributeValue().WithS(document.ChangesetId.ToString())},
    };
    return dictionary.ToArray();
  }
}

dynamoDbClient.PutItem(
  new PutItemRequest().
    WithTableName(AggregateTableName).
    WithExpected(document.ToExpectedValues()).
    WithItem(document.ToItemValues())
  );
</pre>
<p>If the PutItem operation throws an exception indicating that the &#8220;ConditionalCheckFailed&#8221;, we know there was some form of optimistic concurrency. In such a case, it&#8217;s best to repeat the entire operation.</p>
<blockquote><p>I&#8217;ve left duplicate command processing elimination as an exercise for the reader <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p></blockquote>
<p>The only thing left todo is showing the reverse operation, reading. First we need to fetch the pointer to the last stored and approved changeset identifier. I hope you do realize that a consistent read is not strictly necessary, since the likelihood of concurrency should be low.</p>
<pre class="brush: csharp; title: ; notranslate">
var getItemResponse = dynamoDbClient.GetItem(
  new GetItemRequest().
    WithTableName(AggregateTableName).
    WithKey(
      new Key().
        WithHashKeyElement(
          new AttributeValue().
            WithS(aggregateId.ToString())).
        WithRangeKeyElement(null)).
    WithConsistentRead(false).
    WithAttributesToGet(ChangesetIdName)
  );
</pre>
<p>Now that we&#8217;ve bootstrapped the reading process, we keep reading each changeset, until there&#8217;s no more changeset to read. Each approved changeset contains metadata that points to the previous approved changeset. It&#8217;s the responsibility of the calling code todo something useful with the read changesets (e.g. deserialize the content and replay each embedded event into the corresponding aggregate).</p>
<pre class="brush: csharp; title: ; notranslate">
var changesetId = new Guid?(new Guid(getItemResponse.GetItemResult.Item[ChangesetIdName].S));
while(changesetId.HasValue) {
  var getObjectResponse = s3Client.GetObject(
    new GetObjectRequest().
      WithBucketName(ChangesetBucketName).
      WithKey(changesetId.Value.ToString()));
  var document = new ChangesetDocument {
    AggregateId = new Guid(getObjectResponse.Metadata[AggregateIdMetaName]),
    AggregateVersion = Convert.ToInt64(getObjectResponse.Metadata[AggregateVersionMetaName]),
    ChangesetId = new Guid(getObjectResponse.Metadata[ChangesetIdMetaName]),
    Content = getObjectResponse.ResponseStream.ToByteArray()
  };
  var parentChangesetIdAsString = getObjectResponse.Metadata[ParentChangesetIdMetaName];
  if(parentChangesetIdAsString != null) {
    document.ParentChangesetId = new Guid(parentChangesetIdAsString);
  }

  changesetId = document.ParentChangesetId;

  yield return document;
}
</pre>
<p>And that&#8217;s basically all there&#8217;s to it. In reality, you&#8217;ll need a lot more metadata and error handling to make this a success. I should point out that the performance of these services consumed on premise was abominal. I&#8217;m assuming (hoping) that if you consume them from an AWS EC2 instance, performance will be much better.</p>
<h2>Conclusion</h2>
<p>A clever reader will notice that this technique can easily be transposed to other document/data stores. The specifics will vary, but the theme will be the same. The reason for applying this technique has mainly todo with the inherent constraints of certain datastores. The transactional ones &#8211; especially in cloudy environments &#8211; are limited with regard to the number of bytes they can store, which makes them less desirable for storing payloads in (prediction of payload size can be hard at times). There are easy ways to side-step concurrency problems such as serializing all aggregate access to one worker/queue, but that&#8217;s another discussion.</p>
<br />Filed under: <a href='http://seabites.wordpress.com/category/command-query-responsibility-segregation/'>Command &amp; Query Responsibility Segregation</a>, <a href='http://seabites.wordpress.com/category/domain-driven-design/'>Domain Driven Design</a>, <a href='http://seabites.wordpress.com/category/event-sourcing/'>Event sourcing</a>, <a href='http://seabites.wordpress.com/category/messaging/'>Messaging</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/seabites.wordpress.com/376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/seabites.wordpress.com/376/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=376&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://seabites.wordpress.com/2012/02/11/using-amazon-web-services-to-build-a-simple-event-store/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39e267e07e5b6ada467e7e2c559f41b9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yreynhout</media:title>
		</media:content>
	</item>
		<item>
		<title>Viewmodels like you meant it</title>
		<link>http://seabites.wordpress.com/2011/12/31/viewmodels-like-you-meant-it/</link>
		<comments>http://seabites.wordpress.com/2011/12/31/viewmodels-like-you-meant-it/#comments</comments>
		<pubDate>Sat, 31 Dec 2011 14:07:42 +0000</pubDate>
		<dc:creator>yreynhout</dc:creator>
				<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://seabites.wordpress.com/?p=341</guid>
		<description><![CDATA[Although a lot has been written on the topic of viewmodels &#8211; both in the MVVM(*) and MVC UI pattern space &#8211; it strikes me as odd how underused they are. Why muck around with resource files and attribute based validation hints inside your views when you could provide them with a rich viewmodel? Most [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=341&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Although a lot has been written on the topic of <em>viewmodels</em> &#8211; both in the MVVM(*) and MVC UI pattern space &#8211; it strikes me as odd how underused they are. Why muck around with resource files and attribute based validation hints inside your views when you could provide them with a rich viewmodel? Most people tend to think about their viewmodels in terms of <em>input</em>, either directly (e.g. the value of a textbox) or indirectly (e.g. one of the values you choose from a select, or a list from which to select a row). Why isn&#8217;t <em>output</em> equally as important? Why are those magic strings in the view not part of the viewmodel? Why don&#8217;t people question the implicit routing and navigation that goes into their views?</p>
<blockquote><p>Not including output in a view model and <a title="Primitive obsession" href="http://jamesshore.com/Blog/PrimitiveObsession.html">primitive obsession</a> are the main reasons why logic and other dependencies creep into your view.</p></blockquote>
<p>Let&#8217;s take the screen of my post on <a title="Your UI is a statechart" href="http://seabites.wordpress.com/2011/12/08/your-ui-is-a-statechart/" target="_blank">statecharts</a> as an example.</p>
<p><a href="http://seabites.files.wordpress.com/2011/11/masterdetail1.png"><img class="aligncenter size-full wp-image-287" title="Master Detail UI - Example" src="http://seabites.files.wordpress.com/2011/11/masterdetail1.png?w=630&#038;h=399" alt="Master Detail UI - Example" width="630" height="399" /></a><br />
What most people end up with &#8211; if they even put in the effort to craft an explicit view model &#8211; is something along these lines.</p>
<pre class="brush: csharp; title: ; notranslate">
public class TrainStationModel {
  public string FilteredBy { get; set; }
  public List TrainStations { get; set; }
  public TrainStationDetails SelectedTrainStation { get; set; }
}

public class TrainStationItem {
  public Guid TrainStationId { get; set; }
  public string Name { get; set; }
  public string Abbreviation { get; set; }
}

public class TrainStationDetails {
  public Guid TrainStationId { get; set; }
  [Required, StringLength(80, MinLength = 1)]
  public string Name { get; set; }
  [Required, StringLength(40, MinLength = 1)]
  public string Abbreviation { get; set; }
  [Required, Range(0,360)]
  public double Longitude { get; set; }
  [Required, Range(0,360)]
  public double Latitude { get; set; }
}
</pre>
<p>If we&#8217;d want to I18N this screen it would require either a view per culture or a culture+resource aware view. For client-side validation it requires reflective helpers to get the meta-data, sprinkled as attributes on those model properties. Routing and navigation creep into the view as behaviors need to be executed.</p>
<p>Contrast this with what I&#8217;ve been using as viewmodels &#8211; below &#8211; for the past 7 years.</p>
<pre class="brush: csharp; title: ; notranslate">
  public class TrainStationAdministrationModel : Page {
    readonly TextOutputField _header;
    readonly TrainStationBodyModel _body;

    public TrainStationAdministrationModel() : base(&quot;trainstationadministration&quot;) {
      _header = new TextOutputField(&quot;header&quot;, 
        new Text(new StringResource(&quot;EurostarStationsKey&quot;)));
      _body = new TrainStationBodyModel(&quot;body&quot;);
    }

    public TextOutputField Header {
      get { return _header; }
    }

    public TrainStationBodyModel Body {
      get { return _body; }
    }
  }

  public class TrainStationBodyModel : ModelItem {
    readonly Trigger&lt;TextInputField&gt; _filterByTrigger;
    readonly TrainStationDetailModel _detail;
    readonly SelectList&lt;TrainStationListItem&gt; _list;
    readonly Trigger&lt;TextOutputField&gt; _newTrigger;
    
    public TrainStationBodyModel(string id) : base(id) {
      _detail = new TrainStationDetailModel(&quot;detail&quot;);
      _list = new SelectList&lt;TrainStationListItem&gt;(&quot;list&quot;);
      _newTrigger = new Trigger&lt;TextOutputField&gt;(&quot;newtrigger&quot;, &quot;new&quot;, 
        new TextOutputField(&quot;newtext&quot;, 
          new Text(new StringResource(&quot;NewTriggerKey&quot;))));
      _filterByTrigger = new Trigger&lt;TextInputField&gt;(&quot;filterbytrigger&quot;, &quot;filterby&quot;,
        new TextInputField(&quot;filterbytext&quot;, 
          new Text(new StringResource(&quot;FilterByKey&quot;))));
    }

    public Trigger&lt;TextInputField&gt; FilterByTrigger {
      get { return _filterByTrigger; }
    }

    public SelectList&lt;TrainStationListItem&gt; List {
      get { return _list; }
    }

    public TrainStationDetailModel Detail {
      get { return _detail; }
    }

    public Trigger&lt;TextOutputField&gt; NewTrigger {
      get { return _newTrigger; }
    }
  }

  public class TrainStationListItem : SelectListItem {
    readonly Trigger&lt;TextOutputField&gt; _deleteTrigger;
    readonly TextOutputField _name;
    readonly TextOutputField _abbreviation;

    public TrainStationListItem(string id) : base(id) {
      _name = new TextOutputField(&quot;name&quot;, 
        Text.Empty);
      _abbreviation = new TextOutputField(&quot;abbreviation&quot;,
        Text.Empty);
      _deleteTrigger = new Trigger&lt;TextOutputField&gt;(
        &quot;deletetrigger&quot;,
        &quot;delete&quot;,
        id,
        new TextOutputField(&quot;deletetext&quot;, 
          new Text(&quot;DeleteTriggerKey&quot;)));
    }

    public TextOutputField Abbreviation {
      get { return _abbreviation; }
    }

    public TextOutputField Name {
      get { return _name; }
    }

    public Trigger&lt;TextOutputField&gt; DeleteTrigger {
      get { return _deleteTrigger; }
    } 
  }

  public class TrainStationDetailModel : ModelItem {
    readonly TextOutputField _header;
    readonly TrainStationDetailNamingModel _naming;
    readonly TrainStationDetailGeolocationModel _geolocation;
    readonly Trigger&lt;TextOutputField&gt; _saveTrigger;
    readonly Trigger&lt;TextOutputField&gt; _cancelTrigger;

    public TrainStationDetailModel(string id) : base(id) {
      _header = new TextOutputField(&quot;header&quot;, 
        new Text(new StringResource(&quot;ChangeTheSettingsKey&quot;)));
      _naming = new TrainStationDetailNamingModel(&quot;naming&quot;);
      _geolocation = new TrainStationDetailGeolocationModel(&quot;geolocation&quot;);
      _saveTrigger = new Trigger&lt;TextOutputField&gt;(id, &quot;add&quot;,
        new TextOutputField(&quot;savetrigger&quot;,
          new Text(new StringResource(&quot;SaveTriggerKey&quot;))));
      _cancelTrigger = new Trigger&lt;TextOutputField&gt;(id, &quot;add&quot;,
        new TextOutputField(&quot;canceltrigger&quot;,
          new Text(new StringResource(&quot;CancelTriggerKey&quot;))));
    }

    public TextOutputField Header {
      get { return _header; }
    }

    public TrainStationDetailGeolocationModel Geolocation {
      get { return _geolocation; }
    }

    public TrainStationDetailNamingModel Naming {
      get { return _naming; }
    }

    public Trigger&lt;TextOutputField&gt; SaveTrigger {
      get { return _saveTrigger; }
    }

    public Trigger&lt;TextOutputField&gt; CancelTrigger {
      get { return _cancelTrigger; }
    }
  }

  public class TrainStationDetailNamingModel : ModelItem {
    readonly TextOutputField _header;
    readonly TextInputField _name;
    readonly TextInputField _abbreviation;
    
    public TrainStationDetailNamingModel(string id) : base(id) {
      _header = new TextOutputField(&quot;header&quot;, 
        new Text(new StringResource(&quot;NamingKey&quot;)));
      _name = new TextInputField(&quot;name&quot;, 
        new Text(new StringResource(&quot;NameKey&quot;)));
      _abbreviation = new TextInputField(&quot;abbreviation&quot;, 
        new Text(new StringResource(&quot;AbbreviationKey&quot;)));
    }

    public TextOutputField Header {
      get { return _header; }
    }

    public TextInputField Name {
      get { return _name; }
    }

    public TextInputField Abbreviation {
      get { return _abbreviation; }
    }
  }

  public class TrainStationDetailGeolocationModel : ModelItem {
    readonly TextOutputField _header;
    readonly DoubleInputField _longitude;
    readonly DoubleInputField _latitude;

    public TrainStationDetailGeolocationModel(string id) : base(id) {
      _header = new TextOutputField(&quot;header&quot;, 
        new Text(new StringResource(&quot;GeolocationKey&quot;)));
      _longitude = new DoubleInputField(&quot;longitude&quot;, 
        new Text(new StringResource(&quot;LongitudeKey&quot;)));
      _latitude = new DoubleInputField(&quot;latitude&quot;, 
        new Text(new StringResource(&quot;LatitudeKey&quot;)));
    }

    public TextOutputField Header {
      get { return _header; }
    }

    public DoubleInputField Longitude {
      get { return _longitude; }
    }

    public DoubleInputField Latitude {
      get { return _latitude; }
    }
  }
</pre>
<p>Notice the lack of primitives and attributes, the presence of output fields, and routing/navigation using triggers. Input fields cater for values, change tracking, field level validation, provisioning of meta-data to be used as UX enhancements. The explicitness of the models allows for strongly-typed partial views. Text fields and formatted values work together with the current UI culture and resources to allow for seamless I18N. Making viewmodels visitable (e.g. solves the puzzle of how to capture form input in a model) and queryable (e.g. lookup of items in the model &#8211; hence the identifiers) will allow for even greater power inside controllers. Triggers and associated events work nicely together with a hierarchic statechart to control flow. The use of standardized model types allows for augmenting productivity in views by introducing helpers.</p>
<h4>Conclusion</h4>
<p>As always, I left out a lot of the finer details. If there&#8217;s one thing that should stick, it&#8217;s to take a deeper, harder look at how you&#8217;ve been dealing with viewmodels &#8230;</p>
<p><em>(*): not discussed here.</em></p>
<br />Filed under: <a href='http://seabites.wordpress.com/category/ui/'>UI</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/seabites.wordpress.com/341/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/seabites.wordpress.com/341/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=seabites.wordpress.com&#038;blog=17146592&#038;post=341&#038;subd=seabites&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://seabites.wordpress.com/2011/12/31/viewmodels-like-you-meant-it/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39e267e07e5b6ada467e7e2c559f41b9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">yreynhout</media:title>
		</media:content>

		<media:content url="http://seabites.files.wordpress.com/2011/11/masterdetail1.png" medium="image">
			<media:title type="html">Master Detail UI - Example</media:title>
		</media:content>
	</item>
	</channel>
</rss>
