


      <rss version="2.0">
         <channel>
            <title><![CDATA[Vinay Rao | DDD]]></title>
            <link>http://www.simplyvinay.com/</link>
            <description>My Personal Website</description>
            <copyright>Copyright 2005 by Vinay Rao</copyright>
   
      <item>
         <title><![CDATA[DDDBlog Implementation. Part 2]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In my previous post, we looked at setting up the project structure and the tools and libraries that we’ll need to build the application. In this post we’ll look at creating a few base classes.</p>  <p><u><em>Layer SuperType: The Entity Base Class</em></u></p>  <p>Before we start of creating the domain objects, lets first create the base entity object. As discussed before, an <a href="http://www.simplyvinay.com/Post/43/Aspects-Of-DDD.-Part-I.aspx" target="_blank">entity</a> is something that has an identity. This would be our layer supertype because this class will be inherited by all our domain objects that needs to have an identity.</p>  <pre class="ncode"><div style="font-weight: bold; font-size: 10pt; background: #1e1e1e; color: silver; font-family: consolas"><p style="margin: 0px"><span style="color: #cc7832">namespace</span> DDDBlog.Infrastructure.BaseClasses</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">interface</span> <span style="color: #6897bb">IEntity</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">int</span> Key { <span style="color: #cc7832">get</span>; }</p><p style="margin: 0px">&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">abstract</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">Entity</span> : <span style="color: #6897bb">IEntity</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">private</span> <span style="color: #cc7832">int</span> key;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">int</span> Key</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">get</span> { <span style="color: #cc7832">return</span> key; }</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">protected</span> Entity()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">protected</span> Entity(<span style="color: #cc7832">int</span> key)</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">this</span><span style="font-weight: normal">.key = key;</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">bool</span> Equals( <span style="color: #ffc66d">Entity</span> other )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">return</span> Equals( <span style="color: #cc7832">this</span>, other );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">override</span> <span style="color: #cc7832">bool</span> Equals( <span style="color: #cc7832">object</span> obj )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">return</span> Equals( <span style="color: #cc7832">this</span>, obj <span style="color: #cc7832">as</span> <span style="color: #ffc66d">Entity</span> );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">static</span> <span style="color: #cc7832">bool</span> Equals( <span style="color: #ffc66d">Entity</span> obj1, <span style="color: #ffc66d">Entity</span> obj2 )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">if</span> ( <span style="color: #ffc66d">Object</span><span style="font-weight: normal">.Equals( obj1, </span><span style="color: #cc7832">null</span> ) || <span style="color: #ffc66d">Object</span><span style="font-weight: normal">.Equals( obj2, </span><span style="color: #cc7832">null</span> ) ||</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; obj1.GetType() != obj2.GetType() )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">return</span> <span style="color: #cc7832">false</span>;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">if</span> ( ReferenceEquals( obj1, obj2 ) ) <span style="color: #cc7832">return</span> <span style="color: #cc7832">true</span>;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">return</span> obj1.Key == obj2.Key;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">override</span> <span style="color: #cc7832">int</span> GetHashCode()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">return</span> Key.GetHashCode();;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;&#160;&#160; }</p><p style="margin: 0px">}</p></div></pre>

<p>The Entity base class is defined in the Infrastructure layer as the definition of entity is not part of the domain itself but it will be used by the domain. As you can see the Entity class has a key property which would be used as an identity. Also I am taking the key to be integer for simplicity. I have also implemented the equality comparison on the entity object. This is used to compare the entities. </p>

<p><u><em>Entity Tests</em></u></p>

<p>Now lets see some tests on the Entity class. A thing to remember is that in TDD, we would have to write the tests firsts and then come at the object.</p>

<pre class="ncode"><div style="font-weight: bold; font-size: 10pt; background: #1e1e1e; color: silver; font-family: consolas"><p style="margin: 0px"><span style="color: #cc7832">namespace</span> DDDBlog.Tests.Infrastructure.Tests</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> DDDBlog.Infrastructure.BaseClasses;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> Xunit;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">FakeEntity</span> : <span style="color: #ffc66d">Entity</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> FakeEntity()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: #cc7832">base</span>( <span style="color: #6897bb">0</span> )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> FakeEntity( <span style="color: #cc7832">int</span> key )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: #cc7832">base</span>( key )</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">class</span> <span style="color: #ffc66d">EntityTests</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Can_Create_An_Entity()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.NotNull( entity );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><blockquote><p style="margin: 0px">  [<span style="color: #ffc66d">Fact</span>]</p></blockquote><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Newly_Created_Entity_Id_Should_Be_Zero()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>();</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.Equal( entity.Key, <font color="#6897bb">0</font> );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Two_Entities_With_Same_Key_Should_Be_Equal()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity1 = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>( <span style="color: #6897bb">21</span> );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity2 = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>( <span style="color: #6897bb">21</span> );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.Equal( entity1, entity2 );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.Equal( entity1.GetHashCode(), entity2.GetHashCode() );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Two_Entities_With_Diffrent_Key_Should_Not_Be_Equal()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity1 = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>( <span style="color: #6897bb">21</span> );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity2 = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>( <span style="color: #6897bb">3</span> );</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.NotEqual( entity1, entity2 );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.NotEqual( entity1.GetHashCode(), entity2.GetHashCode() );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #ffc66d">Fact</span>]</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">void</span> Reference_To_Same_Entity_Should_Be_Equal()</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity1 = <span style="color: #cc7832">new</span> <span style="color: #ffc66d">FakeEntity</span>( <span style="color: #6897bb">21</span> );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">var</span> entity2 = entity1;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.Equal( entity1, entity2 );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ffc66d">Assert</span><span style="font-weight: normal">.Equal( entity1.GetHashCode(), entity2.GetHashCode() );</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px">&#160;&#160;&#160; }</p><p style="margin: 0px">}</p></div></pre>

<p><u><em>AggregateRoot And IRepository&lt;T&gt;</em></u></p>

<p>Along with the Entity base class, we also need a repository base, and a way to mark an entity as an aggregate root. These are shown in the following snippets</p>

<pre class="ncode"><div style="font-weight: bold; font-size: 10pt; background: #1e1e1e; color: silver; font-family: consolas"><p style="margin: 0px"><span style="color: #cc7832">namespace</span> DDDBlog.Infrastructure.BaseClasses</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">interface</span> <span style="color: #6897bb">IAggregateRoot</span> : <span style="color: #6897bb">IEntity</span> { }</p><p style="margin: 0px">}</p></div></pre>

<pre class="ncode"><div style="font-weight: bold; font-size: 10pt; background: #1e1e1e; color: silver; font-family: consolas"><p style="margin: 0px"><span style="color: #cc7832">namespace</span> DDDBlog.Infrastructure.Repository</p><p style="margin: 0px">{</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> System.Linq;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">using</span> BaseClasses;</p><p style="margin: 0px">&#160;</p><p style="margin: 0px">&#160;&#160;&#160; <span style="color: #cc7832">public</span> <span style="color: #cc7832">interface</span> <span style="color: #6897bb">IRepository</span><span style="font-weight: normal">&lt;T&gt; </span><span style="color: #cc7832">where</span> T : <span style="color: #6897bb">IAggregateRoot</span></p><p style="margin: 0px">&#160;&#160;&#160; {</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; T FindBy( <span style="color: #cc7832">int</span> key );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #6897bb">IQueryable</span><span style="font-weight: normal">&lt;T&gt; FindAll();</span></p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">void</span> Save( T entity );</p><p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #cc7832">void</span> Delete( T entity );</p><p style="margin: 0px">&#160;&#160;&#160; }</p><p style="margin: 0px">}</p></div></pre>

<p>As you can see our Aggregate root base is just an interface that inherits from IEntity. This is done because a repository is ( should be ) provided only on the Aggregate root. You can read a bit on Aggregates and Repositories <a href="http://www.simplyvinay.com/Post/44/Aspects-Of-DDD.-Part-II.aspx" target="_blank">here</a>.</p>

<p>Now that our base classes are ready, we can look at the domain objects of our sample application, which I will be talking about in the next post.</p>

<p>You must know that, I am by no means an expert in DDD, TDD. The code shown above might be wrong as I am just stepping into this part of the world. Please leave a comment/ suggestion if you find something to be wrong or if it could be done better.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/47/DDDBlog-Implementation.-Part-2.aspx]]></link>
         <pubDate>Wed, 08 Apr 2009 04:05:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[DDDBlog Implementation. Part 1]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In my pervious posts, I wrote about the aspects of TDD and DDD a bit, and following up on the series, I will start building the sample application from this post onwards. For the lack of better name, I will be calling the application DDDBlog. </p>  <p><u><em>Setting up the project</em></u></p>  <p>Lets create the project structure as shown below.</p>  <p><a href="http://www.simplyvinay.com/images/blogimages/DDDBlogImplementation.Part1_F101/solution.png"><img title="solution" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="238" alt="solution" src="http://www.simplyvinay.com/images/blogimages/DDDBlogImplementation.Part1_F101/solution_thumb.png" width="246" border="0" /></a> </p>  <p><em><u>DDDBlog.Domain</u></em> : Domain layer is where the Plain-Old CLR Objects reside. These POCOs are entities that form the domain objects for our project. This layer would also contain the repository interface declarations for the domain objects. From a DDD perspective, the Domain layer should be unaware of persistence ( <em>Persistence Ignorance</em> ), so this layer will not have references to the Persistence layer</p>  <p><u><em>DDDBlog.Infrastructure</em></u> : Infrastructure layer provides the bridging for other layers involved in the application. This layer will be used by all other layers. This layer would contain all the base classes required for all other projects. This layer would also define the top level repository interface. I am not sure where the mapping files should fall under this layer or the persistence layer, but for now I think I will go with this layer.</p>  <p><u><em>DDDBlog.Persistence</em></u> :&#160; This layer would contain the actual data access code. Here I will write the actual implementations for the repositories.</p>  <p><u><em>DDDBlog.Tests</em></u> : This layer consists all the tests for all the layers. Its not necessary for us to have single test layer. We can add individual test layer for each of the layers in our application. I however am going with a single test layer.</p>  <p><u><em>DDDBlog.Web</em></u> : This is the web presentation layer for our application.</p>  <p><u><em>Tools and libraries used</em></u></p>  <p><a href="http://www.asp.net/mvc">ASP.NET MVC</a> – Used for the presentation layer. I had thoughts of using Monorail, but preferred ASP.NET MVC as it has come a long way from the CTP’s and I think its pretty good as of now.</p>  <p><a href="http://www.hibernate.org/343.html" target="_blank">NHibernate</a> and <a href="http://fluentnhibernate.org/" target="_blank">FluentNHibernate</a> – These are used for persistence and for the mappings.</p>  <p><a href="http://structuremap.sourceforge.net/Default.htm" target="_blank">StructureMap</a> – This is used for Dependency Injection.</p>  <p><a href="http://www.codeplex.com/xunit" target="_blank">xUnit.Net</a> – This is used for unity testing.</p>  <p><a href="http://code.google.com/p/moq/" target="_blank">MoQ</a> – This is used for mocking.</p>  <p>In this post we have seen how to layer our application and the tools/libraries that’s needed for our project. In my next post we’ll start coding the base classes for our application.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/46/DDDBlog-Implementation.-Part-1.aspx]]></link>
         <pubDate>Mon, 06 Apr 2009 03:30:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Aspects of TDD]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>This post was supposed to be about the layering in my sample project, but I thought I would talk a bit about TDD as its new to me. This is by no means an exhaustive post on TDD </p>  <p><u><em>Test Driven Development</em></u></p>  <p>TDD could be told as an approach in development, where we are writing tests to know the area for which we are building the software. Tests written during the development phase helps you in understanding the domain, and, after development tests helps us document that system. Following are a few things about TDD that I have understood</p>  <p><u><em>The Steps</em></u></p>  <p>1) We write a failing test. We have to identity whether the test is testing what is intended in this step.    <br />2) We make sure the test written in step 1 passes.     <br />3) We refactor the code written in step 2.</p>  <p>We can also segregate a single test into 3 sections viz. <em>Arrange </em>– Where we setup everything that is there to test, <em>Act </em>– We do everything there is to do with the test, <em>Assert </em>– We check our assertions to be true or not.</p>  <p><u><em>But its hard to test</em></u></p>  <p>There are certain areas that are hard to test, like UI ( Its all the more reason to split the UI aspects from the domain model ). Then there is the database which makes it even harder to test. Why? because working with actual db is cumbersome, we have to maintain the state of database for every run of the test. This could be done for integration test, but while in development stages, stubs or mocks could be used, and lastly there is the code of the tests themselves which we’ll have to maintain, but that should be ok, because the tests helps a lot in doing maintenance work.</p>  <p><u><em>Stubs and Mocks</em></u></p>  <p>There will certain areas in our development which when tested might be too slow or difficult to setup for example the UI or the database operation. To overcome these difficulties we can use stubs or mocks.    <br />    <br />A stub is a light weight implementation of a real object. It is a way to setup the expected values that can be used within the test. In tests stubs we create a class that impersonates the actual class that we have to test. For example if we are to test a class that does database operations, we create a stub class that is free of the database and use this stub class in our tests. The thing to note here is that we are testing the database operation methods and not the actual database call and also we provide the input and the output via the stubs. A mock is also an implementation of a real object, but unlike stubs we can set our expectations in mocks on how it should be used and it also provides self validations on the expected results. Mocks are usually created frameworks.</p>  <p>You can read about the differences between mocks and stubs <a href="http://martinfowler.com/articles/mocksArentStubs.html" target="_blank">here</a></p>  <p>I will be writing more on TDD as I dwell more into the development of the sample application.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/45/Aspects-of-TDD.aspx]]></link>
         <pubDate>Mon, 30 Mar 2009 13:10:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Aspects Of DDD. Part II]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>This is a continuation post. You can read the first part <a href="http://www.simplyvinay.com/Post/43/Aspects-Of-DDD.-Part-I.aspx">here</a>.</p>  <p><u><em>Aggregates and Aggregate Root</em></u></p>  <p>In a software there can invariably be many objects, and some of these objects are bound to be related to each other in some way. These objects that are related to each other are known as aggregates and an aggregate root is the object that holds them together. Aggregate root is always an entity. Say for example in a blog application a comment would be related to a post. So post and comments become an aggregate and post becomes the aggregate root as there cant be any comments without a post.</p>  <p>Aggregates are to be considered as one unit with regard to any data manipulation and these aggregates can be accessed from outside only through their aggregate root. This mechanism of accessing the objects only by the root helps in maintain data integrity as individual objects can not be changed directly.</p>  <p><u><em>Factories</em></u></p>  <p>When we are to create an aggregate or an entity, it might become too complex for us to create it in the constructor of the root entity. This is where the factory fits in. The role of the factory is to create an object. These factories are helpful when we are creating an aggregate because when the root is created, all the associated aggregates under the root is also created for us.</p>  <p>The process of object creation by a factory needs to be atomic. This is required because we don’t want to have an aggregate root created without creating the the associated aggregates which might leave the system in a unstable state.</p>  <p>You can read about the factory pattern from my earlier posts <a href="http://www.simplyvinay.com/Post/25/Design-Patterns-For-Dummies.-The-Factory-Method-Pattern.aspx" target="_blank">here</a> and <a href="http://www.simplyvinay.com/Post/27/Design-Patterns-for-Dummies.-The-Abstract-Factory-Pattern.aspx" target="_blank">here</a>.</p>  <p><u><em>Repositories</em></u></p>  <p>In any application, domain objects will need other objects for their operation. So these domain objects could be made to hold references to the other needed objects. If we are to give the&#160; domain objects this behavior of maintaining references, then they become tightly coupled as they are holding on to all the references they need at any given time. </p>  <p>This could be avoided by the using repositories. Repositories are used to maintain the references of objects. Using repositories the domain objects are not involved in maintaining the references to the needed object, instead they can get it from the repository.</p>  <p>Repositories are just a storage place where we can store the data and retrieve it later. These repositories are based on strategy, meaning,&#160; based on different strategies it may use different storage locations for different type of objects. </p>  <p><u><em>Next Steps</em></u></p>  <p>What I have covered in this and my previous post is just the starting point of DDD. There are still many factors into DDD, which probably I will be writing about as I build the application. </p>  <p>In my next post I will be writing about the project structure, why the project is split as it is, and what is the role of each layer.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/44/Aspects-Of-DDD.-Part-II.aspx]]></link>
         <pubDate>Wed, 25 Mar 2009 03:05:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Aspects Of DDD. Part I]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p><u><em>What is DDD?</em></u></p>  <p>DDD could be called as a methodology or a set of patterns which help us in developing a software pertaining to the domain. Here what domain means, is the actual system for which we are building the software. DDD is not breaking away from the core Object Oriented design principles, but to utilize them in the right way. There are many aspects in DDD which we need to consider when building an application. Following are the aspects and their brief explanations from what I have understood.</p>  <p><u><em>The Ubiquitous Language ( UL )</em></u></p>  <p>Developers when building a software tend to wholly dwell into the technical aspects of the the application unlike the domain experts who have an extensive knowledge of the problem domain rather than the technical solution for it. When a domain expert is sharing his expertise, he will using a lot of terms that may not be understood by the development team, thus brining in a communication gap between the two.&#160; </p>  <p>Ubiquitous language in DDD addresses this communication gap between the developers and the business experts. The function of the UL is to connect all parts of the design which would have been led by the domain experts and developers need to follow this. The key point in UL is to identify the the concepts which defines the domain and come up with common terms for those. </p>  <p><u><em>Layered Architecture</em></u></p>  <p>When we develop a software in the OO way, a lot of UI and data specific code is mixed with the domain objects. The problem with this is a change in the UI or the data specific code might change the domain behavior and tracking these changes becomes difficult because to arrive at the domain behavior we’ll have to check the UI/data components which changed the behavior in the first place. </p>  <p>The solution for this is to segregate the software into layers such that the layers are loosely coupled and one layer being dependent only on the layers below it. The main criteria in the layering is to free up the domain objects from the UI/data specific logic. </p>  <p>I will talk about the different layers in a future post where I’ll come up with the initial project structure for the blog application.</p>  <p><u><em>Entities and Value Objects</em></u></p>  <p>Entities and Value Objects form the underlying principles in DDD. Entities are objects which have an identity and this identity is maintained throughout the lifetime of the application. The identity can be represented&#160; differently in different applications, for example an store application can have an Customer entity which can be identified from the CustomerID. </p>  <p>Value Objects are objects that have no identity, meaning that we are representing an object by its attributes alone. Usually Value Objects acts as a part of an entity, for example the Customer entity can have an Address Value object which in itself cannot be identified other than belonging to a customer.</p>  <p><u><em>Services</em></u></p>  <p>When developing a software there are certain behavior which might not fit into entities or value objects, meaning if we add these behavior into the objects then they might give a different meaning to the object, which was not intended. But in OO languages we would need an object to represent this functionality, These objects are declared as services. For example in a store application shipping a product to a customer can be declared as a service.</p>  <p>Services are interfaces which are used in performing some operations on the objects ( entities / value objects ). These operations are the ones that doesn't fit in the objects. </p>  <p>This is the the end of Part I. I will continue with the basics of DDD in my next post.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/43/Aspects-Of-DDD.-Part-I.aspx]]></link>
         <pubDate>Mon, 23 Mar 2009 03:42:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[My foray into DDD, TDD, MVC and NHibernate]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>If you are closely following the things that are happening around the .NET development arena, there is a lot of interest in DDD ( Domain Driven Design ) along with other <a href="http://en.wikipedia.org/wiki/Three-letter_abbreviation" target="_blank">TLA</a>’s like TDD ( Test Driven Development), MVC ( Model-View-Controller ) etc. It was hard for me not to notice the buzz around DDD and got me interested in it. As the title says, I would want to see what these things mean and how to develop an application around or rather using these technologies. </p>  <p>So I am going to start a “Learn as you build” series in which I build a blog engine using these technologies ( or methodologies ). This is not a full fledged blog engine mind you. This is a process of learning new things and blog engine is just right to start of with ( along the lines from this <a href="http://ifacethoughts.net/2007/09/19/want-to-learn-web-programming-write-a-blog-engine/" target="_blank">post</a>). If you are looking for a full featured blog engine, you can have a look at <a href="http://www.dotnetblogengine.net/" target="_blank">BlogEngine.NET</a> or <a href="http://www.dasblog.info/" target="_blank">DasBlog</a> or <a href="http://www.subtextproject.com/" target="_blank">SubText</a> and many more I guess. </p>  <p>Along with these things I would also want to explore NHibernate which is an ORM ( another TLA!!! ) and MoQ ( a mocking framework ). Well I think I am getting into a lot of things, probably more than I can handle, but lets see where these things lead me.</p>  <p>I would appreciate any sort of suggestions that might help me in diving into these things.</p>  <p>More blogs to follow on these lines. </p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/42/My-foray-into-DDD,-TDD,-MVC-and-NHibernate.aspx]]></link>
         <pubDate>Tue, 17 Mar 2009 11:17:00 GMT</pubDate>
      </item>
   
         </channel>
      </rss>  
   
