<?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; Unity Application Block</title>
	<atom:link href="http://RandyPatterson.com/index.php/category/unity-application-block/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>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>POCO and Unity Application Block Part I</title>
		<link>http://RandyPatterson.com/index.php/2008/09/21/poco-and-unity-application-block-part-i-2/</link>
		<comments>http://RandyPatterson.com/index.php/2008/09/21/poco-and-unity-application-block-part-i-2/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 19:48:20 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[Unity Application Block]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://RandyPatterson.com/index.php/2009/11/13/poco-and-unity-application-block-part-i-2/</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-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<div class="dp-highlighter">
<div class="bar">
<div class="tools"><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://www.randypatterson.com/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://www.randypatterson.com/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://www.randypatterson.com/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://www.randypatterson.com/#">?</a></div>
</div>
<ol class="dp-c">
<li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> MainFormPresenter </span></span></li>
<li class="class"><span>{ </span></li>
<li class="alt"><span> </span><span class="keyword">private</span><span> </span><span class="keyword">readonly</span><span> IProductRepository _productRepository; </span></li>
<li class="class"><span> </span></li>
<li class="alt"><span> </span><span class="keyword">public</span><span> MainFormPresenter() </span></li>
<li class="class"><span> { </span></li>
<li class="alt"><span> </span><span class="comment">//Do Something Profound </span><span> </span></li>
<li class="class"><span> } </span></li>
<li class="alt"><span> </span></li>
<li class="class"><span> [InjectionConstructor] </span></li>
<li class="alt"><span> </span><span class="keyword">public</span><span> MainFormPresenter(IProductRepository productRepository) </span></li>
<li class="class"><span> { </span></li>
<li class="alt"><span> _productRepository = productRepository; </span></li>
<li class="class"><span> } </span></li>
<li class="alt"><span> </span></li>
<li class="class"><span> </span><span class="keyword">public</span><span> IList&lt;Product&gt; GetAllProducts() </span></li>
<li class="alt"><span> { </span></li>
<li class="class"><span> </span><span class="keyword">return</span><span> _productRepository </span></li>
<li class="alt"><span> .Query(p =&gt; p.Name.StartsWith(</span><span class="string">&#8220;M&#8221;</span><span>)) </span></li>
<li class="class"><span> .OrderBy(p =&gt; p.Name) </span></li>
<li class="alt"><span> .ThenByDescending(p =&gt; p.ListPrice) </span></li>
<li class="class"><span> .ToList(); </span></li>
<li class="alt"><span> } </span></li>
<li class="class"><span>} </span></li>
</ol>
</div>
<pre class="c#" style="display: none">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-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<div class="dp-highlighter">
<div class="bar">
<div class="tools"><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://www.randypatterson.com/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://www.randypatterson.com/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://www.randypatterson.com/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://www.randypatterson.com/#">?</a></div>
</div>
<ol class="dp-c">
<li class="alt"><span><span>IUnityContainer unityContainer = </span><span class="keyword">new</span><span> UnityContainer(); </span></span></li>
<li class="class"><span> </span></li>
<li class="alt"><span> </span><span class="comment">//Configure Container </span><span> </span></li>
<li class="class"><span>unityContainer.RegisterType&lt;IProductRepository, ProductRepository&gt;() </span></li>
<li class="alt"><span> </span></li>
<li class="class"><span> </span><span class="comment">//Configure constructor for MainFormPresenter </span><span> </span></li>
<li class="alt"><span>unityContainer.Configure&lt;InjectedMembers&gt;() </span></li>
<li class="class"><span> .ConfigureInjectionFor&lt;MainFormPresenter&gt;(</span><span class="keyword">new</span><span> InjectionConstructor(</span><span class="keyword">typeof</span><span> (IProductRepository))); </span></li>
</ol>
</div>
<pre class="c#" style="display: none">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-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<div class="dp-highlighter">
<div class="bar">
<div class="tools"><a onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" href="http://www.randypatterson.com/#">view plain</a><a onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" href="http://www.randypatterson.com/#">copy to clipboard</a><a onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" href="http://www.randypatterson.com/#">print</a><a onclick="dp.sh.Toolbar.Command('About',this);return false;" href="http://www.randypatterson.com/#">?</a></div>
</div>
<ol class="dp-c">
<li class="alt"><span><span>IUnityContainer unityContainer = </span><span class="keyword">new</span><span> UnityContainer(); </span></span></li>
<li class="class"><span> </span></li>
<li class="alt"><span> </span><span class="comment">//Configure Container </span><span> </span></li>
<li class="class"><span>UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection(</span><span class="string">&#8220;unity&#8221;</span><span>); </span></li>
<li class="alt"><span>section.Containers.Default.Configure(unityContainer); </span></li>
</ol>
</div>
<pre class="c#" style="display: none">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="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 565px; height: 460px;">
<pre style="margin: 0em; background-color: #ffffff; 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="margin: 0em; background-color: #f4f4f4; 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="margin: 0em; background-color: #ffffff; 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="margin: 0em; background-color: #ffffb3; 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="margin: 0em; background-color: #ffffb3; 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="margin: 0em; background-color: #ffffb3; 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="margin: 0em; background-color: #ffffb3; 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="margin: 0em; background-color: #ffffb3; 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="margin: 0em; background-color: #ffffff; 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="margin: 0em; background-color: #f4f4f4; 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="margin: 0em; background-color: #ffffff; 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="margin: 0em; background-color: #f4f4f4; 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="margin: 0em; background-color: #ffffff; 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>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><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><script>// <![CDATA[
window.onload = function() {
    dp.SyntaxHighlighter.ClipboardSwf = 'Scripts/clipboard.swf';
    dp.SyntaxHighlighter.HighlightAll('code');
};</p>
<p>// ]]&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/21/poco-and-unity-application-block-part-i-2/&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/21/poco-and-unity-application-block-part-i-2/&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/21/poco-and-unity-application-block-part-i-2/&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/21/poco-and-unity-application-block-part-i-2/&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/21/poco-and-unity-application-block-part-i-2/&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/21/poco-and-unity-application-block-part-i-2/&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/21/poco-and-unity-application-block-part-i-2/&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/21/poco-and-unity-application-block-part-i-2/&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/21/poco-and-unity-application-block-part-i-2/" 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/21/poco-and-unity-application-block-part-i-2/" 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/21/poco-and-unity-application-block-part-i-2/&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/21/poco-and-unity-application-block-part-i-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Presentation on the Unity Application Block at the Sarasota Users Group</title>
		<link>http://RandyPatterson.com/index.php/2008/09/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/</link>
		<comments>http://RandyPatterson.com/index.php/2008/09/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 05:16:51 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Unity Application Block]]></category>
		<category><![CDATA[CodeCamp]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://localhost/2008/09/16/PresentationOnTheUnityApplicationBlockAtTheSarasotaUsersGroup.aspx</guid>
		<description><![CDATA[I will be speaking about the Unity Application Block, Dependency Injection and Inversion of Control at the Sarasota .Net Users Group.
Wed, September. 17, 2008 at 6:00pm &#8211; 8:00pm.&#160;&#160; Location: Sarasota Community Foundation, 2635 Fruitville Rd., Sarasota, FL 34237 (just west of Tuttle on the north side of Fruitville). 
The Microsoft Unity Application Block is a [...]]]></description>
			<content:encoded><![CDATA[<p>I will be speaking about the <a href="http://www.codeplex.com/unity/" target="_blank">Unity Application Block</a>, Dependency Injection and Inversion of Control at the <a href="http://www.sarasotadev.net/" target="_blank">Sarasota .Net Users Group</a>.</p>
<p><strong>Wed, September. 17, 2008 at 6:00pm &#8211; 8:00pm</strong>.&nbsp;&nbsp; Location: <a href="http://www.cfsarasota.org/">Sarasota Community Foundation</a>, 2635 Fruitville Rd., Sarasota, FL 34237 (just west of Tuttle on the north side of Fruitville). </p>
<p>The Microsoft <a href="http://www.codeplex.com/unity">Unity Application Block</a> is a lightweight Dependency Injection Container that is currently being incorporated into the latest releases of Enterprise Library and the Composite Application Library (Prism). This presentation will give an introduction to Dependency Injection and Inversion of Control concepts and an overview of how to use and configure the Unity Application Block to build loosely coupled applications.</p>
<p>Agenda:</p>
<ul>
<li>Overview of Inversion on Control (IoC) Containers
<ul>
<li>Understanding Inversion of Control
<li>Understanding Dependency Injection</li>
</ul>
<li>Unity Application Block
<ul>
<li>Constructor Injection
<li>Property (setter) Injection</li>
</ul>
<li>Unity Configuration
<ul>
<li>Understanding Object Lifetime Management
<li>Using API Configuration
<ul>
<li>RegisterType
<li>RegisterInstance</li>
</ul>
<li>Using Configuration Files</li>
</ul>
<li>Managing Dependencies
<li>Understanding Loosely Coupled Components
<li>When is Dependency Injection Appropriate</li>
</ul>
<p>Presentation slides and code sample</p>
<p><iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-a368b40efafbe8fa.skydrive.live.com/embedrowdetail.aspx/Public/UnityDemo|_Sarasota.zip" frameborder="0" scrolling="no"></iframe></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/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/&amp;title=Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group" 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/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/&amp;title=Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group" 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/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/&amp;title=Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group" 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=Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group&amp;url=http://RandyPatterson.com/index.php/2008/09/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/&amp;title=Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group" 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/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/&amp;title=Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group" 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/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/&amp;title=Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group" 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/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/&amp;title=Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group" 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/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/&amp;title=Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group" 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/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/" 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+Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group+@+http://RandyPatterson.com/index.php/2008/09/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/" 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/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/&amp;t=Presentation+on+the+Unity+Application+Block+at+the+Sarasota+Users+Group" 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/16/presentation-on-the-unity-application-block-at-the-sarasota-users-group/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I am Speaking at the Next IASA Meeting</title>
		<link>http://RandyPatterson.com/index.php/2008/06/25/i-am-speaking-at-the-next-iasa-meeting/</link>
		<comments>http://RandyPatterson.com/index.php/2008/06/25/i-am-speaking-at-the-next-iasa-meeting/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 13:30:20 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Unity Application Block]]></category>

		<guid isPermaLink="false">http://localhost/2008/06/25/IAmSpeakingAtTheNextIASAMeeting.aspx</guid>
		<description><![CDATA[&#160;
Dependency Injection using the Microsoft Unity Application Block
The Microsoft Unity Application Block is a lightweight Dependency Injection Container that is currently being incorporated into the latest releases of Enterprise Library and the Composite Application Library (Prism). This session will give an introduction to Dependency Injection and Inversion of Control concepts and a brief overview of [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<h3><a href="http://martinfowler.com/articles/injection.html" target="_blank">Dependency Injection</a> using the Microsoft <a href="http://codeplex.com/unity" target="_blank">Unity Application Block</a></h3>
<p>The Microsoft Unity Application Block is a lightweight Dependency Injection Container that is currently being incorporated into the latest releases of Enterprise Library and the Composite Application Library (Prism). This session will give an introduction to Dependency Injection and Inversion of Control concepts and a brief overview of how to use the Unity Application Block to build loosely coupled applications.</p>
<p>My goal is to finish by leading a discussion on the pros and cons of Dependency Injection, Loosely coupled and Tightly coupled components. When we leave I hope everyone will have a deeper understanding of these principles and how and when to apply them.</p>
<p><strong><i>When &amp; Where:</i></strong><br />Thursday, June 26, 2008 from 06:30 PM &#8211; 08:30 PM (ET)<br />Microsoft Corporation<br />3000 Bayport Drive<br />Suite 480<br />Tampa, FL 33607 <br /><a href="http://list-manage.com/track/click?u=def661e5fb5b51ddb3bafdd75&amp;id=623dad369a&amp;e=NXdOG97ItE">View a map</a><br /><a href="http://list-manage.com/track/click?u=def661e5fb5b51ddb3bafdd75&amp;id=dcbf1f84de&amp;e=NXdOG97ItE">View 1-Click Directions</a></p>
<p><b>Please be aware that the outside doors lock and the elevators need a security key after 7:00 PM.</b>
<p>&nbsp;</p>
<p><a href="http://www.eventbrite.com/event/107409264/RRPBlog" target="_blank">Please Register for the event:</a></p>
<h3>Update:</h3>
<p>Here are my slides from the presentation:</p>
<p>&nbsp;<iframe style="border-right: #dde5e9 1px solid; padding-right: 0px; border-top: #dde5e9 1px solid; padding-left: 0px; padding-bottom: 0px; margin: 3px; border-left: #dde5e9 1px solid; width: 240px; padding-top: 0px; border-bottom: #dde5e9 1px solid; height: 66px; background-color: #ffffff" marginwidth="0" marginheight="0" src="http://cid-a368b40efafbe8fa.skydrive.live.com/embedrowdetail.aspx/Public/Dependency%20Injection.zip" frameborder="0" scrolling="no"></iframe></p>
<p>For additional information I recommend the following 2 part PodCast on Dependency Injection:</p>
<p><a href="http://altnetpodcast.com/episodes/5-di-and-ioc" target="_blank"><img height="33" alt="podcast" src="http://randypatterson.com/images/IamSpeakingattheNextIASAMeeting_7E5D/podcast.png" width="32" align="left" border="0">&nbsp;<a href="http://altnetpodcast.com/episodes/5-di-and-ioc" target="_blank">Dependency Injection and Inversion of Control</a></a></p>
<p>&nbsp;</p>
<p><a href="http://altnetpodcast.com/episodes/6-more-di-and-ioc" target="_blank"><img height="33" alt="podcast" src="http://randypatterson.com/images/IamSpeakingattheNextIASAMeeting_7E5D/podcast.png" width="32" align="left" border="0">&nbsp;</a><a href="http://altnetpodcast.com/episodes/6-more-di-and-ioc" target="_blank">More Dependency Injection and Inversion of Control</a></p>
<p>&nbsp;</p>
<p>Finally, the following book is highly recommended:</p>
<table border="0">
<tbody>
<tr>
<td valign="top"><a href="http://www.amazon.com/Principles-Patterns-Practices-Robert-Martin/dp/0131857258%3FSubscriptionId%3D0525E2PQ81DD7ZTWTK82%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0131857258"><img src="http://ecx.images-amazon.com/images/I/51Y57BH27TL._SL75_.jpg" border="1"></a></td>
<td valign="top"><b>Agile Principles, Patterns, and Practices in C# (Robert C. Martin Series)</b><br />by Robert C. Martin, Micah Martin</p>
<p><a href="http://www.amazon.com/Principles-Patterns-Practices-Robert-Martin/dp/0131857258%3FSubscriptionId%3D0525E2PQ81DD7ZTWTK82%26tag%3Dws%26linkCode%3Dsp1%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0131857258">Read more about this book&#8230;</a></td>
</tr>
</tbody>
</table>
<!-- 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/06/25/i-am-speaking-at-the-next-iasa-meeting/&amp;title=I+am+Speaking+at+the+Next+IASA+Meeting" 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/06/25/i-am-speaking-at-the-next-iasa-meeting/&amp;title=I+am+Speaking+at+the+Next+IASA+Meeting" 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/06/25/i-am-speaking-at-the-next-iasa-meeting/&amp;title=I+am+Speaking+at+the+Next+IASA+Meeting" 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=I+am+Speaking+at+the+Next+IASA+Meeting&amp;url=http://RandyPatterson.com/index.php/2008/06/25/i-am-speaking-at-the-next-iasa-meeting/&amp;title=I+am+Speaking+at+the+Next+IASA+Meeting" 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/06/25/i-am-speaking-at-the-next-iasa-meeting/&amp;title=I+am+Speaking+at+the+Next+IASA+Meeting" 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/06/25/i-am-speaking-at-the-next-iasa-meeting/&amp;title=I+am+Speaking+at+the+Next+IASA+Meeting" 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/06/25/i-am-speaking-at-the-next-iasa-meeting/&amp;title=I+am+Speaking+at+the+Next+IASA+Meeting" 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/06/25/i-am-speaking-at-the-next-iasa-meeting/&amp;title=I+am+Speaking+at+the+Next+IASA+Meeting" 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/06/25/i-am-speaking-at-the-next-iasa-meeting/" 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+I+am+Speaking+at+the+Next+IASA+Meeting+@+http://RandyPatterson.com/index.php/2008/06/25/i-am-speaking-at-the-next-iasa-meeting/" 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/06/25/i-am-speaking-at-the-next-iasa-meeting/&amp;t=I+am+Speaking+at+the+Next+IASA+Meeting" 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/06/25/i-am-speaking-at-the-next-iasa-meeting/feed/</wfw:commentRss>
		<slash:comments>0</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>Lazy Instantiation with Unity Application Block</title>
		<link>http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/</link>
		<comments>http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/#comments</comments>
		<pubDate>Sun, 30 Mar 2008 05:48:37 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Unit Tests]]></category>
		<category><![CDATA[Unity Application Block]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://localhost/2008/03/30/LazyInstantiationWithUnityApplicationBlock.aspx</guid>
		<description><![CDATA[Configuring the Unity Application Block for just-in-time Instantiation.&#160; 
Out of the box, Unity does not provide a way to configure your dependencies so that they are resolved only if needed.&#160; Lazy Instantiation would be desirable if your dependency is and required only under certain circumstances or you would like to delay object creation of a [...]]]></description>
			<content:encoded><![CDATA[<p>Configuring the <a href="http://codeplex.com/unity" target="_blank">Unity Application Block</a> for just-in-time Instantiation.&nbsp; </p>
<p>Out of the box, Unity does not provide a way to configure your dependencies so that they are resolved only if needed.&nbsp; Lazy Instantiation would be desirable if your dependency is and required only under certain circumstances or you would like to delay object creation of a resource intensive dependency until needed.</p>
<p>For <a href="http://en.wikipedia.org/wiki/Lazy_initialization_pattern" target="_blank">Lazy Instantiation</a>, the first obstacle to overcome when using <a href="http://martinfowler.com/articles/injection.html" target="_blank">IoC Containers</a> like <a href="http://codeplex.com/unity" target="_blank">Unity</a>, is to have your components get a reference to the container so the dependency creation can be delegated to it when the need arises.&nbsp; My first inclination was to wrap Unity in a <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank">Singleton</a> and have the component reference this in order to resolve the dependency.&nbsp; </p>
<p><font size="3">Container.Instance.Resolve&lt;<font color="#07c7fe">IProductService</font>&gt;();</font></p>
<p>Singleton&#8217;s are, however, notoriously difficult to test and very rigid in design.&nbsp; Used in this manner,&nbsp; a Singleton is nothing more than a global variable wrapped in a buzzword.&nbsp; so I wanted a different approach.&nbsp; Since we are using a <a href="http://en.wikipedia.org/wiki/Dependency_injection" target="_blank">Dependency Injection</a> Framework, why not have the container inject a reference to itself into our components.</p>
<p>1.&nbsp; First, configure the Unity container so that it can return a reference to itself.</p>
<p><a href="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image1.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="65" alt="image" src="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image1_thumb.png" width="526" border="0"></a> </p>
<p>2.&nbsp; Next, create a property on your class to get a reference to the Unity container that created it.</p>
<p><a href="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image10.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="119" alt="image" src="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image10_thumb.png" width="341" border="0"></a> </p>
<p>3.&nbsp; Finally, have the container resolve the dependency the first time it&#8217;s referenced.&nbsp; </p>
<p><a href="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image16.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="181" alt="image" src="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image16_thumb.png" width="548" border="0"></a> </p>
<p><font size="3"></font><font size="3">Unit Testing</font></p>
<p>Testing is straight forward as well.&nbsp;&nbsp; You could have your unit test use reflection to reach in and set the private field (_productService) to your mock object.&nbsp; However,&nbsp; giving your Unit Tests access to private members violates the &#8220;<a href="http://xunitpatterns.com/Principles%20of%20Test%20Automation.html#Use%20the%20Front%20Door%20First" target="_blank">Use the Front Door First</a>&#8221; Test Automation Principle and should be avoided if possible.&nbsp; Alternatively, you could create a mock object for IUnityContainer and have it return a mock object for IProductService when the Resolve method is called.</p>
<p>Using <a href="http://www.ayende.com/projects/rhino-mocks/downloads.aspx" target="_blank">RhinoMocks</a>, the Unit Test would look something like this:</p>
<p>&nbsp;</p>
<p><a href="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="424" alt="image" src="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image_thumb.png" width="698" border="0"></a> </p>
<p><font size="3"></font>&nbsp;</p>
<p>&nbsp;<a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2frandypatterson.com%2f2008%2f03%2f30%2fLazyInstantiationWithUnityApplicationBlock.aspx"><img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2frandypatterson.com%2f2008%2f03%2f30%2fLazyInstantiationWithUnityApplicationBlock.aspx" 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/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" 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/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" 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/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" 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=Lazy+Instantiation+with+Unity+Application+Block&amp;url=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" 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/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" 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/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" 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/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" 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/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" 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/03/30/lazy-instantiation-with-unity-application-block/" 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+Lazy+Instantiation+with+Unity+Application+Block+@+http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/" 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/03/30/lazy-instantiation-with-unity-application-block/&amp;t=Lazy+Instantiation+with+Unity+Application+Block" 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/03/30/lazy-instantiation-with-unity-application-block/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
