<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>public class Wheel { public void Roll()....</title>
        <link>http://nathan.whiteboard-it.com/Default.aspx</link>
        <description>Rewriting it all whenever I can</description>
        <language>en-US</language>
        <copyright>Nathan Stott</copyright>
        <managingEditor>nrstott@gmail.com</managingEditor>
        <generator>Subtext Version 1.9.5.177</generator>
        <image>
            <title>public class Wheel { public void Roll()....</title>
            <url>http://nathan.whiteboard-it.com/images/RSS2Image.gif</url>
            <link>http://nathan.whiteboard-it.com/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Review of ASP.NET MVC in Action</title>
            <category>ASP.NET MVC</category>
            <link>http://nathan.whiteboard-it.com/archive/2009/07/06/review-of-asp.net-mvc-in-action.aspx</link>
            <description>&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;I recently read &lt;a href="http://www.manning.com/palermo/"&gt;ASP.NET MVC in Action&lt;/a&gt; by Jeffrey Palermo, Ben Scheirman, and Jimmy Bogard. The publication is an excellent introduction to the the ASP.NET MVC framework. The authors provide excellent background on the problems facing the MS development community and the solutions that ASP.NET MVC has offered to these problems. These problems include the general ineptitude of the view engines with which traditional ASP development and even &lt;a href="http://www.w3schools.com/aspnet/aspnet_forms.asp"&gt;webforms&lt;/a&gt; development were driven. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;ASP.NET MVC is the clear answer to seperation of concerns that webforms attempted to give us with the code-behind files. The introductory chapter will get the beginner up-and-running quickly. The diagrams illustrate the points that the concise text makes. Even those without any experience with an MVC framework will find the directions and explanations well-thought-out and easy to follow. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;Beyond the introductory content, I was genuinely impressed with the depth that the authors dove into the community and th&lt;/span&gt;&lt;/span&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;e framework. &lt;a href="http://haacked.com/"&gt;Phil Haack's&lt;/a&gt; route debugger, a piece of code intimately familiar to those of us who have been following the development of the framework since its early days, is mentioned and code based upon it is provided. The authors discuss such advanced techniques as &lt;a href="http://nayyeri.net/blog/custom-controller-factory-in-asp-net-mvc/"&gt;creating a custom controller factory&lt;/a&gt;, a must for those of us who employ &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;dependency injection&lt;/a&gt;. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;Each new concept is introduced with illustrative code examples. I was particularlly impressed with Chatper 9: Ajax in ASP.NET MVC. A note on unobrusive javascript reads: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;blockquote style="MARGIN-RIGHT: 0px" dir="ltr"&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;&lt;em&gt;You might notice throughout this chapter that I prefer unobtrusive Javascript. This means that the functionality of the page degrades gracefully in the absence of Javascript.&lt;/em&gt; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;This shows careful attention by the authors to provide guidance on best practices. You will find this type of detail throughout the book. This is very refreshing because the Microsoft development community has lagged behind other communities in following wise guidelines and setting sensible best-practices. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;The new Microsfot embrace of open-source is also made obvious as the authors present &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; code in the AJAX chapter. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;Chapter 10, hosting and deplpoyment, is a testament to the thoroughness with which the framework is covered. Deploying on IIS6 and earlier, something that can be quite tricky, is covered well. The configuration option of using .aspx extensions as well as the option to use URL rewriting are covered. URL rewriting can be quite difficult, but the authors provide examples to bring it all together. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;The ASP.NET MVC framework is a breath of fresh-air for the Microsoft development world. It is a sensible, logical framework that allows extensability. ASP.NET MVC and &lt;a href="http://msdn.microsoft.com/en-us/netframework/aa663324.aspx"&gt;WCF&lt;/a&gt; are the two best frameworks that Microsoft has ever produced in my opinion. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: Arial; WHITE-SPACE: pre-wrap; COLOR: rgb(64,64,64); FONT-SIZE: 12px" class="Apple-style-span"&gt;ASP.NET MVC in Action will guide you from your first project through advanced topics such as AJAX and deploying on suboptimal (read IIS6) hosting environments. The writing style is clear and concise. Diagrams and code examples are abundant. I recommend it for anyone looking for a great resource for learning about or becoming a better user of the ASP.NET MVC framework. &lt;/span&gt;&lt;/span&gt;&lt;br class="Apple-interchange-newline" /&gt;
&lt;/p&gt;&lt;img src="http://nathan.whiteboard-it.com/aggbug/18.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nathan Stott</dc:creator>
            <guid>http://nathan.whiteboard-it.com/archive/2009/07/06/review-of-asp.net-mvc-in-action.aspx</guid>
            <pubDate>Mon, 06 Jul 2009 17:09:05 GMT</pubDate>
            <comments>http://nathan.whiteboard-it.com/archive/2009/07/06/review-of-asp.net-mvc-in-action.aspx#feedback</comments>
            <wfw:commentRss>http://nathan.whiteboard-it.com/comments/commentRss/18.aspx</wfw:commentRss>
            <trackback:ping>http://nathan.whiteboard-it.com/services/trackbacks/18.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET MVC Domain Driven Design Deux</title>
            <category>NHibernate</category>
            <category>ASP.NET MVC</category>
            <category>Architecture</category>
            <category>Domain Driven Design</category>
            <link>http://nathan.whiteboard-it.com/archive/2009/03/06/asp.net-mvc-domain-driven-design-deux.aspx</link>
            <description>&lt;h4&gt;Series Navigation&lt;/h4&gt;
&lt;ol&gt;
    &lt;li&gt;&lt;a href="http://nathan.whiteboard-it.com/archive/2009/03/01/asp.net-mvc-domain-driven-design.aspx"&gt;Setup&lt;/a&gt; &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://nathan.whiteboard-it.com/archive/2009/03/06/asp.net-mvc-domain-driven-design-deux.aspx"&gt;Mapping and Entity Equality&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Entity Equality&lt;/h3&gt;
&lt;p&gt;We've got some more house keeping to do on our Entity object before proceding.  Because entities are comporable based upon Id, a standard use of the == operator will produce undesirable results.  When using an ORM, this is particularly important.  You will often have instances of objects, especially objects in child relationships, that are the same as, but not reference equal to, one another.&lt;/p&gt;
&lt;h3&gt;Test Cases for Desired Entity Equality Behavior&lt;/h3&gt;
&lt;p&gt;I'm a believer in documentation via test cases, so here are my test cases that demonstrate the desired behavior:&lt;/p&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;    [&lt;span style="COLOR: #ffc66d"&gt;TestFixture&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;EntityEqualityContext&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        [&lt;span style="COLOR: #ffc66d"&gt;Test&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; TwoTransientEntitiesShouldNotBeEqual()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity1 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity2 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #ffc66d"&gt;Assert&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.AreNotEqual(entity1, entity2, &lt;/span&gt;&lt;span style="COLOR: #a5c25c"&gt;"Different transient entities should not be equal."&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        [&lt;span style="COLOR: #ffc66d"&gt;Test&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; TwoReferencesToSameTransientEntityShouldBeEqual()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity1 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity2 = entity1;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #ffc66d"&gt;Assert&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.AreEqual(entity1, entity2, &lt;/span&gt;&lt;span style="COLOR: #a5c25c"&gt;"Two references to the same transient entity should be equal."&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        [&lt;span style="COLOR: #ffc66d"&gt;Test&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; EntitiesWithSameIdShouldBeEqual()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity1 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity2 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            entity1.SetId(&lt;span style="COLOR: #6897bb"&gt;1&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            entity2.SetId(&lt;span style="COLOR: #6897bb"&gt;1&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #ffc66d"&gt;Assert&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.AreEqual(entity1, entity2, &lt;/span&gt;&lt;span style="COLOR: #a5c25c"&gt;"Entities with same id should be equal."&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        [&lt;span style="COLOR: #ffc66d"&gt;Test&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; EntitiesWithDifferentIdShouldNotBeEqual()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity1 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity2 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            entity1.SetId(&lt;span style="COLOR: #6897bb"&gt;1&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            entity2.SetId(&lt;span style="COLOR: #6897bb"&gt;2&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #ffc66d"&gt;Assert&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.AreNotEqual(entity1, entity2, &lt;/span&gt;&lt;span style="COLOR: #a5c25c"&gt;"Entities with different ids should not be equal."&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        [&lt;span style="COLOR: #ffc66d"&gt;Test&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; EntityShouldNotEqualTransientEntity()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity1 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            entity1.SetId(&lt;span style="COLOR: #6897bb"&gt;1&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity2 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #ffc66d"&gt;Assert&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.AreNotEqual(entity1, entity2, &lt;/span&gt;&lt;span style="COLOR: #a5c25c"&gt;"Entity and transient entity should not be equal."&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        [&lt;span style="COLOR: #ffc66d"&gt;Test&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; EntitiesWithSameIdButDifferentTypesShouldNotBeEqual()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity1 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; entity2 = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;OtherEntity&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            entity1.SetId(&lt;span style="COLOR: #6897bb"&gt;1&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            entity2.SetId(&lt;span style="COLOR: #6897bb"&gt;1&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #ffc66d"&gt;Assert&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.AreNotEqual(entity1, entity2, &lt;/span&gt;&lt;span style="COLOR: #a5c25c"&gt;"Entities of different types should not be equal even if they have the same id."&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; A test entity class created so that we can excercise &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; the functionality of the entity equaltiy members.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestEntity&lt;/span&gt; : &lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;private&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; id;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;override&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; Id&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                &lt;span style="COLOR: #cc7832"&gt;get&lt;/span&gt; { &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; id; }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; SetId(&lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                &lt;span style="COLOR: #cc7832"&gt;this&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.id = id;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;OtherEntity&lt;/span&gt; : &lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;private&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; id;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;override&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; Id&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                &lt;span style="COLOR: #cc7832"&gt;get&lt;/span&gt; { &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; id; }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; SetId(&lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                &lt;span style="COLOR: #cc7832"&gt;this&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.id = id;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Summarized, these tests say that entities with id of 0 should only be equal if they are the same object.  This is reference equality.  Entities with an id should be equal if their ids are equal and they are of the same type.  This is &lt;a target="_blank" href="http://en.csharp-online.net/CSharp_Canonical_Forms—Identity_Equality"&gt;identity equality&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Entity with Equaltiy Members&lt;/h3&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;abstract&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;: &lt;span style="COLOR: #6897bb"&gt;IEquatable&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;private&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; id;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;private&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; transientHashCode;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;virtual&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; Id { &lt;span style="COLOR: #cc7832"&gt;get&lt;/span&gt; { &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; id; } }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;override&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;bool&lt;/span&gt; Equals(&lt;span style="COLOR: #cc7832"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; Equals(obj &lt;span style="COLOR: #cc7832"&gt;as&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;virtual&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;bool&lt;/span&gt; Equals(&lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;if&lt;/span&gt; (obj == &lt;span style="COLOR: #cc7832"&gt;null&lt;/span&gt;) &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;false&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;if&lt;/span&gt; (IsTransient)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; ReferenceEquals(&lt;span style="COLOR: #cc7832"&gt;this&lt;/span&gt;, obj);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; obj.Id == Id &amp;amp;&amp;amp; obj.GetType() == GetType();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;override&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; GetHashCode()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;if&lt;/span&gt; (IsTransient)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                &lt;span style="COLOR: #cc7832"&gt;if&lt;/span&gt; (transientHashCode == &lt;span style="COLOR: #6897bb"&gt;0&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                    transientHashCode = &lt;span style="COLOR: #cc7832"&gt;base&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.GetHashCode();&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; transientHashCode;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; id;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;private&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;bool&lt;/span&gt; IsTransient&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;get&lt;/span&gt; { &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; Id == &lt;span style="COLOR: #6897bb"&gt;0&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    }&lt;!--EndFragment--&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;For more information about why insuring that your entity equality is in order, please see &lt;a target="_blank" href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/04/identity-field-equality-and-hash-code.aspx"&gt;The NHibernate FAQ&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As this is a blog example, we will need a class to represent posts.&lt;/p&gt;
&lt;h3&gt;The Post Class&lt;/h3&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;Post&lt;/span&gt; : &lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;, &lt;span style="COLOR: #6897bb"&gt;IEquatable&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #ffc66d"&gt;Post&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;virtual&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;string&lt;/span&gt; Title { &lt;span style="COLOR: #cc7832"&gt;get&lt;/span&gt;; &lt;span style="COLOR: #cc7832"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;virtual&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;string&lt;/span&gt; Body { &lt;span style="COLOR: #cc7832"&gt;get&lt;/span&gt;; &lt;span style="COLOR: #cc7832"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; Indicates whether the current object is equal to another object of the same type.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; true if the current object is equal to the &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;paramref name="other" /&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt; parameter; otherwise, false.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;param name="other"&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; An object to compare with this object.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;virtual&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;bool&lt;/span&gt; Equals(&lt;span style="COLOR: #ffc66d"&gt;Post&lt;/span&gt; other)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;base&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.Equals(other);&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    }&lt;!--EndFragment--&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;In the future, we will add other properties like a collection of comments.  For now, we will keep it simple in order to demonstrate the fluent mapping.&lt;/p&gt;
&lt;h3&gt;What's the Deal with the Virtual Members&lt;/h3&gt;
&lt;p&gt;NHibernate makes use of proxies for advanced features like lazy loading.  The default proxy factory uses &lt;a target="_blank" href="http://www.castleproject.org/dynamicproxy/index.html"&gt;Castle's DynamicProxy&lt;/a&gt;.  DyanmicProxy generates inheritance based proxies; therefore, it can only work if members are marked as virtual so that it can override them.  There are other proxy choices; however, I will not be covering them.  Making members virtual is a small price to pay for the conveniance in my opinion.&lt;/p&gt;
&lt;h3&gt;Mapping the Post Entity to the Database&lt;/h3&gt;
&lt;p&gt;Generating the mappings for your entities could not be easier with fluent nhibernates automapper. &lt;/p&gt;
&lt;p&gt;Here is a simple utitliy class to add to the Service project:&lt;/p&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;DomainMapper&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;private&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;readonly&lt;/span&gt; &lt;span style="COLOR: #6897bb"&gt;IPersistenceConfigurer&lt;/span&gt; persistenceConfigurer;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; DomainMapper(&lt;span style="COLOR: #6897bb"&gt;IPersistenceConfigurer&lt;/span&gt; persistenceConfigurer)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;this&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.persistenceConfigurer = persistenceConfigurer;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; Configure(&lt;span style="COLOR: #ffc66d"&gt;Configuration&lt;/span&gt; configuration)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #ffc66d"&gt;Fluently&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.Configure(configuration).Mappings(mapping =&amp;gt; mapping.AutoMappings.Add(GetMapper())).Database(&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                persistenceConfigurer).BuildSessionFactory();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;protected&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;virtual&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;AutoPersistenceModel&lt;/span&gt; GetMapper()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; persistenceModel = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;AutoPersistenceModel&lt;/span&gt;(&lt;span style="COLOR: #cc7832"&gt;typeof&lt;/span&gt; (&lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;).Assembly)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                .Where(type =&amp;gt; !type.IsAbstract &amp;amp;&amp;amp; &lt;span style="COLOR: #cc7832"&gt;typeof&lt;/span&gt; (&lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;).IsAssignableFrom(type))&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                .WithConvention(convention =&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                    {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                        convention.IdConvention =&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                            id =&amp;gt; id.Access.AsReadOnlyPropertyThroughCamelCaseField().GeneratedBy.HiLo(&lt;span style="COLOR: #6897bb"&gt;100&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.ToString());&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                        convention.IsBaseType = type =&amp;gt; type == &lt;span style="COLOR: #cc7832"&gt;typeof&lt;/span&gt; (&lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                        convention.DefaultStringLength = &lt;span style="COLOR: #6897bb"&gt;255&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                    });&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; persistenceModel;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;I like to use HiLo for the id convention and avoid using Native Identity.  For more information about the identity generators available using NHibernate, please see &lt;a href="http://vadivelk.net/archive/2008/11/06/primary-key-generators-in-nhibernate.aspx"&gt;Vadi's Rants&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The DefaultStringLength is just a personal preference.  The default is 100 if you do not specify a convention.&lt;/p&gt;
&lt;p&gt;The IsBaseType is necesarry so fluent nhibernate does not create joined table entities from everything that inherits from Entity.&lt;/p&gt;
&lt;p&gt;In the Blog.Service.Test project, add this utility class:&lt;/p&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestDomainMapper&lt;/span&gt;: &lt;span style="COLOR: #ffc66d"&gt;DomainMapper&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; TestDomainMapper() : &lt;span style="COLOR: #cc7832"&gt;base&lt;/span&gt;(&lt;span style="COLOR: #ffc66d"&gt;SQLiteConfiguration&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.Standard.InMemory().ShowSql())&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;This class simply inerits from DomainMapper and provides a SQLite in memory database as the persistence configuration.  Using an in memory database for our tests will make them run much faster.  As the project grows, it is advisable to also run your integration tests vs the actual database that you will be using; however, running them against an in memory database during standard development saves time and is very conveniant.&lt;/p&gt;
&lt;p&gt;In my first post, I attempted to make a thorough list of the tools that would be used in the project.  I neglected at least one though: SQLite.  I may have neglected more, so do not be surprised if we have to add more binaries throughout the series to our libraries directory.  System.Data.SQLite can be downloaded &lt;a href="http://sqlite.phxsoftware.com/"&gt;here&lt;/a&gt;.  Add it to &lt;strong&gt;trunk/libraries/sqlite&lt;/strong&gt; and reference it from the Blog.Service.Test project.&lt;/p&gt;
&lt;p&gt;Fluent NHibernate provides a useful helper for integration tests: PersistenceSpecification&amp;lt;T&amp;gt;.  Here is what the initial Post integration test looks like:&lt;/p&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;[&lt;span style="COLOR: #ffc66d"&gt;TestFixture&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;MappingIntegrationContext&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;{&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;protected&lt;/span&gt; &lt;span style="COLOR: #6897bb"&gt;ISession&lt;/span&gt; Session { &lt;span style="COLOR: #cc7832"&gt;get&lt;/span&gt;; &lt;span style="COLOR: #cc7832"&gt;private&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    [&lt;span style="COLOR: #ffc66d"&gt;SetUp&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; BeforeEachTest()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; configuration = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;Configuration&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestDomainMapper&lt;/span&gt;().Configure(configuration);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; sessionFactory = configuration.BuildSessionFactory();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        Session = sessionFactory.OpenSession();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #6897bb"&gt;IDbConnection&lt;/span&gt; connection = Session.Connection;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;string&lt;/span&gt;[] scripts = configuration.GenerateSchemaCreationScript(&lt;span style="COLOR: #ffc66d"&gt;Dialect&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.GetDialect(configuration.Properties));&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        scripts.ToList().ForEach(script =&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                     {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                         &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; cmd = connection.CreateCommand();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                         cmd.CommandText = script;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                         cmd.ExecuteNonQuery();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                     });&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        Session.Flush();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    [&lt;span style="COLOR: #ffc66d"&gt;Test&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; PostIntegrationTest()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;PersistenceSpecification&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #ffc66d"&gt;Post&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;gt;(Session)&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            .CheckProperty(x =&amp;gt; x.Title, &lt;span style="COLOR: #a5c25c"&gt;"A Test Post"&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            .CheckProperty(x =&amp;gt; x.Body, &lt;span style="COLOR: #a5c25c"&gt;"This is where the content of my post will be.  Yay."&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            .VerifyTheMappings();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;This is a good start, but obviously as our domain model grows and we need more test classes, this SetUp can be abstracted into its own base class that other test classes can inherit from.  &lt;/p&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;abstract&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;FixtureBase&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;{&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    [&lt;span style="COLOR: #ffc66d"&gt;SetUp&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; BeforeEachTest()&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; configuration = &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;Configuration&lt;/span&gt;();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;TestDomainMapper&lt;/span&gt;().Configure(configuration);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; sessionFactory = configuration.BuildSessionFactory();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        Session = sessionFactory.OpenSession();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #6897bb"&gt;IDbConnection&lt;/span&gt; connection = Session.Connection;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;string&lt;/span&gt;[] scripts = configuration.GenerateSchemaCreationScript(&lt;span style="COLOR: #ffc66d"&gt;Dialect&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.GetDialect(configuration.Properties));&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        scripts.ToList().ForEach(script =&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                     {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                         &lt;span style="COLOR: #cc7832"&gt;var&lt;/span&gt; cmd = connection.CreateCommand();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                         cmd.CommandText = script;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                         cmd.ExecuteNonQuery();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;                                     });&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        Session.Flush();&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;protected&lt;/span&gt; &lt;span style="COLOR: #6897bb"&gt;ISession&lt;/span&gt; Session { &lt;span style="COLOR: #cc7832"&gt;get&lt;/span&gt;; &lt;span style="COLOR: #cc7832"&gt;private&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Now the noise of the SetUp method can be taken out of the MappingIntegrationContext and we have a reusable base class for future test classes.&lt;/p&gt;
&lt;h3&gt;Implementing IRepository&lt;/h3&gt;
&lt;p&gt;Next, we will implement our repository.  NHibernate.Burrow AppBlock makes this trivial.  Reference NHibernate.Burrow.AppBlock from the Blog.Service assembly.&lt;/p&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="COLOR: #cc7832"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="COLOR: #cc7832"&gt;using&lt;/span&gt; Blog.Domain;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="COLOR: #cc7832"&gt;using&lt;/span&gt; NHibernate.Burrow.AppBlock.DAOBases;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="COLOR: #cc7832"&gt;namespace&lt;/span&gt; Blog.Service&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;{&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;Repository&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;lt;T&amp;gt;: &lt;/span&gt;&lt;span style="COLOR: #ffc66d"&gt;GenericDAO&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;lt;T&amp;gt;, &lt;/span&gt;&lt;span style="COLOR: #6897bb"&gt;IRepository&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;lt;T&amp;gt; &lt;/span&gt;&lt;span style="COLOR: #cc7832"&gt;where&lt;/span&gt; T : &lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; Find an entity by unique identifier.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;param name="id"&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;Unique identifier of the entity.&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;Entity if found, &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;null&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt; otherwise.&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; T Find(&lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;base&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.Get(id);&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; Find all entities.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;Sequence of entities found.&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #6897bb"&gt;IEnumerable&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;lt;T&amp;gt; FindAll()&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;base&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.FindAll();&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; Save (insert or update as appropriate) &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;paramref name="entity"/&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt; on next commit of the unit of work.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;param name="entity"&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;Entity to be saved.&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; Save(T entity)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;base&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.Save(entity);&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; Delete &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;paramref name="entity"/&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt; on next commit of the unit of work.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;param name="entity"&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;Entity to be deleted.&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;new&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; Delete(T entity)&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;            &lt;span style="COLOR: #cc7832"&gt;base&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;.Delete(entity);&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;        }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;    }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;For the basic functionality, AppBlock provides everything that we need.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: Some of you want the sample project that I am working from, so I will add a zip of it soon.&lt;/p&gt;
&lt;p&gt;In the next post, we will wire up the Inversion of Control in our MVC app and create a controller for Post CRUD.&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2009%2f03%2f06%2fasp.net-mvc-domain-driven-design-deux.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2009%2f03%2f06%2fasp.net-mvc-domain-driven-design-deux.aspx" /&gt;&lt;/a&gt;&lt;img src="http://nathan.whiteboard-it.com/aggbug/17.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nathan Stott</dc:creator>
            <guid>http://nathan.whiteboard-it.com/archive/2009/03/06/asp.net-mvc-domain-driven-design-deux.aspx</guid>
            <pubDate>Fri, 06 Mar 2009 16:21:57 GMT</pubDate>
            <comments>http://nathan.whiteboard-it.com/archive/2009/03/06/asp.net-mvc-domain-driven-design-deux.aspx#feedback</comments>
            <slash:comments>16</slash:comments>
            <wfw:commentRss>http://nathan.whiteboard-it.com/comments/commentRss/17.aspx</wfw:commentRss>
            <trackback:ping>http://nathan.whiteboard-it.com/services/trackbacks/17.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.NET MVC Domain Driven Design</title>
            <category>ASP.NET MVC</category>
            <category>Architecture</category>
            <category>Domain Driven Design</category>
            <link>http://nathan.whiteboard-it.com/archive/2009/03/01/asp.net-mvc-domain-driven-design.aspx</link>
            <description>&lt;h4&gt;Series Navigation&lt;/h4&gt;
&lt;ol&gt;
    &lt;li&gt;&lt;a href="http://nathan.whiteboard-it.com/archive/2009/03/01/asp.net-mvc-domain-driven-design.aspx"&gt;Setup&lt;/a&gt; &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://nathan.whiteboard-it.com/archive/2009/03/06/asp.net-mvc-domain-driven-design-deux.aspx"&gt;Mapping and Entity Equality&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Introduction&lt;/h3&gt;
&lt;p&gt;In this series of posts, I will detail how I setup projects using the principles of domain driven design in &lt;a href="http://www.asp.net/mvc/"&gt;ASP.NET MVC&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The tools and libraries we will use are as follows:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://www.nhforge.org/index.htm"&gt;NHibernate&lt;/a&gt; &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://fluentnhibernate.org/"&gt;Fluent NHibernate&lt;/a&gt; &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://nhcontrib.wiki.sourceforge.net/BurrowHome"&gt;NHibernate.Burrow&lt;/a&gt; &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.codeplex.com/MVCContrib"&gt;MVCContrib&lt;/a&gt; &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.nunit.org/index.php"&gt;NUnit&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The sample app will be the canonical blog example. This series will be light on the discussion of the theory of DDD and heavy on the nuts and bolts of asp.net MVC implementation.&lt;/p&gt;
&lt;h3&gt;Project Structure&lt;/h3&gt;
&lt;p&gt;Create a new directory for the project.  I'm calling mine DDDSample.  Following subversion convention, create a trunk and branches folder inside of DDDSample.  Inside of trunk, create three directories: src, tools, and libraries.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;'src'&lt;/em&gt; directory is where our code will go.  The &lt;em&gt;'tools'&lt;/em&gt; directory will have NUnit and in the future, NAnt.  The &lt;em&gt;'libraries'&lt;/em&gt; directory will contain the third party binaries that the project references.&lt;/p&gt;
&lt;h3&gt;Gathering the Libraries and Tools&lt;/h3&gt;
&lt;p&gt;1) Download the latest binary of fluent nhibernate from &lt;a href="http://fluentnhibernate.org/downloads"&gt;http://fluentnhibernate.org/downloads&lt;/a&gt;.  Put the fluent nhibernate binaries in &lt;strong&gt;trunk/libraries/fluent-nhibernate&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;2) Download NHibernate.Burrow from &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446"&gt;http://sourceforge.net/project/showfiles.php?group_id=216446&lt;/a&gt;.  Put the burrow binaries in &lt;strong&gt;trunk/libraries/nhibernate.burrow&lt;/strong&gt;.  Important Note: Sometimes the latest version of NHibernate.Burrow will not have been built against the same version of NHibernate as fluent nhibernate was.  Therefore, it is important that you be able to build NHibernate.Burrow, and the rest of the libraries that follow, from source.  NHibernate.Burrow is a part of &lt;a href="http://nhcontrib.wiki.sourceforge.net/"&gt;NHContrib&lt;/a&gt;.  The NHContrib svn repository is located at &lt;font face="Arial"&gt;&lt;a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk"&gt;https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk&lt;/a&gt;.  There is a build script in the root directory of the NHibernate.Burrow subdirectory of the project.  To rebuild with a different version of NHibernate, just replace the NHibernate dll in &lt;strong&gt;NHibernate.Burrow/lib/NHibernate&lt;/strong&gt; with the binary of NHibernate from the fluent nhibernate.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;3) Download NUnit from &lt;a href="http://www.nunit.org/index.php?p=download"&gt;http://www.nunit.org/index.php?p=download&lt;/a&gt;.  Get the zip distribution . Unzip the entire contents into &lt;strong&gt;trunk/tools/nunit&lt;/strong&gt;.  It is important that you include the full NHibernate distribution with your project so when we add an automated build tool later it will be able to run the unit tests without assuming that every developer has installed the correct version of NUnit from msi.&lt;/p&gt;
&lt;p&gt;4) Download Moq from &lt;a href="http://code.google.com/p/moq/downloads/list"&gt;http://code.google.com/p/moq/downloads/list&lt;/a&gt;.  Put the binaries in &lt;strong&gt;trunk/libraries/moq&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;5) Download MVCContrib from &lt;a href="http://www.codeplex.com/MVCContrib"&gt;http://www.codeplex.com/MVCContrib&lt;/a&gt;.  Put the binaries in &lt;strong&gt;trunk/libraries/mvccontrib&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;As an added measure of safety against change, I like to reference the MVC libraries themselves from binaries I store in my svn repository instead of from source.  You don't have to do this, but if you choose to, I put System.Web.Mvc, System.Web.Routing, and System.Web.Abstractions in &lt;strong&gt;trunk/libraries/mvc&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Your completed project structure should look like this:&lt;/p&gt;
&lt;p&gt;&lt;img height="248" alt="" width="184" src="/images/nathan_whiteboard-it_com/DDDSample/ProjectStructure.png" /&gt;&lt;/p&gt;
&lt;h3&gt;Creating the Projects&lt;/h3&gt;
&lt;p&gt;Our mvc project and class libraries will go in trunk/src.  &lt;/p&gt;
&lt;p&gt;Create an asp.net MVC project.  For this sample, I'm calling mine Blog.Web.  &lt;/p&gt;
&lt;p&gt;Create two class libraries: Blog.Domain and Blog.Service.  Blog.Domain is where our POCO (Plain Old C# Objects) entities and our repository interfaces will reside.  Blog.Service is where the business logic that ties the application together will live.&lt;/p&gt;
&lt;p&gt;Reference Blog.Domain and Blog.Service from Blog.Web.  Reference Blog.Domain from Blog.Service. &lt;/p&gt;
&lt;p&gt;Create a new solution folder (right click on project, hover over add, choose new solution folder) called Test.  This is where our test projects will go.&lt;/p&gt;
&lt;p&gt;Create three class libraries: Blog.Web.Test, Blog.Domain.Test, and Blog.Service.Test.  In each of these projects reference NUnit.Framework from /trunk/tools/nunit.&lt;/p&gt;
&lt;p&gt;More test projects may be needed later for separating unit tests from integration tests, but this is how I like to start with one unit test project for each of my projects.&lt;/p&gt;
&lt;p&gt;The solution should now look like this:&lt;/p&gt;
&lt;p&gt;&lt;img height="146" alt="" width="202" src="/images/nathan_whiteboard-it_com/DDDSample/SolutionStructure.png" /&gt;&lt;/p&gt;
&lt;h3&gt;Entity Base Class&lt;/h3&gt;
&lt;p&gt;In the domain, create an object ot be the base class for entities.  An entity is an object that has an identity.   &lt;/p&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    1&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;namespace&lt;/span&gt; Blog.Domain&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    2&lt;/span&gt; {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    3&lt;/span&gt;     &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;abstract&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;class&lt;/span&gt; &lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    4&lt;/span&gt;     {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    5&lt;/span&gt;         &lt;span style="COLOR: #cc7832"&gt;private&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; id;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    6&lt;/span&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    7&lt;/span&gt;         &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;virtual&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; Id { &lt;span style="COLOR: #cc7832"&gt;get&lt;/span&gt; { &lt;span style="COLOR: #cc7832"&gt;return&lt;/span&gt; id; } }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    8&lt;/span&gt;     }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    9&lt;/span&gt; }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;There is no way to set the id property of the Entity base class.  This is by design.  Our persistence layer will use NHiberante generators to generate the identity values when we save new entities to the database.&lt;/p&gt;
&lt;h3&gt;Repositories&lt;/h3&gt;
&lt;p&gt;Just what are repositories?  Repositories are an abstraction of the way we manage &lt;a href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html"&gt;aggregates&lt;/a&gt;.  Repositories should correspond only to aggregate roots.   An aggregate is a cluster of objects that are treated as a unit.  The aggregate root is the root object of the unit.&lt;/p&gt;
&lt;p&gt;In Domain Driven Design, the repository interfaces are a part of the domain, but the implementations are not.  &lt;/p&gt;
&lt;h3&gt;Generic Repository of Entities&lt;/h3&gt;
&lt;p&gt;All entity repositories will need some base functionality, so we define IRepository&amp;lt;T&amp;gt; with some basic methods that we expect any repository to need. &lt;/p&gt;
&lt;div style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; BACKGROUND: black; COLOR: white; FONT-FAMILY: Courier New"&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    1&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    2&lt;/span&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    3&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;namespace&lt;/span&gt; Blog.Domain&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    4&lt;/span&gt; {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    5&lt;/span&gt;     &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    6&lt;/span&gt;     &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; Repository of entities.  &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    7&lt;/span&gt;     &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    8&lt;/span&gt;     &lt;span style="COLOR: #cc7832"&gt;public&lt;/span&gt; &lt;span style="COLOR: #cc7832"&gt;interface&lt;/span&gt; &lt;span style="COLOR: #6897bb"&gt;IRepository&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;lt;T&amp;gt; &lt;/span&gt;&lt;span style="COLOR: #cc7832"&gt;where&lt;/span&gt; T : &lt;span style="COLOR: #ffc66d"&gt;Entity&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;    9&lt;/span&gt;     {&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   10&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   11&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; Find an entity by unique identifier.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   12&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   13&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;param name="id"&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;Unique identifier of the entity.&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   14&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;Entity if found, &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;null&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt; otherwise.&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   15&lt;/span&gt;         T Find(&lt;span style="COLOR: #cc7832"&gt;int&lt;/span&gt; id);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   16&lt;/span&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   17&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   18&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; Find all entities.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   19&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   20&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;Sequence of entities found.&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   21&lt;/span&gt;         &lt;span style="COLOR: #6897bb"&gt;IEnumerable&lt;/span&gt;&lt;span style="FONT-WEIGHT: normal"&gt;&amp;lt;T&amp;gt; FindAll();&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   22&lt;/span&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   23&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   24&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; Save (insert or update as appropriate) &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;paramref name="entity"/&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt; on next commit of the unit of work.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   25&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   26&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;param name="entity"&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;Entity to be saved.&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   27&lt;/span&gt;         &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; Save(T entity);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   28&lt;/span&gt; &lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   29&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   30&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; Delete &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;paramref name="entity"/&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt; on next commit of the unit of work.&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   31&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   32&lt;/span&gt;         &lt;span style="COLOR: gray"&gt;///&lt;/span&gt;&lt;span style="COLOR: green"&gt; &lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;param name="entity"&amp;gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;Entity to be deleted.&lt;/span&gt;&lt;span style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   33&lt;/span&gt;         &lt;span style="COLOR: #cc7832"&gt;void&lt;/span&gt; Delete(T entity);&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   34&lt;/span&gt;     }&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;&lt;span style="FONT-WEIGHT: normal; COLOR: #2b91af"&gt;   35&lt;/span&gt; }&lt;/p&gt;
&lt;/div&gt;
&lt;h3&gt;Conclusions&lt;/h3&gt;
&lt;p&gt;In this post we have done a lot of nuts and bolts work.  The amount of setup required may seem daunting, but as stated earlier, the third party tools and libraries will pay dividends in the long-run.  &lt;/p&gt;
&lt;p&gt;In the &lt;a href="http://nathan.whiteboard-it.com/archive/2009/03/06/asp.net-mvc-domain-driven-design-deux.aspx"&gt;next post&lt;/a&gt;, we will create a Post entity, create a concrete implementation of IRepository&amp;lt;Post&amp;gt; using NHibernate, map Post to a database using Fluent NHibernate, and test our persistence model.&lt;/p&gt;
&lt;p&gt;&lt;!--EndFragment--&gt;&lt;!--EndFragment--&gt;&lt;/p&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2009%2f03%2f01%2fasp.net-mvc-domain-driven-design.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2009%2f03%2f01%2fasp.net-mvc-domain-driven-design.aspx" /&gt;&lt;/a&gt;&lt;img src="http://nathan.whiteboard-it.com/aggbug/16.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nathan Stott</dc:creator>
            <guid>http://nathan.whiteboard-it.com/archive/2009/03/01/asp.net-mvc-domain-driven-design.aspx</guid>
            <pubDate>Sun, 01 Mar 2009 19:12:27 GMT</pubDate>
            <comments>http://nathan.whiteboard-it.com/archive/2009/03/01/asp.net-mvc-domain-driven-design.aspx#feedback</comments>
            <slash:comments>20</slash:comments>
            <wfw:commentRss>http://nathan.whiteboard-it.com/comments/commentRss/16.aspx</wfw:commentRss>
            <trackback:ping>http://nathan.whiteboard-it.com/services/trackbacks/16.aspx</trackback:ping>
        </item>
        <item>
            <title>Register Repository Implementations Automatically with a Castle Facility</title>
            <category>NHibernate</category>
            <category>Architecture</category>
            <category>Patterns</category>
            <category>Domain Driven Design</category>
            <link>http://nathan.whiteboard-it.com/archive/2009/02/26/register-repository-implementations-automatically-with-a-castle-facility.aspx</link>
            <description>&lt;p&gt;Isn't it annoying to have to register all your repository implementations by hand?  When you get to a certain number of them, it becomes quite a task.  If you want to register them for multiple interfaces, it gets even worse.&lt;/p&gt;
&lt;p&gt;When using &lt;a href="http://www.castleproject.org/container/index.html"&gt;Windsor&lt;/a&gt; as your inversion of control container, separating your repository registrations into a &lt;a href="http://www.castleproject.org/container/documentation/trunk/concepts/facility.html"&gt;facility&lt;/a&gt; is a good start.  In general though, all our repositories follow a simple pattern and should be easily registerable automatically following an algorithm.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://domaindrivendesign.org/"&gt;Domain driven design&lt;/a&gt; tells us that the &lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/10/08/the-repository-pattern.aspx"&gt;repository&lt;/a&gt; interfaces are a part of the domain but the implementations are not.  Therefore, we usually have two assemblies that are important.&lt;/p&gt;
&lt;p&gt;This is a typical project layout:&lt;/p&gt;
&lt;p&gt;&lt;img height="185" alt="" width="616" src="/images/nathan_whiteboard-it_com/Automatic Repository Registration/assemblies.png" /&gt;&lt;/p&gt;
&lt;p&gt;Our facility needs to allow us to configure what assemblies it searches for types and the base type of the repository. &lt;/p&gt;
&lt;h2&gt;Specification&lt;/h2&gt;
&lt;p&gt;Here is a simple test describing the desired behavior:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;    [TestFixture]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RepositoryRegistrationFacilityTestFixture
    {
        [Test]
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ShouldRegisterCustomerRepository()
        {
            var kernel = &lt;span class="kwrd"&gt;new&lt;/span&gt; Castle.MicroKernel.DefaultKernel();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
            var facility = &lt;span class="kwrd"&gt;new&lt;/span&gt; RepositoryRegistrationFacility(&lt;span class="kwrd"&gt;typeof&lt;/span&gt; (IRepository&amp;lt;&amp;gt;), &lt;span class="kwrd"&gt;typeof&lt;/span&gt; (Customer).Assembly);&lt;br /&gt;&lt;br /&gt;
            facility.Init(kernel,&lt;br /&gt;                &lt;span class="kwrd"&gt;new&lt;/span&gt; Castle.Core.Configuration.MutableConfiguration(&lt;span class="str"&gt;"facility-config"&lt;/span&gt;));&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
            kernel.Resolve&amp;lt;ICustomerRepository&amp;gt;().ShouldBeInstanceOfType(&lt;span class="kwrd"&gt;typeof&lt;/span&gt; (CustomerRepository));
            kernel.Resolve&amp;lt;IRepository&amp;lt;Customer&amp;gt;&amp;gt;().ShouldBeInstanceOfType(&lt;span class="kwrd"&gt;typeof&lt;/span&gt; (CustomerRepository));&lt;br /&gt;
        }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IRepository&amp;lt;T&amp;gt;
        {
        }
        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ICustomerRepository : IRepository&amp;lt;Customer&amp;gt;
        {    
        }
        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CustomerRepository : ICustomerRepository
        {
        }
        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Customer
        {
        }
    }
&lt;/pre&gt;
&lt;h2&gt;Implementation&lt;/h2&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RepositoryRegistrationFacility : AbstractFacility
    {
        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Assembly[] assemblies;
        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Type genericRepositoryType;

        &lt;span class="kwrd"&gt;public&lt;/span&gt; RepositoryRegistrationFacility(Type genericRepositoryType, &lt;span class="kwrd"&gt;params&lt;/span&gt; Assembly[] assemblies)
        {
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.assemblies = assemblies;
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.genericRepositoryType = genericRepositoryType;
        }

        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Init()
        {
            Predicate&amp;lt;Type&amp;gt; isGenericRepositoryInterface =
                type =&amp;gt;
                type.GetInterfaces().Contains(
                    x =&amp;gt; x.IsGenericType &amp;amp;&amp;amp; x.GetGenericTypeDefinition().IsAssignableFrom(genericRepositoryType));

            IEnumerable&amp;lt;Type&amp;gt; types =
                assemblies.Aggregate(Enumerable.Empty&amp;lt;Type&amp;gt;(), (accumulator, assembly) =&amp;gt; accumulator.Concat(assembly.GetTypes()));

            IEnumerable&amp;lt;Type&amp;gt; repositoryInterfaces = types.Where(
                type =&amp;gt; type.IsInterface &amp;amp;&amp;amp; !type.IsGenericTypeDefinition &amp;amp;&amp;amp; isGenericRepositoryInterface(type));

            repositoryInterfaces
                .ForEach(interfaceType =&amp;gt;
                             {
                                 Type implementor =
                                     types.FirstOrDefault(type =&amp;gt; interfaceType.IsAssignableFrom(type) &amp;amp;&amp;amp; !type.IsAbstract);
                                 &lt;span class="kwrd"&gt;if&lt;/span&gt; (implementor == &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="kwrd"&gt;return&lt;/span&gt;;

                                 IEnumerable&amp;lt;Type&amp;gt; genericRepositoryInterfaceTypes = implementor.GetInterfaces().Where(
                                     x =&amp;gt;
                                     x.IsGenericType &amp;amp;&amp;amp;
                                     x.GetGenericTypeDefinition().IsAssignableFrom(genericRepositoryType));

                                 Kernel.Register(
                                     Component.For(interfaceType).ImplementedBy(implementor).Forward(
                                         genericRepositoryInterfaceTypes));
                             });
        }
    }&lt;/pre&gt;
&lt;p&gt;Now in fluent configuration you can just do this: &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;
&lt;pre class="csharpcode"&gt;container.AddFacility(&lt;span class="str"&gt;"persistent-repository-facility"&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; RepositoryRegistrationFacility(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IRepository&amp;lt;&amp;gt;), &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IRepository&amp;lt;&amp;gt;).Assembly, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(SomethingRepository).Assembly));&lt;/pre&gt;
&lt;br /&gt;
&lt;p&gt;I have not tested the binsor registration, but you should be able to do something like this to register it using binsor: &lt;/p&gt;
&lt;pre class="csharpcode"&gt;facility RepositoryRegistrationFacility:
    genericRepositoryType: IRepository
    assemblies: array(System.Reflection.Assembly, IRepository.Assembly, SomethingRepository.Assembly)&lt;/pre&gt;
&lt;br /&gt;
&lt;p&gt;I hope this is useful.&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2009%2f02%2f26%2fregister-repository-implementations-automatically-with-a-castle-facility.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2009%2f02%2f26%2fregister-repository-implementations-automatically-with-a-castle-facility.aspx" /&gt;&lt;/a&gt;&lt;img src="http://nathan.whiteboard-it.com/aggbug/15.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nathan Stott</dc:creator>
            <guid>http://nathan.whiteboard-it.com/archive/2009/02/26/register-repository-implementations-automatically-with-a-castle-facility.aspx</guid>
            <pubDate>Thu, 26 Feb 2009 17:52:34 GMT</pubDate>
            <comments>http://nathan.whiteboard-it.com/archive/2009/02/26/register-repository-implementations-automatically-with-a-castle-facility.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://nathan.whiteboard-it.com/comments/commentRss/15.aspx</wfw:commentRss>
            <trackback:ping>http://nathan.whiteboard-it.com/services/trackbacks/15.aspx</trackback:ping>
        </item>
        <item>
            <title>Building Domain Specific Languages in Boo by Oren Eini</title>
            <category>DSL</category>
            <category>Boo</category>
            <link>http://nathan.whiteboard-it.com/archive/2009/01/31/building-domain-specific-languages-in-boo-by-oren-eini.aspx</link>
            <description>&lt;div style="MARGIN: 0in 0in 10pt"&gt;Building Domain Specific Languages (&lt;a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language"&gt;DSL&lt;/a&gt;) in &lt;a href="http://boo.codehaus.org/"&gt;Boo&lt;/a&gt; by &lt;a href="http://ayende.com/about-me.aspx"&gt;Oren Eini&lt;/a&gt; leads the intermediate to advanced programmer through the process of building several DSLs in the Boo programming language. It is not an introduction to Boo, but rather it is assumed that the user is advanced enough so that picking up a new language is not an insurmountable prerequisite. A Boo tutorial and reference is provided in the appendix.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;The writing style is inviting an informal. The first two paragraphs will immediately endear programmers to Oren Eini:&lt;/div&gt;
&lt;div style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0in; BORDER-TOP: medium none; PADDING-LEFT: 0in; PADDING-BOTTOM: 4pt; MARGIN-LEFT: 0.65in; BORDER-LEFT: medium none; MARGIN-RIGHT: 0.65in; PADDING-TOP: 0in"&gt;
&lt;div style="MARGIN: 10pt 0in 14pt"&gt;&lt;strong&gt;&lt;em&gt;At first, there was the bit. And the bit shifted left, and the bit shifted right, and there was the byte. The byte grew into a word, and then into double word. And the developer saw the work and it was good. And the evening and the morning were the first day.&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div styl2e="MARGIN: 10pt 0in 14pt"&gt;&lt;strong&gt;&lt;em&gt;And on the next day, the developer came back to the work and couldn’t figure out what he was thinking last night, and spent the whole day figuring that out. &lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Oren chose to write about building DSLs in Boo specifically because of the excellent &lt;a href="http://en.wikipedia.org/wiki/Language-oriented_programming"&gt;language-oriented programming&lt;/a&gt; features available in the Boo programming language. This is not a book about the nuts and bolts of building a language from scratch. That would not truly be a treatise on DSLs as the purpose of DSLs is increased abstraction. That increased abstraction drives Oren’s craft. He makes no reservations about the fact that code maintainability and not performance is his primary concern. &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;This is a book about getting your feet wet and jumping right into building usable real-world DSLs.   This book is immediately applicable to the work performed by hundreds of thousands of programmers every day.  The DSLs presented in the book are pertinent to the work of enterprise developers. Thorough code samples of the fully functional DSLs are provided.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;&lt;a href="https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/rhino-dsl/"&gt;Rhino DSL&lt;/a&gt; is Oren’s low-level implementation of a language factory. It is leveraged throughout the book. Sufficient detail into the design goals and implementation of the library are given so that it could be easily reproduced even if it were not freely available as open source. Rhino DSL is licensed under the BSD license. I have used it. It is wonderful and I see no reason to reinvent the wheel.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Testing, versioning, and creating IDEs to polish your DSLs are topics treated thoroughly. Oren teaches you how and when to version your DSL. He presents several strategies for versioning. He drives home the points that automated testing is not an option, it’s a requirement.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;I found the chapter on creating an IDE invaluable. Oren recommends using Sharp Develop. It is freely distributable and allows you to build a stand-alone DSL editor to distribute as a part of your package. The chapter begins by demonstrating how to provide a good text editing experience with syntax highlighting and code completion and then moves on to advanced topics such as how to create visualizations to round out the editing experience. This task is made easier thanks to the ability to use the abstract syntax tree (AST) provided by the Boo Parser. Score another one for Boo!&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Not all of Oren’s advice is about programming itself. He provides insights into the business-aspect of creating DSLs as well. The first emphasis box in the documentation chapter is titled “Ensure an initial positive impression.”   The common-sense statements in this box provide polish to the book, much as he is trying to teach the reader how to provide polish to a DSL. &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;These sentences captured my interest intently: &lt;/div&gt;
&lt;div style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0in; BORDER-TOP: medium none; PADDING-LEFT: 0in; PADDING-BOTTOM: 4pt; MARGIN-LEFT: 0.65in; BORDER-LEFT: medium none; MARGIN-RIGHT: 0.65in; PADDING-TOP: 0in"&gt;
&lt;div style="MARGIN: 10pt 0in 14pt"&gt;&lt;strong&gt;&lt;em&gt;A project that looks good is pleasant to use, easier to demo and in general easier to get funding for.&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="MARGIN: 10pt 0in 14pt"&gt;&lt;strong&gt;&lt;em&gt;The first stage should be a very easy one, even if you have to design to enable that specifically.&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;How often as programmers do we neglect to create a good first-impression of our amazing products? The code is well-tested and flawless, but it takes another computer scientist of equal skill an hour to get anything working. &lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;Oren’s blend of humor and common-sense advice makes the book a joy to read. His example code and discussion of abstract concepts will challenge you to become a better programmer. Oren brings you into his world, one where everything is done correctly. All code is maintainable, self-documenting, and easy-to-read.&lt;/div&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;
&lt;div style="MARGIN: 0in 0in 10pt"&gt;If you are a .NET developer, I cannot encourage you enough to pick up this book. Oren’s thoughts are valuable even if you never write a DSL. The concepts of language-oriented programming are &lt;a href="http://www.infoq.com/news/2007/09/Language-oriented-programming"&gt;becoming more main-stream&lt;/a&gt;, and this book is an excellent introduction that will open your eyes to the practical implications of language-oriented design. &lt;/div&gt;
&lt;/div&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2009%2f01%2f31%2fbuilding-domain-specific-languages-in-boo-by-oren-eini.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2009%2f01%2f31%2fbuilding-domain-specific-languages-in-boo-by-oren-eini.aspx" /&gt;&lt;/a&gt;&lt;img src="http://nathan.whiteboard-it.com/aggbug/14.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nathan Stott</dc:creator>
            <guid>http://nathan.whiteboard-it.com/archive/2009/01/31/building-domain-specific-languages-in-boo-by-oren-eini.aspx</guid>
            <pubDate>Sat, 31 Jan 2009 19:39:00 GMT</pubDate>
            <comments>http://nathan.whiteboard-it.com/archive/2009/01/31/building-domain-specific-languages-in-boo-by-oren-eini.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://nathan.whiteboard-it.com/comments/commentRss/14.aspx</wfw:commentRss>
            <trackback:ping>http://nathan.whiteboard-it.com/services/trackbacks/14.aspx</trackback:ping>
        </item>
        <item>
            <title>State Pattern</title>
            <category>Patterns</category>
            <link>http://nathan.whiteboard-it.com/archive/2008/09/01/state-pattern.aspx</link>
            <description>&lt;h3&gt;State Pattern&lt;/h3&gt;
The state pattern is a behavioral pattern that allows an object to partially change its type at runtime.  This is useful in situations where we can not change the type of a base object, but wish to represent the state of it in encapsulated classes.&lt;br /&gt;
&lt;br /&gt;
Let us consider a payroll system where employees are compensated and promoted based upon their current position.  &lt;br /&gt;
We could use a property to store the employees current position. &lt;br /&gt;
&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Employee&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; EmployeePosition Position { get; set; }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Promote()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;switch&lt;/span&gt; (Position)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;case&lt;/span&gt; EmployeePosition.JuniorDeveloper:&lt;br /&gt;                    Position = EmployeePosition.SeniorDeveloper;&lt;br /&gt;                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;br /&gt;                &lt;span class="kwrd"&gt;case&lt;/span&gt; EmployeePosition.SeniorDeveloper:&lt;br /&gt;                    Position = EmployeePosition.TechnicalLead;&lt;br /&gt;                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;br /&gt;                &lt;span class="kwrd"&gt;case&lt;/span&gt; EmployeePosition.TechnicalLead:&lt;br /&gt;                    &lt;span class="rem"&gt;// This is as high as it goes.&lt;/span&gt;                     &lt;span class="kwrd"&gt;&lt;br /&gt;                    break&lt;/span&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Compensate(Accounting accounting)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt; salary = 0;&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;switch&lt;/span&gt; (Position)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;case&lt;/span&gt; EmployeePosition.JuniorDeveloper:&lt;br /&gt;                    salary = 40000;&lt;br /&gt;                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;br /&gt;                &lt;span class="kwrd"&gt;case&lt;/span&gt; EmployeePosition.SeniorDeveloper:&lt;br /&gt;                    salary = 75000;&lt;br /&gt;                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;br /&gt;                &lt;span class="kwrd"&gt;case&lt;/span&gt; EmployeePosition.TechnicalLead:&lt;br /&gt;                    salary = 100000;&lt;br /&gt;                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            accounting.Pay(&lt;span class="kwrd"&gt;this&lt;/span&gt;, salary);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;enum&lt;/span&gt; EmployeePosition&lt;br /&gt;    {&lt;br /&gt;        JuniorDeveloper,&lt;br /&gt;        SeniorDeveloper,&lt;br /&gt;        TechnicalLead&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Accounting&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Pay(Employee employee, &lt;span class="kwrd"&gt;int&lt;/span&gt; dollars)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="rem"&gt;// Pay the employee&lt;/span&gt;         &lt;br /&gt;        }     &lt;br /&gt;    }&lt;/pre&gt;
This code is brittle.  Changing the EmployeePosition enum requires changing multiple methods in the Employee domain object.  The logic is spread thin and the encapsulation is non-existent.&lt;br /&gt;
&lt;h3&gt;A Better Way&lt;/h3&gt;
A better way to do this is to use the state pattern.  With the state pattern, the EmployeePosition enum is replaced by an abstract class representing the employees current position.&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AbstractEmploymentState&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Employee employee;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; AbstractEmploymentState(Employee employee)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.employee = employee;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; Employee Employee { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; employee; } }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Promote();&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Compensate(Accounting accounting);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Employee&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; AbstractEmploymentState employmentState;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; AbstractEmploymentState EmploymentState&lt;br /&gt;        {&lt;br /&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; employmentState; }&lt;br /&gt;            set { employmentState = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Promote()&lt;br /&gt;        {&lt;br /&gt;            EmploymentState.Promote();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Compensnate(Accounting accounting)&lt;br /&gt;        {&lt;br /&gt;            EmploymentState.Compensate(accounting);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; JuniorDeveloperState: AbstractEmploymentState&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; JuniorDeveloperState(Employee employee) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(employee)&lt;br /&gt;        {&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Promote()&lt;br /&gt;        {&lt;br /&gt;            Employee.EmploymentState = &lt;span class="kwrd"&gt;new&lt;/span&gt; SeniorDeveloperState(Employee);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Compensate(Accounting accounting)&lt;br /&gt;        {&lt;br /&gt;            accounting.Pay(Employee, 40000);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SeniorDeveloperState: AbstractEmploymentState&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; SeniorDeveloperState(Employee employee) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(employee)&lt;br /&gt;        {&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Promote()&lt;br /&gt;        {&lt;br /&gt;            Employee.EmploymentState = &lt;span class="kwrd"&gt;new&lt;/span&gt; TechinicalLeadState(Employee);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Compensate(Accounting accounting)&lt;br /&gt;        {&lt;br /&gt;            accounting.Pay(Employee, 75000);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TechinicalLeadState: AbstractEmploymentState&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; TechinicalLeadState(Employee employee) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(employee)&lt;br /&gt;        {&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Promote()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="rem"&gt;// Do Nothing, this is as high as it goes for now.&lt;/span&gt;         &lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Compensate(Accounting accounting)&lt;br /&gt;        {&lt;br /&gt;            accounting.Pay(Employee, 100000);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Accounting&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Pay(Employee employee, &lt;span class="kwrd"&gt;int&lt;/span&gt; dollars)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="rem"&gt;// Pay the employee&lt;/span&gt;         &lt;br /&gt;        }     &lt;br /&gt;    }&lt;/pre&gt;
In this example, employees are compensated and promoted based upon their positions.  The domain object, Employee, delegates these actions to the AbstractEmploymentState object.  &lt;br /&gt;
&lt;br /&gt;
Note how simplified the Promote and Compensate methods of Employee are:&lt;br /&gt;
&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Promote()&lt;br /&gt;        {&lt;br /&gt;            EmploymentState.Promote();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Compensate(Accounting accounting)&lt;br /&gt;        {&lt;br /&gt;            EmploymentState.Compensate(accounting);&lt;br /&gt;        }&lt;/pre&gt;
Adding a new state, or increasing the complexity of the logic involved in compensation and promotion of the Employees is encapsulated, leaving the design flexible.&lt;br /&gt;
&lt;h3&gt;Polymorphic Employee&lt;/h3&gt;
Ok, so polymorphism is good.  Every OO programmer knows that, right?  So why don't we just make the Employee class polymorphic with subclasses for each position i.e. JuniorDeveloper, SeniorDeveloper, and TechnicalLead are all classes that inherit from Employee and implement the appropriate Compensate and Promote logic.&lt;br /&gt;
&lt;br /&gt;
In this case, implementing the promote logic in the subclasses would be quite challenging as we can not change the type of a class at runtime.  It could be done by changing the Promote method signature to return an Employee.  The subclasses could then each implement Promote that returned an appropriate subclass of employee.  However, this solution feels contrived.  It is also more difficult to understand for a future maintenance programmer and could lead to hard to find bugs.&lt;br /&gt;
&lt;br /&gt;
Another reason we may not wish to have a polymoprhic Employee class is because there may be multiple state objects in a single domain object.  What if we need not only to compensate and promote employees based upon their position, but also to assign vacation days based upon the project to which the empoloyee is currently assigned?  A situation where we needed a class for TechnicalLeadWhoIsWorkingOnProjectX arises.&lt;br /&gt;
&lt;br /&gt;
Another reason to use the state pattern instead of full blown polymoprhism of the domain object is when using an ORM such as NHibernate.  NHibernate with Single Table Inheritance does not make it easy to change the type of a persistent object.  You can do with with SQL, but it's a clumbsy solution.  &lt;br /&gt;
&lt;h3&gt;Conclusions&lt;/h3&gt;
The state pattern is a useful tool in a programmer's swiss army knife of design patterns.  It can help you avoid ugly switch statements and encapsulate domain logic in a flexible manner.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2008%2f09%2f01%2fstate-pattern.aspx"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2008%2f09%2f01%2fstate-pattern.aspx" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://nathan.whiteboard-it.com/aggbug/13.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nathan Stott</dc:creator>
            <guid>http://nathan.whiteboard-it.com/archive/2008/09/01/state-pattern.aspx</guid>
            <pubDate>Mon, 01 Sep 2008 20:12:24 GMT</pubDate>
            <comments>http://nathan.whiteboard-it.com/archive/2008/09/01/state-pattern.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://nathan.whiteboard-it.com/comments/commentRss/13.aspx</wfw:commentRss>
            <trackback:ping>http://nathan.whiteboard-it.com/services/trackbacks/13.aspx</trackback:ping>
        </item>
        <item>
            <title>Data Transfer Objects </title>
            <category>Architecture</category>
            <category>Patterns</category>
            <link>http://nathan.whiteboard-it.com/archive/2008/08/23/data-transfer-objects.aspx</link>
            <description>&lt;h2&gt;Data Transfer Objects Pattern&lt;/h2&gt;
Data Transfer Objects (DTOs) are a necesarry time-sink when working with &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Service-oriented_architecture"&gt;SOA&lt;/a&gt;.  Proper SOA dictates that you must not pass domain objects across service boundaries.  In many cases, such as when using an ORM, it is extremely difficult to pass your domain objects across the service boundaries even if you choose to disreguard the SOA guidance.  Also, there may be good reasons why you do not want to make your domain objects serializable.&lt;br /&gt;
&lt;h3&gt; DTOs Defined&lt;/h3&gt;
So what exactly are DTOs?  They are objects that have no behavior outside of accessors and mutators of their properties.  Often times DTOs line up one-to-one with domain objects as in the following example.&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span class="rem"&gt;&lt;br /&gt;/// Domain Object&lt;/span&gt; &lt;span class="rem"&gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Company&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; name;&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; taxId;&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Company(&lt;span class="kwrd"&gt;string&lt;/span&gt; name, &lt;span class="kwrd"&gt;string&lt;/span&gt; taxId)&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.name = name;&lt;br /&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.taxId = taxId;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; name; } }&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; TaxId { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; taxId; } }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span class="rem"&gt;/// Data Transfer Object&lt;/span&gt; &lt;br /&gt;&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CompanyDTO&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; TaxId { get; set; }&lt;br /&gt;}&lt;/pre&gt;
&lt;br /&gt;
Other times DTOs may need to carry data for a request that has no direct representation as a single object in the domain.  Consider a service that needs to expose data about a company and its owner.  Obviously the clients shouldn't have to request data about the company and then data about the owner in a separate request.  Doing so would incur the costs of two round trips, and the owner data is a part of a single logical unit as exposed by the service.&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span class="rem"&gt;/// A company&lt;/span&gt; &lt;br /&gt;&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Company&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; name;&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; taxId;&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Company(&lt;span class="kwrd"&gt;string&lt;/span&gt; name, &lt;span class="kwrd"&gt;string&lt;/span&gt; taxId)&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.name = name;&lt;br /&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.taxId = taxId;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; name; } }&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; TaxId { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; taxId; } }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Owner Owner { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span class="rem"&gt;/// Owner of a company&lt;/span&gt; &lt;span class="rem"&gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Owner&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Company Company { get; set; }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; FirstName { get; set; }&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; LastName { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span class="rem"&gt;&lt;br /&gt;/// Data Transfer Object&lt;/span&gt; &lt;br /&gt;&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CompanyDTO&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; TaxId { get; set; }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; OwnerName { get; set; }&lt;br /&gt;}&lt;/pre&gt;
&lt;h3&gt;From Domain Object to DTO and Back Again&lt;/h3&gt;
The assembler pattern, a subset of the mapping pattern, is used to translate DTOs to and from domain objects.&lt;br /&gt;
&lt;br /&gt;
&lt;img height="204" width="231" alt="" src="/images/nathan_whiteboard-it_com/DTO Pattern/AssemblerPattern.jpg" /&gt;&lt;br /&gt;
&lt;br /&gt;
It is possible to create a reusable conventions based assembler.  I will talk more about this in the future.  For small projects, the cost of creating assemblers for the DTOs is trivial.  For larger ones, it is still worth the effort for the sake of encapsulation.&lt;br /&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span class="rem"&gt;&lt;br /&gt;/// Assembler for Company and CompanyDTO.&lt;/span&gt; &lt;br /&gt;&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ICompanyAssembler&lt;br /&gt;{&lt;br /&gt;    CompanyDTO Convert(Company company);&lt;br /&gt;    Company Convert(CompanyDTO DTO);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;h3&gt;What is so useful about an object that has no behavior?&lt;/h3&gt;
DTOs may seem like weak objects because of their lack of behavior; but this is what makes them the one object that is safe to pass across a service boundary.  A client that interacts with your service may, and often will, have a different domain from the service itself.  In fact, many different clients with many different domains may interact with your service.  It is difficult and undesirable to share the same domain model between a service and all its potential clients.  &lt;br /&gt;
&lt;br /&gt;
Consider again the Company example.  Let us assume that the service we are writing is a better business bureau catalog of companies.  The domain object, Company, may include such methods as "FileComplaint" or "RevokeLicense."  &lt;br /&gt;
&lt;br /&gt;
This service may be consumed by an auction site that wants to use the companies names and ratings but has no need to file complaints and does not have the ability to revoke a license.  The company domain object for the auction site will include methods such as "AddAuction" or "AssociateWithReview."  &lt;br /&gt;
&lt;br /&gt;
Sharing the domain objects between the auction site and the better business bureau service would pollute both domains; not to mention the fact that they may, and in this example certainly would, have different owners who do not wish to share code bases.&lt;br /&gt;
&lt;br /&gt;
Sharing DTOs, because of their lack of behavior, works fine though.  The DTOs serve as a simple, abstracted definition for the data that the service and its clients must exchange.&lt;br /&gt;
&lt;br /&gt;
&lt;img height="262" width="611" src="/images/nathan_whiteboard-it_com/DTO Pattern/BBBExample(1).jpg" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
DTOs also insulate your service clients from changes to the domain of the service and vice versa.  You are free to change your domain objects as much as you want and leave the DTOs alone.  The assembler can simply be updated as the domain objects are updated.&lt;br /&gt;
&lt;br /&gt;
If the domain objects were shared by the clients, every change to a domain object would directly impact clients, greatly increasing the costs of maintaining the service. &lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2008%2f08%2f23%2fdata-transfer-objects.aspx"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2008%2f08%2f23%2fdata-transfer-objects.aspx" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://nathan.whiteboard-it.com/aggbug/12.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nathan Stott</dc:creator>
            <guid>http://nathan.whiteboard-it.com/archive/2008/08/23/data-transfer-objects.aspx</guid>
            <pubDate>Sun, 24 Aug 2008 04:08:44 GMT</pubDate>
            <comments>http://nathan.whiteboard-it.com/archive/2008/08/23/data-transfer-objects.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://nathan.whiteboard-it.com/comments/commentRss/12.aspx</wfw:commentRss>
            <trackback:ping>http://nathan.whiteboard-it.com/services/trackbacks/12.aspx</trackback:ping>
        </item>
        <item>
            <title>MVC Forms WIth Validation Open Sourced</title>
            <category>ASP.NET MVC</category>
            <link>http://nathan.whiteboard-it.com/archive/2008/08/17/mvc-forms-with-validation-open-sourced.aspx</link>
            <description>I've set up a google code repository &lt;a target="_blank" href="http://code.google.com/p/mvc-forms/"&gt;here &lt;/a&gt;for the ASP.NET MVC Forms With Validation framework I discussed in &lt;a href="http://nathan.whiteboard-it.com/archive/2008/08/10/mvc-forms-framework-with-validation.aspx"&gt;this post&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
I have made some alterations to it since the post that I will review in a later posting.&lt;br /&gt;
&lt;br /&gt;
Briefly, the major changes are as follows:&lt;br /&gt;
&lt;ol&gt;
    &lt;li&gt;Multiple fieldsets are now supported in the forms.&lt;/li&gt;
    &lt;li&gt;I have taken the first steps towards supporting objects that contain child objects.  A 1-1 relationship is supported in the current revision.  A new fieldset is generated for each child object.&lt;/li&gt;
    &lt;li&gt;Strategies have been revamped so that they no longer are mapped to types.  Now a strategy is allowed to return null if it does not want to handle a property descriptor passed to it.  If it returns null, the next strategy in the list will be tried.&lt;/li&gt;
&lt;/ol&gt;
Check it out.  Tell me what you think.  I am accepting patches and if you want to be a contributor, contact me through the contact form on this blog.
&lt;br /&gt;&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2008%2f08%2f17%2fmvc-forms-with-validation-open-sourced.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2008%2f08%2f17%2fmvc-forms-with-validation-open-sourced.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://nathan.whiteboard-it.com/aggbug/11.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nathan Stott</dc:creator>
            <guid>http://nathan.whiteboard-it.com/archive/2008/08/17/mvc-forms-with-validation-open-sourced.aspx</guid>
            <pubDate>Sun, 17 Aug 2008 21:25:30 GMT</pubDate>
            <comments>http://nathan.whiteboard-it.com/archive/2008/08/17/mvc-forms-with-validation-open-sourced.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://nathan.whiteboard-it.com/comments/commentRss/11.aspx</wfw:commentRss>
            <trackback:ping>http://nathan.whiteboard-it.com/services/trackbacks/11.aspx</trackback:ping>
        </item>
        <item>
            <title>Forms Framework with Validation for ASP.NET MVC</title>
            <category>ASP.NET MVC</category>
            <link>http://nathan.whiteboard-it.com/archive/2008/08/10/mvc-forms-framework-with-validation.aspx</link>
            <description>&lt;span style="background-color: rgb(255, 255, 153);"&gt;&lt;span style="font-weight: bold;"&gt;Update&lt;/span&gt;: This project has been open sourced.  See details in &lt;/span&gt;&lt;a style="background-color: rgb(255, 255, 153);" href="http://nathan.whiteboard-it.com/archive/2008/08/17/mvc-forms-with-validation-open-sourced.aspx"&gt;this post&lt;/a&gt;&lt;span style="background-color: rgb(255, 255, 153);"&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: rgb(255, 255, 153);"&gt;&lt;span style="font-weight: bold;"&gt;Another Update&lt;/span&gt;: This article was written using asp.net MVC preview 4.  The better option now is to use &lt;a href="http://www.codeplex.com/MVCContrib"&gt;MVCContrib's&lt;/a&gt; validators or the MVCContrib fluent html project in general with &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.idataerrorinfo.aspx"&gt;IDataErrorInfo&lt;/a&gt;.  I leave this post here as a reference.  Also, if you wish to have a more Django style forms framework, this would be a great starting point.  The code is freely available for anyone who wishes to use it.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
One thing that is notably missing from ASP.NET MVC is a good way to handle forms and their validation.  To resolve this issue, I started on a simple forms framework this weekend.&lt;br /&gt;
&lt;h3&gt;The end goal&lt;/h3&gt;
&lt;img height="193" width="392" src="/images/nathan_whiteboard-it_com/FormFramework/ContactAction.jpg" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
I don't particularly like the action filter to handle the insertion of the model as a parameter.  I would prefer it be done via windsor and interceptors; however, for the first go round I have decided to keep the castle stack out of this.  The technique could easily be adapted to use MVC Contrib's WindsorControllerFactory and interceptors so that attributes do not have to be on every action you wish to use a form helper with.  More on that later.&lt;br /&gt;
&lt;br /&gt;
For those of you who want to skip the reading and get straight to the code, &lt;a href="http://nathan.whiteboard-it.com/SampleCode/WB.Forms.zip"&gt;download the sample project&lt;/a&gt;.  Look at the /Home/Contact page.  &lt;span style="font-weight: bold;"&gt;Note&lt;/span&gt;: the sample project depends on &lt;a href="http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=15389"&gt;MVC Preview 4&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;The Components&lt;/h3&gt;
&lt;ul&gt;
    &lt;li&gt;Field - A field is the smallest unit of input and validation in a form.&lt;/li&gt;
    &lt;li&gt;Widget - A widget is an abstraction of HTML template text for input.&lt;/li&gt;
    &lt;li&gt;FormBase - Base class for form helpers.&lt;/li&gt;
    &lt;li&gt;ModelForm - A form auto created from a POCO model.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Widget&lt;/h3&gt;
A widget has a name, a value, and some attributes.  The name and value properties are by default shortcuts to the name and value attributes of the widget's attribute collection.  However, this behavior is overridable in subclasses.  A widget also has a way of rendering itself as XHTML.&lt;br /&gt;
&lt;h3&gt;Field&lt;/h3&gt;
A field has a name, a value, and a widget.  By default a field is required, and it has a publicly exposed validate method so that it can be asked to validate its value.  It also has the ability to output itself as XHTML.  &lt;br /&gt;
&lt;h3&gt;FormBase&lt;/h3&gt;
FormBase, the base class for forms contains a collection of fields, a method to validate the fields, and a method to load the values of fields from a name value collection.  The latter facilitates the loading of data from a browser request.&lt;br /&gt;
&lt;br /&gt;
The first concrete implementation of FormBase I created was ModelForm.  A ModelForm accepts a generic type argument and in its constructor takes an object of that type.  It uses this object to generate fields for the form.  The generation logic is implemented using the strategy pattern so that it is easily customizable.&lt;br /&gt;
&lt;br /&gt;
Here is the strategy interface&lt;br /&gt;
&lt;br /&gt;
&lt;img height="128" width="342" src="/images/nathan_whiteboard-it_com/FormFramework/IFieldStrategy.jpg" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
The ModelForm registers default strategies&lt;br /&gt;
&lt;br /&gt;
&lt;img height="173" width="426" src="/images/nathan_whiteboard-it_com/FormFramework/RegisterStrategies.jpg" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
The constructor allows you to pass in your own strategies&lt;br /&gt;
&lt;br /&gt;
&lt;img height="278" width="698" src="/images/nathan_whiteboard-it_com/FormFramework/StrategyCtor.jpg" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
With that, we have everything we need to make a form from a POCO.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Model Action Filter&lt;/h3&gt;
One of the neatest things ASP.NET MVC does is allow you to make controllers with parameters that will be filled in from the request.  &lt;br /&gt;
&lt;br /&gt;
I wanted to be able to do this with my form classes as well.&lt;br /&gt;
&lt;br /&gt;
For the first go round, I decided not to use what I would prefer: Windsor and IInterceptors.  Instead I integrated the MVC way by using their action filters.&lt;br /&gt;
&lt;br /&gt;
The filters give us everything we need to set the values for a parameter.  We have access to the parameters through the ActionMethod.GetParmaeters() method and we can set the parameters via the ActionParameters dictionary.&lt;br /&gt;
&lt;br /&gt;
Here is the action filter&lt;br /&gt;
&lt;br /&gt;
&lt;img height="321" width="700" src="/images/nathan_whiteboard-it_com/FormFramework/ModelFilterAttribute.jpg" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
FormFactory is a simple helper class that uses a strategy pattern to create forms based upon the type passed in and a NameValueCollection.&lt;br /&gt;
&lt;br /&gt;
Settings filterContext.Action&lt;br /&gt;
&lt;br /&gt;
Now, this action will work!&lt;br /&gt;
&lt;br /&gt;
&lt;img height="193" width="392" src="/images/nathan_whiteboard-it_com/FormFramework/ContactAction.jpg" alt="" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Rendering the Form in a View&lt;br /&gt;
&lt;/h3&gt;
I provided three canonical methods for rendering the form:&lt;br /&gt;
&lt;br /&gt;
AsDiv - renders the form with each field wrapped in a div&lt;br /&gt;
AsTable - render the form with each field as a table row&lt;br /&gt;
AsList - render the form with each field as a list item of an unordered list&lt;br /&gt;
&lt;br /&gt;
I also provided a AsCustom method that allows you to specify an XElement to wrap the form fields inside of and an XElement to use as the parent for the children generated by the field instances.&lt;br /&gt;
&lt;br /&gt;
If you want even more flexibility, the rendering is completely overridable by subclassing.&lt;br /&gt;
&lt;br /&gt;
Rendering the form is as simple as passing it to a view and calling AsDiv() or your preferred alternative.&lt;br /&gt;
&lt;br /&gt;
All output is valid XHTML.  Invalid fields receive an error class.&lt;br /&gt;
&lt;br /&gt;
Rendered&lt;br /&gt;
&lt;img height="309" width="377" alt="" src="/images/nathan_whiteboard-it_com/FormFramework/ContactForm.jpg" /&gt;&lt;br /&gt;
&lt;br /&gt;
Validation&lt;br /&gt;
&lt;img height="276" width="468" alt="" src="/images/nathan_whiteboard-it_com/FormFramework/ContactFormValidation.jpg" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://nathan.whiteboard-it.com/SampleCode/WB.Forms.zip"&gt;Download the Sample Project&lt;/a&gt;&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Note&lt;/span&gt;: the sample project depends on &lt;a href="http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=15389"&gt;MVC Preview 4&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2008%2f08%2f10%2fmvc-forms-framework-with-validation.aspx"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fnathan.whiteboard-it.com%2farchive%2f2008%2f08%2f10%2fmvc-forms-framework-with-validation.aspx" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://nathan.whiteboard-it.com/aggbug/10.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nathan Stott</dc:creator>
            <guid>http://nathan.whiteboard-it.com/archive/2008/08/10/mvc-forms-framework-with-validation.aspx</guid>
            <pubDate>Sun, 10 Aug 2008 17:08:49 GMT</pubDate>
            <comments>http://nathan.whiteboard-it.com/archive/2008/08/10/mvc-forms-framework-with-validation.aspx#feedback</comments>
            <slash:comments>20</slash:comments>
            <wfw:commentRss>http://nathan.whiteboard-it.com/comments/commentRss/10.aspx</wfw:commentRss>
            <trackback:ping>http://nathan.whiteboard-it.com/services/trackbacks/10.aspx</trackback:ping>
        </item>
        <item>
            <title>Building NHibernate From Source</title>
            <category>NHibernate</category>
            <link>http://nathan.whiteboard-it.com/archive/2008/08/04/building-nhibernate-from-source.aspx</link>
            <description>Recently, on &lt;a target="_blank" href="http://groups.google.com/group/nhusers"&gt;nhusers&lt;/a&gt;, the NHibernate users mailing list, I've seen questions from people wishing to get started building NHibernate from source.  Some people are unfamiliar with open source in the .NET world and the tools used for it, so I made this beginner level screencast about how to get started building NHibernate from source.&lt;br /&gt;
&lt;br /&gt;
You can download the screencast &lt;a href="http://nathan.whiteboard-it.com/SampleCode/nhibernate_from_source_101.zip"&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Here is a list of the prerequisites you will need to follow along:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://nant.sourceforge.net"&gt;NAnt&lt;/a&gt;&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://nunit.org/index.php"&gt;NUnit&lt;/a&gt;&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://tortoisesvn.tigris.org/"&gt;TortoiseSVN&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://www.testdriven.net/"&gt;TestDriven.NET&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
For reference, you can find the official NHibernate document about getting started from source &lt;a target="_blank" href="http://www.hibernate.org/428.html"&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Note&lt;/span&gt;: I realized after I posted this that I forgot to provide a link to the free codec needed to view this screencast.  You will need to d/l the free camstudio lossless codec (&lt;a target="_blank" href="http://nathan.whiteboard-it.com/SampleCode/CamStudioCodec10.zip"&gt;here&lt;/a&gt;) to view this video.  Extract the files, right click on the .inf, and select install.  You will receive a warning, but it's ok.  This is a widely used screencasting codec and you can find a lot of info about it via google.&lt;br /&gt;
In the future, I'm going to convert my videos to flash and stream them to make things easier all-around.&lt;br /&gt;
Sorry for the inconveniance.&lt;img src="http://nathan.whiteboard-it.com/aggbug/9.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nathan Stott</dc:creator>
            <guid>http://nathan.whiteboard-it.com/archive/2008/08/04/building-nhibernate-from-source.aspx</guid>
            <pubDate>Mon, 04 Aug 2008 13:13:57 GMT</pubDate>
            <comments>http://nathan.whiteboard-it.com/archive/2008/08/04/building-nhibernate-from-source.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://nathan.whiteboard-it.com/comments/commentRss/9.aspx</wfw:commentRss>
            <trackback:ping>http://nathan.whiteboard-it.com/services/trackbacks/9.aspx</trackback:ping>
        </item>
    </channel>
</rss>