<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/"><channel><title>Design Patterns C#</title><link>http://davidhayden.com/blog/dave/category/22.aspx</link><description>Design Patterns C#</description><managingEditor>Dave Hayden</managingEditor><dc:language>en-US</dc:language><generator>.Text Version 0.95.2004.101</generator><item><dc:creator>Dave Hayden</dc:creator><title>Day of Patterns &amp; Practices Tomorrow - Tampa, Florida RoadShow</title><link>http://davidhayden.com/blog/dave/archive/2008/01/30/DayOfPatternsPracticesTomorrowTampaFloridaRoadShow.aspx</link><pubDate>Wed, 30 Jan 2008 10:02:00 GMT</pubDate><guid>http://davidhayden.com/blog/dave/archive/2008/01/30/DayOfPatternsPracticesTomorrowTampaFloridaRoadShow.aspx</guid><description>&lt;P&gt;&lt;A href="http://www.pnpguidance.net/"&gt;&lt;FONT face=Verdana size=2&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" alt="Day of Patterns &amp;amp; Practices" src="/photos/dayofpatternsandpracticeseventtampa.jpg"&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Looking forward to seeing everyone at the &lt;/FONT&gt;&lt;A href="http://www.pnpguidance.net/FreeDayPatternsAndPracticesTampa2008.aspx"&gt;&lt;FONT face=Verdana size=2&gt;Day of Patterns &amp;amp; Practices&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; Event tomorrow in Tampa!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Presentations include:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;Web Client Software Factory v2.0&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;Web Service Software Factory: Modeling Edition&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;TDD and Model-View-Presenter&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;ASP.NET MVC Framework&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src ="http://davidhayden.com/blog/dave/aggbug/3449.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Dave Hayden</dc:creator><title>Software Development Tip: Avoid Race Conditions Using Tester-Doer Pattern</title><link>http://davidhayden.com/blog/dave/archive/2007/09/26/SoftwareDevelopmentTipAvoidRaceConditionsUsingTesterDoerPattern.aspx</link><pubDate>Wed, 26 Sep 2007 21:49:00 GMT</pubDate><guid>http://davidhayden.com/blog/dave/archive/2007/09/26/SoftwareDevelopmentTipAvoidRaceConditionsUsingTesterDoerPattern.aspx</guid><description>One of the patterns that came up today in a code review was the Tester-Doer Pattern. One sees this pattern a lot especially when the “doer” side of the equation has a pretty decent performance penalty. Essentially, you test a condition to make sure you need to do the “doer“ operation and pay that penalty. Sometimes this can cause race conditions. Read more...&lt;img src ="http://davidhayden.com/blog/dave/aggbug/3346.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Dave Hayden</dc:creator><title>Chain of Responsibility Pattern - Builder Pattern - Fluent Interfaces</title><link>http://davidhayden.com/blog/dave/archive/2007/09/19/ChainOfResponsibilityPatternBuilderPatternFluentInterfaces.aspx</link><pubDate>Wed, 19 Sep 2007 16:21:00 GMT</pubDate><guid>http://davidhayden.com/blog/dave/archive/2007/09/19/ChainOfResponsibilityPatternBuilderPatternFluentInterfaces.aspx</guid><description>Using the Chain of Responsibility Pattern, Builder Pattern, and Fluent Interfaces to find the appropriate constructor to inject dependencies into as part of the sample Dependency Injection Application Block I created using the Application Block Software Factory in Enterprise Library 3.1. Read more...&lt;img src ="http://davidhayden.com/blog/dave/aggbug/3333.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Dave Hayden</dc:creator><title>Model View Presenter Pattern - User Interface Design Patterns</title><link>http://davidhayden.com/blog/dave/archive/2006/09/13/Model_View_Presenter.aspx</link><pubDate>Wed, 13 Sep 2006 17:27:00 GMT</pubDate><guid>http://davidhayden.com/blog/dave/archive/2006/09/13/Model_View_Presenter.aspx</guid><description>&lt;H1&gt;&lt;FONT face=Verdana&gt;Model-View-Presenter Pattern - User Interface Design Patterns&lt;/FONT&gt;&lt;/H1&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;by &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;David Hayden&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; ( &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;ASP.NET Developer&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; )&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I was reading a few blog posts today and noticed that Scott Cate gave a presentation on the Model View Presenter Pattern at the Florida.NET User Group. According to his post, it looks like he rocked the house. I am envious here in Sarasota, wishing I could have seen the presentation for which he provided the &lt;/FONT&gt;&lt;A href="http://weblogs.asp.net/scottcate/archive/2006/09/13/Florida.NET-User-Group-a-Success-_2D002D00_-Thanks-Dave-Noderer.aspx" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;source code&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Still, I feel like I was there, because I downloaded the source code to see what he had built from scratch during the presentation. If you are new to the Model View Presenter Pattern and looking for a gentle introduction to it, I highly recomment looking at the source code. Scott uses table adapters for data access and uses the ASP.NET Page as the view. Simple and straight forward without adding a lot of extra fat to make the subject more complex than it has to be. Great job, Scott. Maybe we can get you to Sarasota for a similar presentation.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I played with Scott's source code a bit, &lt;/FONT&gt;&lt;FONT face=Verdana size=2&gt;evolving it into something that I could unit test. This isn't necessary, but I enjoyed the exercise :) Right now as the Presenter class stands, it is tied directly to the table adapter&amp;nbsp;that retrieves data:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; MiramarFAQ.Presenter
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; RetrieveFaqPresenter
    {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;readonly&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IRetrieveFaqView view;
        
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; RetrieveFaqPresenter (IRetrieveFaqView view)
        {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.view &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; view;
        }
        
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; RetrieveFAQ()
        {
            &lt;FONT color=#ff0000&gt;FaqTableAdapter adapter &lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT color=#ff0000&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR: #000000"&gt;&lt;FONT color=#ff0000&gt; FaqTableAdapter();&lt;/FONT&gt;
            DataTable faq &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; adapter.GetDataByFaqId( view.FaqId );
            
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;...&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;        }
    }
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I changed the Presenter code a bit so it looked like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; MiramarFAQ.Presenter
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; RetrieveFaqPresenter
    {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;readonly&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IRetrieveFaqView view;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;readonly&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;FONT color=#ff0000&gt;IFaqDataService&lt;/FONT&gt;&lt;FONT color=#ff0000&gt; service&lt;/FONT&gt;;

        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; RetrieveFaqPresenter(IRetrieveFaqView view) :
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(view, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; SqlFaqDataService()) { }

        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; RetrieveFaqPresenter(IRetrieveFaqView view,&lt;BR&gt;                                     IFaqDataService service)
        {
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.view &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; view;
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;.service &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; service;
        }

        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; RetrieveFAQ()
        {
            DataTable faq &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;FONT color=#ff0000&gt;service&lt;/FONT&gt;.GetDataByFaqId(view.FaqId);
            
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;...&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;        }
    }
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;All I have done is introduced an &lt;STRONG&gt;IFaqDataService&lt;/STRONG&gt; as a dependency to the presenter as oppose to hardcoding the TableAdapter in the code. This gives me a bit more pluggability of the data access layer as well as offers more testability.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; MiramarFAQ.Data
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;interface&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IFaqDataService
    {
        DataTable GetDataByFaqId(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; FaqId);
    }
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; MiramarFAQ.Data
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; SqlFaqDataService : IFaqDataService
    {
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;#region&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IFaqDataService Members&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;

        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; DataTable GetDataByFaqId(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; FaqId)
        {
            FaqTableAdapter adapter &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; FaqTableAdapter();
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; adapter.GetDataByFaqId(FaqId);
        }

        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;#endregion&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
    }
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Now that we have introduced a dependency on only an interface, &lt;STRONG&gt;IFaqDataService&lt;/STRONG&gt;, in the presenter class, we can create a stub for our unit testing:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; StubbedFaqDataService : IFaqDataService
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;#region&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IFaqDataService Members&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; DataTable GetDataByFaqId(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; FaqId)
    {
        Faq.FaqDataTable dt &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Faq.FaqDataTable();
        dt.AddFaqRow(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008080"&gt;Title&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008080"&gt;Body&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008080"&gt;Author&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008080"&gt;tag1,tag2&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;,&lt;BR&gt;               &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008080"&gt;testing...&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;, DateTime.Now);
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dt;
    }

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;#endregion&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I won't bore you with details of the unit tests, but you can see where all of this takes you. You lose that dependency on TableAdapters, and focus your dependency on an interface ( contract ), called IFaqDataService. This opens the doorway for pluggable data access services, dependency injection, and unit testing.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Again, I wish I could have been there for Scott's presentation on Model View Presenter, because the code only tells half of the story. I bet the dialogue was fantastic. Thanks to all the INETA speakers who devote time to helping the community. Thanks to Scott for sharing his code so I could vicariously be there via Sarasota, Florida. Don't forget to check out Scott's &lt;/FONT&gt;&lt;A href="http://weblogs.asp.net/scottcate/archive/2006/09/13/Florida.NET-User-Group-a-Success-_2D002D00_-Thanks-Dave-Noderer.aspx" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;source code&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;&lt;FONT face=Verdana&gt;Other Resources&lt;/FONT&gt;&lt;/H2&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;In addition to Scott Cate's wonderful example,&amp;nbsp;here are a number of great articles by some very smart people:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/jeremy.miller/archive/2006/02/01/137457.aspx" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;Test Driven Development with ASP.Net and the Model View Presenter Pattern&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://codebetter.com/blogs/jeremy.miller/archive/2006/02/16/138382.aspx" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;More Thoughts on Model View Presenter&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.codeproject.com/useritems/ModelViewPresenter.asp" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;Model View Presenter with ASP.NET&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://haacked.com/archive/2006/08/09/ASP.NETSupervisingControllerModelViewPresenterFromSchematicToUnitTestsToCode.aspx" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;ASP.NET Supervising Controller (Model View Presenter) From Schematic To Unit Tests to Code&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Source: &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;David Hayden&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; ( &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;ASP.NET Developer&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; )&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Filed: &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/category/22.aspx?Show=All"&gt;&lt;FONT face=Verdana size=2&gt;Design Patterns&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src ="http://davidhayden.com/blog/dave/aggbug/3035.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Dave Hayden</dc:creator><title>Contract-First-Design and Dependency Injection ( Microkernel )</title><link>http://davidhayden.com/blog/dave/archive/2006/09/05/Contract_First_Design_Dependency_Injection.aspx</link><pubDate>Tue, 05 Sep 2006 19:00:00 GMT</pubDate><guid>http://davidhayden.com/blog/dave/archive/2006/09/05/Contract_First_Design_Dependency_Injection.aspx</guid><description>&lt;H1&gt;&lt;FONT face=Verdana&gt;Contract-First-Design and Dependency Injection ( Microkernel )&lt;/FONT&gt;&lt;/H1&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;by &lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;David Hayden&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; ( &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;Florida .NET Developer&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; )&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;A few weeks ago I talked about &lt;A href="http://davidhayden.com/blog/dave/archive/2006/07/21/DependencyInjectionInversionOfControl.aspx"&gt;Dependency Injection Tools&lt;/A&gt; as a way to remove service locator and factory classes from my code:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2006/07/21/DependencyInjectionInversionOfControl.aspx"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;Dependency Injection Tools and Inversion of Control - Applying Domain-Driven Design and Patterns&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Although I have heard of dependency injection, inversion of control, and other related topics for some time, that was my first time using a dependency injection tool to provide the loose-coupling desired in many of my applications.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Today I had a chance to read a blog post by Ralf Sudelb&amp;#252;cher, entitled &lt;/FONT&gt;&lt;A href="http://weblogs.asp.net/ralfw/archive/2006/08/02/Dynamic-component-binding-made-easier-_2D00_-An-easy-to-use-Microkernel-to-help-reap-Contract_2D00_First_2D00_Design-benefits-in-.NET-programs.aspx" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;Dynamic component binding made easier - An easy to use Microkernel to help reap Contract-First-Design benefits in .NET programs&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;. Ralf describes the use of a Microkernel ( depedency injection like ) as a means to support Contract-First-Design. I think the post is excellent and worth the time to read if you need or would enjoy&amp;nbsp;a primer on dynamic binding of concrete services that implement contracts.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I, honestly, didn't know what Contract-First-Design meant until I read Ralf's post. He mentioned it as such:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;#8220;CFD views software as consisting of components. To avoid any heated discussion on what this term could possibly mean, let me try a very practical explanation: Think of a component as an assembly or a set of related assemblies. This assembly (or set of assemblies) you want to use together with other components to build a software solution.&amp;#8220;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;he further mentions the following:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;#8220;...&amp;nbsp; So just think: Components encapsulate some functionality I want to separate from other functionality in other components.&amp;#8220;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;&lt;FONT face=Verdana&gt;Dependency-Inversion Principle&lt;/FONT&gt;&lt;/H2&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Sounds like I have been using Contract-First-Design for quite some time, because it is essentially good use of the &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2005/06/10/1261.aspx"&gt;&lt;FONT face=Verdana size=2&gt;Dependency-Inversion Principle&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; in object-oriented programming as I discuss in the following post:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2005/06/10/1261.aspx"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;The Dependency-Inversion Principle - Chapter 11 - Agile Software Development Principles Patterns and Practices&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The Dependency-Inversion Principles states that:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face=Verdana size=2&gt;High-level modules should not depend on low-level modules. Both should depend on abstractions.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Verdana size=2&gt;Abstractions should not depend on details. Details should depend on abstractions.&lt;/FONT&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P align=center&gt;&lt;IMG alt="Dependency-Inversion Principle" src="http://www.davidhayden.com/photos/dependency-inversion%20principle.gif"&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;If you look at the code I just mentioned in the previous post, &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2006/09/01/ActiveRecordToDomainObjectsAndDataAccessObjects.aspx"&gt;&lt;FONT face=Verdana size=2&gt;Active Record To Domain Objects and Data Access Objects - Data Access Design Patterns&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;, and shown below, you will see that my CustomerDAO is not dependent on a concrete class that implements IDatabase, but the contract IDatabase. CustomerDAO itself implements a contract, ICustomerDAO, that is consumed by a client that uses the ICustomerDAO service - yet has no clue the concrete class, CustomerDAO, exists or will be the provider of the service.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;You can get crazy with this loose-coupling if it is based upon &amp;#8220;speculative design&amp;#8220; and you are adding extension points where no extension is necessary, but in general, loose-coupling and Contract-First-Design is a nice quality in your applications. As Ralf so eloquently puts it, it allows you to develop components independent of one another in parallel as well as offers great testability through mocks and stubs.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerDAO : ICustomerDAO
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IDatabase _database;
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerDAO()
    {
        _database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; DatabaseFactory.CreateDatabase();
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerDAO(IDatabase database)
    {
        Validation.AssertIsNotNull(database);
        _database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; database;
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer FindById(...)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Save(Customer customer)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Update(Customer customer)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Delete(Customer customer)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer FindByEmailAddress(...)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerCollection FindByState(...)
    {
        ...
    }
}
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Source: &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;David Hayden&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; ( &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;Florida .NET Developer&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; )&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Filed: &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/category/22.aspx?Show=All"&gt;&lt;FONT face=Verdana size=2&gt;Design Patterns&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src ="http://davidhayden.com/blog/dave/aggbug/3030.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Dave Hayden</dc:creator><title>O/R Mapping Caching for Performance vs. Uniquing ( Identity Map Design Pattern )</title><link>http://davidhayden.com/blog/dave/archive/2006/09/05/ORMapperCachingPerformanceIdentityMap.aspx</link><pubDate>Tue, 05 Sep 2006 11:38:00 GMT</pubDate><guid>http://davidhayden.com/blog/dave/archive/2006/09/05/ORMapperCachingPerformanceIdentityMap.aspx</guid><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Frans writes a great post about &lt;/FONT&gt;&lt;A href="http://weblogs.asp.net/fbouma/archive/2006/08/31/Why-a-cache-in-an-O_2F00_R-mapper-doesn_2700_t-make-it-fetch-data-faster_2E00_.aspx" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;Why a cache in an O/R mapper doesn't make it fetch data faster&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;. In fact, depending on how the cache is used, it could actually degrade performance as the management of the cache plays a factor in the performance of the &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/category/24.aspx?Show=All"&gt;&lt;FONT face=Verdana size=2&gt;O/R Mapper&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Frans mentions that the O/R Mapper's cache may not be so much about performance as it is about &lt;EM&gt;uniquing.&lt;/EM&gt; Martin Fowler in Patterns of Enterprise Application Architecture refers to &lt;EM&gt;uniquing&lt;/EM&gt;&amp;nbsp;as the Identity Map Object-Relational Behavioral Pattern - &amp;#8220;Ensures that each object gets loaded only once by keeping every loaded object in a map. Looks up objects using the map when referring to them.&amp;#8221;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The idea here is that multiple requests for the same database entity in a given context refer to the same object in memory. Therefore if I ask for the same Customer multiple times in the same context, the customer will only be retrieved once from the database and placed in&amp;nbsp;an identity map. Future&amp;nbsp;requests for the same customer in the same context will pull the customer from the identity map (&amp;nbsp;usually a hash table )&amp;nbsp;and not from the database.&amp;nbsp;In general&amp;nbsp;uniquing is referred to as a good thing, but future requests for old, stale data from an identity map in a given context may not be desirable in your application.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Frans does a much better job explaining&amp;nbsp;caching in O/R Mappers&amp;nbsp;with another must read &lt;/FONT&gt;&lt;A href="http://weblogs.asp.net/fbouma/archive/2006/08/31/Why-a-cache-in-an-O_2F00_R-mapper-doesn_2700_t-make-it-fetch-data-faster_2E00_.aspx" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;post&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Source: &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;David Hayden&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; ( &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;Sarasota Web Design&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; )&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Filed: &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/category/24.aspx?Show=All"&gt;&lt;FONT face=Verdana size=2&gt;O/R Mappers&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;, &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/category/22.aspx?Show=All"&gt;&lt;FONT face=Verdana size=2&gt;Design Patterns&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src ="http://davidhayden.com/blog/dave/aggbug/3028.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Dave Hayden</dc:creator><title>Active Record To Domain Objects and Data Access Objects - Data Access Design Patterns</title><link>http://davidhayden.com/blog/dave/archive/2006/09/01/ActiveRecordToDomainObjectsAndDataAccessObjects.aspx</link><pubDate>Fri, 01 Sep 2006 14:48:00 GMT</pubDate><guid>http://davidhayden.com/blog/dave/archive/2006/09/01/ActiveRecordToDomainObjectsAndDataAccessObjects.aspx</guid><description>&lt;H1&gt;&lt;FONT face=Verdana&gt;Active Record To Domain Objects and Data Access Objects - Data Access Design Patterns&lt;/FONT&gt;&lt;/H1&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;by &lt;A href="http://www.davidhayden.com/"&gt;David Hayden&lt;/A&gt; ( &lt;A href="http://www.davidhayden.com/"&gt;Florida .NET Developer&lt;/A&gt; )&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;In my previous post&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2006/09/01/ActiveRecordDesignPatternStaticFinderMethods.aspx"&gt;&lt;FONT face=Verdana size=2&gt;Active Record Design Pattern Doesn't Require Static Finder Methods&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I discussed the fact that the Active Record Design Pattern doesn't require &lt;U&gt;static&lt;/U&gt; finder methods for encapsulating common SQL queries to return one or more active record objects. One could move those static finder methods to a separate class, such as CustomerFinder, mentioned&amp;nbsp;here again from&amp;nbsp;the previous post:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerFinder : ICustomerFinder
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IDatabase _database;
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerFinder()
    {
        _database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; DatabaseFactory.CreateDatabase();
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerFinder(IDatabase database)
    {
        Validation.AssertIsNotNull(database);
        _database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; database;
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer FindById(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; id)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer FindByEmailAddress(...)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerCollection FindByState(...)
    {
        ...
    }
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;To each their own, but IMHO&amp;nbsp;this is rather undesirable, because when a change occurs in the database we now need to change the persistence logic in 2 classes: 1) The Active Record&amp;nbsp;Class which has read and write SQL capabilities, and 2) The Finder&amp;nbsp;Class which handles SQL finder queries.&amp;nbsp; You will also end up probably having 2 base classes or more (instead of a single layer supertype ) to refactor common code from the various active record and finder classes.&lt;/FONT&gt;&lt;/P&gt;
&lt;H2&gt;&lt;FONT face=Verdana&gt;Data Access Objects&lt;/FONT&gt;&lt;/H2&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;If static methods using active record is a problem, then the answer is not to go half-way and create Finder Classes, but to move to &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2005/05/09/1012.aspx"&gt;&lt;FONT face=Verdana size=2&gt;Data Access Objects&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; (DAO). DAO does nothing more than separate the persistence&amp;nbsp;logic for a class into a separate class. All persistent logic in the Active Record and Finder Classes gets moved to a class that is responsible for all data access needs for the class, leaving the original class with only data and domain logic behavior. The CustomerDAO&amp;nbsp;Class doesn't look much differently from the CustomerFinder Class actually. It has just accepted additional behavior that was in the Customer Class:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer&lt;FONT color=#ff0000&gt;DAO&lt;/FONT&gt; : ICustomer&lt;FONT color=#ff0000&gt;DAO
&lt;/FONT&gt;{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IDatabase _database;
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer&lt;FONT color=#ff0000&gt;DAO&lt;/FONT&gt;()
    {
        _database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; DatabaseFactory.CreateDatabase();
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer&lt;FONT color=#ff0000&gt;DAO&lt;/FONT&gt;(IDatabase database)
    {
        Validation.AssertIsNotNull(database);
        _database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; database;
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer FindById(...)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;FONT color=#ff0000&gt;Save&lt;/FONT&gt;(Customer customer)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;FONT color=#ff0000&gt;Update&lt;/FONT&gt;(Customer customer)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;FONT color=#ff0000&gt;Delete&lt;/FONT&gt;(Customer customer)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer FindByEmailAddress(...)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerCollection FindByState(...)
    {
        ...
    }
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;This is a trivial example and DAO objects will vary, but I think it shows the point. You will probably end up with a layer supertype for the domain layer and the data access layer for refactoring common code, etc:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerDAO : &lt;FONT color=#ff0000&gt;DAOBase&lt;/FONT&gt;, ICustomerDAO
{
    ...
}

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer : &lt;FONT color=#ff0000&gt;Business&lt;/FONT&gt;&lt;FONT color=#ff0000&gt;Base&lt;/FONT&gt;
{
    ...
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;&lt;FONT face=Verdana&gt;Conclusion&lt;/FONT&gt;&lt;/H2&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;If ActiveRecord Static Finder Methods are troublesome, you could create a Finder Class for each domain object. IMHO the effort is not worth the headache. Move to &lt;STRONG&gt;Data Access Objects&lt;/STRONG&gt; and leave your domain objects &lt;STRONG&gt;persistent ignorant&lt;/STRONG&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Source: &lt;A href="http://www.davidhayden.com/"&gt;David Hayden&lt;/A&gt; ( &lt;A href="http://www.davidhayden.com/"&gt;Florida .NET Developer&lt;/A&gt; )&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Filed: &lt;A href="http://davidhayden.com/blog/dave/category/22.aspx?Show=All"&gt;Design Patterns&lt;/A&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src ="http://davidhayden.com/blog/dave/aggbug/3027.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Dave Hayden</dc:creator><title>Active Record Design Pattern Doesn't Require Static Finder Methods</title><link>http://davidhayden.com/blog/dave/archive/2006/09/01/ActiveRecordDesignPatternStaticFinderMethods.aspx</link><pubDate>Fri, 01 Sep 2006 11:21:00 GMT</pubDate><guid>http://davidhayden.com/blog/dave/archive/2006/09/01/ActiveRecordDesignPatternStaticFinderMethods.aspx</guid><description>&lt;H1&gt;Active Record Design Pattern Doesn't Require Static Finder Methods&lt;/H1&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;by &lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;David Hayden&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; ( &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;Sarasota .NET Developer&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; )&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The Active Record Design Pattern that I have talked about in a few posts:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2006/06/21/2995.aspx"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;Domain-Driven Design Using Active Record in .NET&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2006/06/11/2985.aspx"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;ActiveRecord Pattern and Layer Supertype - Domain Model and Domain-Driven Design&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2006/06/12/2986.aspx"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;Castle ActiveRecord - Active Record Pattern Built on NHibernate - ASP.NET C#&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2006/06/10/2984.aspx"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;Active Record Design Pattern - Domain Driven Design and Domain Layer - Object Persistence&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;describes an object that encapsulates a row in a database table or view. The object has the ability to read and write itself to the datastore as well as&amp;nbsp;contains domain logic. As I mentioned in the previous Active Record Posts, it is most successful with forms-over-data applications, where you have simple business rules and the &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2006/08/24/DatabaseModelIsDomainModel.aspx"&gt;&lt;FONT face=Verdana size=2&gt;database model is the domain model&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;. For these types of projects you will use some type of &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/category/15.aspx?Show=All"&gt;&lt;FONT face=Verdana size=2&gt;code generation&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; and/or &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/category/24.aspx?Show=All"&gt;&lt;FONT face=Verdana size=2&gt;O/R Mapper&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; to generate the classes for you, which contain mostly data access code.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;For convenience, you will often find &lt;STRONG&gt;Static Finder Methods&lt;/STRONG&gt; on these classes to wrap common queries&amp;nbsp;that return&amp;nbsp;Active Record Objects. I say convenience, because it has become a&amp;nbsp;common practice to use static finder methods.&amp;nbsp;Static finder methods on the class&amp;nbsp;will probably be the first place a developer will look to find such functionality when you tell them you are using the Active Record Design Pattern.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;However, you don't need to use static finder methods. You could use a separate class. Hence, you could have a &lt;STRONG&gt;Customer Class&lt;/STRONG&gt; and a &lt;STRONG&gt;CustomerFinder Class&lt;/STRONG&gt;. The Customer Class would represent a single row in a database table that can read and write itself to the datastore. When you want to find a customer or a collection of customers, you could&amp;nbsp;execute instance methods on the CustomerFinder Class.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerFinder : ICustomerFinder
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; IDatabase _database;
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerFinder()
    {
        _database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; DatabaseFactory.CreateDatabase();
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerFinder(IDatabase database)
    {
        Validation.AssertIsNotNull(database);
        _database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; database;
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer FindById(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; id)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Customer FindByEmailAddress(...)
    {
        ...
    }
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; CustomerCollection FindByState(...)
    {
        ...
    }
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Whether you want to create a separate finder class is up to you. My only point is that Active Record does not require you to have static methods. It is pure convenience. If the static methods get in your way, move them to a separate class.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Source: &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;David Hayden&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; ( &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;Sarasota .NET Developer&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; )&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Filed: &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/category/22.aspx?Show=All"&gt;&lt;FONT face=Verdana size=2&gt;Design Patterns&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src ="http://davidhayden.com/blog/dave/aggbug/3026.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Dave Hayden</dc:creator><title>Dependency Injection Tools and Inversion of Control - Applying Domain-Driven Design and Patterns</title><link>http://davidhayden.com/blog/dave/archive/2006/07/21/DependencyInjectionInversionOfControl.aspx</link><pubDate>Fri, 21 Jul 2006 13:16:00 GMT</pubDate><guid>http://davidhayden.com/blog/dave/archive/2006/07/21/DependencyInjectionInversionOfControl.aspx</guid><description>&lt;H1&gt;&lt;FONT face=Verdana&gt;Dependency Injection Tools and Inversion of Control - Applying Domain-Driven Design and Patterns&lt;/FONT&gt;&lt;/H1&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;by &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;David Hayden&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; ( &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;Sarasota Web Developer&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; )&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Kudos to Erik Dornenburg, who finally made the light go off in my thick brain as to the value of &lt;STRONG&gt;Dependency Injection Tools&lt;/STRONG&gt;. Erik is a guest author in Jimmy Nilsson's new book, &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/archive/2006/07/03/ApplyingDomainDrivenDesignAndPatterns.aspx"&gt;&lt;FONT face=Verdana size=2&gt;Applying Domain-Driven Design and Patterns&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;, and he wrote the most excellent chapter, called &lt;STRONG&gt;Inversion of Control and Dependency Injection&lt;/STRONG&gt;. Here is how it happend :)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;&lt;FONT face=Verdana&gt;Service Locators and Factories&lt;/FONT&gt;&lt;/H2&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;During some down time this week I started updating some CodeSmith Templates that create an administrative backend to a database. The templates work fine, but I&amp;nbsp;thought I would&amp;nbsp;update&amp;nbsp;them to use ASP.NET Master Pages and Generics. While going through the code, I came across my typical use of &lt;STRONG&gt;Service Locator&lt;/STRONG&gt; and &lt;STRONG&gt;Factory&lt;/STRONG&gt; Classes&amp;nbsp;that offer a bit of loose coupling and testability of the data access code. Here is an example of the Factory Class, for example, that creates the Data Access Objects used in the application:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; DAOFactory
{
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; T Create&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;T&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;()
    {
        IDatabase db &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Registry.Fetch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;IDatabase&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();
        T dao &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Registry.Fetch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;T&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();
        dao.Database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; db;
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; dao;
    }
}&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;You would use the code like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #000000"&gt;ICustomerDAO dao &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; DAOFactory.Create&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;ICustomerDAO&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The &lt;STRONG&gt;Registry Class&lt;/STRONG&gt; used in the Factory is essentially a &lt;STRONG&gt;Service Locator&lt;/STRONG&gt;.&amp;nbsp;The Service Locator reads a name-value collection of concrete classes at runtime that implement the services needed by the application. Thus, when you call&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #000000"&gt;Registry.Fetch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;ICustomerDAO&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;it is essentially retrieving the concrete class that implements ICustomerDAO, which is&amp;nbsp;set in the configuration file or changed at runtime. More than likely this will just be CustomerDAO :)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Additionally, the Data Access Objects&amp;nbsp;are dependent on an IDatabase Class, which is responsible for the actual database calls and specific for each database. Although in this case I have only written one class that implements IDatabase, SqlServerDatabase, the idea is that you could have versions for Oracle, MySql, etc. This IDatabase dependency gets injected into the DAO via a setter in the Factory Class as mentioned above:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #000000"&gt;IDatabase db &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Registry.Fetch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;IDatabase&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();
T dao &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; Registry.Fetch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;T&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;();
dao.Database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; db; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Inject Dependency&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;All of this is pretty standard use of good object-oriented principles and design patterns to keep your application loosely-coupled, offer testability, and provide pluggability. But, this is stuff I don't really need to do, because Dependency Injection Tools already do this for me and do a better job - DOH!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;&lt;FONT face=Verdana&gt;Dependency Injection Tools&lt;/FONT&gt;&lt;/H2&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Around the same time that I started updating the CodeSmith Templates, I also promised myself that I would look at &lt;A href="http://codebetter.com/blogs/jeremy.miller/"&gt;Jeremy Miller&lt;/A&gt;'s &lt;A href="http://structuremap.sourceforge.net/Default.htm" target=_blank&gt;StructureMap&lt;/A&gt; that he has been talking about off and on via his blog. My reason for doing so was mainly to better understand what he felt so passionate about to create and talk about in several posts. As it happens, StructureMap just happens to be a &lt;STRONG&gt;Dependency Injection Tool&lt;/STRONG&gt;, one of several for the .NET Framework.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Now the concept of Dependency Injection is not new, although I seem to hear it more and more these days. It essentially means that objects don't go out and get other&amp;nbsp;objects that they depend upon; instead the dependent objects are given ( injected ) to them. The perfect example is my DAOFactory above. The Data Access Object doesn't go out an get the IDatabase Service it is dependent upon, I populate it via a property setter in the Factory.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #000000"&gt;dao.Database &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; db; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt; Inject Dependency&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;This is great for testability, because normally when I build and test these classes I will create test stubs that read data from an&amp;nbsp;xml&amp;nbsp;file, for example,&amp;nbsp;so that I am not dependent on a database or other external process.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Now what I had not realized is that a Dependency Injection Tool is essentially a Service Locator and Factory in one. Either during runtime or in a configuration file, you can specify all the services ( StructureMap calls these Plug-In Families ) and their concrete implementations ( Plug-Ins ) and their dependencies.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;By using StructureMap, I can remove my Factory&amp;nbsp;and Service Locator&amp;nbsp;Classes completely from the application. I map the services and dependencies in a separate configuration file, called StructureMap.config, and one call does everything:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #000000"&gt;ICustomerDAO dao &lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt; (ICustomerDAO)ObjectFactory.&lt;BR&gt;                  GetInstance(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;typeof&lt;/SPAN&gt;&lt;SPAN style="COLOR: #000000"&gt;(ICustomerDAO));&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Behind the scenes, StructureMap finds the appropriate implementation of ICustomerDAO and injects it with the appropriate implementation of IDatabase which is also injected with the appropriate connection string. It's a thing of beauty and I feel really late to the show :)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;&lt;FONT face=Verdana&gt;Conclusion&lt;/FONT&gt;&lt;/H2&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;If you haven't embraced a Dependency Injection Tool in your application, I highly recommend doing so. It makes your applications far more loosely coupled and avoids one from having to wire up all this stuff yourself using Factories, Registries, and Service Locators.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;By no means am I pushing StructureMap. There are other tools for the .NET Framework: Spring.NET, Castle Windsor, ObjectBuilder, etc. I've downloaded a number&amp;nbsp;of them and will be trying them out in the upcoming weeks.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H2&gt;&lt;FONT face=Verdana&gt;Resources:&lt;/FONT&gt;&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://structuremap.sourceforge.net/Default.htm" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;StructureMap&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.springframework.net/" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;Spring.NET&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://castleproject.org/index.php/Windsor_Container" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;Castle - Windsor&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.martinfowler.com/articles/injection.html" target=_blank&gt;&lt;FONT face=Verdana size=2&gt;Inversion of Control Containers and the Dependency Injection pattern&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Source:&amp;nbsp; &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;David Hayden&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; ( &lt;/FONT&gt;&lt;A href="http://www.davidhayden.com/"&gt;&lt;FONT face=Verdana size=2&gt;Sarasota Web Developer&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt; )&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Filed: &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/category/22.aspx?Show=All"&gt;&lt;FONT face=Verdana size=2&gt;Design Patterns&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana size=2&gt;, &lt;/FONT&gt;&lt;A href="http://davidhayden.com/blog/dave/category/10.aspx?Show=All"&gt;&lt;FONT face=Verdana size=2&gt;.NET Tools&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src ="http://davidhayden.com/blog/dave/aggbug/3007.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Dave Hayden</dc:creator><title>Agile Principles, Patterns, and Practices in C# by Robert Martin</title><link>http://davidhayden.com/blog/dave/archive/2006/07/03/AgilePrinciplesPatternsAndPractices.aspx</link><pubDate>Mon, 03 Jul 2006 23:34:00 GMT</pubDate><guid>http://davidhayden.com/blog/dave/archive/2006/07/03/AgilePrinciplesPatternsAndPractices.aspx</guid><description>I just got back from a long and much needed vacation. While spending most of the day trying to make a dent in my Inbox, I came across an email from Amazon mentioning a new book from Robert Martin, called Agile Principles, Patterns, and Practices in C#...&lt;img src ="http://davidhayden.com/blog/dave/aggbug/2999.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>