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

<channel>
	<title>JDT&#039;s Blog &#187; DDD</title>
	<atom:link href="http://jdt.toron.be/blog/tag/ddd/feed/" rel="self" type="application/rss+xml" />
	<link>http://jdt.toron.be/blog</link>
	<description>JDT on computers, programming and pie</description>
	<lastBuildDate>Tue, 03 Nov 2009 17:52:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ubiquitous language: what is so blindingly obvious you can&#8217;t see it?</title>
		<link>http://jdt.toron.be/blog/2009/08/ubiquitous-language-what-is-so-blindingly-obvious-you-cant-see-it/</link>
		<comments>http://jdt.toron.be/blog/2009/08/ubiquitous-language-what-is-so-blindingly-obvious-you-cant-see-it/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 20:59:26 +0000</pubDate>
		<dc:creator>JDT</dc:creator>
				<category><![CDATA[Various]]></category>
		<category><![CDATA[DDD]]></category>

		<guid isPermaLink="false">http://jdt.toron.be/blog/?p=19</guid>
		<description><![CDATA[Ubiquitous Language is the term Eric Evans uses in Domain Driven 	Design for the practice of building up a common, rigorous language 	between developers and users. This language should be based on the 	Domain Model used in the software &#8211; hence the need for it to be 	rigorous, since software doesn&#8217;t cope well with ambiguity.
&#8211; [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>Ubiquitous Language is the term Eric Evans uses in <a href="http://www.amazon.com/gp/product/0321125215">Domain Driven 	Design</a> for the practice of building up a common, rigorous language 	between developers and users. This language should be based on the 	<a href="http://martinfowler.com/eaaCatalog/domainModel.html">Domain Model</a> used in the software &#8211; hence the need for it to be 	rigorous, since software doesn&#8217;t cope well with ambiguity.<br />
&#8211; <a href="http://www.martinfowler.com/bliki/UbiquitousLanguage.html" target="_blank">Martin Fowler</a></em></p></blockquote>
<p><strong>Abstract</strong></p>
<p>In which the author outlines the basics of the &#8216;Ledger Pattern&#8217;, an addition to the ubiquitous language and domain model to help facilitate a more natural approach to DDD and OO. It is specifically designed to deal with issues of validation and object creation that occur during object instantiation.</p>
<p><strong>Concrete</strong></p>
<p>Some things are obvious.  Some things are very obvious. And some things are so obvious that you actually need to take a step back in order to see them.  <a href="http://www.udidahan.com" target="_blank">Udi Dahan</a> helped me take that step back some time ago with his article &#8216;<a href="http://www.udidahan.com/2009/06/29/dont-create-aggregate-roots/" target="_blank">Don&#8217;t create aggregate roots</a>&#8216;.  While I agree with him on most of what he details in his post, I feel that something is missing.</p>
<p>What Udi points out is that we should never create instances of domain objects from the service layer (or command handler layer, whatever the layer is that coördinates calls to domain objects). Instead, it is the responsibility of an aggregate root to create a new entity that belongs to his aggregate.  In some cases, this is obvious:</p>
<pre>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw4">class</span> Order
<span class="br0">&#123;</span>
&nbsp; <span class="kw1">public</span> <span class="kw1">void</span> AddOrderLine<span class="br0">&#40;</span>OrderLine line<span class="br0">&#41;</span>
&nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; orderLines.<span class="me1">Add</span><span class="br0">&#40;</span>line<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>

&nbsp; <span class="kw1">public</span> <span class="kw1">void</span> AddOrderLine<span class="br0">&#40;</span>Product product, <span class="kw4">int</span> quantity<span class="br0">&#41;</span>
&nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; orderLines.<span class="me1">Add</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> OrderLine<span class="br0">&#40;</span>product, quantity<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>The latter method is actually the better: it is very easy to perform validation on the parameters passed before the orderline object is constructed.  This is closely linked to something I blogged on before: domain object validation is done on a context basis.</p>
<p>And even if you believe that validation can be done, consider the example where we create a customer</p>
<pre>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">void</span> Handle<span class="br0">&#40;</span>CreateCustomerCommand cmd<span class="br0">&#41;</span>
<span class="br0">&#123;</span>
&nbsp; ICustomerRepository repo <span class="sy0">=</span> RepositoryFactory.<span class="me1">Get</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; Customer customer <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> Customer<span class="br0">&#40;</span>cmd.<span class="me1">FirstName</span>, cmd.<span class="me1">LastName</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; repo.<span class="me1">Save</span><span class="br0">&#40;</span>customer<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>Where is the validation code now? Is it in the constructor? If so, what if we need different validation logic? What if our application goes from being an application used in a store to a web application? Now a customer comes through your website and you need to ensure that an email-address is supplied. We have to face facts: this solution is not adequate.</p>
<p>What we need is a domain concept that fits with the &#8216;this object can add customers&#8217;. So we use our common sense: in the store, the Employee-object can add a user. But who adds employees? Well, that would be the store owner. But where does he come from?</p>
<p>Back up a minute. In the past few sentences I&#8217;ve introduced a whole slew of new terms into the ubiquitous language. All of these need to be modeled, coded, tested and used. And what have we really achieved? Nothing much. No matter how far up the chain you push things, at some point you will run into a simple truth: you need an object that is not created and stored in the normal run of the system but that has been present from the start. Therefore I propose the following guideline:</p>
<blockquote><p><em>For every aggregate root that is not the entity of any other aggregate root that can act as its creator, define an artificial aggregate root.<br />
</em></p></blockquote>
<p>I call this artificial root a ledger, because it performs a function similar to an  order or purchase <a href="http://en.wikipedia.org/wiki/Ledger" target="_blank">ledger</a>.  Our customers are prime candidates for this: unless the ubiquitous language explicitly calls for some other object to create customers our CustomerLedger takes care of creating and deleting customers.  Note that all these ledgers to is provide a place to create and delete instances, updates are still processed by the actual domain objects.</p>
<p>Ledgers are also a prime place for validation: if a customer must have a first and last name, the ledger validates this. If our customers are now registering through the website the ledger can be extended to handle the adding of customers with the associated required validation of email addresses.</p>
<p>The odd thing about legders is that they are aggregate roots (and therefore entities) that have no identity*. All instances of a ledger class are equal to each other (disregarding lazy-loading and the likes) because they always contain the same data.</p>
<p>Here is a code example detailing what a ledger looks like.  This uses my implementation of the <a href="http://martinfowler.com/eaaDev/Notification.html" target="_blank">Notification Pattern</a> to let the repository check if the object is in a valid state and to allow it to report this to the presentation layer.</p>
<pre>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw4">class</span> CustomerLedger
<span class="br0">&#123;</span>
&nbsp; <span class="kw1">private</span> IList errorList<span class="sy0">;</span>

&nbsp; <span class="kw1">public</span> CustomerLedger<span class="br0">&#40;</span><span class="br0">&#41;</span>
&nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; errorList <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> List<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>

&nbsp; <span class="kw1">public</span> IList Customers <span class="br0">&#123;</span> get<span class="sy0">;</span> set<span class="sy0">;</span> <span class="br0">&#125;</span>

&nbsp; <span class="kw1">public</span> ICommandResult AddCustomer<span class="br0">&#40;</span><span class="kw4">String</span> firstName, <span class="kw4">String</span> lastName<span class="br0">&#41;</span>
&nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; Validate.<span class="me1">IsNotNullOrEmpty</span><span class="br0">&#40;</span>firstName, <span class="st0">&quot;First name null or empty&quot;</span>, errorList<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; Validate.<span class="me1">IsNotNullOrEmpty</span><span class="br0">&#40;</span>lastName, <span class="st0">&quot;Last name null or empty&quot;</span>, errorList<span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>errorList.<span class="me1">Count</span> <span class="sy0">==</span> 0<span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; Customers.<span class="me1">Add</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> Customer<span class="br0">&#40;</span>firstName, lastName<span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> CommandResult<span class="br0">&#40;</span>errorList<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>This code is seemingly not very different from regular aggregate root code, and rightly so!  The only actual difference is the way the object is treated from the repository.</p>
<pre>
<div class="codesnip-container" >
<div class="csharp codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw4">interface</span> ICustomerLedgerRepository
<span class="br0">&#123;</span>
&nbsp; CustomerLedger Get<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; ICommandResult Save<span class="br0">&#40;</span>CustomerLedger ledger<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>No id&#8217;s, no fancy lookups: there is no difference between the instances*.</p>
<p><strong>Conclusion</strong></p>
<p>The ledger concept is quite confusing at first, and it is a certain risk to implement: it can be confusing to users, domain experts and programmers alike. To introduce the concept into the ubiquitous language is to soil this language with something that is inherently a technical concern, but at the same time a valid domain problem. Everything that enters your system has an origin, and you need to model this origin up to a certain point.If you talk in terms of roles rather than objects it is a lot easier to introduce: the concept of a &#8216;CustomerAdder&#8217; makes sense: your system needs something that adds customers.</p>
<p>Technically, this can not be considered a design pattern. But I&#8217;m going to call it the &#8216;<strong>Ledger Pattern</strong>&#8216; anyway. It&#8217;s either that or calling it &#8216;the concept formerly known as the artifical aggregate root for aggregate roots that lack natural aggregate roots in the ubiquitous language&#8217;. Let&#8217;s see you type that ten times in a heated mailing list discussion.</p>
<p><em>* All my ledgers actually do have an identity to facilitate ORM mapping and retrieval. This means that every aggregate root that is managed by a ledger also has a foreign key that points to this ledger. My database contains a ledger-table which contains an id and the name of the legder. </em></p>
]]></content:encoded>
			<wfw:commentRss>http://jdt.toron.be/blog/2009/08/ubiquitous-language-what-is-so-blindingly-obvious-you-cant-see-it/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bring on the default constructors</title>
		<link>http://jdt.toron.be/blog/2009/08/bring-on-the-default-constructors/</link>
		<comments>http://jdt.toron.be/blog/2009/08/bring-on-the-default-constructors/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 17:05:51 +0000</pubDate>
		<dc:creator>JDT</dc:creator>
				<category><![CDATA[Various]]></category>
		<category><![CDATA[DDD]]></category>

		<guid isPermaLink="false">http://jdt.toron.be/blog/?p=16</guid>
		<description><![CDATA[The term “default constructor” refers to a constructor that is automatically generated in the absence of explicit constructors (and perhaps under other circumstances); this automatically provide constructor is usually a nullary constructor. In specification or discussion of some languages, “default constructor” may additionally refer to any constructor that may be called without arguments, either because [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>The term “<strong>default constructor</strong>” refers to a <a title="Constructor (computer science)" href="http://en.wikipedia.org/wiki/Constructor_%28computer_science%29">constructor</a> that is automatically generated in the absence of explicit constructors (and perhaps under other circumstances); this automatically provide constructor is usually a <a title="Nullary constructor" href="http://en.wikipedia.org/wiki/Nullary_constructor">nullary constructor</a>. In specification or discussion of some languages, “<strong>default constructor</strong>” may additionally refer to any constructor that may be called without <a title="Argument (computer science)" href="http://en.wikipedia.org/wiki/Argument_%28computer_science%29">arguments</a>, either because it is a nullary constructor or because all of its <a title="Parameter (computer science)" href="http://en.wikipedia.org/wiki/Parameter_%28computer_science%29">parameters</a> have default values.<br />
&#8211; <a href="http://en.wikipedia.org/wiki/Default_constructor" target="_blank">Wikipedia</a><br />
</em></p></blockquote>
<p><strong>Abstract</strong></p>
<p>In which the author outlines why default constructors on domain objects are not a &#8216;code smell&#8217; for NHibernate and are actually required in most situations.</p>
<p><strong>Concrete</strong></p>
<p>I remember when I first started working with (N)Hibernate. It seemed like  a great tool: no more writing SQL queries, no more writing mapping code, but there was that <em>expletive</em> requirement to have a default no-arguments constructor. So I made that private as to not allow the constructor to be abused, but it still seemed bad: the domain object needs to conform to what an infrastructure framework wants.  It is only now, years after my first encounter with NHibernate, that I see that having a default constructor is not a bad thing. If anything, default constructors on domain objects are <em>good things</em>.</p>
<p>Let&#8217;s look at the reason why we need constructors. The <em>raison d&#8217;être</em> for a constructor is to force code that news up an instance of a class to provide other objects that said class needs.  In the context of domain layer and domain objects we tend to think of this as &#8216;creating the object in a state that is valid&#8217;. But what is a valid state?  When talking with programmers, analysts or domain experts it&#8217;s very easy to define what is a valid state.  In a classical order system with customers valid state will probably include a customer having a first name, last name and valid address details.  But is this really such a big requirement that we have to validate this in the constructor?</p>
<p>Weather or not a domain object is valid is in some cases not up to the object itself, and this is especially true for new instances.  Fact of the matter is that the only thing that determines if a domain object is valid is <em>context</em>.  Why does a customer need a first name, last name and an address? Because we need to be able to ship orders to him.</p>
<p>Read that again. <em>We need to ship orders to him</em>. If we are not shipping orders we do not need to validate anything. This brings the concept of validation into a totally different area. We no longer need to think about validating domain objects after creation, we are now validating pre-conditions for object creation.  This validation is always external to the object being created, allowing you to use the same domain object for different use-cases, without the need to write, test and maintain a gazillion different constructors, or even worse &#8211; deal with constructors that have the same parameters but different validation requirements. (The question of where the actual validation occurs for certain domain objects &#8211; such as aggregate roots &#8211; is a matter I&#8217;l talk about in a different post.)</p>
<p><strong>Conclusion</strong></p>
<p>And that, ladies and gentlemen, is why I like default constructors. Because I really don&#8217;t care if my domain objects are valid or not. All I care about is that they are valid in the <em>context </em>I&#8217;m using them in. And validating that can be done with re-useable code, tried and tested through unittests and talks with domain experts. Full of DDD and OO goodness, just the way I like it.</p>
]]></content:encoded>
			<wfw:commentRss>http://jdt.toron.be/blog/2009/08/bring-on-the-default-constructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
