<?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 - ASP.NET MVC Framework</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 - ASP.NET MVC Framework</title>
      <link>http://www.timbarcz.com/blog/</link>
    </image>
    <language>en-us</language>
    <copyright>Tim Barcz</copyright>
    <lastBuildDate>Mon, 13 Jul 2009 03:13:46 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=5dfc88b7-a5e3-4bf9-8616-ad9ee961f464</trackback:ping>
      <pingback:server>http://www.timbarcz.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.timbarcz.com/blog/PermaLink,guid,5dfc88b7-a5e3-4bf9-8616-ad9ee961f464.aspx</pingback:target>
      <dc:creator>Tim Barcz</dc:creator>
      <wfw:comment>http://www.timbarcz.com/blog/CommentView,guid,5dfc88b7-a5e3-4bf9-8616-ad9ee961f464.aspx</wfw:comment>
      <wfw:commentRss>http://www.timbarcz.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=5dfc88b7-a5e3-4bf9-8616-ad9ee961f464</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Last week <a href="http://www.lostechies.com/blogs/jimmy_bogard">Jimmy Bogard</a> posted
why he believed the <a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/07/03/how-not-to-do-dependency-injection-in-nerddinner.aspx">dependency
injection pattern appearing in the NerdDinner application was flawed</a>.  I
want to take the opportunity to say the pattern in <a href="http://nerddinner.codeplex.com/">NerdDinner</a> really
isn't that bad and I want to explain why.  Before I go on I will say that I entirely
agree with Jimmy's thoughts on the pattern for my own code and you won't find the
anti-pattern Jimmy describes in my code.
</p>
        <p>
So why isn't the pattern <strong>that</strong> bad? I see Dependency Injection as
generally having at least two major benefits:
</p>
        <ul>
          <li>
Testability 
</li>
          <li>
Flexibility (most often achieved by using an IoC container) 
</li>
        </ul>
        <p>
The pattern under scrutiny in NerdDinner accounts for testability, of which I am a
firm believer in, but lacks in flexibility.  If I had to rank the two items above,
I would rank testability above flexibility.  Testability is a core tenet on which
I stand for with the code I write.
</p>
        <h2>Explain Again Why NerdDinner is "Flawed"
</h2>
        <p>
To recap here is the code from Jimmy's post:
</p>
        <div>
          <div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);">
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">
              <span style="color: rgb(96, 96, 96);"> 1:</span>
              <span style="color: rgb(0, 0, 255);">public</span>
              <span style="color: rgb(0, 0, 255);">class</span> SearchController
: Controller {</pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);">
              <span style="color: rgb(96, 96, 96);"> 2:</span>  </pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">
              <span style="color: rgb(96, 96, 96);"> 3:</span> IDinnerRepository
dinnerRepository;</pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);">
              <span style="color: rgb(96, 96, 96);"> 4:</span>  </pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">
              <span style="color: rgb(96, 96, 96);"> 5:</span>
              <span style="color: rgb(0, 128, 0);">//</span>
            </pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);">
              <span style="color: rgb(96, 96, 96);"> 6:</span>
              <span style="color: rgb(0, 128, 0);">//
Dependency Injection enabled constructors</span>
            </pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">
              <span style="color: rgb(96, 96, 96);"> 7:</span>  </pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);">
              <span style="color: rgb(96, 96, 96);"> 8:</span>
              <span style="color: rgb(0, 0, 255);">public</span> SearchController()</pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">
              <span style="color: rgb(96, 96, 96);"> 9:</span> : <span style="color: rgb(0, 0, 255);">this</span>(<span style="color: rgb(0, 0, 255);">new</span> DinnerRepository())
{</pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);">
              <span style="color: rgb(96, 96, 96);"> 10:</span> }</pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">
              <span style="color: rgb(96, 96, 96);"> 11:</span>  </pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);">
              <span style="color: rgb(96, 96, 96);"> 12:</span>
              <span style="color: rgb(0, 0, 255);">public</span> SearchController(IDinnerRepository
repository) {</pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">
              <span style="color: rgb(96, 96, 96);"> 13:</span> dinnerRepository
= repository;</pre>
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);">
              <span style="color: rgb(96, 96, 96);"> 14:</span> }</pre>
          </div>
        </div>
        <p>
The first constructor, when called, "news up" a new DinnerRepository() and passes
it into another constructor on the same object which is generally considered "poor
man's dependency injection".  This pattern is considered bad (this an anti-pattern)
because some, of which Jimmy is one, believe you shouldn't "new up" new objects this
way, choosing instead to allow a container to inject the primal dependency. 
The problem I have with this view is that for many applications a container is overkill. 
Many applications will only have one concrete implementation of a class, so in many
ways this constructor and the object creation could be seen as providing a sensible
default to the application, allowing the consumer to create an instance without worry
about what else the object needs.
</p>
        <h2>But You Said You Don't Write Your Code that Way?
