<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>ploeh blog - Castle Windsor</title>
    <link>http://blog.ploeh.dk/</link>
    <description>Mark Seemann's .NET blog</description>
    <language>en-us</language>
    <copyright>Mark Seemann</copyright>
    <lastBuildDate>Tue, 18 May 2010 05:27:56 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>mark@seemann.ms</managingEditor>
    <webMaster>mark@seemann.ms</webMaster>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=f1a71969-0584-4a15-9395-9f2ac65f104b</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,f1a71969-0584-4a15-9395-9f2ac65f104b.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,f1a71969-0584-4a15-9395-9f2ac65f104b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=f1a71969-0584-4a15-9395-9f2ac65f104b</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of Castle Windsor’s facilities addresses wiring up of WCF services. So far, the
sparse documentation for the WCF Facility seems to indicate <a href="http://www.castleproject.org/container/facilities/trunk/wcf/index.html">that
you have to configure your container in a global.asax</a>. That’s not much to my liking.
First of all, it reeks of ASP.NET, and secondly, it’s not going to work if you expose
WCF over protocols other than HTTP.
</p>
        <p>
However, now that we know that <a href="http://blog.ploeh.dk/2010/05/17/ServiceHostFactoryLifetime.aspx">a
custom ServiceHostFactory is effectively a Singleton</a>, a much better alternative
is to derive from the WCF Facility’s DefaultServiceHost class:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 FooServiceHostFactory\cf0  : \par     \cf2 DefaultServiceHostFactory\cf0 \par \{\par     \cf1 public\cf0  FooServiceHostFactory()\par         : \cf1 base\cf0 (\cf2 FooServiceHostFactory\cf0 .CreateKernel())\par     \{\par     \}\par \par     \cf1 private\cf0  \cf1 static\cf0  \cf2 IKernel\cf0  CreateKernel()\par     \{\par         \cf1 var\cf0  container = \cf1 new\cf0  \cf2 WindsorContainer\cf0 ();\par \par         container.AddFacility&lt;\cf2 WcfFacility\cf0 &gt;();\par \par         container.Register(\cf2 Component\cf0 \par             .For&lt;\cf2 FooService\cf0 &gt;()\par             .LifeStyle.Transient);\par         container.Register(\cf2 Component\cf0 \par             .For&lt;\cf2 IBar\cf0 &gt;()\par             .ImplementedBy&lt;\cf2 Bar\cf0 &gt;());\par \par         \cf1 return\cf0  container.Kernel;\par     \}\par \}\par }
-->
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">FooServiceHostFactory</span> : </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">DefaultServiceHostFactory</span></pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span> FooServiceHostFactory()</pre>
          <pre style="margin: 0px">        : <span style="color: blue">base</span>(<span style="color: #2b91af">FooServiceHostFactory</span>.CreateKernel())</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: blue">static</span><span style="color: #2b91af">IKernel</span> CreateKernel()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">var</span> container
= <span style="color: blue">new</span><span style="color: #2b91af">WindsorContainer</span>();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">        container.AddFacility&lt;<span style="color: #2b91af">WcfFacility</span>&gt;();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">        container.Register(<span style="color: #2b91af">Component</span></pre>
          <pre style="margin: 0px">            .For&lt;<span style="color: #2b91af">FooService</span>&gt;()</pre>
          <pre style="margin: 0px">            .LifeStyle.Transient);</pre>
          <pre style="margin: 0px">        container.Register(<span style="color: #2b91af">Component</span></pre>
          <pre style="margin: 0px">            .For&lt;<span style="color: #2b91af">IBar</span>&gt;()</pre>
          <pre style="margin: 0px">            .ImplementedBy&lt;<span style="color: #2b91af">Bar</span>&gt;());</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span> container.Kernel;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Although it feels a little odd to create a container and then not really use it, but
only its Kernel property, this works like a charm. It correctly wires up this FooService:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 FooService\cf0  : \cf2 IFooService\cf0 \par \{\par     \cf1 private\cf0  \cf1 readonly\cf0  \cf2 IBar\cf0  bar;\par \par     \cf1 public\cf0  FooService(\cf2 IBar\cf0  bar)\par     \{\par         \cf1 if\cf0  (bar == \cf1 null\cf0 )\par         \{\par             \cf1 throw\cf0  \cf1 new\cf0  \cf2 ArgumentNullException\cf0 (\cf3 "bar"\cf0 );\par         \}\par \par         \cf1 this\cf0 .bar = bar;\par     \}\par \par \cf1     #region\cf0  IFooService Members\par \par     \cf1 public\cf0  \cf1 string\cf0  Foo()\par     \{\par         \cf1 return\cf0  \cf1 this\cf0 .bar.Baz;\par     \}\par \par \cf1     #endregion\cf0 \par \}\par }
-->
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">FooService</span> : <span style="color: #2b91af">IFooService</span></pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: blue">readonly</span><span style="color: #2b91af">IBar</span> bar;</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span> FooService(<span style="color: #2b91af">IBar</span> bar)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">if</span> (bar
== <span style="color: blue">null</span>)</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: blue">throw</span><span style="color: blue">new</span><span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"bar"</span>);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.bar
= bar;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">
            <span style="color: blue">   
#region</span> IFooService Members</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">string</span> Foo()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span><span style="color: blue">this</span>.bar.Baz;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">
            <span style="color: blue">   
#endregion</span>
          </pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
However, instead of the static CreateKernel method that creates the IKernel instance, <a href="http://groups.google.com/group/castle-project-users/browse_thread/thread/5401a7189f8295af/2dabe6cbb3c63488?q=">I
suggest that the WCF Facility utilizes the Factory Method pattern</a>. As the WCF
Facility has not yet been released, perhaps there’s still time for that change.
</p>
        <p>
In any case, the WCF Facility saves you from writing a lot of infrastructure code
if you would like to wire your WCF services with Castle Windsor.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=f1a71969-0584-4a15-9395-9f2ac65f104b" />
      </body>
      <title>Sneak view at Castle’s WCF Facility</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,f1a71969-0584-4a15-9395-9f2ac65f104b.aspx</guid>
      <link>http://blog.ploeh.dk/2010/05/18/SneakViewAtCastlesWCFFacility.aspx</link>
      <pubDate>Tue, 18 May 2010 05:27:56 GMT</pubDate>
      <description>&lt;p&gt;
