<?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:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Tim Barcz - Windsor</title>
    <link>http://www.timbarcz.com/blog/</link>
    <description>My Code is My Craft</description>
    <image>
      <url>http://www.timbarcz.com/blog/content/binary/channelImage.jpg</url>
      <title>Tim Barcz - Windsor</title>
      <link>http://www.timbarcz.com/blog/</link>
    </image>
    <language>en-us</language>
    <copyright>Tim Barcz</copyright>
    <lastBuildDate>Mon, 30 Mar 2009 02:56:27 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>blog@timbarcz.com</managingEditor>
    <webMaster>blog@timbarcz.com</webMaster>
    <item>
      <trackback:ping>http://www.timbarcz.com/blog/Trackback.aspx?guid=8493245c-62af-4c83-9470-0f04e0a667e8</trackback:ping>
      <pingback:server>http://www.timbarcz.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.timbarcz.com/blog/PermaLink,guid,8493245c-62af-4c83-9470-0f04e0a667e8.aspx</pingback:target>
      <dc:creator>Tim Barcz</dc:creator>
      <wfw:comment>http://www.timbarcz.com/blog/CommentView,guid,8493245c-62af-4c83-9470-0f04e0a667e8.aspx</wfw:comment>
      <wfw:commentRss>http://www.timbarcz.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=8493245c-62af-4c83-9470-0f04e0a667e8</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I don't want to be a wet blanket, but someone's got to put a stop to the IoC love
fest going on out there.  An Inversion of Control (IoC) container , for those
of you were aren't yet familiar, allows you to retrieve instances of objects at runtime. 
A relatively common solution to a common problem. In the .NET world there is no lack
of choices of IoC containers:
</p>
        <ul>
          <li>
            <a href="http://www.castleproject.org/container/index.html">Castle's Winsdor/MicroKernel</a>
          </li>
          <li>
            <a href="http://structuremap.sourceforge.net/Default.htm">StructureMap</a>
          </li>
          <li>
            <a href="http://www.springframework.net/">Spring.NET</a>
          </li>
          <li>
            <a href="http://code.google.com/p/autofac/">AutoFac</a>
          </li>
          <li>
            <a href="http://ninject.org/">Ninject</a>
          </li>
          <li>
            <a href="http://msdn.microsoft.com/en-us/library/cc468366.aspx">Unity</a>
          </li>
          <li>
            <a href="http://funq.codeplex.com/">Funq</a>
          </li>
        </ul>
        <p>
(I won't go fully into IoC, but If you'd like more resource you can read <a href="http://martinfowler.com/articles/injection.html">Martin
Fowler's article on the subject</a> or post specific questions in the comments or
contact me through.  Read on, there is value here in this post for you.)
</p>
        <p>
          <strong>Let me state clearly now, IoC containers are a means to an end and NOT a feature
of your application.</strong> The goal is to have loosely coupled applications, IoC
containers help get you there, but you can absolutely have a loosely coupled application
without an IoC container (see the <a href="http://www.dofactory.com/Patterns/Patterns.aspx">Gang
of Four creational patterns</a>).
</p>
        <p>
Occasionally I'll hear some comment about IoC and how great a certain container is
or how an application would be so much better if another, sexier, container was used. 
Here are some recent <a href="http://www.twitter.com">tweets</a> that illustrate:
</p>
        <ul>
          <li>
Man i love #StructureMap, always finding new things i can do, and have fun doing them 
</li>
          <li>
really need to look at structuremap, but we''ve invested a lot into castle with our
own facilities/resolvers/etc 
</li>
          <li>
Ninject is da bomb! 
</li>
          <li>
Switched from Windsor MicroKernel to Ninject in 10KLOC project in &lt; 1 hour by creating
a little proxy. Now to complete the conversion. 
</li>
          <li>
It is a talk on Castle Windsor so i guess Ninject will not do. 
</li>
          <li>