</h2>
        <p>
Absolutely correct, I don't write my code this way.  Why? I already have a container
set up in my application and so to not use it would be foolish.  We also have
a complex application/domain, sometimes with multiple concrete implementations of
an interface.
</p>
        <p>
I used to write code this way using this pattern. Really. I don't feel ashamed to
say so. The reason we adopted the dependency injection (DI) pattern in the application
where this overloaded constructor "anti-pattern" was used was because DI allows for
testability, and as I have previously mentioned, this "anti-pattern" doesn't stand
in the way of testing.
</p>
        <p>
I further believe that most developers must make a certain progression on their own. 
We can write and share our experiences but in the end every developer out there must
some day be convinced in their own time. \They must come to the conclusion on their
own, like I did, that using "poor man's injection" will work for many many scenarios
but will get ugly fast in others.
</p>
        <h2>What's Really Wrong with "Poor Man's Dependency Injection"?
</h2>
        <p>
For simple applications/domain, typically not much (again, my opinion).  For
complex domain the issue you will run into is maintainability/flexibility.  If
you get to a complex domain and you use the pattern from NerdDinner you will someday
have:
</p>
        <div>
          <div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);">
            <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;">
              <span style="color: rgb(96, 96, 96);"> 1:</span>
              <span style="color: rgb(0, 0, 255);">public</span> SearchController(<span style="color: rgb(0, 0, 255);">new</span> SearchService(<span style="color: rgb(0, 0, 255);">new</span> SearchLogService(<span style="color: rgb(0, 0, 255);">new</span> Logger()), <span style="color: rgb(0, 0, 255);">new</span> SearchRepository(<span style="color: rgb(0, 0, 255);">new</span> SessionBuilder()), <span style="color: rgb(0, 0, 255);">new</span> SearchResultFormatter(<span style="color: rgb(0, 0, 255);">new</span> FormatProvider())))</pre>
          </div>
        </div>
        <p>
Ugly for sure.  This is why it's generally considered an "Anti-Pattern". 
However for applications where the controller is the service-layer and controllers
therefore use repositories, I would not be too concerned with the use of this pattern.
</p>
        <p>
While Jimmy did a great job fixing the application, you first have to believe the
application needed fixing.  Which of course, depends on context.  How big/complex
is your application/domain?  Jimmy's application/domain is sufficiently complex
as is mine, yours may not be.  For NerdDinner I believe the pattern was a suitable
choice.
</p>
        <img width="0" height="0" src="http://www.timbarcz.com/blog/aggbug.ashx?id=5dfc88b7-a5e3-4bf9-8616-ad9ee961f464" />
      </body>
      <title>Why There's Nothing Wrong With Dependency Injection in NerdDinner</title>
      <guid isPermaLink="false">http://www.timbarcz.com/blog/PermaLink,guid,5dfc88b7-a5e3-4bf9-8616-ad9ee961f464.aspx</guid>
      <link>http://www.timbarcz.com/blog/WhyTheresNothingWrongWithDependencyInjectionInNerdDinner.aspx</link>
      <pubDate>Mon, 13 Jul 2009 03:13:46 GMT</pubDate>
      <description>&lt;p&gt;