One of Castle Windsor’s facilities addresses wiring up of WCF services. So far, the
sparse documentation for the WCF Facility seems to indicate &lt;a href="http://www.castleproject.org/container/facilities/trunk/wcf/index.html"&gt;that
you have to configure your container in a global.asax&lt;/a&gt;. That’s not much to my liking.
First of all, it reeks of ASP.NET, and secondly, it’s not going to work if you expose
WCF over protocols other than HTTP.
&lt;/p&gt;
&lt;p&gt;
However, now that we know that &lt;a href="http://blog.ploeh.dk/2010/05/17/ServiceHostFactoryLifetime.aspx"&gt;a
custom ServiceHostFactory is effectively a Singleton&lt;/a&gt;, a much better alternative
is to derive from the WCF Facility’s DefaultServiceHost class:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 FooServiceHostFactory\cf0  : \par     \cf2 DefaultServiceHostFactory\cf0 \par \{\par     \cf1 public\cf0  FooServiceHostFactory()\par         : \cf1 base\cf0 (\cf2 FooServiceHostFactory\cf0 .CreateKernel())\par     \{\par     \}\par \par     \cf1 private\cf0  \cf1 static\cf0  \cf2 IKernel\cf0  CreateKernel()\par     \{\par         \cf1 var\cf0  container = \cf1 new\cf0  \cf2 WindsorContainer\cf0 ();\par \par         container.AddFacility&amp;lt;\cf2 WcfFacility\cf0 &amp;gt;();\par \par         container.Register(\cf2 Component\cf0 \par             .For&amp;lt;\cf2 FooService\cf0 &amp;gt;()\par             .LifeStyle.Transient);\par         container.Register(\cf2 Component\cf0 \par             .For&amp;lt;\cf2 IBar\cf0 &amp;gt;()\par             .ImplementedBy&amp;lt;\cf2 Bar\cf0 &amp;gt;());\par \par         \cf1 return\cf0  container.Kernel;\par     \}\par \}\par }
--&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FooServiceHostFactory&lt;/span&gt; : &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;DefaultServiceHostFactory&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; FooServiceHostFactory()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;span style="color: blue"&gt;base&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;FooServiceHostFactory&lt;/span&gt;.CreateKernel())&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IKernel&lt;/span&gt; CreateKernel()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; container
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WindsorContainer&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.AddFacility&amp;lt;&lt;span style="color: #2b91af"&gt;WcfFacility&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: #2b91af"&gt;FooService&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .LifeStyle.Transient);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: #2b91af"&gt;IBar&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ImplementedBy&amp;lt;&lt;span style="color: #2b91af"&gt;Bar&lt;/span&gt;&amp;gt;());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; container.Kernel;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Although it feels a little odd to create a container and then not really use it, but
only its Kernel property, this works like a charm. It correctly wires up this FooService:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 FooService\cf0  : \cf2 IFooService\cf0 \par \{\par     \cf1 private\cf0  \cf1 readonly\cf0  \cf2 IBar\cf0  bar;\par \par     \cf1 public\cf0  FooService(\cf2 IBar\cf0  bar)\par     \{\par         \cf1 if\cf0  (bar == \cf1 null\cf0 )\par         \{\par             \cf1 throw\cf0  \cf1 new\cf0  \cf2 ArgumentNullException\cf0 (\cf3 "bar"\cf0 );\par         \}\par \par         \cf1 this\cf0 .bar = bar;\par     \}\par \par \cf1     #region\cf0  IFooService Members\par \par     \cf1 public\cf0  \cf1 string\cf0  Foo()\par     \{\par         \cf1 return\cf0  \cf1 this\cf0 .bar.Baz;\par     \}\par \par \cf1     #endregion\cf0 \par \}\par }
--&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FooService&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;IFooService&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IBar&lt;/span&gt; bar;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; FooService(&lt;span style="color: #2b91af"&gt;IBar&lt;/span&gt; bar)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (bar
== &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"bar"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.bar
= bar;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
#region&lt;/span&gt; IFooService Members&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; Foo()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.bar.Baz;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
#endregion&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
However, instead of the static CreateKernel method that creates the IKernel instance, &lt;a href="http://groups.google.com/group/castle-project-users/browse_thread/thread/5401a7189f8295af/2dabe6cbb3c63488?q="&gt;I
suggest that the WCF Facility utilizes the Factory Method pattern&lt;/a&gt;. As the WCF
Facility has not yet been released, perhaps there’s still time for that change.
&lt;/p&gt;
&lt;p&gt;
In any case, the WCF Facility saves you from writing a lot of infrastructure code
if you would like to wire your WCF services with Castle Windsor.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=f1a71969-0584-4a15-9395-9f2ac65f104b" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,f1a71969-0584-4a15-9395-9f2ac65f104b.aspx</comments>
      <category>Castle Windsor</category>
      <category>Dependency Injection</category>
      <category>Services</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=ce797146-6cc9-4322-b40d-990395d69e36</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,ce797146-6cc9-4322-b40d-990395d69e36.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,ce797146-6cc9-4322-b40d-990395d69e36.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=ce797146-6cc9-4322-b40d-990395d69e36</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I recently had the need to change the lifestyles of all components in a WindsorContainer
(read on to the end if you want to know why). This turned out to be amazingly simple
to do.
</p>
        <p>
The problem was this: I had numerous components registered in a WindsorContainer,
some of them as Singletons, some as Transients and yet again some as PerWebRequest.
Configuration was even defined in numerous IWindsorInstallers, including some distributed
XML files. I now needed to spin up a second container with the same configuration
as the first one, <em>except</em> that the lifestyles should be all Singletons across
the board.
</p>
        <p>
This can be easily accomplished by implementing a custom IContributeComponentModelConstruction
type. Here’s a simple example:
</p>
        <p>
Consider this IWindsorInstaller:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 FooInstaller\cf0  : \cf2 IWindsorInstaller\cf0 \par \{\par \cf1     #region\cf0  IWindsorInstaller Members\par \par     \cf1 public\cf0  \cf1 void\cf0  Install(\cf2 IWindsorContainer\cf0  container,\par         \cf2 IConfigurationStore\cf0  store)\par     \{\par         container.Register(\cf2 Component\cf0 \par             .For&lt;\cf2 IFoo\cf0 &gt;()\par             .ImplementedBy&lt;\cf2 Foo\cf0 &gt;()\par             .LifeStyle.Transient);\par     \}\par \par \cf1     #endregion\cf0 \par \}\par }
-->
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">FooInstaller</span> : <span style="color: #2b91af">IWindsorInstaller</span></pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">
            <span style="color: blue">   
#region</span> IWindsorInstaller Members</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">void</span> Install(<span style="color: #2b91af">IWindsorContainer</span> container,</pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">IConfigurationStore</span> store)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        container.Register(<span style="color: #2b91af">Component</span></pre>
          <pre style="margin: 0px">            .For&lt;<span style="color: #2b91af">IFoo</span>&gt;()</pre>
          <pre style="margin: 0px">            .ImplementedBy&lt;<span style="color: #2b91af">Foo</span>&gt;()</pre>
          <pre style="margin: 0px">            .LifeStyle.Transient);</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">
            <span style="color: blue">   
#endregion</span>
          </pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The important point to notice is that it registers the lifestyle as Transient. In
other words, this container will always return new Foo instances:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 var\cf0  container = \cf1 new\cf0  \cf2 WindsorContainer\cf0 ();\par container.Install(\cf1 new\cf0  \cf2 FooInstaller\cf0 ());\par }
-->
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> container
= <span style="color: blue">new</span><span style="color: #2b91af">WindsorContainer</span>();</pre>
          <pre style="margin: 0px">container.Install(<span style="color: blue">new</span><span style="color: #2b91af">FooInstaller</span>());</pre>
        </div>
        <p>
We can override this behavior by adding this custom IContributeComponentModelConstruction:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 SingletonEqualizer\cf0  :\par     \cf2 IContributeComponentModelConstruction\cf0 \par \{\par     \cf1 public\cf0  \cf1 void\cf0  ProcessModel(\cf2 IKernel\cf0  kernel, \par         \cf2 ComponentModel\cf0  model)\par     \{\par         model.LifestyleType = \cf2 LifestyleType\cf0 .Singleton;\par     \}\par \}\par }
-->
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">SingletonEqualizer</span> :</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IContributeComponentModelConstruction</span></pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span><span style="color: blue">void</span> ProcessModel(<span style="color: #2b91af">IKernel</span> kernel, </pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">ComponentModel</span> model)</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        model.LifestyleType = <span style="color: #2b91af">LifestyleType</span>.Singleton;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this very simple example, I always set the lifestyle type to the same value, but
obviously we can write as complex code in the ProcessModel method as we would like.
We can now configure the container like this:
</p>
        <!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 var\cf0  container = \cf1 new\cf0  \cf2 WindsorContainer\cf0 ();\par container.Kernel.ComponentModelBuilder\par     .AddContributor(\cf1 new\cf0  \cf2 SingletonEqualizer\cf0 ());\par container.Install(\cf1 new\cf0  \cf2 FooInstaller\cf0 ());\par }