Can't imagine why somebody would use Ninject beside the fact that it's PURE AND UTTER
AWESOME. Yeah! Ninject FTW. 
</li>
        </ul>
        <p>
Some of the statements above are probably innocuous, but some rub me slightly the
wrong way.  Specifically the two comments above about switching from one container
to another. There are probably times when the choice of one container over another
makes sense, but I would bet that these scenarios are the exception and not the rule.
</p>
        <p>
Choosing one container over another won't make your application a success, the architecture,
or lack there of, will have far more of a say on your application than will your container
choice. As yoda might say, "The container you choose does not a good app make". 
</p>
        <img width="0" height="0" src="http://www.timbarcz.com/blog/aggbug.ashx?id=8493245c-62af-4c83-9470-0f04e0a667e8" />
      </body>
      <title>Your IoC Container Choice is Not a Feature of Your Application</title>
      <guid isPermaLink="false">http://www.timbarcz.com/blog/PermaLink,guid,8493245c-62af-4c83-9470-0f04e0a667e8.aspx</guid>
      <link>http://www.timbarcz.com/blog/YourIoCContainerChoiceIsNotAFeatureOfYourApplication.aspx</link>
      <pubDate>Mon, 30 Mar 2009 02:56:27 GMT</pubDate>
      <description>&lt;p&gt;
