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

<channel>
	<title>Randy Patterson &#187; C#</title>
	<atom:link href="http://RandyPatterson.com/index.php/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://RandyPatterson.com</link>
	<description>Code To Live, Live To Code</description>
	<lastBuildDate>Sat, 24 Apr 2010 01:18:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ReSharper and the Method Group Refactor</title>
		<link>http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/</link>
		<comments>http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 22:01:59 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[ReSharper]]></category>

		<guid isPermaLink="false">http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/</guid>
		<description><![CDATA[While writing code similar to the following lines, ReSharper suggested the “Replace With Method Group” Refactoring for Line 2.
&#160;


   1: var names = Directory.GetFiles(@&#34;c:\Program Files&#34;).ToList();

   2: names.ForEach(n =&#62; Console.WriteLine(n));


The Method Group shorthand is usually found in places where you take the following code 
myButton.Click += new EventHandler(myButton_Click);
and replace it with this.

myButton.Click [...]]]></description>
			<content:encoded><![CDATA[<div id="codeSnippetWrapper">While writing code similar to the following lines, ReSharper suggested the “Replace With Method Group” Refactoring for Line 2.</div>
<div>&#160;</div>
<div>
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> var names = Directory.GetFiles(<span style="color: #006080">@&quot;c:\Program Files&quot;</span>).ToList();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> names.ForEach(n =&gt; Console.WriteLine(n));</pre>
<p><!--CRLF--></div>
</div>
<p>The Method Group shorthand is usually found in places where you take the following code </p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">myButton.Click += <span style="color: #0000ff">new</span> EventHandler(myButton_Click);</pre>
<p>and replace it with this.</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">myButton.Click += myButton_Click;</pre>
<p>The C# compiler can infer the usage of the EventHandler class, giving you cleaner, less noisy code. </p>
<p></div>
<div>Letting Resharper apply the Method Group refactoring produces the following code.</div>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> var names = Directory.GetFiles(<span style="color: #006080">@&quot;c:\ProgramFiles&quot;</span>).ToList();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> names.ForEach(Console.WriteLine);</pre>
<p><!--CRLF--></div>
</div>
<div id="codeSnippetWrapper">&#160;</div>
<div>I was pleasantly surprised by the results. Apparently, I don’t have to explicitly declare the intermediate lambda variable ‘n’ just so I can pass it into the WriteLine method. The compiler is smart enough to infer this for me.&#160; Although a little confusing at first, this shorthand notation is pretty nice.&#160; I doubt I would have realized this without ReSharpers’ refactoring suggestions, one of the many reasons I’m a R# Junkie.</div>
<div>
  </div>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/&amp;title=ReSharper+and+the+Method+Group+Refactor" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/&amp;title=ReSharper+and+the+Method+Group+Refactor" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/&amp;title=ReSharper+and+the+Method+Group+Refactor" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=ReSharper+and+the+Method+Group+Refactor&amp;url=http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/&amp;title=ReSharper+and+the+Method+Group+Refactor" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/&amp;title=ReSharper+and+the+Method+Group+Refactor" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/&amp;title=ReSharper+and+the+Method+Group+Refactor" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/&amp;title=ReSharper+and+the+Method+Group+Refactor" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/&amp;title=ReSharper+and+the+Method+Group+Refactor" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+ReSharper+and+the+Method+Group+Refactor+@+http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/&amp;t=ReSharper+and+the+Method+Group+Refactor" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://RandyPatterson.com/index.php/2010/03/12/resharper-and-the-method-group-refactor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>POCO and Unity Application Block Part II</title>
		<link>http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/</link>
		<comments>http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 19:00:01 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Unity Application Block]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://localhost/2008/09/28/POCOAndUnityApplicationBlockPartII.aspx</guid>
		<description><![CDATA[In my previous post I covered the configuration of the Unity Application Block for Constructor Injection without modifying the class being injected.&#160; In this post I will cover Property Injection and how write API and XML configuration.
The following code has a dependency on the ILogger class exposed through a public property in line 10 (Property [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://randypatterson.com/2008/09/22/POCOAndUnityApplicationBlockPartI.aspx">previous post</a> I covered the configuration of the <a href="http://www.codeplex.com/unity">Unity Application Block</a> for Constructor Injection without modifying the class being injected.&nbsp; In this post I will cover Property Injection and how write API and XML configuration.</p>
<p>The following code has a dependency on the <font color="#0080ff">ILogger</font> class exposed through a public property in line 10 (Property Injection).</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:1c27040d-9acd-47f8-94e8-cc2cbfb0204d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">public class MainFormPresenter
{
    private readonly IProductRepository _productRepository;

    public MainFormPresenter(IProductRepository productRepository)
    {
        _productRepository = productRepository;
    }

    public ILogger Logger { protected get; set; }

    public IList&lt;Product&gt; GetAllProducts()
    {
        Logger.Log("Start LINQ Query");

        return _productRepository
            .Query(p =&gt; p.Name.StartsWith("M"))
            .OrderBy(p =&gt; p.Name)
            .ThenByDescending(p =&gt; p.ListPrice)
            .ToList();
    }
}
</pre>
</div>
</p>
<p>Without the [<font color="#0080ff">Dependency</font>] attribute, Unity has no idea that we need a class created and injected into the Logger Property.&nbsp; Using the container API it&#8217;s a trivial matter of informing Unity of our requirements. Lines 9 and 10 in the code below will tell Unity that whenever class <font color="#0080c0">MainFormPresenter</font> is created, inject Property &#8220;Logger&#8221; with a class that matches it&#8217;s type.&nbsp;&nbsp; In addition, since the property type is <font color="#0080c0">ILogger</font>, Unity needs a mapping to a concrete class or it will throw an exception (line 7).</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:abaaafd0-21cc-4621-b129-0ff6c1da30d5" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">IUnityContainer unityContainer = new UnityContainer();

//Configure Container
unityContainer
    .RegisterType&lt;IProductRepository, ProductRepository&gt;()
    .RegisterType&lt;DataContext, AdventureWorksDataContext&gt;()
    .RegisterType&lt;ILogger, DebugLogger&gt;();

unityContainer.Configure&lt;InjectedMembers&gt;()
    .ConfigureInjectionFor&lt;MainFormPresenter&gt;(new InjectionProperty("Logger"));
</pre>
</div>
<p>The following XML snippet will perform the same configuration</p>
<pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  1: <span style="color: #0000ff">&lt;</span><span style="color: #800000">type</span> <span style="color: #ff0000">type</span>=<span style="color: #0000ff">"UnityDemo.Views.MainFormPresenter, WindowsFormsApplication3"</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: <span style="color: #0000ff">&lt;</span><span style="color: #800000">typeConfig</span> <span style="color: #ff0000">extensionType</span>=<span style="color: #0000ff">"Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration"</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffb3">  3: 	<span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span> =<span style="color: #0000ff">"Logger"</span> <span style="color: #ff0000">propertyType</span>=<span style="color: #0000ff">"UnityDemo.Logging.ILogger, WindowsFormsApplication3"</span><span style="color: #0000ff">/&gt;</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: <span style="color: #0000ff">&lt;/</span><span style="color: #800000">typeConfig</span><span style="color: #0000ff">&gt;</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  5: <span style="color: #0000ff">&lt;/</span><span style="color: #800000">type</span><span style="color: #0000ff">&gt;</span>
</pre>
</pre>
<p>&nbsp;</p>
<h3>Related Posts:</h3>
<p><a href="POCO and Unity Application Block Part I">POCO and Unity Application Block Part I &#8211; Constructor Injection</a></p>
<p>&nbsp;</p>
<p>	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shCore.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushBash.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushCpp.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushCSharp.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushCss.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushDelphi.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushDiff.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushGroovy.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushJava.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushJScript.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushPhp.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushPlain.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushPython.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushRuby.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushScala.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushSql.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushVb.js"></script><br />
	<script type="text/javascript" src="/Scripts/SyntaxHighlighter2/scripts/shBrushXml.js"></script></p>
<link type="text/css" rel="stylesheet" href="/Scripts/SyntaxHighlighter2/styles/shCore.css"/>
<link type="text/css" rel="stylesheet" href="/Scripts/SyntaxHighlighter2/styles/shThemeDefault.css"/>
	<script type="text/javascript">
		SyntaxHighlighter.config.clipboardSwf = '/Scripts/SyntaxHighlighter2/scripts/clipboard.swf';
		SyntaxHighlighter.all();
	</script></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/&amp;title=POCO+and+Unity+Application+Block+Part+II" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/&amp;title=POCO+and+Unity+Application+Block+Part+II" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/&amp;title=POCO+and+Unity+Application+Block+Part+II" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=POCO+and+Unity+Application+Block+Part+II&amp;url=http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/&amp;title=POCO+and+Unity+Application+Block+Part+II" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/&amp;title=POCO+and+Unity+Application+Block+Part+II" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/&amp;title=POCO+and+Unity+Application+Block+Part+II" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/&amp;title=POCO+and+Unity+Application+Block+Part+II" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/&amp;title=POCO+and+Unity+Application+Block+Part+II" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+POCO+and+Unity+Application+Block+Part+II+@+http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/&amp;t=POCO+and+Unity+Application+Block+Part+II" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://RandyPatterson.com/index.php/2008/09/28/poco-and-unity-application-block-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>POCO and Unity Application Block Part I</title>
		<link>http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/</link>
		<comments>http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 05:13:48 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Unity Application Block]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://localhost/2008/09/22/POCOAndUnityApplicationBlockPartI.aspx</guid>
		<description><![CDATA[Using Plain Old CLR Objects (POCO) with the Unity Application Block
Plain old CLR objects is a term borrowed from Java (POJO) that refers to objects that are not entangled with framework specific code or dependencies.  For Example, when classes have multiple constructors you can add the [InjectionConstructor] Attribute to the constructor that you want Unity [...]]]></description>
			<content:encoded><![CDATA[<p>Using Plain Old CLR Objects (POCO) with the <a href="http://www.codeplex.com/unity">Unity Application Block</a></p>
<p>Plain old CLR objects is a term borrowed from Java (POJO) that refers to objects that are not entangled with framework specific code or dependencies.  For Example, when classes have multiple constructors you can add the [<span style="color: #0080ff;">InjectionConstructor</span>] Attribute to the constructor that you want Unity to use.</p>
<div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:e598e936-42f7-4a3b-b6f4-a2600af9bac4" class="wlWriterSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre class="c#">public class MainFormPresenter
{
    private readonly IProductRepository _productRepository;

    public MainFormPresenter()
    {
        //Do Something Profound
    }

    [InjectionConstructor]
    public MainFormPresenter(IProductRepository productRepository)
    {
        _productRepository = productRepository;
    }

    public IList&lt;Product&gt; GetAllProducts()
    {
        return _productRepository
            .Query(p =&gt; p.Name.StartsWith("M"))
            .OrderBy(p =&gt; p.Name)
            .ThenByDescending(p =&gt; p.ListPrice)
            .ToList();
    }
}</pre>
</div>
<p>This, however, is where POCO advocates become vocal.  The problem is by adding the attribute our class has been &#8220;polluted&#8221; with Unity specific code and it would become difficult to replace Unity with another framework.  Fortunately, Unity also allows you to accomplish the same thing using API configuration or XML configuration files.</p>
<h3>Selecting a Constructor using the Unity API</h3>
<p>Using the Unity API to configure which constructor to use takes just one line of code:</p>
<div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:dc85c5df-2fae-4d6e-8f6e-9a6894e7b42d" class="wlWriterSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre class="c#">IUnityContainer unityContainer = new UnityContainer();

//Configure Container
unityContainer.RegisterType&lt;IProductRepository, ProductRepository&gt;()

//Configure constructor for MainFormPresenter
unityContainer.Configure&lt;InjectedMembers&gt;()
    .ConfigureInjectionFor&lt;MainFormPresenter&gt;(new InjectionConstructor(typeof (IProductRepository)));</pre>
</div>
<p>Lines 7 and 8 tell unity that when the class <span style="color: #0080ff;">MainFormPresenter</span> is instantiated use the constructor that has a single parameter of type  <span style="color: #0080ff;">IProductRepository</span>. This information is difficult to find in the documentation but is straight forward and easy to read.</p>
<h3>Selecting a Constructor using XML</h3>
<p>Configuring Unity using an XML file is a bit more verbose but fairly easy to understand nonetheless. The first step is to tell Unity to load your config file using code similar to this.</p>
<div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:dcdeaac2-5906-482c-9590-26c80d1685f8" class="wlWriterSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre class="c#">IUnityContainer unityContainer = new UnityContainer();

//Configure Container
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(unityContainer);</pre>
</div>
<p>Next, update your App.config, Web.config or whatever config file you choose to use to include information to inform Unity which constructor to use.</p>
<pre style="background-color: #fbfbfb; min-height: 40px; width: 650px; overflow: auto; border: #cecece 1px solid; padding: 5px;">
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  1: <span style="color: #0000ff">&lt;</span><span style="color: #800000">types</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  2:   <span style="color: #0000ff">&lt;</span><span style="color: #800000">type</span> <span style="color: #ff0000">type</span>=<span style="color: #0000ff">"UnityDemo.Views.MainFormPresenter, WindowsFormsApplication3"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  3:     <span style="color: #0000ff">&lt;</span><span style="color: #800000">typeConfig</span> <span style="color: #ff0000">extensionType</span>=<span style="color: #0000ff">"Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="background-color: #ffffb3; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  4:       <span style="color: #0000ff">&lt;</span><span style="color: #800000">constructor</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="background-color: #ffffb3; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  5:         <span style="color: #0000ff">&lt;</span><span style="color: #800000">param</span> <span style="color: #ff0000">name</span> =<span style="color: #0000ff">"productRepository"</span> <span style="color: #ff0000">parameterType</span>=<span style="color: #0000ff">"AWRepository.Products.IProductRepository, AWRepository"</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="background-color: #ffffb3; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  6:           <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span> <span style="color: #0000ff">/&gt;</span></pre>
<pre style="background-color: #ffffb3; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  7:         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">param</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="background-color: #ffffb3; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  8:       <span style="color: #0000ff">&lt;/</span><span style="color: #800000">constructor</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  9:     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">typeConfig</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 10:   <span style="color: #0000ff">&lt;/</span><span style="color: #800000">type</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 11:   <span style="color: #0000ff">&lt;</span><span style="color: #800000">type</span> <span style="color: #ff0000">type</span>=<span style="color: #0000ff">"AWRepository.Products.IProductRepository, AWRepository"</span></pre>
<pre style="background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 12:         <span style="color: #ff0000">mapTo</span>=<span style="color: #0000ff">"AWRepository.Products.ProductRepository, AWRepository"</span><span style="color: #0000ff">/&gt;</span></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> 13: <span style="color: #0000ff">&lt;/</span><span style="color: #800000">types</span><span style="color: #0000ff">&gt;</span></pre>
</pre>
<p>Lines 4-8 inform unity which constructor to use in much the same way as the API configuration preceding it.  The <span style="color: #0080ff;">&lt;dependency /&gt;</span> element tells unity to resolve the parameter type (<span style="color: #0080ff;">IProductRepository</span> in this case) using the mapping it already contains. </p>
<h3>Conclusion</h3>
<p>Allowing configuration information, such as constructor selection, without modifying the injected class not only allows POCO but also allows us to use non-friendly dependency injection classes that we cannot modify.   For example,  Generated classes for LINQ to SQL <a title="DataContext Class" href="http://msdn2.microsoft.com/bb298245.aspx">DataContext</a> contains 4 constructors, two with one parameter and two with two parameters. Due to the similar constructor signatures Unity will not be able to determine which one to use and will throw an exception if no guidance is provided. Fortunately we can use API or XML configuration to inform Unity how to handle these unfriendly classes.  I tend to favor the use of Attributes because they are easier to use and easier to understand intent. However, It is critical to provide indirect configuration to allow the use of classes that are not friendly to dependency Injection and to allow POCO classes when there is a need.</p>
<p>In my next post I will discuss how to Configure Unity for Property Injection without using the [<span style="color: #0080ff;">Dependency</span>] Attribute</p>
<p> </p>
<p>Related Posts:</p>
<p><a href="http://randypatterson.com/2008/09/28/POCOAndUnityApplicationBlockPartII.aspx">POCO and Unity Application Block Part II &#8211; Property Injection</a> </p>
<p> </p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2frandypatterson.com%2f2008%2f09%2f22%2fPOCOAndUnityApplicationBlockPartI.aspx"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2frandypatterson.com%2f2008%2f09%2f22%2fPOCOAndUnityApplicationBlockPartI.aspx" border="0" alt="kick it on DotNetKicks.com" /></a> <script src="/Scripts/shCore.js"></script><script src="/Scripts/shBrushCSharp.js"></script></p>
<p><script type="text/javascript">// <![CDATA[
 window.onload = function() {     dp.SyntaxHighlighter.ClipboardSwf = '/Scripts/clipboard.swf';     dp.SyntaxHighlighter.HighlightAll('code'); };
// ]]&gt;</script></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/&amp;title=POCO+and+Unity+Application+Block+Part+I" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/&amp;title=POCO+and+Unity+Application+Block+Part+I" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/&amp;title=POCO+and+Unity+Application+Block+Part+I" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=POCO+and+Unity+Application+Block+Part+I&amp;url=http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/&amp;title=POCO+and+Unity+Application+Block+Part+I" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/&amp;title=POCO+and+Unity+Application+Block+Part+I" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/&amp;title=POCO+and+Unity+Application+Block+Part+I" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/&amp;title=POCO+and+Unity+Application+Block+Part+I" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/&amp;title=POCO+and+Unity+Application+Block+Part+I" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+POCO+and+Unity+Application+Block+Part+I+@+http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/&amp;t=POCO+and+Unity+Application+Block+Part+I" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://RandyPatterson.com/index.php/2008/09/22/poco-and-unity-application-block-part-i/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Resharper Tip #1 &#8211; Highlight Usages</title>
		<link>http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/</link>
		<comments>http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/#comments</comments>
		<pubDate>Sun, 13 Jul 2008 05:28:06 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[ReSharper]]></category>

		<guid isPermaLink="false">http://localhost/2008/07/13/ResharperTip1HighlightUsages.aspx</guid>
		<description><![CDATA[I just love ReSharper (R#) by JetBrains, it has substantially increased my productivity while writing, and navigating code. 
Quickly highlight all usages of a symbol within the current file.&#160;&#160; To use,&#160; place your cursor on the Type or Variable that you want to highlight then press Shift+Ctrl+F7
 
The Write operations are highlighted in light Red [...]]]></description>
			<content:encoded><![CDATA[<p>I just love <a href="http://www.jetbrains.com/resharper/" target="_blank">ReSharper</a> (R#) by JetBrains, it has substantially increased my productivity while writing, and navigating code. </p>
<p>Quickly highlight all usages of a symbol within the current file.&nbsp;&nbsp; To use,&nbsp; place your cursor on the Type or Variable that you want to highlight then press Shift+Ctrl+F7</p>
<p><a href="http://randypatterson.com/images/ResharperTip1HighlightUsages_11F56/image.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="324" alt="Highlight Usages" src="http://randypatterson.com/images/ResharperTip1HighlightUsages_11F56/image_thumb.png" width="668" border="0"></a> </p>
<p>The Write operations are highlighted in light Red and the Read operations are highlighted in light Blue.&nbsp; Pressing ESC removes the highlights. Furthermore, the R# Marker Bar to the right of your code uses to same colors to highlight all usages in the current file.&nbsp; Just click on the horizontal mark to quickly navigate to the usage.</p>
<p><a href="http://randypatterson.com/images/ResharperTip1HighlightUsages_11F56/image_3.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://randypatterson.com/images/ResharperTip1HighlightUsages_11F56/image_thumb_3.png" width="128" border="0"></a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/&amp;title=Resharper+Tip+%231+%26%238211%3B+Highlight+Usages" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/&amp;title=Resharper+Tip+%231+%26%238211%3B+Highlight+Usages" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/&amp;title=Resharper+Tip+%231+%26%238211%3B+Highlight+Usages" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Resharper+Tip+%231+%26%238211%3B+Highlight+Usages&amp;url=http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/&amp;title=Resharper+Tip+%231+%26%238211%3B+Highlight+Usages" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/&amp;title=Resharper+Tip+%231+%26%238211%3B+Highlight+Usages" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/&amp;title=Resharper+Tip+%231+%26%238211%3B+Highlight+Usages" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/&amp;title=Resharper+Tip+%231+%26%238211%3B+Highlight+Usages" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/&amp;title=Resharper+Tip+%231+%26%238211%3B+Highlight+Usages" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Resharper+Tip+%231+%26%238211%3B+Highlight+Usages+@+http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/&amp;t=Resharper+Tip+%231+%26%238211%3B+Highlight+Usages" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://RandyPatterson.com/index.php/2008/07/13/resharper-tip-1-highlight-usages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unity Application Block Event Broker</title>
		<link>http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/</link>
		<comments>http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/#comments</comments>
		<pubDate>Fri, 02 May 2008 10:02:08 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Unity Application Block]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://localhost/2008/05/02/UnityApplicationBlockEventBroker.aspx</guid>
		<description><![CDATA[The Unity Application Block IoC Container comes with a little know extension for creating loosely coupled events called the Simple Event Broker.&#160; The Unity Event Broker supports multiple publishers and multiple subscribers to the same event name.&#160; The decoupled model prevents subscribers from knowing about publishers and publishers from knowing about subscribers.
&#160;
 
&#160;
The Event Broker [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.codeplex.com/unity">Unity Application Block</a> <a href="http://www.martinfowler.com/articles/injection.html">IoC Container</a> comes with a little know extension for creating <a href="http://msdn.microsoft.com/en-us/library/0czwd3tx(VS.80).aspx">loosely coupled events</a> called the <em>Simple Event Broker</em>.&nbsp; The Unity Event Broker supports multiple publishers and multiple subscribers to the same event name.&nbsp; The decoupled model prevents subscribers from knowing about publishers and publishers from knowing about subscribers.</p>
<p>&nbsp;</p>
<p><a href="http://randypatterson.com/images/UnityEventBroker_A692/image.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="129" alt="image" src="http://randypatterson.com/images/UnityEventBroker_A692/image_thumb.png" width="543" border="0"></a> </p>
<p>&nbsp;</p>
<p>The Event Broker source code can be found under the Unity Quick Starts normally located here </p>
<p>&#8220;C:\Program Files\Microsoft Unity Application Block 1.0\UnityQuickStarts.zip\UnityQuickStarts\CS\EventBroker.sln&#8221;.&nbsp; </p>
<p>In order to use the Extension make sure you have your project reference the <em>EventBrokerExtension.dll</em> and the <em>SimpleEventBroker.dll</em>.</p>
<p>Next, you need to configure Unity by adding the Event Broker Extension to the Container.</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:3b0693e4-8518-4e74-a05c-b5a8770b4d10" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            UnityContainer rootContainer = new UnityContainer();

            rootContainer.AddNewExtension&lt;SimpleEventBrokerExtension&gt;();

            Application.Run(rootContainer.Resolve&lt;DefaultForm&gt;());
        }</pre>
</div>
<p>&nbsp;</p>
<p>This gives you two new Attributes recognized by Unity, <font face="Courier New" size="2">[Publishes(<em>eventName</em>)] </font>and <font size="2">[SubscribesTo(<em>eventName</em>)] </font>where <strong><em>eventName </em></strong>is a string that uniquely identifies the loosely coupled event.</p>
<p>The attribute [<em>Publishes]</em> is applied to an event and lets the event broker know that when this event is raised, any methods that are subscribed to <strong><em>eventName </em></strong>are called.&nbsp; </p>
<p>For Example:</p>
</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:cf4d7b1c-4fa0-4855-a031-48c2deb4736c" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">        [Publishes("event://Transaction/Complete")]
        public event EventHandler TransactionComplete;
</pre>
</div>
<table cellspacing="0" cellpadding="2" width="402" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<p align="center"><font color="#0080ff" size="3">Best Practice:</font></p>
</td>
</tr>
<tr>
<td valign="top" width="400">
<p align="left">It is recommended that you not hard code the Event Name parameter.&nbsp; Instead use a class that contains the event names as public string constants.</p>
<p align="left">[SubscribesTo(EventTopicNames.TransactionComplete)]</p>
<p>This prevents typos in the event name string and also allows a Usages Search in Visual Studio to quickly locate all Publishers and all Subscribers of an event.</p>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>The event broker will create a loosely coupled event named &#8220;event://Transaction/Completed&#8221; [1] and link the .NET event <em>TransactionComplete </em>[2]<em>&nbsp;</em>to it<em>. </em>The following subscriber code </p>
</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:9417ed51-a984-48b1-8a7e-804a7e5dff25" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">        [SubscribesTo("event://Transaction/Complete")]
        public void TransactionCompleteHandler(object sender, EventArgs e)
        {
            //Do Something.....
        }
</pre>
</div>
<p>informs the Event Broker that method TransactionCompleteHandler [3] needs to be called whenever the loosely coupled event named &#8220;event://Transaction/Completed&#8221; is published. Notice that neither the publisher nor the subscriber is aware of the other. </p>
<h3>Conclusion</h3>
<p align="left">The ability for your application to communicate between controls without resorting to directly linking one class to another increases the reusability of your code.&nbsp; The Unity Event Broker is, as it&#8217;s name implies, a rather simple implementation of loosely coupled events but it is a good start none the less.&nbsp; for example,&nbsp; I would like to see a way for subscribers to indicate that methods should be called on a background thread instead of always on the Publisher&#8217;s thread. In future posts I will show how to register events for publication when you cannot add the [<em>Publishes]</em> attribute directly to an event declaration (useful for Button and Menu Click events or third part controls)</p>
<p>&nbsp;</p>
<ol>
<li>I<font size="1"> use the URL style string to name the decoupled events purely out of habit.&nbsp; Obviously, any string will do but I find the URL format to be clean and easy to read.</font>
<li><font size="1">Events must be scoped public in order for the Event Broker to detect the Publishes attribute</font>
<li><font size="1">The subscription method must be scoped public in order for the Event Broker to detect the SubscribesTo attribute</font></li>
</ol>
<p>&nbsp;</p>
<link href="Styles/SyntaxHighlighter.css" type="text/css" rel="stylesheet"></link>
<script language="javascript" src="Scripts/shCore.js"></script></p>
<p><script language="javascript" src="Scripts/shBrushCSharp.js"></script></p>
<p><script language="javascript">
window.onload = function() {
    dp.SyntaxHighlighter.ClipboardSwf = 'Scripts/clipboard.swf';
    dp.SyntaxHighlighter.HighlightAll('code');
};
 </script></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/&amp;title=Unity+Application+Block+Event+Broker" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/&amp;title=Unity+Application+Block+Event+Broker" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/&amp;title=Unity+Application+Block+Event+Broker" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Unity+Application+Block+Event+Broker&amp;url=http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/&amp;title=Unity+Application+Block+Event+Broker" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/&amp;title=Unity+Application+Block+Event+Broker" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/&amp;title=Unity+Application+Block+Event+Broker" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/&amp;title=Unity+Application+Block+Event+Broker" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/&amp;title=Unity+Application+Block+Event+Broker" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Unity+Application+Block+Event+Broker+@+http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/&amp;t=Unity+Application+Block+Event+Broker" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://RandyPatterson.com/index.php/2008/05/02/unity-application-block-event-broker/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2008 Express Released</title>
		<link>http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/</link>
		<comments>http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/#comments</comments>
		<pubDate>Sat, 24 Nov 2007 14:57:43 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://localhost/2007/11/24/VisualStudio2008ExpressReleased.aspx</guid>
		<description><![CDATA[If you don&#8217;t have an MSDN subscription and you can&#8217;t wait to try out the new Visual Studio 2008 features you can download the express edition and learn the new features while you wait for the professional edition to be available in stores.
http://www.microsoft.com/express/


Bookmark It




















]]></description>
			<content:encoded><![CDATA[<p>If you don&#8217;t have an MSDN subscription and you can&#8217;t wait to try out the new Visual Studio 2008 features you can download the express edition and learn the new features while you wait for the professional edition to be available in stores.</p>
<p><a title="http://www.microsoft.com/express/" href="http://www.microsoft.com/express/">http://www.microsoft.com/express/</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/&amp;title=Visual+Studio+2008+Express+Released" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/&amp;title=Visual+Studio+2008+Express+Released" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/&amp;title=Visual+Studio+2008+Express+Released" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Visual+Studio+2008+Express+Released&amp;url=http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/&amp;title=Visual+Studio+2008+Express+Released" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/&amp;title=Visual+Studio+2008+Express+Released" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/&amp;title=Visual+Studio+2008+Express+Released" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/&amp;title=Visual+Studio+2008+Express+Released" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/&amp;title=Visual+Studio+2008+Express+Released" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Visual+Studio+2008+Express+Released+@+http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/&amp;t=Visual+Studio+2008+Express+Released" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://RandyPatterson.com/index.php/2007/11/24/visual-studio-2008-express-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to design a Fluent Interface</title>
		<link>http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/</link>
		<comments>http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 17:25:09 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://localhost/2007/09/26/HowToDesignAFluentInterface.aspx</guid>
		<description><![CDATA[Martin Fowler coined the term &#8220;FluentInterface&#8221; to describe objects that expose an interface that flows, and is designed to be readable and concise.&#160; The cost of this fluency is additional effort required to design the interface for your object and the slight increase in complexity.&#160; These types of interfaces are often utilized to create configurations [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Martin_Fowler" target="_blank">Martin Fowler</a> coined the term &#8220;<a href="http://martinfowler.com/bliki/FluentInterface.html" target="_blank">FluentInterface</a>&#8221; to describe objects that expose an interface that <em>flows, </em>and is designed to be readable and concise.&nbsp; The cost of this fluency is additional effort required to design the interface for your object and the slight increase in complexity.&nbsp; These types of interfaces are often utilized to create configurations for your objects but can progress into an internal <a href="http://martinfowler.com/bliki/DomainSpecificLanguage.html" target="_blank">Domain Specific Language</a> or <span class="acronym" title="Domain Specific Language">DSL</span>.</p>
<p><em>Configuration Fluent Interface Example: (sometimes called Method Chaining</em>) </p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:58ea0d9a-72a2-4290-8cc9-ff27000ce650" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#fddf39">Order</span><br />
<span style="background:#000000;color:#fddf39">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">AddFreeShipping</span><span style="background:#000000;color:#ffffff">()</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;.</span><span style="background:#000000;color:#fddf39">IncludeItem</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#8acccf">10</span><span style="background:#000000;color:#ffffff">)</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;.</span><span style="background:#000000;color:#fddf39">SetQuantity</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#8acccf">2</span><span style="background:#000000;color:#ffffff">);</span></div>
</div>
</div>
<p><em>DSL Fluent Interface Example:&nbsp; (<a href="http://ayende.com/projects/rhino-mocks.aspx" target="_blank">Rhino Mocks</a>)</em>&nbsp;</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5997c476-765b-4a20-9a63-8bb7bd84a825" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#fddf39">Expect</span><br />
<span style="background:#000000;color:#fddf39">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Call</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">entityMock</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">GetID</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#8acccf">12</span><span style="background:#000000;color:#ffffff">))</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;.</span><span style="background:#000000;color:#fddf39">IgnoreArguments</span><span style="background:#000000;color:#ffffff">()</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;.</span><span style="background:#000000;color:#fddf39">Repeat</span><br />
<span style="background:#000000;color:#fddf39">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Once</span><span style="background:#000000;color:#ffffff">()</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;.</span><span style="background:#000000;color:#fddf39">Return</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">entityDataSet</span><span style="background:#000000;color:#ffffff">);</span></div>
</div>
</div>
<p>
<h3>Show me the Code</h3>
</p>
<p><br/></p>
<p>Fluent interfaces are best explained by showing some code examples, so I&#8217;ll take the rather prosaic <em>Person</em> object and create a fluent interface for it.&nbsp;&nbsp;&nbsp; </p>
<p><em>Take a common Person object and write some code to instantiate and Initialize the object</em></p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:11a747a8-a7f7-4cb2-b0e3-0dd51b7a4a29" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">class</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#b3b300">Person</span><br />
<span style="background:#000000;color:#b3b300">{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">private</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_firstName</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">private</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_lastName</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">private</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">int</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_age</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">bool</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_isActive</span><span style="background:#000000;color:#ffffff">;</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">firstName</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">lastName</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#ff8000">int</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">age</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#ff8000">bool</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">isActive</span><span style="background:#000000;color:#ffffff">)</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_firstName</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#fddf39">firstName</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_lastName</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#fddf39">lastName</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_age</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#fddf39">age</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_isActive</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#fddf39">isActive</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">FirstName</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">get</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_firstName</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">set</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#fddf39">_firstName</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">value</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">LastName</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">get</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_lastName</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">set</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#fddf39">_lastName</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">value</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">int</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Age</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">get</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_age</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">set</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#fddf39">_age</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">value</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">bool</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">IsActive</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">get</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_isActive</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">set</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#fddf39">_isActive</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">value</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span><br />
<span style="background:#000000;color:#ffffff">}</span></div>
</div>
</div>
<p>This is a common pattern where the constructor is used to quickly set the properties of the class.&nbsp; The initialization code would look something like this. </p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c37962ad-6f91-4783-b54b-bfdca548780d" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">new</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;Frank&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#00ff00">&quot;Pat&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#8acccf">30</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#ff8000">true</span><span style="background:#000000;color:#ffffff">); </span></div>
</div>
</div>
<p>Although this code is concise it is not very readable. Is &#8220;Frank&#8221; the first name or the last name? What does the value <font color="#0000ff">true</font> represent? The readability issue becomes more problematic as the number of construction parameters increase.&nbsp; One solution, and the point of this post, is to write a <a href="http://martinfowler.com/bliki/FluentInterface.html" target="_blank">Fluent Interface</a> for our Person class.&nbsp; The idea here is to allow each property to be set through a method call and then have that method return a reference to itself so you can continue on with next method call (often called method chaining). </p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1ea32156-e0cd-43c0-9ad6-9f3651240b50" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">SetLastName</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;Frank&quot;</span><span style="background:#000000;color:#ffffff">).</span><span style="background:#000000;color:#fddf39">SetFirstName</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;Pat&quot;</span><span style="background:#000000;color:#ffffff">);</span></div>
</div>
</div>
<p>This makes our code concise and far easier to read, we now know that &#8220;Frank&#8221; is the last name.&nbsp; However, an obvious issue with this pattern is that we are now cluttering up our class with methods that, when taken out of context, make little sense.&nbsp; We have a <font color="#0000ff">FirstName</font> property and a <font color="#0000ff">SetFirstName</font> method defined on our class that obfuscates the intent. A better approach is to create an internal class that is accessed through a <font color="#0000ff">Set </font>property and exposes only the <a href="http://martinfowler.com/bliki/FluentInterface.html" target="_blank">Fluent Interface</a>.</p>
<p>&nbsp;</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:41e90882-d9b6-4b3f-973e-7906d55217f2" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">class</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#b3b300">Person</span><br />
<span style="background:#000000;color:#b3b300">{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">()</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_set</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">new</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">PersonFluentInterface</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">this</span><span style="background:#000000;color:#ffffff">);</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">private</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_firstName</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">private</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_LastName</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">private</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">int</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_age</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">private</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">readonly</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">PersonFluentInterface</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_set</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">private</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">bool</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_isActive</span><span style="background:#000000;color:#ffffff">;</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">PersonFluentInterface</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Set</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">get</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_set</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">FirstName</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">get</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_firstName</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">set</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#fddf39">_firstName</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">value</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">LastName</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">get</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_LastName</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">set</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#fddf39">_LastName</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">value</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">int</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Age</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">get</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_age</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">set</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#fddf39">_age</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">value</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">bool</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">IsActive</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">get</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_isActive</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">set</span><span style="background:#000000;color:#ffffff"> { </span><span style="background:#000000;color:#fddf39">_isActive</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">value</span><span style="background:#000000;color:#ffffff">; }</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">class</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#b3b300">PersonFluentInterface</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">private</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">readonly</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">_person</span><span style="background:#000000;color:#ffffff">;</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">PersonFluentInterface</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff">)</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_person</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">PersonFluentInterface</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">FirstName</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">firstName</span><span style="background:#000000;color:#ffffff">)</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_person</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">FirstName</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#fddf39">firstName</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">this</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">PersonFluentInterface</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">string</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">lastName</span><span style="background:#000000;color:#ffffff">)</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_person</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#fddf39">lastName</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">this</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">PersonFluentInterface</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Age</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">int</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">age</span><span style="background:#000000;color:#ffffff">)</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_person</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Age</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#fddf39">age</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">this</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">PersonFluentInterface</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">IsActive</span><span style="background:#000000;color:#ffffff">()</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_person</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">IsActive</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">true</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">this</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</span></p>
<p><span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">public</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">PersonFluentInterface</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">IsNotActive</span><span style="background:#000000;color:#ffffff">()</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#fddf39">_person</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">IsActive</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">false</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#ff8000">this</span><span style="background:#000000;color:#ffffff">;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;}</span><br />
<span style="background:#000000;color:#ffffff">}</span></div>
</div>
</div>
<p>Now the code is clean, concise and quite readable.</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6fd6fdca-132d-42e3-ab8e-a0e426c387df" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">new</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">();</span><br />
<span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Set</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">FirstName</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;Pat&quot;</span><span style="background:#000000;color:#ffffff">).</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;Frank&quot;</span><span style="background:#000000;color:#ffffff">).</span><span style="background:#000000;color:#fddf39">Age</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#8acccf">30</span><span style="background:#000000;color:#ffffff">).</span><span style="background:#000000;color:#fddf39">IsActive</span><span style="background:#000000;color:#ffffff">();</span></div>
</div>
</div>
<p>&nbsp;</p>
<h3>Conclusion</h3>
<p>&nbsp;</p>
<p>This pattern for fluent interfaces is often seen in object configuration and setup.&nbsp; It&#8217;s fairly easy to design and the objects configuration options are readily discoverable using intellisense.&nbsp; As the complexity of an objects setup increased, a fluent interface becomes a more attractive option to ease the initialization burden. </p>
<p>&nbsp;
<p>&nbsp;</p>
</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http://randypatterson.com/2007/09/26/HowToDesignAFluentInterface.aspx"><img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://randypatterson.com/2007/09/26/HowToDesignAFluentInterface.aspx&amp;border=&amp;fgcolor=&amp;bgcolor=&amp;cfgcolor=&amp;cbgcolor="></a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/&amp;title=How+to+design+a+Fluent+Interface" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/&amp;title=How+to+design+a+Fluent+Interface" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/&amp;title=How+to+design+a+Fluent+Interface" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=How+to+design+a+Fluent+Interface&amp;url=http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/&amp;title=How+to+design+a+Fluent+Interface" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/&amp;title=How+to+design+a+Fluent+Interface" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/&amp;title=How+to+design+a+Fluent+Interface" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/&amp;title=How+to+design+a+Fluent+Interface" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/&amp;title=How+to+design+a+Fluent+Interface" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+How+to+design+a+Fluent+Interface+@+http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/&amp;t=How+to+design+a+Fluent+Interface" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://RandyPatterson.com/index.php/2007/09/26/how-to-design-a-fluent-interface/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Power of the Predicate&lt;T&gt;</title>
		<link>http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/</link>
		<comments>http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/#comments</comments>
		<pubDate>Wed, 19 Sep 2007 00:40:57 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://localhost/2007/09/19/ThePowerOfThePredicateltTgt.aspx</guid>
		<description><![CDATA[The very flexible generic collection List&#60;T&#62; contains several methods that take a predicate as it&#8217;s parameter.&#160; Coupled with Anonymous Methods this provides powerfully concise code for filtering, searching and sorting your collections.



List&#60;Person&#62; People = new List&#60;Person&#62;(); 
People.Add(new Person(&#34;Randy&#34;, &#34;Patterson&#34;, 40));
People.Add(new Person(&#34;John&#34;, &#34;Smith&#34;, 15));
People.Add(new Person(&#34;Caity&#34;, &#34;Johnson&#34;, 13));
People.Add(new Person(&#34;Jody&#34;, &#34;Patterson&#34;, 39));
People.Add(new Person(&#34;Chloe&#34;, &#34;Dog&#34;, 30));
People.Add(new Person(&#34;Corey&#34;, &#34;Patterson&#34;, 18));



For [...]]]></description>
			<content:encoded><![CDATA[<p>The very flexible generic collection <a href="http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx" target="_blank">List&lt;T&gt;</a> contains several methods that take a <a href="http://m-w.com/dictionary/predicate" target="_blank">predicate</a> as it&#8217;s parameter.&nbsp; Coupled with <a href="http://msdn2.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx" target="_blank">Anonymous Methods</a> this provides powerfully concise code for filtering, searching and sorting your collections.</p>
<p></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4944cb15-47d9-4150-be5c-a9243071f5ee" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#b3b300">List</span><span style="background:#000000;color:#ffffff">&lt;</span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">&gt; </span><span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#ff8000">new</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#b3b300">List</span><span style="background:#000000;color:#ffffff">&lt;</span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">&gt;(); </span><br />
<span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Add</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">new</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;Randy&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#00ff00">&quot;Patterson&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#8acccf">40</span><span style="background:#000000;color:#ffffff">));</span><br />
<span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Add</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">new</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;John&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#00ff00">&quot;Smith&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#8acccf">15</span><span style="background:#000000;color:#ffffff">));</span><br />
<span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Add</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">new</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;Caity&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#00ff00">&quot;Johnson&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#8acccf">13</span><span style="background:#000000;color:#ffffff">));</span><br />
<span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Add</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">new</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;Jody&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#00ff00">&quot;Patterson&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#8acccf">39</span><span style="background:#000000;color:#ffffff">));</span><br />
<span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Add</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">new</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;Chloe&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#00ff00">&quot;Dog&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#8acccf">30</span><span style="background:#000000;color:#ffffff">));</span><br />
<span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Add</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#ff8000">new</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#00ff00">&quot;Corey&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#00ff00">&quot;Patterson&quot;</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#8acccf">18</span><span style="background:#000000;color:#ffffff">));</span></div>
</div>
</div>
<p></p>
<p>For Example,&nbsp; to filter the collection and display only my family members requires a single line of code</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:879da8c3-f760-48a4-94a2-f254a2ef3919" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#56b9d6">IList</span><span style="background:#000000;color:#ffffff">&lt;</span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff">&gt; </span><span style="background:#000000;color:#fddf39">family</span><span style="background:#000000;color:#ffffff"> = </span><span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">FindAll</span><span style="background:#000000;color:#ffffff">(</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">delegate</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff">) </span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff"> == &#8220;</span><span style="background:#000000;color:#fddf39">Patterson&#8221;</span><span style="background:#000000;color:#ffffff">; </span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;});</span></div>
</div>
</div>
<p>And produces the following output</p>
<p><a href="$image8.png"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="$image_thumb2.png" width="226" height="53"></a> </p>
<p>To remove the teenagers (not a bad proposition) also requires a single line </p>
<p>&nbsp;</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:394858ee-e48c-4b24-b4b8-644807db5099" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">RemoveAll</span><span style="background:#000000;color:#ffffff">(</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">delegate</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff">) </span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{ </span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Age</span><span style="background:#000000;color:#ffffff"> &gt; </span><span style="background:#000000;color:#8acccf">12</span><span style="background:#000000;color:#ffffff"> &amp;&amp; </span><span style="background:#000000;color:#fddf39">person</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Age</span><span style="background:#000000;color:#ffffff"> &lt; </span><span style="background:#000000;color:#8acccf">20</span><span style="background:#000000;color:#ffffff">; });</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}</span></div>
</div>
</div>
<p><br/></p>
<h3>Sorting</h3>
<p><br/></p>
<p>Sorting requires a <a href="http://msdn2.microsoft.com/en-us/library/tfakywbh.aspx" target="_blank">Comparison&lt;T&gt;</a> delegate instead of <a href="http://msdn2.microsoft.com/en-us/library/bfcke1bz.aspx" target="_blank">Predicate&lt;T&gt;</a> but operates in much the same fashion.</p>
<p></p>
<p>For example, sorting by last name requires this single line of code:</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c93092fb-56ec-431b-92f7-33222a757e38" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Sort</span><span style="background:#000000;color:#ffffff">(</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">delegate</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">x</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">y</span><span style="background:#000000;color:#ffffff">) </span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">x</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">CompareTo</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">y</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff">);</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;});</span></div>
</div>
</div>
<p>a slight modification will sort the list in descending order: </p>
<p></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1b206f70-6469-45d8-b933-81611568783f" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Sort</span><span style="background:#000000;color:#ffffff">(</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">delegate</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">x</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">y</span><span style="background:#000000;color:#ffffff">) </span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{ </span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">y</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">CompareTo</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">x</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff">); </span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;});</span></div>
</div>
</div>
<p>To sort by Last Name then First Name would look something like this:</p>
<p></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e1e0d7e7-6f1d-4d69-9fb4-862624c6fcdc" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #000000; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="background:#000000;color:#fddf39">People</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">Sort</span><span style="background:#000000;color:#ffffff">(</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">delegate</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">x</span><span style="background:#000000;color:#ffffff">, </span><span style="background:#000000;color:#fddf39">Person</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">y</span><span style="background:#000000;color:#ffffff">)&#160;&#160;&#160;&#160;</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">if</span><span style="background:#000000;color:#ffffff"> (</span><span style="background:#000000;color:#fddf39">x</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff"> == </span><span style="background:#000000;color:#fddf39">y</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff">)</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">x</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">FirstName</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">CompareTo</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">y</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">FirstName</span><span style="background:#000000;color:#ffffff">);</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">else</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</span><span style="background:#000000;color:#ff8000">return</span><span style="background:#000000;color:#ffffff"> </span><span style="background:#000000;color:#fddf39">x</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">CompareTo</span><span style="background:#000000;color:#ffffff">(</span><span style="background:#000000;color:#fddf39">y</span><span style="background:#000000;color:#ffffff">.</span><span style="background:#000000;color:#fddf39">LastName</span><span style="background:#000000;color:#ffffff">);</span><br />
<span style="background:#000000;color:#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;});</span></div>
</div>
</div>
<p><br/></p>
<h3>Conclusion</h3>
<p><br/></p>
<p>I&#8217;ve just scratched the surface on what the generic List collection is capable of providing. There are several other generic methods that take delegates as parameters, such as <a href="http://msdn2.microsoft.com/en-us/library/bwabdf9z.aspx" target="_blank">ForEach</a> and <a href="http://msdn2.microsoft.com/en-us/library/73fe8cwf.aspx" target="_blank">ConvertAll</a>, that open up interesting possibilities in your code.&nbsp;&nbsp; Overall I find the Anonymous Method syntax a bit <a href="http://www.answers.com/topic/kluge?cat=technology" target="_blank">kludgy</a> but C# 3.0 promises to reduce the syntactical noise and give us true (almost) <a href="http://www.davidhayden.com/blog/dave/archive/2006/11/30/LambdaExpressionsExtensionMethodsLINQ.aspx" target="_blank">Lambda Expressions</a>.&nbsp; </p>
<p><p><a href="http://www.dotnetkicks.com/kick/?url=http://randypatterson.com/2007/09/19/ThePowerOfThePredicateltTgt.aspx"><img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://randypatterson.com/2007/09/19/ThePowerOfThePredicateltTgt.aspx"></a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/&amp;title=The+Power+of+the+Predicate%26lt%3BT%26gt%3B" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/&amp;title=The+Power+of+the+Predicate%26lt%3BT%26gt%3B" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/&amp;title=The+Power+of+the+Predicate%26lt%3BT%26gt%3B" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=The+Power+of+the+Predicate%26lt%3BT%26gt%3B&amp;url=http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/&amp;title=The+Power+of+the+Predicate%26lt%3BT%26gt%3B" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/&amp;title=The+Power+of+the+Predicate%26lt%3BT%26gt%3B" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/&amp;title=The+Power+of+the+Predicate%26lt%3BT%26gt%3B" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/&amp;title=The+Power+of+the+Predicate%26lt%3BT%26gt%3B" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/&amp;title=The+Power+of+the+Predicate%26lt%3BT%26gt%3B" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+The+Power+of+the+Predicate%26lt%3BT%26gt%3B+@+http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/&amp;t=The+Power+of+the+Predicate%26lt%3BT%26gt%3B" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://RandyPatterson.com/index.php/2007/09/19/the-power-of-the-predicatet/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