-->
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">var</span> container
= <span style="color: blue">new</span><span style="color: #2b91af">WindsorContainer</span>();</pre>
          <pre style="margin: 0px">container.Kernel.ComponentModelBuilder</pre>
          <pre style="margin: 0px">    .AddContributor(<span style="color: blue">new</span><span style="color: #2b91af">SingletonEqualizer</span>());</pre>
          <pre style="margin: 0px">container.Install(<span style="color: blue">new</span><span style="color: #2b91af">FooInstaller</span>());</pre>
        </div>
        <p>
With this configuration we will now get the same instance of Foo every time we Resolve
IFoo.
</p>
        <p>
We did I need this? Because my application is a web application and I’m using the
PerWebRequest lifestyle in a number of places. However, I needed to spin up a second
container that would compose object hierarchies for a background process. This background
process needs the same component configuration as the rest of the application, but
can’t use the PerWebRequest lifestyle as there will be no web request available to
the background process. Hence the need to change lifestyles across the board.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ce797146-6cc9-4322-b40d-990395d69e36" />
      </body>
      <title>Changing Windsor lifestyles after the fact</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,ce797146-6cc9-4322-b40d-990395d69e36.aspx</guid>
      <link>http://blog.ploeh.dk/2010/04/26/ChangingWindsorLifestylesAfterTheFact.aspx</link>
      <pubDate>Mon, 26 Apr 2010 05:09:42 GMT</pubDate>
      <description>&lt;p&gt;
I recently had the need to change the lifestyles of all components in a WindsorContainer
(read on to the end if you want to know why). This turned out to be amazingly simple
to do.
&lt;/p&gt;
&lt;p&gt;
The problem was this: I had numerous components registered in a WindsorContainer,
some of them as Singletons, some as Transients and yet again some as PerWebRequest.
Configuration was even defined in numerous IWindsorInstallers, including some distributed
XML files. I now needed to spin up a second container with the same configuration
as the first one, &lt;em&gt;except&lt;/em&gt; that the lifestyles should be all Singletons across
the board.
&lt;/p&gt;
&lt;p&gt;
This can be easily accomplished by implementing a custom IContributeComponentModelConstruction
type. Here’s a simple example:
&lt;/p&gt;
&lt;p&gt;
Consider this IWindsorInstaller:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 FooInstaller\cf0  : \cf2 IWindsorInstaller\cf0 \par \{\par \cf1     #region\cf0  IWindsorInstaller Members\par \par     \cf1 public\cf0  \cf1 void\cf0  Install(\cf2 IWindsorContainer\cf0  container,\par         \cf2 IConfigurationStore\cf0  store)\par     \{\par         container.Register(\cf2 Component\cf0 \par             .For&amp;lt;\cf2 IFoo\cf0 &amp;gt;()\par             .ImplementedBy&amp;lt;\cf2 Foo\cf0 &amp;gt;()\par             .LifeStyle.Transient);\par     \}\par \par \cf1     #endregion\cf0 \par \}\par }
--&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FooInstaller&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;IWindsorInstaller&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
#region&lt;/span&gt; IWindsorInstaller Members&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Install(&lt;span style="color: #2b91af"&gt;IWindsorContainer&lt;/span&gt; container,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IConfigurationStore&lt;/span&gt; store)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: #2b91af"&gt;IFoo&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ImplementedBy&amp;lt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .LifeStyle.Transient);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
#endregion&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The important point to notice is that it registers the lifestyle as Transient. In
other words, this container will always return new Foo instances:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 var\cf0  container = \cf1 new\cf0  \cf2 WindsorContainer\cf0 ();\par container.Install(\cf1 new\cf0  \cf2 FooInstaller\cf0 ());\par }
--&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; container
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WindsorContainer&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;container.Install(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FooInstaller&lt;/span&gt;());&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
We can override this behavior by adding this custom IContributeComponentModelConstruction:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 public\cf0  \cf1 class\cf0  \cf2 SingletonEqualizer\cf0  :\par     \cf2 IContributeComponentModelConstruction\cf0 \par \{\par     \cf1 public\cf0  \cf1 void\cf0  ProcessModel(\cf2 IKernel\cf0  kernel, \par         \cf2 ComponentModel\cf0  model)\par     \{\par         model.LifestyleType = \cf2 LifestyleType\cf0 .Singleton;\par     \}\par \}\par }
--&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SingletonEqualizer&lt;/span&gt; :&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IContributeComponentModelConstruction&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ProcessModel(&lt;span style="color: #2b91af"&gt;IKernel&lt;/span&gt; kernel, &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;ComponentModel&lt;/span&gt; model)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; model.LifestyleType = &lt;span style="color: #2b91af"&gt;LifestyleType&lt;/span&gt;.Singleton;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this very simple example, I always set the lifestyle type to the same value, but
obviously we can write as complex code in the ProcessModel method as we would like.
We can now configure the container like this:
&lt;/p&gt;
&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 var\cf0  container = \cf1 new\cf0  \cf2 WindsorContainer\cf0 ();\par container.Kernel.ComponentModelBuilder\par     .AddContributor(\cf1 new\cf0  \cf2 SingletonEqualizer\cf0 ());\par container.Install(\cf1 new\cf0  \cf2 FooInstaller\cf0 ());\par }
--&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; container
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WindsorContainer&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;container.Kernel.ComponentModelBuilder&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .AddContributor(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SingletonEqualizer&lt;/span&gt;());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;container.Install(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FooInstaller&lt;/span&gt;());&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
With this configuration we will now get the same instance of Foo every time we Resolve
IFoo.
&lt;/p&gt;
&lt;p&gt;
We did I need this? Because my application is a web application and I’m using the
PerWebRequest lifestyle in a number of places. However, I needed to spin up a second
container that would compose object hierarchies for a background process. This background
process needs the same component configuration as the rest of the application, but
can’t use the PerWebRequest lifestyle as there will be no web request available to
the background process. Hence the need to change lifestyles across the board.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ce797146-6cc9-4322-b40d-990395d69e36" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,ce797146-6cc9-4322-b40d-990395d69e36.aspx</comments>
      <category>Castle Windsor</category>
      <category>Dependency Injection</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=2aef793d-f3f4-479f-9144-b26e093f8376</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,2aef793d-f3f4-479f-9144-b26e093f8376.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,2aef793d-f3f4-479f-9144-b26e093f8376.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=2aef793d-f3f4-479f-9144-b26e093f8376</wfw:commentRss>
      <slash:comments>7</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Reacting to my <a href="http://blog.ploeh.dk/2010/01/25/DependencyInjectionInversionInNET.aspx">previous
post</a>, <a href="http://kozmic.pl/Default.aspx">Krzysztof Koźmic</a> was so kind
to point out to me that I really should be using an IWindsorInstaller instead of writing
the registration code in a static helper method (it <em>did</em> make me cringe a
bit).
</p>
        <p>
As it turns out, IWindsorInstaller is not a particularly well-described feature of <a href="http://castleproject.org/container/index.html">Castle
Windsor</a>, so here's a quick introduction. Fortunately, it is very easy to understand.
</p>
        <p>