Last week &lt;a href="http://www.lostechies.com/blogs/jimmy_bogard"&gt;Jimmy Bogard&lt;/a&gt; posted
why he believed the &lt;a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/07/03/how-not-to-do-dependency-injection-in-nerddinner.aspx"&gt;dependency
injection pattern appearing in the NerdDinner application was flawed&lt;/a&gt;.&amp;nbsp; I
want to take the opportunity to say the pattern in &lt;a href="http://nerddinner.codeplex.com/"&gt;NerdDinner&lt;/a&gt; really
isn't that bad and I want to explain why.&amp;nbsp; Before I go on I will say that I entirely
agree with Jimmy's thoughts on the pattern for my own code and you won't find the
anti-pattern Jimmy describes in my code.
&lt;/p&gt;
&lt;p&gt;
So why isn't the pattern &lt;strong&gt;that&lt;/strong&gt; bad? I see Dependency Injection as
generally having at least two major benefits:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Testability 
&lt;/li&gt;
&lt;li&gt;
Flexibility (most often achieved by using an IoC container) 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The pattern under scrutiny in NerdDinner accounts for testability, of which I am a
firm believer in, but lacks in flexibility.&amp;nbsp; If I had to rank the two items above,
I would rank testability above flexibility.&amp;nbsp; Testability is a core tenet on which
I stand for with the code I write.
&lt;/p&gt;
&lt;h2&gt;Explain Again Why NerdDinner is "Flawed"
&lt;/h2&gt;
&lt;p&gt;
To recap here is the code from Jimmy's post:
&lt;/p&gt;
&lt;div&gt;
&lt;div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 1:&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; SearchController
: Controller {&lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 2:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 3:&lt;/span&gt; IDinnerRepository
dinnerRepository;&lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 4:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 5:&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;//&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 6:&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;//
Dependency Injection enabled constructors&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 7:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 8:&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; SearchController()&lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 9:&lt;/span&gt; : &lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; DinnerRepository())
{&lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 10:&lt;/span&gt; }&lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 11:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 12:&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; SearchController(IDinnerRepository
repository) {&lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 13:&lt;/span&gt; dinnerRepository
= repository;&lt;/pre&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 14:&lt;/span&gt; }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The first constructor, when called, "news up" a new DinnerRepository() and passes
it into another constructor on the same object which is generally considered "poor
man's dependency injection".&amp;nbsp; This pattern is considered bad (this an anti-pattern)
because some, of which Jimmy is one, believe you shouldn't "new up" new objects this
way, choosing instead to allow a container to inject the primal dependency.&amp;nbsp;
The problem I have with this view is that for many applications a container is overkill.&amp;nbsp;
Many applications will only have one concrete implementation of a class, so in many
ways this constructor and the object creation could be seen as providing a sensible
default to the application, allowing the consumer to create an instance without worry
about what else the object needs.
&lt;/p&gt;
&lt;h2&gt;But You Said You Don't Write Your Code that Way?
&lt;/h2&gt;
&lt;p&gt;
Absolutely correct, I don't write my code this way.&amp;nbsp; Why? I already have a container
set up in my application and so to not use it would be foolish.&amp;nbsp; We also have
a complex application/domain, sometimes with multiple concrete implementations of
an interface.
&lt;/p&gt;
&lt;p&gt;
I used to write code this way using this pattern. Really. I don't feel ashamed to
say so. The reason we adopted the dependency injection (DI) pattern in the application
where this overloaded constructor "anti-pattern" was used was because DI allows for
testability, and as I have previously mentioned, this "anti-pattern" doesn't stand
in the way of testing.
&lt;/p&gt;
&lt;p&gt;
I further believe that most developers must make a certain progression on their own.&amp;nbsp;
We can write and share our experiences but in the end every developer out there must
some day be convinced in their own time. \They must come to the conclusion on their
own, like I did, that using "poor man's injection" will work for many many scenarios
but will get ugly fast in others.
&lt;/p&gt;
&lt;h2&gt;What's Really Wrong with "Poor Man's Dependency Injection"?
&lt;/h2&gt;
&lt;p&gt;
For simple applications/domain, typically not much (again, my opinion).&amp;nbsp; For
complex domain the issue you will run into is maintainability/flexibility.&amp;nbsp; If
you get to a complex domain and you use the pattern from NerdDinner you will someday
have:
&lt;/p&gt;
&lt;div&gt;
&lt;div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);"&gt;
&lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"&gt;&lt;span style="color: rgb(96, 96, 96);"&gt; 1:&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; SearchController(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SearchService(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SearchLogService(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Logger()), &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SearchRepository(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SessionBuilder()), &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; SearchResultFormatter(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; FormatProvider())))&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Ugly for sure.&amp;nbsp; This is why it's generally considered an "Anti-Pattern".&amp;nbsp;
However for applications where the controller is the service-layer and controllers
therefore use repositories, I would not be too concerned with the use of this pattern.
&lt;/p&gt;
&lt;p&gt;
While Jimmy did a great job fixing the application, you first have to believe the
application needed fixing.&amp;nbsp; Which of course, depends on context.&amp;nbsp; How big/complex
is your application/domain?&amp;nbsp; Jimmy's application/domain is sufficiently complex
as is mine, yours may not be.&amp;nbsp; For NerdDinner I believe the pattern was a suitable
choice.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.timbarcz.com/blog/aggbug.ashx?id=5dfc88b7-a5e3-4bf9-8616-ad9ee961f464" /&gt;</description>
      <comments>http://www.timbarcz.com/blog/CommentView,guid,5dfc88b7-a5e3-4bf9-8616-ad9ee961f464.aspx</comments>
      <category>ASP.NET MVC Framework</category>
      <category>IoC</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>