I don't want to be a wet blanket, but someone's got to put a stop to the IoC love
fest going on out there.&amp;#160; An Inversion of Control (IoC) container , for those
of you were aren't yet familiar, allows you to retrieve instances of objects at runtime.&amp;#160;
A relatively common solution to a common problem. In the .NET world there is no lack
of choices of IoC containers:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://www.castleproject.org/container/index.html"&gt;Castle's Winsdor/MicroKernel&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://structuremap.sourceforge.net/Default.htm"&gt;StructureMap&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.springframework.net/"&gt;Spring.NET&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://code.google.com/p/autofac/"&gt;AutoFac&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://ninject.org/"&gt;Ninject&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/cc468366.aspx"&gt;Unity&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://funq.codeplex.com/"&gt;Funq&lt;/a&gt; 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
(I won't go fully into IoC, but If you'd like more resource you can read &lt;a href="http://martinfowler.com/articles/injection.html"&gt;Martin
Fowler's article on the subject&lt;/a&gt; or post specific questions in the comments or
contact me through.&amp;#160; Read on, there is value here in this post for you.)
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Let me state clearly now, IoC containers are a means to an end and NOT a feature
of your application.&lt;/strong&gt; The goal is to have loosely coupled applications, IoC
containers help get you there, but you can absolutely have a loosely coupled application
without an IoC container (see the &lt;a href="http://www.dofactory.com/Patterns/Patterns.aspx"&gt;Gang
of Four creational patterns&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
Occasionally I'll hear some comment about IoC and how great a certain container is
or how an application would be so much better if another, sexier, container was used.&amp;#160;
Here are some recent &lt;a href="http://www.twitter.com"&gt;tweets&lt;/a&gt; that illustrate:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Man i love #StructureMap, always finding new things i can do, and have fun doing them 
&lt;/li&gt;
&lt;li&gt;
really need to look at structuremap, but we''ve invested a lot into castle with our
own facilities/resolvers/etc 
&lt;/li&gt;
&lt;li&gt;
Ninject is da bomb! 
&lt;/li&gt;
&lt;li&gt;
Switched from Windsor MicroKernel to Ninject in 10KLOC project in &amp;lt; 1 hour by creating
a little proxy. Now to complete the conversion. 
&lt;/li&gt;
&lt;li&gt;
It is a talk on Castle Windsor so i guess Ninject will not do. 
&lt;/li&gt;
&lt;li&gt;
Can't imagine why somebody would use Ninject beside the fact that it's PURE AND UTTER
AWESOME. Yeah! Ninject FTW. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Some of the statements above are probably innocuous, but some rub me slightly the
wrong way.&amp;#160; Specifically the two comments above about switching from one container
to another. There are probably times when the choice of one container over another
makes sense, but I would bet that these scenarios are the exception and not the rule.
&lt;/p&gt;
&lt;p&gt;
Choosing one container over another won't make your application a success, the architecture,
or lack there of, will have far more of a say on your application than will your container
choice. As yoda might say, &amp;quot;The container you choose does not a good app make&amp;quot;. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.timbarcz.com/blog/aggbug.ashx?id=8493245c-62af-4c83-9470-0f04e0a667e8" /&gt;</description>
      <comments>http://www.timbarcz.com/blog/CommentView,guid,8493245c-62af-4c83-9470-0f04e0a667e8.aspx</comments>
      <category>IoC</category>
      <category>Rant</category>
      <category>Tools</category>
      <category>Windsor</category>
    </item>
    <item>
      <trackback:ping>http://www.timbarcz.com/blog/Trackback.aspx?guid=8a12ded7-7dd8-491b-9dbb-559cc3c516fa</trackback:ping>
      <pingback:server>http://www.timbarcz.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.timbarcz.com/blog/PermaLink,guid,8a12ded7-7dd8-491b-9dbb-559cc3c516fa.aspx</pingback:target>
      <dc:creator>Tim Barcz</dc:creator>
      <wfw:comment>http://www.timbarcz.com/blog/CommentView,guid,8a12ded7-7dd8-491b-9dbb-559cc3c516fa.aspx</wfw:comment>
      <wfw:commentRss>http://www.timbarcz.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=8a12ded7-7dd8-491b-9dbb-559cc3c516fa</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Shhh...don't tell anyone but we don't unit test all of our code.  We're striving
to get all developers and managers on board with unit testing but we're not there
yet.  Despite our delinquency in writing tests one thing we do try our very best
to do is keep mistakes from happening again.  If we find a bug in the system,
before fixing it, we go and write a unit test for that problem.  We verify the
test fails, the go and write the code that fixes it.  This recognizes that bugs
will appear in the system, but once fixed we'll insulate ourselves from that bug happening
again.
</p>
        <p>
We had one such case last week.  We were getting a yellow-screen error after
a bunch of commits by developers.  The error stemmed from a component in Windsor
that did not have all of it's dependencies met.  In other words, we registered
a component which relied on another component which was not registered.  We already
had written a test to compile and test our Windsor configuration, however that test
was not mature enough and needed some tweaking.  
</p>
        <p>
In our application we leverage the new ASP.Net MVC.  We also take advantage of
the ability to leverage an IoC container, Windsor, to create controller instances
when a web request comes in.  When the application starts, we register all of
the controllers in the application into Windsor using reflection.  Therefore
our Windsor configuration at runtime is made up of two things, the XML configuration
and the controllers which are registered on startup.  The "bug" here
was that one of our controllers had a dependency on it that was not registered in
Windsor.  When you ran the test by itself, the test would pass, since it was
only loading up the components from the Windsor configuration.
</p>
        <p>
Wanting to protect us in the future, we acknowledged there was something missing from
our test and set out to find it.  To create the failing test was relatively simple. 
All that we needed to do replicate in our test what we were doing at runtime. 
Therefore we needed to register all implementers of IController, just like we do on
application startup.  Below I've 
</p>
        <p>
Before:
</p>
        <div>
          <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 1:</span> IWindsorContainer
container = <span style="color: #0000ff">new</span> WindsorContainer(<span style="color: #006080">"windsor.config.xml"</span>);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 2:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 3:</span>
              <span style="color: #0000ff">foreach</span> (var
handler <span style="color: #0000ff">in</span> container.Kernel.GetAssignableHandlers(<span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">object</span>)))</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 4:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 5:</span> Console.WriteLine(<span style="color: #006080">"{0}\n\t-
{1}"</span>, handler.ComponentModel.Service, handler.ComponentModel.Implementation);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 6:</span> container.Resolve(handler.ComponentModel.Service);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 7:</span> }</pre>
          </div>
        </div>
        <p>
After:
</p>
        <div>
          <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 1:</span> IWindsorContainer
container = <span style="color: #0000ff">new</span> WindsorContainer(<span style="color: #006080">"windsor.config.xml"</span>);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 2:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 3:</span> var
assm = <span style="color: #0000ff">new</span> HomeController(<span style="color: #0000ff">null</span>).GetType().Assembly;</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 4:</span>
              <span style="color: #008000">//add
all controllers</span>
            </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 5:</span>
              <span style="color: #0000ff">foreach</span> (Type
type <span style="color: #0000ff">in</span> assm.GetTypes())</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 6:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 7:</span>
              <span style="color: #0000ff">if</span> (<span style="color: #0000ff">typeof</span>(IController).IsAssignableFrom(type)
&amp;&amp; !type.IsAbstract)</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 8:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 9:</span> container.AddComponent(type.Name.ToLower(),
type);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 10:</span> }</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 11:</span> }</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 12:</span>  </pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 13:</span>
              <span style="color: #0000ff">foreach</span> (var
handler <span style="color: #0000ff">in</span> container.Kernel.GetAssignableHandlers(<span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">object</span>)))</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 14:</span> {</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 15:</span> Console.WriteLine(<span style="color: #006080">"{0}\n\t-
{1}"</span>, handler.ComponentModel.Service, handler.ComponentModel.Implementation);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
              <span style="color: #606060"> 16:</span> container.Resolve(handler.ComponentModel.Service);</pre>
            <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">
              <span style="color: #606060"> 17:</span> }</pre>
          </div>
        </div>
        <p>
Now that this test is in place, I am much more confident that we won't see the error
we were seeing in product again, we've got a test which warns us if something isn't
quite right.  Our initial test didn't quite get it all right.  <strong>Mistakes/bugs
in code will happen, hopefully few and far between, but when a bug does show up, go
write a test that verifies the bug before going off an fixing it.  Then make
the test pass, which should fix the bug.</strong></p>
        <img width="0" height="0" src="http://www.timbarcz.com/blog/aggbug.ashx?id=8a12ded7-7dd8-491b-9dbb-559cc3c516fa" />
      </body>
      <title>Windsor Components, the ASP.NET MVC Framework, and Bug Verification Tests</title>
      <guid isPermaLink="false">http://www.timbarcz.com/blog/PermaLink,guid,8a12ded7-7dd8-491b-9dbb-559cc3c516fa.aspx</guid>
      <link>http://www.timbarcz.com/blog/WindsorComponentsTheASPNETMVCFrameworkAndBugVerificationTests.aspx</link>
      <pubDate>Fri, 14 Nov 2008 16:44:22 GMT</pubDate>
      <description>&lt;p&gt;
Shhh...don't tell anyone but we don't unit test all of our code.&amp;#160; We're striving
to get all developers and managers on board with unit testing but we're not there
yet.&amp;#160; Despite our delinquency in writing tests one thing we do try our very best
to do is keep mistakes from happening again.&amp;#160; If we find a bug in the system,
before fixing it, we go and write a unit test for that problem.&amp;#160; We verify the
test fails, the go and write the code that fixes it.&amp;#160; This recognizes that bugs
will appear in the system, but once fixed we'll insulate ourselves from that bug happening
again.
&lt;/p&gt;
&lt;p&gt;
We had one such case last week.&amp;#160; We were getting a yellow-screen error after
a bunch of commits by developers.&amp;#160; The error stemmed from a component in Windsor
that did not have all of it's dependencies met.&amp;#160; In other words, we registered
a component which relied on another component which was not registered.&amp;#160; We already
had written a test to compile and test our Windsor configuration, however that test
was not mature enough and needed some tweaking.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
In our application we leverage the new ASP.Net MVC.&amp;#160; We also take advantage of
the ability to leverage an IoC container, Windsor, to create controller instances
when a web request comes in.&amp;#160; When the application starts, we register all of
the controllers in the application into Windsor using reflection.&amp;#160; Therefore
our Windsor configuration at runtime is made up of two things, the XML configuration
and the controllers which are registered on startup.&amp;#160; The &amp;quot;bug&amp;quot; here
was that one of our controllers had a dependency on it that was not registered in
Windsor.&amp;#160; When you ran the test by itself, the test would pass, since it was
only loading up the components from the Windsor configuration.
&lt;/p&gt;
&lt;p&gt;
Wanting to protect us in the future, we acknowledged there was something missing from
our test and set out to find it.&amp;#160; To create the failing test was relatively simple.&amp;#160;
All that we needed to do replicate in our test what we were doing at runtime.&amp;#160;
Therefore we needed to register all implementers of IController, just like we do on
application startup.&amp;#160; Below I've 
&lt;/p&gt;
&lt;p&gt;
Before:
&lt;/p&gt;
&lt;div&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 1:&lt;/span&gt; IWindsorContainer
container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WindsorContainer(&lt;span style="color: #006080"&gt;&amp;quot;windsor.config.xml&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 2:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 3:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var
handler &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; container.Kernel.GetAssignableHandlers(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;)))&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 4:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 5:&lt;/span&gt; Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;{0}\n\t-
{1}&amp;quot;&lt;/span&gt;, handler.ComponentModel.Service, handler.ComponentModel.Implementation);&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 6:&lt;/span&gt; container.Resolve(handler.ComponentModel.Service);&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 7:&lt;/span&gt; }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
After:
&lt;/p&gt;
&lt;div&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 1:&lt;/span&gt; IWindsorContainer
container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WindsorContainer(&lt;span style="color: #006080"&gt;&amp;quot;windsor.config.xml&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 2:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 3:&lt;/span&gt; var
assm = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; HomeController(&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;).GetType().Assembly;&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 4:&lt;/span&gt; &lt;span style="color: #008000"&gt;//add
all controllers&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 5:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (Type
type &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; assm.GetTypes())&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 6:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 7:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IController).IsAssignableFrom(type)
&amp;amp;&amp;amp; !type.IsAbstract)&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 8:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 9:&lt;/span&gt; container.AddComponent(type.Name.ToLower(),
type);&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 10:&lt;/span&gt; }&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 11:&lt;/span&gt; }&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 12:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 13:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var
handler &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; container.Kernel.GetAssignableHandlers(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;)))&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 14:&lt;/span&gt; {&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 15:&lt;/span&gt; Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;{0}\n\t-
{1}&amp;quot;&lt;/span&gt;, handler.ComponentModel.Service, handler.ComponentModel.Implementation);&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 16:&lt;/span&gt; container.Resolve(handler.ComponentModel.Service);&lt;/pre&gt;
&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt; 17:&lt;/span&gt; }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Now that this test is in place, I am much more confident that we won't see the error
we were seeing in product again, we've got a test which warns us if something isn't
quite right.&amp;#160; Our initial test didn't quite get it all right.&amp;#160; &lt;strong&gt;Mistakes/bugs
in code will happen, hopefully few and far between, but when a bug does show up, go
write a test that verifies the bug before going off an fixing it.&amp;#160; Then make
the test pass, which should fix the bug.&lt;/strong&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.timbarcz.com/blog/aggbug.ashx?id=8a12ded7-7dd8-491b-9dbb-559cc3c516fa" /&gt;</description>
      <comments>http://www.timbarcz.com/blog/CommentView,guid,8a12ded7-7dd8-491b-9dbb-559cc3c516fa.aspx</comments>
      <category>ASP.NET MVC Framework</category>
      <category>Caslte</category>
      <category>Testing</category>
      <category>Windsor</category>
    </item>
  </channel>
</rss>