The idea is simply to package configuration code in reusable modules (just like the
Guice modules from <a href="http://blog.objectmentor.com/articles/2010/01/17/dependency-injection-inversion">Uncle
Bob's post</a>).
</p>
        <p>
Refactoring the bootstrap code from my previous post, I can now move all the container
configuration code into a reusable module:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">BillingContainerInstaller</span> : <span style="color: #2b91af">IWindsorInstaller</span></p>
          <p style="margin: 0px">
{
</p>
          <p style="margin: 0px">
            <span style="color: blue">    #region</span> IWindsorInstaller Members
</p>
          <p style="margin: 0px">
 
</p>
          <p style="margin: 0px">
    <span style="color: blue">public</span><span style="color: blue">void</span> Install(<span style="color: #2b91af">IWindsorContainer</span> container,
</p>
          <p style="margin: 0px">
        <span style="color: #2b91af">IConfigurationStore</span> store)
</p>
          <p style="margin: 0px">
    {
</p>
          <p style="margin: 0px">
        container.AddComponent&lt;<span style="color: #2b91af">TransactionLog</span>, 
</p>
          <p style="margin: 0px">
            <span style="color: #2b91af">DatabaseTransactionLog</span>&gt;();
</p>
          <p style="margin: 0px">
        container.AddComponent&lt;<span style="color: #2b91af">CreditCardProcessor</span>,
</p>
          <p style="margin: 0px">
            <span style="color: #2b91af">MyCreditCardProcessor</span>&gt;();
</p>
          <p style="margin: 0px">
        container.AddComponent&lt;<span style="color: #2b91af">BillingService</span>&gt;();
</p>
          <p style="margin: 0px">
    }
</p>
          <p style="margin: 0px">
 
</p>
          <p style="margin: 0px">
            <span style="color: blue">    #endregion</span>
          </p>
          <p style="margin: 0px">
}
</p>
        </div>
        <p>
While I was at it, I also changed from the fluent registration API to the generic
registration methods as I didn't really need the full API, but I could have left it
as it was.
</p>
        <p>
BillingContainerInstaller implements IWindsorInstaller, and I can now configure my
container instance like this:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">var</span> container = <span style="color: blue">new</span><span style="color: #2b91af">WindsorContainer</span>();
</p>
          <p style="margin: 0px">
container.Install(<span style="color: blue">new</span><span style="color: #2b91af">BillingContainerInstaller</span>());
</p>
        </div>
        <p>
The Install method takes a parameter array of IWindsorInstaller instances, so you
can pass as many as you'd like.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2aef793d-f3f4-479f-9144-b26e093f8376" />
      </body>
      <title>IWindsorInstaller</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,2aef793d-f3f4-479f-9144-b26e093f8376.aspx</guid>
      <link>http://blog.ploeh.dk/2010/01/26/IWindsorInstaller.aspx</link>
      <pubDate>Tue, 26 Jan 2010 19:24:51 GMT</pubDate>
      <description>&lt;p&gt;
Reacting to my &lt;a href="http://blog.ploeh.dk/2010/01/25/DependencyInjectionInversionInNET.aspx"&gt;previous
post&lt;/a&gt;, &lt;a href="http://kozmic.pl/Default.aspx"&gt;Krzysztof Koźmic&lt;/a&gt; was so kind
to point out to me that I really should be using an IWindsorInstaller instead of writing
the registration code in a static helper method (it &lt;em&gt;did&lt;/em&gt; make me cringe a
bit).
&lt;/p&gt;
&lt;p&gt;
As it turns out, IWindsorInstaller is not a particularly well-described feature of &lt;a href="http://castleproject.org/container/index.html"&gt;Castle
Windsor&lt;/a&gt;, so here's a quick introduction. Fortunately, it is very easy to understand.
&lt;/p&gt;
&lt;p&gt;
The idea is simply to package configuration code in reusable modules (just like the
Guice modules from &lt;a href="http://blog.objectmentor.com/articles/2010/01/17/dependency-injection-inversion"&gt;Uncle
Bob's post&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
Refactoring the bootstrap code from my previous post, I can now move all the container
configuration code into a reusable module:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BillingContainerInstaller&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;IWindsorInstaller&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/span&gt; IWindsorInstaller Members
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Install(&lt;span style="color: #2b91af"&gt;IWindsorContainer&lt;/span&gt; container,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IConfigurationStore&lt;/span&gt; store)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.AddComponent&amp;lt;&lt;span style="color: #2b91af"&gt;TransactionLog&lt;/span&gt;, 
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;DatabaseTransactionLog&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.AddComponent&amp;lt;&lt;span style="color: #2b91af"&gt;CreditCardProcessor&lt;/span&gt;,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyCreditCardProcessor&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.AddComponent&amp;lt;&lt;span style="color: #2b91af"&gt;BillingService&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
While I was at it, I also changed from the fluent registration API to the generic
registration methods as I didn't really need the full API, but I could have left it
as it was.
&lt;/p&gt;
&lt;p&gt;
BillingContainerInstaller implements IWindsorInstaller, and I can now configure my
container instance like this:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;var&lt;/span&gt; container = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WindsorContainer&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
container.Install(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BillingContainerInstaller&lt;/span&gt;());
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
The Install method takes a parameter array of IWindsorInstaller instances, so you
can pass as many as you'd like.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2aef793d-f3f4-479f-9144-b26e093f8376" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,2aef793d-f3f4-479f-9144-b26e093f8376.aspx</comments>
      <category>Castle Windsor</category>
      <category>Dependency Injection</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=224fbfca-7f4a-4d5c-a713-8db5e0720fe2</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,224fbfca-7f4a-4d5c-a713-8db5e0720fe2.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,224fbfca-7f4a-4d5c-a713-8db5e0720fe2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=224fbfca-7f4a-4d5c-a713-8db5e0720fe2</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
About a week ago <a href="http://blog.objectmentor.com/articles/category/uncle-bobs-blatherings">Uncle
Bob</a> published a post on <a href="http://blog.objectmentor.com/articles/2010/01/17/dependency-injection-inversion">Dependency
Injection Inversion</a> that caused quite a stir in the tiny part of the .NET community
I usually pretend to hang out with. Twitter was alive with much debate, but <a href="http://ayende.com/Blog/Default.aspx">Ayende</a> seems
to <a href="http://ayende.com/Blog/archive/2010/01/22/rejecting-dependency-injection-inversion.aspx">sum
up</a> the .NET DI community's sentiment pretty well:
</p>
        <blockquote>
          <p>
if this is a typical example of IoC usage in the Java world, then [Uncle Bob] should
peek over the fence to see how IoC is commonly implemented in the .Net space
</p>
        </blockquote>
        <p>
Despite having initially left a more or less positive note to Uncle Bob's post, after
having re-read it carefully, I am beginning to think the same, but instead of just <em>telling</em> everyone
how much greener the grass is on the .NET side, let me <em>show</em> you.
</p>
        <p>
First of all, let's translate Uncle Bob's BillingService to C#:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">BillingService</span>
          </p>
          <p style="margin: 0px">
{
</p>
          <p style="margin: 0px">
    <span style="color: blue">private</span><span style="color: blue">readonly</span><span style="color: #2b91af">CreditCardProcessor</span> processor;
</p>
          <p style="margin: 0px">
    <span style="color: blue">private</span><span style="color: blue">readonly</span><span style="color: #2b91af">TransactionLog</span> transactionLog;
</p>
          <p style="margin: 0px">
 
</p>
          <p style="margin: 0px">
    <span style="color: blue">public</span> BillingService(<span style="color: #2b91af">CreditCardProcessor</span> processor,
</p>
          <p style="margin: 0px">
        <span style="color: #2b91af">TransactionLog</span> transactionLog)
</p>
          <p style="margin: 0px">
    {
</p>
          <p style="margin: 0px">
        <span style="color: blue">if</span> (processor
== <span style="color: blue">null</span>)
</p>
          <p style="margin: 0px">
        {
</p>
          <p style="margin: 0px">
            <span style="color: blue">throw</span><span style="color: blue">new</span><span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"processor"</span>);
</p>
          <p style="margin: 0px">
        }
</p>
          <p style="margin: 0px">
        <span style="color: blue">if</span> (transactionLog
== <span style="color: blue">null</span>)
</p>
          <p style="margin: 0px">
        {
</p>
          <p style="margin: 0px">
            <span style="color: blue">throw</span><span style="color: blue">new</span><span style="color: #2b91af">ArgumentNullException</span>(<span style="color: #a31515">"transactionLog"</span>);
</p>
          <p style="margin: 0px">
        }
</p>
          <p style="margin: 0px">
 
</p>
          <p style="margin: 0px">
        <span style="color: blue">this</span>.processor
= processor;
</p>
          <p style="margin: 0px">
        <span style="color: blue">this</span>.transactionLog
= transactionLog;
</p>
          <p style="margin: 0px">
    }
</p>
          <p style="margin: 0px">
 
</p>
          <p style="margin: 0px">
    <span style="color: blue">public</span><span style="color: blue">void</span> ProcessCharge(<span style="color: blue">int</span> amount, <span style="color: blue">string</span> id)
</p>
          <p style="margin: 0px">
    {
</p>
          <p style="margin: 0px">
        <span style="color: blue">var</span> approval
= <span style="color: blue">this</span>.processor.Approve(amount, id);
</p>
          <p style="margin: 0px">
        <span style="color: blue">this</span>.transactionLog.Log(<span style="color: blue">string</span>.Format(
</p>
          <p style="margin: 0px">
            <span style="color: #a31515">"Transaction
by {0} for {1} {2}"</span>, id, amount, 
</p>
          <p style="margin: 0px">
            <span style="color: blue">this</span>.GetApprovalCode(approval)));
</p>
          <p style="margin: 0px">
    }
</p>
          <p style="margin: 0px">
 
</p>
          <p style="margin: 0px">
    <span style="color: blue">private</span><span style="color: blue">string</span> GetApprovalCode(<span style="color: blue">bool</span> approval)
</p>
          <p style="margin: 0px">
    {
</p>
          <p style="margin: 0px">
        <span style="color: blue">return</span> approval
? <span style="color: #a31515">"approved"</span> : <span style="color: #a31515">"denied"</span>;
</p>
          <p style="margin: 0px">
    }
</p>
          <p style="margin: 0px">
}
</p>
        </div>
        <p>
It's nice how easy it is to translate Java code to C#, but apart from casing and other
minor deviations, let's focus on the main difference. I've added Guard Clauses to
protect the injected dependencies against null values as I consider this an essential
and required part of <strong>Constructor Injection</strong> – I think Uncle Bob should
have added those as well, but he might have omitted them for brevity.
</p>
        <p>
If you disregard the Guard Clauses, the C# version is a logical line of code shorter
than the Java version because it has no DI attribute like Guice's @Inject.
</p>
        <p>
Does this mean that we can't do DI with the C# version of BillingService? Uncle Bob
seems to imply that we can do <em>Dependency Inversion</em>, but not <em>Dependency
Injection</em> - or is it the other way around? I can't really make head or tails
of that part of the post…
</p>
        <p>
The interesting part is that in .NET, <em>there's no difference!</em> We can use DI
Containers with the BillingService without sprinkling DI attributes all over our code
base. The BillingService class has no reference to any DI Container.
</p>
        <p>
It does, however, use the central DI pattern <strong>Constructor Injection</strong>.
.NET DI Containers know all about this pattern, and with .NET's static type system
they know all they need to know to wire dependencies up correctly. (I thought that
Java had a static type system as well, but perhaps I am mistaken.) The .NET DI Containers
will figure it out for you – you don't have to explicitly tell them how to invoke
a constructor with two parameters.
</p>
        <p>
We can write an entire application by using <strong>Constructor Injection</strong> and
stacking dependencies <em>without ever referencing a container!</em></p>
        <p>
Like the Lean concept of the <em>Last Responsible Moment</em>, we can wait until the
application's entry point to decide how we will wire up the dependencies.
</p>
        <p>
As Uncle Bob suggests, we can use <strong>Poor Man's DI</strong> and manually create
the dependencies directly in Main, but as <a href="http://ayende.com/Blog/archive/2010/01/22/rejecting-dependency-injection-inversion.aspx">Ayende
correctly observes</a>, that only looks like an attractive alternative because the
example is so simple. For complex dependency graphs, a DI Container is a much better
choice.
</p>
        <p>
With the C# version of BillingService, which DI Container must we select?
</p>
        <p>
It doesn't matter: we can choose whichever one we would like because we have been
following patterns instead of using a framework.
</p>
        <p>
Here's an example of an implementation of Main using <a href="http://castleproject.org/">Castle
Windsor</a>:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">static</span>
            <span style="color: blue">void</span> Main(<span style="color: blue">string</span>[]
args)
</p>
          <p style="margin: 0px">
{
</p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> container = <span style="color: blue">new</span><span style="color: #2b91af">WindsorContainer</span>();
</p>
          <p style="margin: 0px">
    <span style="color: #2b91af">Program</span>.Configure(container);
</p>
          <p style="margin: 0px">
 
</p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> billingService =
</p>
          <p style="margin: 0px">
        container.Resolve&lt;<span style="color: #2b91af">BillingService</span>&gt;();
</p>
          <p style="margin: 0px">
    billingService.ProcessCharge(<span style="color: #a52a2a">2034</span>, <span style="color: #a31515">"Bob"</span>);
</p>
          <p style="margin: 0px">
}
</p>
        </div>
        <p>
This looks a lot like Uncle Bob's first Guice example, but instead of injecting a
BillingModule into the container, we can configure it inline or in a helper method:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">private</span>
            <span style="color: blue">static</span>
            <span style="color: blue">void</span> Configure(<span style="color: #2b91af">WindsorContainer</span> container)
</p>
          <p style="margin: 0px">
{
</p>
          <p style="margin: 0px">
    container.Register(<span style="color: #2b91af">Component</span></p>
          <p style="margin: 0px">
        .For&lt;<span style="color: #2b91af">TransactionLog</span>&gt;()
</p>
          <p style="margin: 0px">
        .ImplementedBy&lt;<span style="color: #2b91af">DatabaseTransactionLog</span>&gt;());
</p>
          <p style="margin: 0px">
    container.Register(<span style="color: #2b91af">Component</span></p>
          <p style="margin: 0px">
        .For&lt;<span style="color: #2b91af">CreditCardProcessor</span>&gt;()
</p>
          <p style="margin: 0px">
        .ImplementedBy&lt;<span style="color: #2b91af">MyCreditCardProcessor</span>&gt;());
</p>
          <p style="margin: 0px">
    container.Register(<span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">BillingService</span>&gt;());
</p>
          <p style="margin: 0px">
}
</p>
        </div>
        <p>
This corresponds more or less to the Guice-specific BillingModule, although Windsor
also requires us to register the concrete BillingService as a component (this last
step varies a bit from DI Container to DI Container – it is, for example, redundant
in <a href="http://www.codeplex.com/unity">Unity</a>).
</p>
        <p>
Imagine that in the future we want to rewire this program to use a different DI Container.
The only piece of code we need to change is this <strong>Composition Root</strong>.
We need to change the container declaration and configuration and then we are ready
to use a different DI Container.
</p>
        <p>
The bottom line is that Uncle Bob's <em>Dependency Injection Inversion</em> is redundant
in .NET. Just use a few well-known design patterns and principles and you can write
entire applications with DI-friendly, DI-agnostic code bases.
</p>
        <p>
I recently posted a <a href="http://stackoverflow.com/questions/2045904/dependency-inject-di-friendly-library/2047657#2047657">first
take on guidelines for writing DI-agnostic code</a>. I plan to evolve these guiding
principles and make them a part of <a href="http://www.manning.com/seemann">my upcoming
book</a>.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=224fbfca-7f4a-4d5c-a713-8db5e0720fe2" />
      </body>
      <title>Dependency Injection Inversion in .NET</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,224fbfca-7f4a-4d5c-a713-8db5e0720fe2.aspx</guid>
      <link>http://blog.ploeh.dk/2010/01/25/DependencyInjectionInversionInNET.aspx</link>
      <pubDate>Mon, 25 Jan 2010 20:48:27 GMT</pubDate>
      <description>&lt;p&gt;
About a week ago &lt;a href="http://blog.objectmentor.com/articles/category/uncle-bobs-blatherings"&gt;Uncle
Bob&lt;/a&gt; published a post on &lt;a href="http://blog.objectmentor.com/articles/2010/01/17/dependency-injection-inversion"&gt;Dependency
Injection Inversion&lt;/a&gt; that caused quite a stir in the tiny part of the .NET community
I usually pretend to hang out with. Twitter was alive with much debate, but &lt;a href="http://ayende.com/Blog/Default.aspx"&gt;Ayende&lt;/a&gt; seems
to &lt;a href="http://ayende.com/Blog/archive/2010/01/22/rejecting-dependency-injection-inversion.aspx"&gt;sum
up&lt;/a&gt; the .NET DI community's sentiment pretty well:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
if this is a typical example of IoC usage in the Java world, then [Uncle Bob] should
peek over the fence to see how IoC is commonly implemented in the .Net space
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Despite having initially left a more or less positive note to Uncle Bob's post, after
having re-read it carefully, I am beginning to think the same, but instead of just &lt;em&gt;telling&lt;/em&gt; everyone
how much greener the grass is on the .NET side, let me &lt;em&gt;show&lt;/em&gt; you.
&lt;/p&gt;
&lt;p&gt;
First of all, let's translate Uncle Bob's BillingService to C#:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BillingService&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CreditCardProcessor&lt;/span&gt; processor;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TransactionLog&lt;/span&gt; transactionLog;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; BillingService(&lt;span style="color: #2b91af"&gt;CreditCardProcessor&lt;/span&gt; processor,
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;TransactionLog&lt;/span&gt; transactionLog)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (processor
== &lt;span style="color: blue"&gt;null&lt;/span&gt;)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"processor"&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (transactionLog
== &lt;span style="color: blue"&gt;null&lt;/span&gt;)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"transactionLog"&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.processor
= processor;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.transactionLog
= transactionLog;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ProcessCharge(&lt;span style="color: blue"&gt;int&lt;/span&gt; amount, &lt;span style="color: blue"&gt;string&lt;/span&gt; id)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; approval
= &lt;span style="color: blue"&gt;this&lt;/span&gt;.processor.Approve(amount, id);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.transactionLog.Log(&lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Transaction
by {0} for {1} {2}"&lt;/span&gt;, id, amount, 
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.GetApprovalCode(approval)));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; GetApprovalCode(&lt;span style="color: blue"&gt;bool&lt;/span&gt; approval)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; approval
? &lt;span style="color: #a31515"&gt;"approved"&lt;/span&gt; : &lt;span style="color: #a31515"&gt;"denied"&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
It's nice how easy it is to translate Java code to C#, but apart from casing and other
minor deviations, let's focus on the main difference. I've added Guard Clauses to
protect the injected dependencies against null values as I consider this an essential
and required part of &lt;strong&gt;Constructor Injection&lt;/strong&gt; – I think Uncle Bob should
have added those as well, but he might have omitted them for brevity.
&lt;/p&gt;
&lt;p&gt;
If you disregard the Guard Clauses, the C# version is a logical line of code shorter
than the Java version because it has no DI attribute like Guice's @Inject.
&lt;/p&gt;
&lt;p&gt;
Does this mean that we can't do DI with the C# version of BillingService? Uncle Bob
seems to imply that we can do &lt;em&gt;Dependency Inversion&lt;/em&gt;, but not &lt;em&gt;Dependency
Injection&lt;/em&gt; - or is it the other way around? I can't really make head or tails
of that part of the post…
&lt;/p&gt;
&lt;p&gt;
The interesting part is that in .NET, &lt;em&gt;there's no difference!&lt;/em&gt; We can use DI
Containers with the BillingService without sprinkling DI attributes all over our code
base. The BillingService class has no reference to any DI Container.
&lt;/p&gt;
&lt;p&gt;
It does, however, use the central DI pattern &lt;strong&gt;Constructor Injection&lt;/strong&gt;.
.NET DI Containers know all about this pattern, and with .NET's static type system
they know all they need to know to wire dependencies up correctly. (I thought that
Java had a static type system as well, but perhaps I am mistaken.) The .NET DI Containers
will figure it out for you – you don't have to explicitly tell them how to invoke
a constructor with two parameters.
&lt;/p&gt;
&lt;p&gt;
We can write an entire application by using &lt;strong&gt;Constructor Injection&lt;/strong&gt; and
stacking dependencies &lt;em&gt;without ever referencing a container!&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
Like the Lean concept of the &lt;em&gt;Last Responsible Moment&lt;/em&gt;, we can wait until the
application's entry point to decide how we will wire up the dependencies.
&lt;/p&gt;
&lt;p&gt;
As Uncle Bob suggests, we can use &lt;strong&gt;Poor Man's DI&lt;/strong&gt; and manually create
the dependencies directly in Main, but as &lt;a href="http://ayende.com/Blog/archive/2010/01/22/rejecting-dependency-injection-inversion.aspx"&gt;Ayende
correctly observes&lt;/a&gt;, that only looks like an attractive alternative because the
example is so simple. For complex dependency graphs, a DI Container is a much better
choice.
&lt;/p&gt;
&lt;p&gt;
With the C# version of BillingService, which DI Container must we select?
&lt;/p&gt;
&lt;p&gt;
It doesn't matter: we can choose whichever one we would like because we have been
following patterns instead of using a framework.
&lt;/p&gt;
&lt;p&gt;
Here's an example of an implementation of Main using &lt;a href="http://castleproject.org/"&gt;Castle
Windsor&lt;/a&gt;:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[]
args)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; container = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WindsorContainer&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Program&lt;/span&gt;.Configure(container);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; billingService =
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.Resolve&amp;lt;&lt;span style="color: #2b91af"&gt;BillingService&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; billingService.ProcessCharge(&lt;span style="color: #a52a2a"&gt;2034&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"Bob"&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
This looks a lot like Uncle Bob's first Guice example, but instead of injecting a
BillingModule into the container, we can configure it inline or in a helper method:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Configure(&lt;span style="color: #2b91af"&gt;WindsorContainer&lt;/span&gt; container)
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: #2b91af"&gt;TransactionLog&lt;/span&gt;&amp;gt;()
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ImplementedBy&amp;lt;&lt;span style="color: #2b91af"&gt;DatabaseTransactionLog&lt;/span&gt;&amp;gt;());
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: #2b91af"&gt;CreditCardProcessor&lt;/span&gt;&amp;gt;()
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ImplementedBy&amp;lt;&lt;span style="color: #2b91af"&gt;MyCreditCardProcessor&lt;/span&gt;&amp;gt;());
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;.For&amp;lt;&lt;span style="color: #2b91af"&gt;BillingService&lt;/span&gt;&amp;gt;());
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
This corresponds more or less to the Guice-specific BillingModule, although Windsor
also requires us to register the concrete BillingService as a component (this last
step varies a bit from DI Container to DI Container – it is, for example, redundant
in &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
Imagine that in the future we want to rewire this program to use a different DI Container.
The only piece of code we need to change is this &lt;strong&gt;Composition Root&lt;/strong&gt;.
We need to change the container declaration and configuration and then we are ready
to use a different DI Container.
&lt;/p&gt;
&lt;p&gt;
The bottom line is that Uncle Bob's &lt;em&gt;Dependency Injection Inversion&lt;/em&gt; is redundant
in .NET. Just use a few well-known design patterns and principles and you can write
entire applications with DI-friendly, DI-agnostic code bases.
&lt;/p&gt;
&lt;p&gt;
I recently posted a &lt;a href="http://stackoverflow.com/questions/2045904/dependency-inject-di-friendly-library/2047657#2047657"&gt;first
take on guidelines for writing DI-agnostic code&lt;/a&gt;. I plan to evolve these guiding
principles and make them a part of &lt;a href="http://www.manning.com/seemann"&gt;my upcoming
book&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=224fbfca-7f4a-4d5c-a713-8db5e0720fe2" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,224fbfca-7f4a-4d5c-a713-8db5e0720fe2.aspx</comments>
      <category>Castle Windsor</category>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=ee8935b9-d418-4078-99e6-71eec380680b</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,ee8935b9-d418-4078-99e6-71eec380680b.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,ee8935b9-d418-4078-99e6-71eec380680b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=ee8935b9-d418-4078-99e6-71eec380680b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my previous posts I discussed <a href="http://blog.ploeh.dk/2009/12/01/GlobalErrorHandlingInASPNETMVC.aspx">how
to enable global error handling in ASP.NET MVC</a> and <a href="http://blog.ploeh.dk/2009/12/07/LoggingExceptionFilter.aspx">how
to inject a logging interface into the error handler</a>. In these posts, I simplified
things a bit to get my points across.
</p>
        <p>
In production we don't use a custom ErrorHandlingControllerFactory to configure all
Controllers with error handling, nor do we instantiate IExceptionFilters manually.
What I described was the Poor Man's Dependency Injection (DI) approach, which I find
most appropriate when describing DI concepts.
</p>
        <p>
However, we really use <a href="http://castleproject.org/container/index.html">Castle
Windsor</a> currently, so the wiring looks a bit different although it's still exactly
the same thing that's going on. Neither ErrorHandlingActionInvoker nor LoggingExceptionFilter
are any different than I have already described, but for completeness I wanted to
share a bit of our Windsor code.
</p>
        <p>
This is how we really wire our Controllers:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;}??\fs20 container.Register(\cf3 AllTypes\par ??\cf0     .FromAssemblyContaining(representativeControllerType)\par ??    .BasedOn&lt;\cf3 Controller\cf0 &gt;()\par ??    .ConfigureFor&lt;\cf3 Controller\cf0 &gt;(reg =&gt; \par ??        reg.LifeStyle.PerWebRequest.ServiceOverrides(\par ??            \cf4 new\cf0  \{ ActionInvoker = \cf4 typeof\cf0 (\par ??                \cf3 ErrorHandlingControllerActionInvoker\cf0 )\par ??                .FullName \})));}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">container.Register(<span style="color: #2b91af">AllTypes</span></pre>
          <pre style="margin: 0px">    .FromAssemblyContaining(representativeControllerType)</pre>
          <pre style="margin: 0px">    .BasedOn&lt;<span style="color: #2b91af">Controller</span>&gt;()</pre>
          <pre style="margin: 0px">    .ConfigureFor&lt;<span style="color: #2b91af">Controller</span>&gt;(reg
=&gt; </pre>
          <pre style="margin: 0px">        reg.LifeStyle.PerWebRequest.ServiceOverrides(</pre>
          <pre style="margin: 0px">            <span style="color: blue">new</span> {
ActionInvoker = <span style="color: blue">typeof</span>(</pre>
          <pre style="margin: 0px">                <span style="color: #2b91af">ErrorHandlingControllerActionInvoker</span>)</pre>
          <pre style="margin: 0px">                .FullName })));</pre>
        </div>
        <p>
Most of this statement simply instructs Windsor to scan all types in the specified
assembly for Controller implementations and register them. The interesting part is
the ServiceOverrides method call that uses Windsor's rather excentric syntax for defining
that the ActionInvoker property should be wired up with an instance of the component
registered as ErrorHandlingControllerActionInvoker.
</p>
        <p>
Since ErrorHandlingControllerActionInvoker itself expects an IExceptionFilter instance
we need to configure at least one of these as well. Instead of one, however, we have
two:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;}??\fs20 container.Register(\cf3 Component\cf0 .For&lt;\cf3 IExceptionFilter\cf0 &gt;()\par ??    .ImplementedBy&lt;\cf3 LoggingExceptionFilter\cf0 &gt;());\par ??container.Register(\cf3 Component\cf0 .For&lt;\cf3 IExceptionFilter\cf0 &gt;()\par ??    .ImplementedBy&lt;\cf3 HandleErrorAttribute\cf0 &gt;());}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">container.Register(<span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">IExceptionFilter</span>&gt;()</pre>
          <pre style="margin: 0px">    .ImplementedBy&lt;<span style="color: #2b91af">LoggingExceptionFilter</span>&gt;());</pre>
          <pre style="margin: 0px">container.Register(<span style="color: #2b91af">Component</span>.For&lt;<span style="color: #2b91af">IExceptionFilter</span>&gt;()</pre>
          <pre style="margin: 0px">    .ImplementedBy&lt;<span style="color: #2b91af">HandleErrorAttribute</span>&gt;());</pre>
        </div>
        <p>
This is Windsor's elegant way of registering <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorators</a>:
you simply register the Decorator before the decorated type, and it'll Auto-Wire everything
for you.
</p>
        <p>
Finally, we use a ControllerFactory very similar to the WindsorControllerFactory from
the <a href="http://www.codeplex.com/MVCContrib">MVC Contrib</a> project.
</p>
        <p>
To reiterate: You don't have to use Castle Windsor to enable global error handling
or logging in ASP.NET MVC. You can code it by hand as I've demonstrated in my previous
posts, or you can use some other DI Container. The purpose of this post was simply
to demonstrate one way to take it to the next level.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ee8935b9-d418-4078-99e6-71eec380680b" />
      </body>
      <title>Wiring ASP.NET MVC Error Handling with Castle Windsor</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,ee8935b9-d418-4078-99e6-71eec380680b.aspx</guid>
      <link>http://blog.ploeh.dk/2009/12/14/WiringASPNETMVCErrorHandlingWithCastleWindsor.aspx</link>
      <pubDate>Mon, 14 Dec 2009 06:59:32 GMT</pubDate>
      <description>&lt;p&gt;
In my previous posts I discussed &lt;a href="http://blog.ploeh.dk/2009/12/01/GlobalErrorHandlingInASPNETMVC.aspx"&gt;how
to enable global error handling in ASP.NET MVC&lt;/a&gt; and &lt;a href="http://blog.ploeh.dk/2009/12/07/LoggingExceptionFilter.aspx"&gt;how
to inject a logging interface into the error handler&lt;/a&gt;. In these posts, I simplified
things a bit to get my points across.
&lt;/p&gt;
&lt;p&gt;
In production we don't use a custom ErrorHandlingControllerFactory to configure all
Controllers with error handling, nor do we instantiate IExceptionFilters manually.
What I described was the Poor Man's Dependency Injection (DI) approach, which I find
most appropriate when describing DI concepts.
&lt;/p&gt;
&lt;p&gt;
However, we really use &lt;a href="http://castleproject.org/container/index.html"&gt;Castle
Windsor&lt;/a&gt; currently, so the wiring looks a bit different although it's still exactly
the same thing that's going on. Neither ErrorHandlingActionInvoker nor LoggingExceptionFilter
are any different than I have already described, but for completeness I wanted to
share a bit of our Windsor code.
&lt;/p&gt;
&lt;p&gt;
This is how we really wire our Controllers:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;}??\fs20 container.Register(\cf3 AllTypes\par ??\cf0     .FromAssemblyContaining(representativeControllerType)\par ??    .BasedOn&amp;lt;\cf3 Controller\cf0 &amp;gt;()\par ??    .ConfigureFor&amp;lt;\cf3 Controller\cf0 &amp;gt;(reg =&amp;gt; \par ??        reg.LifeStyle.PerWebRequest.ServiceOverrides(\par ??            \cf4 new\cf0  \{ ActionInvoker = \cf4 typeof\cf0 (\par ??                \cf3 ErrorHandlingControllerActionInvoker\cf0 )\par ??                .FullName \})));}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;container.Register(&lt;span style="color: #2b91af"&gt;AllTypes&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .FromAssemblyContaining(representativeControllerType)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .BasedOn&amp;lt;&lt;span style="color: #2b91af"&gt;Controller&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ConfigureFor&amp;lt;&lt;span style="color: #2b91af"&gt;Controller&lt;/span&gt;&amp;gt;(reg
=&amp;gt; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; reg.LifeStyle.PerWebRequest.ServiceOverrides(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; {
ActionInvoker = &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;ErrorHandlingControllerActionInvoker&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .FullName })));&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Most of this statement simply instructs Windsor to scan all types in the specified
assembly for Controller implementations and register them. The interesting part is
the ServiceOverrides method call that uses Windsor's rather excentric syntax for defining
that the ActionInvoker property should be wired up with an instance of the component
registered as ErrorHandlingControllerActionInvoker.
&lt;/p&gt;
&lt;p&gt;
Since ErrorHandlingControllerActionInvoker itself expects an IExceptionFilter instance
we need to configure at least one of these as well. Instead of one, however, we have
two:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;}??\fs20 container.Register(\cf3 Component\cf0 .For&amp;lt;\cf3 IExceptionFilter\cf0 &amp;gt;()\par ??    .ImplementedBy&amp;lt;\cf3 LoggingExceptionFilter\cf0 &amp;gt;());\par ??container.Register(\cf3 Component\cf0 .For&amp;lt;\cf3 IExceptionFilter\cf0 &amp;gt;()\par ??    .ImplementedBy&amp;lt;\cf3 HandleErrorAttribute\cf0 &amp;gt;());}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;.For&amp;lt;&lt;span style="color: #2b91af"&gt;IExceptionFilter&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ImplementedBy&amp;lt;&lt;span style="color: #2b91af"&gt;LoggingExceptionFilter&lt;/span&gt;&amp;gt;());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;.For&amp;lt;&lt;span style="color: #2b91af"&gt;IExceptionFilter&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ImplementedBy&amp;lt;&lt;span style="color: #2b91af"&gt;HandleErrorAttribute&lt;/span&gt;&amp;gt;());&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This is Windsor's elegant way of registering &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;Decorators&lt;/a&gt;:
you simply register the Decorator before the decorated type, and it'll Auto-Wire everything
for you.
&lt;/p&gt;
&lt;p&gt;
Finally, we use a ControllerFactory very similar to the WindsorControllerFactory from
the &lt;a href="http://www.codeplex.com/MVCContrib"&gt;MVC Contrib&lt;/a&gt; project.
&lt;/p&gt;
&lt;p&gt;
To reiterate: You don't have to use Castle Windsor to enable global error handling
or logging in ASP.NET MVC. You can code it by hand as I've demonstrated in my previous
posts, or you can use some other DI Container. The purpose of this post was simply
to demonstrate one way to take it to the next level.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ee8935b9-d418-4078-99e6-71eec380680b" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,ee8935b9-d418-4078-99e6-71eec380680b.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>Castle Windsor</category>
      <category>Dependency Injection</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=4276a194-a567-47ab-80b3-23c2813537f3</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,4276a194-a567-47ab-80b3-23c2813537f3.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,4276a194-a567-47ab-80b3-23c2813537f3.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=4276a194-a567-47ab-80b3-23c2813537f3</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When using <a href="http://castleproject.org/container/index.html">Castle Windsor</a> in
web applications you would want to register many of your components with a lifestyle
that is associated with a single request. This is the purpose of the PerWebRequest
lifestyle.
</p>
        <p>
If you try that with ASP.NET MVC on IIS7, you are likely to receive the following
error message:
</p>
        <blockquote>
          <p>
Looks like you forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule 
<br />
Add '&lt;add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule,
Castle.MicroKernel" /&gt;' to the &lt;httpModules&gt; section on your web.config.
</p>
        </blockquote>
        <p>
Unfortunately, following the instructions in the error message doesn't help. There's
a <a href="http://groups.google.com/group/castle-project-users/browse_thread/thread/d44d96f4b548611e/1c33a54539f8abf7">discussion
about this issue</a> on the Castle Project Users forum, but the gist of it is that
if you don't need to <em>resolve</em> components during application startup, this
shouldn't be an issue, and indeed it isn't – it seems to be something else. 
</p>
        <p>
In my case I seem to have solved the problem by registering the HTTP module in configuration/system.webServer/modules
instead of configuration/system.web/httpModules. 
</p>
        <p>
Although I haven't had the opportunity to dive into the technical details to understand <em>why</em> this
works, this seems to solve the problem on both Windows Vista and Windows Server 2008.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=4276a194-a567-47ab-80b3-23c2813537f3" />
      </body>
      <title>Using Castle Windsor's PerWebRequest lifestyle with ASP.NET MVC on IIS7</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,4276a194-a567-47ab-80b3-23c2813537f3.aspx</guid>
      <link>http://blog.ploeh.dk/2009/11/17/UsingCastleWindsorsPerWebRequestLifestyleWithASPNETMVCOnIIS7.aspx</link>
      <pubDate>Tue, 17 Nov 2009 12:44:37 GMT</pubDate>
      <description>&lt;p&gt;
When using &lt;a href="http://castleproject.org/container/index.html"&gt;Castle Windsor&lt;/a&gt; in
web applications you would want to register many of your components with a lifestyle
that is associated with a single request. This is the purpose of the PerWebRequest
lifestyle.
&lt;/p&gt;
&lt;p&gt;
If you try that with ASP.NET MVC on IIS7, you are likely to receive the following
error message:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Looks like you forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule 
&lt;br&gt;
Add '&amp;lt;add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule,
Castle.MicroKernel" /&amp;gt;' to the &amp;lt;httpModules&amp;gt; section on your web.config.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Unfortunately, following the instructions in the error message doesn't help. There's
a &lt;a href="http://groups.google.com/group/castle-project-users/browse_thread/thread/d44d96f4b548611e/1c33a54539f8abf7"&gt;discussion
about this issue&lt;/a&gt; on the Castle Project Users forum, but the gist of it is that
if you don't need to &lt;em&gt;resolve&lt;/em&gt; components during application startup, this
shouldn't be an issue, and indeed it isn't – it seems to be something else. 
&lt;p&gt;
In my case I seem to have solved the problem by registering the HTTP module in configuration/system.webServer/modules
instead of configuration/system.web/httpModules. 
&lt;p&gt;
Although I haven't had the opportunity to dive into the technical details to understand &lt;em&gt;why&lt;/em&gt; this
works, this seems to solve the problem on both Windows Vista and Windows Server 2008.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=4276a194-a567-47ab-80b3-23c2813537f3" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,4276a194-a567-47ab-80b3-23c2813537f3.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>Castle Windsor</category>
      <category>Dependency Injection</category>
    </item>
  </channel>
</rss>