<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" 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:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>IoC</title>
        <link>http://ayende.com/Blog/category/506.aspx</link>
        <description>IoC</description>
        <language>en-US</language>
        <copyright>Ayende Rahien</copyright>
        <managingEditor>Ayende@ayende.com</managingEditor>
        <generator>Subtext Version 2.0.0.0</generator>
        <item>
            <title>Windsor 2.1 is out &amp;ndash; now with Silverlight support</title>
            <link>http://ayende.com/Blog/archive/2010/01/13/windsor-2.1-is-out-ndash-now-with-silverlight-support.aspx</link>
            <description>&lt;p&gt;&lt;strong&gt;&lt;a href="http://devlicio.us/members/xtoff/default.aspx"&gt;Krzysztof Koźmic&lt;/a&gt;&lt;/strong&gt; has just &lt;a href="http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/01/12/castle-windsor-2-1-dynamic-proxy-2-2-and-more-released.aspx?utm_source=feedburner&amp;amp;utm_medium=feed&amp;amp;utm_campaign=Feed:+Devlicious+(Devlicio.us)&amp;amp;utm_content=Google+Reader"&gt;announced it&lt;/a&gt;, so I will skip posting the details here and just let you read the entire announcement on his blog.&lt;/p&gt;  &lt;p&gt;One thing that deserves special attention is that, as you can read from the post title, this Windsor release is for both .NET 3.5 and for Silverlight 3.0.&lt;/p&gt;  &lt;p&gt;Have fun…&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/11283.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2010/01/13/windsor-2.1-is-out-ndash-now-with-silverlight-support.aspx</guid>
            <pubDate>Tue, 12 Jan 2010 23:39:00 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2010/01/13/windsor-2.1-is-out-ndash-now-with-silverlight-support.aspx#feedback</comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/11283.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Optimizing Windsor</title>
            <link>http://ayende.com/Blog/archive/2009/09/18/optimizing-windsor.aspx</link>
            <description>&lt;p&gt;Recently we got a bug report about the performance of Windsor when registering large number of components (thousands). I decided to sit down and investigate this, and found out something that was troublesome. &lt;/p&gt;  &lt;p&gt;Internally, registering a component would trigger a check for all registered components that are waiting for a dependency. If you had a lot of components that were waiting for dependency, registering a new component degenerated to an O(N^2) operation, where N was the number of components with waiting dependencies.&lt;/p&gt;  &lt;p&gt;Luckily, there was no real requirement for an O(N^2) operation, and I was able to change that to an O(N) operation.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Huge&lt;/em&gt; optimization win, right?&lt;/p&gt;  &lt;p&gt;In numbers, we are talking about 9.2 &lt;em&gt;seconds&lt;/em&gt; to register 500 components with no matching dependencies. After the optimization, we dropped that to 500 &lt;em&gt;milliseconds&lt;/em&gt;. And when we are talking about larger number of components, this is still a problem.&lt;/p&gt;  &lt;p&gt;After optimization, registering 5,000 components with no matching dependencies took 44.5 seconds. That is better than before (where no one has the patience to try and figure out the number), but I think we can improve up it.&lt;/p&gt;  &lt;p&gt;The problem is that we are still paying that O(N) cost for &lt;em&gt;each registration&lt;/em&gt;. Now, to suppose systems that already uses Windsor, we can’t really change the way Windsor handle registrations by default, so I came up with the following syntax, that safely change the way Windsor handles registration:&lt;/p&gt;  &lt;blockquote&gt;   &lt;div id="codeSnippetWrapper"&gt;     &lt;pre id="codeSnippet" class="csharpcode"&gt;var kernel = &lt;span class="kwrd"&gt;new&lt;/span&gt; DefaultKernel();&lt;br /&gt;&lt;strong&gt;&lt;em&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (kernel.OptimizeDependencyResolution())&lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 500; i++)&lt;br /&gt;    {&lt;br /&gt;        kernel.AddComponent(&lt;span class="str"&gt;"key"&lt;/span&gt; + i, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt;), &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt;));&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using this method, registering 5,000 components drops down to 2.5 seconds. &lt;/p&gt;

&lt;p&gt;I then spent additional time finding all the other nooks and crannies where optimizations hid, dropping the performance down to 1.4 seconds.&lt;/p&gt;

&lt;p&gt;Now, I have to say that this is &lt;em&gt;not &lt;/em&gt;linear performance improvement. Registering 20,000 components will take about 25 seconds. This is not a scenario that we worry over much about.&lt;/p&gt;

&lt;p&gt;The best thing about the non linear curve is that for a 1,000 components, which is what we &lt;em&gt;do&lt;/em&gt; care about, registration takes 240 milliseconds. Most applications don’t get to have a thousand components, anyway.&lt;/p&gt;

&lt;p&gt;There are also other improvements made in the overall runtime performance of Windsor, but those would be very hard to notice outside of a tight loop.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/11127.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2009/09/18/optimizing-windsor.aspx</guid>
            <pubDate>Fri, 18 Sep 2009 07:06:56 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2009/09/18/optimizing-windsor.aspx#feedback</comments>
            <slash:comments>18</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/11127.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Why I released Windsor?</title>
            <link>http://ayende.com/Blog/archive/2009/05/08/why-i-released-windsor.aspx</link>
            <description>&lt;p&gt;Because getting &lt;a href="http://castle.uservoice.com/pages/16605-unofficial-castle-project-feedback-forum/suggestions/168104-kick-ayende-until-he-releases-windsor?direct_login_token=07e288937b357cb0b71f6dfe1efe580d&amp;amp;subdomain=castle&amp;amp;tracking_code=85a17ce065093ac02b037d648136a1b5"&gt;kicked&lt;/a&gt; that many times will &lt;em&gt;hurt&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://ayende.com/Blog/images/ayende_com/Blog/200905072355.jpg" width="480" height="88" alt="200905072355.jpg" /&gt;&lt;/p&gt;
&lt;img src="http://ayende.com/Blog/aggbug/10914.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2009/05/08/why-i-released-windsor.aspx</guid>
            <pubDate>Thu, 07 May 2009 21:56:23 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2009/05/08/why-i-released-windsor.aspx#feedback</comments>
            <slash:comments>7</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10914.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Castle Windsor 2.0 RTM Released</title>
            <link>http://ayende.com/Blog/archive/2009/05/05/castle-windsor-2.0-rtm-released.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/CastleWindsor2.0RTMReleased_1365/image_8.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; margin-left: 0px; border-left: 0px; margin-right: 0px; border-bottom: 0px" height="219" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/CastleWindsor2.0RTMReleased_1365/image_thumb_3.png" width="343" align="right" border="0" /&gt;&lt;/a&gt;Some would say that it is &lt;em&gt;about&lt;/em&gt; time, I would agree. Windsor might not be the OSS project in pre release state for the longest time (I think that the honor belong to Hurd), but it spent enough time at that state to at least deserve a honorary mention.&lt;/p&gt;  &lt;p&gt;That was mostly because, although Windsor was production ready for the last three or four years or so, most of the people making use of it were happy to make use of the trunk version. &lt;/p&gt;  &lt;p&gt;If you will look, you won’t find Windsor 1.0, only release candidates for 1.0. As I believe I mentioned, Windsor has been production ready for a long time, and for the full release we decided to skip the 1.0 designator, which doesn’t really fit, and go directly to 2.0&lt;/p&gt;  &lt;p&gt;The last Windsor release (RC3) was almost a year and a half ago, and in the meantime, much has improved in Windsor land. Adding upon the already superb engine and facilities, we have fitted Windsor to the 3.5 release of the .Net framework, created a full fledged fluent API to support easy configuration, allowed more granular control over the behavior of the container when selecting components and handlers and improved overall performance.&lt;/p&gt;  &lt;p&gt;All in all, pretty good stuff, even if I say so myself. Just to give you an idea, the list of changes from the previous release goes for quite a while, so I am going to let the short listing above to stand in its place.&lt;/p&gt;  &lt;p&gt;You can get the new release from the &lt;a href="https://sourceforge.net/project/showfiles.php?group_id=124416&amp;amp;package_id=136235"&gt;source forge site&lt;/a&gt;. &lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10913.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2009/05/05/castle-windsor-2.0-rtm-released.aspx</guid>
            <pubDate>Mon, 04 May 2009 22:22:51 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2009/05/05/castle-windsor-2.0-rtm-released.aspx#feedback</comments>
            <slash:comments>33</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10913.aspx</wfw:commentRss>
        </item>
        <item>
            <title>MEF &amp; Open Generic Types </title>
            <link>http://ayende.com/Blog/archive/2009/03/22/mef-open-generic-types.aspx</link>
            <description>&lt;p&gt;I read &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/03/21/why-doesn-t-mef-support-open-generics-for-exports-because-mef-is-not-type-based.aspx"&gt;Glenn' s post&lt;/a&gt; about MEF's &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/03/21/why-doesn-t-mef-support-open-generics-for-exports-because-mef-is-not-type-based.aspx"&gt;not supporting open generic types&lt;/a&gt; with somewhat resembling shock. The idea that it isn't supporting this never even crossed my mind, it was a given that this is a mandatory feature for any container in the .NET land.&lt;/p&gt;
&lt;p&gt;Just to give you an idea, what this means is that you can't register Repository&amp;lt;T&amp;gt; and then resolve Repository&amp;lt;Order&amp;gt;. In 2006, I wrote an &lt;a href="http://msdn.microsoft.com/en-us/library/aa973811.aspx"&gt;article for MSDN&lt;/a&gt; detailing what has since became a very common use of this pattern. Generic specialization is not something that I would consider optional, it is one of the most common usage patterns of containers in the .NET land. IRepository&amp;lt;T&amp;gt; is probably the most common example that people quote, but there &lt;a href="http://ayende.com/Blog/archive/2007/10/21/The-IoC-mind-set-Validation.aspx"&gt;are others as well&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This is not a simple feature, let me make it clear. Not simple at all. I should know, I implement that feature for both Object Builder and Windsor. But that is not what I would consider an optional one.&lt;/p&gt;
&lt;p&gt;I am even more disturbed by the actual reasoning behind not supporting this. It is a technical limitation of MEF because internally all components are resolved by string matching, rather than CLR Types. This decision is &lt;em&gt;severely&lt;/em&gt; limiting the things that MEF can do. Not supporting what is (in my opinion) is a pretty crucial feature is one example of that, but there are other implications. It means that you can't really do things like polymorphic resolutions, that your choices in extending the container are very limited, because the container isn't going to carry the information that is required to make those decision.&lt;/p&gt;
&lt;p&gt;I would advice the MEF team to rethink the decision to base the component resolution on strings. At this point in time, it is still possible to change things ( and yes, I &lt;em&gt;know&lt;/em&gt; it isn't as easy as I make it seems ), because not supporting open generic types is bad, but not having the &lt;em&gt;ability&lt;/em&gt; to do so, and the reason for that (not keeping CLR Type information) are even worse. I get that MEF needs to work with DLR objects as well, but that means that MEF makes the decision to have lousier support for CLR idioms for the benefit of the DLR.&lt;/p&gt;
&lt;p&gt;Considering the usage numbers for both of them, I can't see this being a good decision. It is certainly possible to support them both, but if there are any tradeoffs that have to be made, I would suggest that it should be the DLR, and not the CLR, which would be the second class role.&lt;/p&gt;
&lt;img src="http://ayende.com/Blog/aggbug/10829.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2009/03/22/mef-open-generic-types.aspx</guid>
            <pubDate>Sun, 22 Mar 2009 17:00:00 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2009/03/22/mef-open-generic-types.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10829.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Didja know? Merging Windsor configuration with automatic registration</title>
            <link>http://ayende.com/Blog/archive/2008/12/31/didja-know-merging-windsor-configuration-with-automatic-registration.aspx</link>
            <description>&lt;p&gt;One of the most annoying things that we have to do during development is updating configuration files. That is why convention over configuration is such a successful concept. The problem is what to do when you can mostly use the convention, but need to supply configuration values as well.&lt;/p&gt; &lt;p&gt;Well, one of the nice things about Windsor is the ability to merge several sources of information transparently. Given this configuration:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
	&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;configSections&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
		&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;section&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"castle"&lt;/span&gt;
			&lt;span style="color: #ff0000"&gt;type&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
	&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;configSections&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
	&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;castle&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
		&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;facilities&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
			&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;facility&lt;/span&gt; &lt;span style="color: #ff0000"&gt;id&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"rhino.esb"&lt;/span&gt; &lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
				&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;bus&lt;/span&gt; &lt;span style="color: #ff0000"&gt;threadCount&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"1"&lt;/span&gt;
						 &lt;span style="color: #ff0000"&gt;numberOfRetries&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"5"&lt;/span&gt;
						 &lt;span style="color: #ff0000"&gt;endpoint&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"msmq://localhost/demo.backend"&lt;/span&gt;
             &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
				&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;messages&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
				&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;messages&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
			&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;facility&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
		&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;facilities&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
		&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;components&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
			&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;component&lt;/span&gt; &lt;span style="color: #ff0000"&gt;id&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;"Demo.Backend.SendEmailConsumer"&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
				&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;parameters&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
					&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;host&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;smtp.gmail.com&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;host&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
					&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;port&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;587&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;port&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
					&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;password&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;*****&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;password&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
					&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;username&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;*****@ayende.com&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;username&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
					&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;enableSsl&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;enableSsl&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
					&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;from&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;*****@ayende.com&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;from&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
					&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;fromDisplayName&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Something&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;fromDisplayName&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
				&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;parameters&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
			&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;component&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
		&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;components&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
	&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;castle&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;configuration&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;And this auto registration:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;var container = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WindsorContainer(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XmlInterpreter());
container.Register(
	AllTypes.Of(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt; (ConsumerOf&amp;lt;&amp;gt;))
		.FromAssembly(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(Program).Assembly)
	);
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;We now get the benefit of both convention and configuration. We can let the convention pick up anything that we need, and configure just the values that we really have to configure.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10699.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/12/31/didja-know-merging-windsor-configuration-with-automatic-registration.aspx</guid>
            <pubDate>Wed, 31 Dec 2008 18:35:27 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/12/31/didja-know-merging-windsor-configuration-with-automatic-registration.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10699.aspx</wfw:commentRss>
        </item>
        <item>
            <title>First Steps with Post Sharp</title>
            <link>http://ayende.com/Blog/archive/2008/10/09/first-steps-with-post-sharp.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://postsharp.org/"&gt;PostSharp&lt;/a&gt; is an AOP framework that works using byte code weaving. That is, it re-writes your IL to add behaviors to it. From my point of view, it is like having the cake (interception, byte code weaving) and eating it (I haven't even &lt;em&gt;looked&lt;/em&gt; at the PostSharp source code, just used the binary release).&lt;/p&gt; &lt;p&gt;My initial spike with it went very well. Here it is:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;[Serializable]
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Logger : OnFieldAccessAspect
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnGetValue(FieldAccessEventArgs eventArgs)
    {
        Console.WriteLine(eventArgs.InstanceTag);
        Console.WriteLine("&lt;span style="color: #8b0000"&gt;get value&lt;/span&gt;");
        &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.OnGetValue(eventArgs);
    }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; InstanceTagRequest GetInstanceTagRequest()
    {
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; InstanceTagRequest("&lt;span style="color: #8b0000"&gt;logger&lt;/span&gt;", &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Guid("&lt;span style="color: #8b0000"&gt;4f8a4963-82bf-4d32-8775-42cc3cd119bd&lt;/span&gt;"), &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;);
    }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnSetValue(FieldAccessEventArgs eventArgs)
    {
        &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;?)eventArgs.InstanceTag ?? 0;
        eventArgs.InstanceTag = i + 1;
        Console.WriteLine("&lt;span style="color: #8b0000"&gt;set value&lt;/span&gt;");
        &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.OnSetValue(eventArgs);
    }
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;This is an aspect that run on each field access. It is not really useful, but it helps to show how things works. A couple of things that are I think are insanely useful:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Aspects are instantiated at compile time, allowed time to set themselves up, then serialized to an resource in the assembly. At runtime, they are de-serialized and ready to run. The possibilities this give you are amazing.&lt;/li&gt;
&lt;li&gt;InstanceTag is a way to keep additional data &lt;em&gt;per aspect&lt;/em&gt;.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Now, let us assume that I want to add the aspect to this code:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;[Logger]
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Customer
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Note, there &lt;em&gt;is&lt;/em&gt; no field. (Well, there is, it is generated by the compiler). Now we compile and run the PostSharp post compile step. With that, we can now investigate what is going on.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/FirstStepswithPostSharp_1175E/image_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="345" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/FirstStepswithPostSharp_1175E/image_thumb.png" width="860" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;As you can see, we are deserializing the attribute and storing it in a field that we can now access. Let us check the Customer implementation now:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/FirstStepswithPostSharp_1175E/image_6.png"&gt;&lt;img height="287" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/FirstStepswithPostSharp_1175E/image_thumb_2.png" width="391" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;We have the logger field, which is used for something, but we also have the ~get~&amp;lt;Name&amp;gt;k__Backingfield and ~set~&amp;lt;Name&amp;gt;k__BackingField. &amp;lt;Name&amp;gt;k__BackingField (and I would love to hear the story behind that) is the compiler generated field that was created for us. The ~get~... and ~set~ are generated by PostSharp. Before we look at them, we will look at the implementation of Name.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/FirstStepswithPostSharp_1175E/image_10.png"&gt;&lt;img height="189" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/FirstStepswithPostSharp_1175E/image_thumb_4.png" width="264" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Where it used to call the field directly, now it is doing this via a method call. And now we can look at those method calls.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/FirstStepswithPostSharp_1175E/image_12.png"&gt;&lt;img height="303" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/FirstStepswithPostSharp_1175E/image_thumb_5.png" width="890" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;There is a lot going on here. We create a new field access event arg, call the aspect method, and return the value. Note that the &lt;em&gt;state&lt;/em&gt; (instance tag) is stored in the object as well, for each field access.&lt;/p&gt;
&lt;p&gt;It looks &lt;em&gt;very &lt;/em&gt;well done.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10551.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/10/09/first-steps-with-post-sharp.aspx</guid>
            <pubDate>Thu, 09 Oct 2008 17:52:25 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/10/09/first-steps-with-post-sharp.aspx#feedback</comments>
            <slash:comments>10</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10551.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Windsor - IModelInterceptersSelector</title>
            <link>http://ayende.com/Blog/archive/2008/10/05/windsor-imodelinterceptersselector.aspx</link>
            <description>&lt;p&gt;In my &lt;a href="http://ayende.com/Blog/archive/2008/10/05/components-implementations-and-contextual-decisions.aspx"&gt;previous post&lt;/a&gt; I introduced the basis of context as an architectural pattern. Now I want to talk about how we can implement that using Windsor and a new extensibility point: IModelInterceptersSelector.&lt;/p&gt; &lt;p&gt;The interface is defined as:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #808080"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// Select the appropriate interecptors based on the application specific&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// business logic&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IModelInterceptorsSelector
{
    &lt;span style="color: #808080"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// Select the appropriate intereceptor references.&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// The intereceptor references aren't neccessarily registered in the model.Intereceptors&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;param name="model"&amp;gt;The model to select the interceptors for&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;returns&amp;gt;The intereceptors for this model (in the current context) or a null reference&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;remarks&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// If the selector is not interested in modifying the interceptors for this model, it &lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// should return a null reference and the next selector in line would be executed (or the default&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// model.Interceptors).&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// If the selector return a non null value, this is the value that is used, and the model.Interectors are ignored, if this&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// is not the desirable behavior, you need to merge your interceptors with the ones in model.Interecptors yourself.&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;/remarks&amp;gt;&lt;/span&gt;
    InterceptorReference[] SelectInterceptors(ComponentModel model);

    &lt;span style="color: #808080"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// Determain whatever the specified has interecptors.&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// The selector should only return true from this method if it has determained that is&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// a model that it would likely add interceptors to.&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;param name="model"&amp;gt;The model&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;returns&amp;gt;Whatever this selector is likely to add intereceptors to the specified model&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; HasInterceptors(ComponentModel model);
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;And registering it in the container is simply:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;container.Kernel.ProxyFactory.AddInterceptorSelector(selector);&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Interceptors are the basis of AOP, but traditionally, you didn't get a lot of choices in how you compose your interceptors at runtime. Using IModelInterceptersSelector make it extremely easy to modify the selection of interceptors based on relevant business logic. &lt;/p&gt;
&lt;p&gt;Let us take the following example. We have a warehouse service that we want to add caching to. However, we can't use the cache in the request comes from the fulfillment service. First, we define the caching interceptor, then, we define the logic that controls adding or removing it.&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; WarehouseCachingInterceptorSelector : IModelInterceptorsSelector
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; InterceptorReference[] SelectInterceptors(ComponentModel model)
    {
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(model.Service!=&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IWarehouse))
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(Origin.IsFromFulfillment)
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; InterceptorReference[]{&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; InterceptorReference(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(WarehouseCachingInterceptor)), };
    }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; HasInterceptors(ComponentModel model)
    {
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; model.Service == &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt; (IWarehouse);
    }
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;And now we get caching for everything except for fulfillment. And we get this in a clean and very easy to understand way. :-D&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10545.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/10/05/windsor-imodelinterceptersselector.aspx</guid>
            <pubDate>Sun, 05 Oct 2008 13:43:42 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/10/05/windsor-imodelinterceptersselector.aspx#feedback</comments>
            <slash:comments>9</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10545.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Windsor - IHandlerSelector</title>
            <link>http://ayende.com/Blog/archive/2008/10/05/windsor-ihandlerselector.aspx</link>
            <description>&lt;p&gt;In my &lt;a href="http://ayende.com/Blog/archive/2008/10/05/components-implementations-and-contextual-decisions.aspx"&gt;previous post&lt;/a&gt; I introduced the basis of context as an architectural pattern. Now I want to talk about how we can implement that using Windsor and a new extensibility point: IHandlerSelector.&lt;/p&gt; &lt;p&gt;The interface is defined as:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #808080"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// Implementors of this interface allow to extend the way the container perform&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// component resolution based on some application specific business logic.&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// &amp;lt;remarks&amp;gt;&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// This is the sibling interface to &amp;lt;seealso cref="ISubDependencyResolver"/&amp;gt;.&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// This is dealing strictly with root components, while the &amp;lt;seealso cref="ISubDependencyResolver"/&amp;gt; is dealing with&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// dependent components.&lt;/span&gt;
&lt;span style="color: #808080"&gt;/// &amp;lt;/remarks&amp;gt;&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IHandlerSelector
{
    &lt;span style="color: #808080"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// Whatever the selector has an opinion about resolving a component with the &lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// specified service and key.&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;param name="key"&amp;gt;The service key - can be null&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;param name="service"&amp;gt;The service interface that we want to resolve&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; HasOpinionAbout(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; key, Type service);

    &lt;span style="color: #808080"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// Select the appropriate handler from the list of defined handlers.&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// The returned handler should be a member from the &amp;lt;paramref name="handlers"/&amp;gt; array.&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;param name="key"&amp;gt;The service key - can be null&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;param name="service"&amp;gt;The service interface that we want to resolve&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;param name="handlers"&amp;gt;The defined handlers&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color: #808080"&gt;/// &amp;lt;returns&amp;gt;The selected handler, or null&amp;lt;/returns&amp;gt;&lt;/span&gt;
    IHandler SelectHandler(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; key, Type service, IHandler[] handlers);
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;And registering it in the container is simply:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;container.Kernel.AddHandlerSelector(selector);&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;A handler selector is asked if it wants to express an opinion on a particular component resolution, based on key (optional) and type. Assuming we say yes, we are called to select the appropriate handler from all the registered handlers that &lt;em&gt;can&lt;/em&gt; satisfy that request.&lt;/p&gt;
&lt;p&gt;Let us say that we want to recover from the database being down by serving an implementation that reads from only the cache, we can implement it thusly:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; DataAccessHandlerSelector : IHandlerSelector
{
	&lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; databaseIsDown = &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; DataAccessHandlerSelector()
	{
		DatabaseMonitor.OnChangedState += 
			state =&amp;gt; databaseIsDown = state == DatabaseState.Down;
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; HasOpinionAbout(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; key, Type service)
	{
		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; databaseIsDown &amp;amp;&amp;amp; service == &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IRepository);
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IHandler SelectHandler(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; key, Type service, IHandler[] handlers)
	{
		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; handlers.Where(x=&amp;gt;x.ComponentModel.Implementation == &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(CacheOnlyRepository)).First();
   	}

}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Now we automatically replace, based on our own logic and the current context what type of component the container should resolve.&lt;/p&gt;
&lt;p&gt;I am giving the example of detecting infrastructure change, but as important, and as interesting, is the ability to easily use this in order to select services in a multi tenant environment. We can use this approach to perform service overrides all over the place in a way that is natural, easy and extremely powerful.&lt;/p&gt;
&lt;p&gt;Have fun...&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10544.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/10/05/windsor-ihandlerselector.aspx</guid>
            <pubDate>Sun, 05 Oct 2008 13:33:59 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/10/05/windsor-ihandlerselector.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10544.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Components, Implementations and Contextual Decisions</title>
            <link>http://ayende.com/Blog/archive/2008/10/05/components-implementations-and-contextual-decisions.aspx</link>
            <description>&lt;p&gt;I am a big believer in using context in order to drive a system. What do I mean by that? &lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Note, I am going to talk about the problem in general, and its solution implementation using Windsor. The example is fictitious and is here to represent the problem in a way that allow me to talk about it in isolation, it doesn't necessarily represent good design.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;It seems like just about all the applications that I had to deal with recently had to have the notion of system variability. Now, let us make it clear. System variability is a fancy name for the if statement. The problem with the if statement is that when you have a lot of them, it gets pretty tricky to understand what is going on with the system. That is why a common refactoring is replace conditional with polymorphism. &lt;/p&gt; &lt;p&gt;What I am usually talking about is "when we are in this condition, we should do X, otherwise, we should do Y". Let us take the simple idea of a warehouse service. If we are making a call from the web site, it is okay to return data that may not be accurate to the second. If we are calling from the fulfillment service, we need accurate, up to date results. A simple way of handling this is:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; ItemIsPhysicallyOnTheShelve(Guid id)
{
	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(Origin == Originators.Website)&lt;span style="color: #008000"&gt;//can use caching&lt;/span&gt;
	{
		var result = Cache.Get&amp;lt;&lt;span style="color: #0000ff"&gt;bool&lt;/span&gt;?&amp;gt;("&lt;span style="color: #8b0000"&gt;item-on-shelve-&lt;/span&gt;" + id)
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(result.HasValue)
			&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; result.Value;
	}
	&lt;span style="color: #008000"&gt;// actual work and putting in cache&lt;/span&gt;
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;A more interesting example might be different business rules for making order authorization, based on whatever we have a strategic customer or not. In both cases, we have some context for the operation that modify the way that we deal with this operation.&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsValid(Order order, ValidationSummary summary)
{
	IRule[] rules = CurrentCustomer.IsStrategic ?
		strategicCutomerRules : normalCustomerRules;
	&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;(IRule rule &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; rules)
	{
		rule.Validate(order, summary);
	}
	&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; summary.HasErrors;
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;One way of dealing with that is as you see in the code samples, get the state from somewhere and make decisions based on that. Another, more advance option is to create:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;IWarehouseService&lt;/li&gt;
&lt;li&gt;DefaultWarehouseService&lt;/li&gt;
&lt;li&gt;CachingWarehouseServiceDecorator&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;And because decorators are really annoying, we will use AOP to deal with it by creating a caching interceptor.&lt;/p&gt;
&lt;p&gt;Now the issue is mere configuration, I can deal with that by flipping bits in the container configuration. The second example can be solved by creating two components with different rule sets and using that. The problem is that this remove the coding issues, but it creates a more subtle and much harder to deal with problems.&lt;/p&gt;
&lt;p&gt;If I rely on the container configuration alone, I suddenly have logic there. &lt;em&gt;Important business logic&lt;/em&gt;. That is not a good idea, I think. Especially since this means that at some point my code has to make an explicit decision about what component to use, and that breaks the infrastructure independence rule.&lt;/p&gt;
&lt;p&gt;What this boil down to is that now I have to manage a lot of the complexity in the application using the container configuration and tie the working of the system into it. That works if the number of variables that I have to juggle is small, but if I have a lot of axes (plural: axis)  that are orthogonal to one another, it is getting complex very fast.&lt;/p&gt;
&lt;p&gt;My solution for that problem is to define a service and its context as a cohesive unit. That is, the concept of a service contains its interface, all of its implementations and the business logic required to select which implementation (and configuration) to choose for a given context.&lt;/p&gt;
&lt;p&gt;In the warehouse example above, what we will have is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;IWarehouseService&lt;/li&gt;
&lt;li&gt;DefainltWarehouseService&lt;/li&gt;
&lt;li&gt;WarehouseCachingInterceptor&lt;/li&gt;
&lt;li&gt;&lt;em&gt;WarehouseInterceptorsSelector&lt;/em&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Now all of those are part of the same service. The last one is where we isolate the actual decision about what type of implementation we should get. In this case, we use Windsor's IModelInterecptorsSelector to add additional, context bound, interceptors to the service.&lt;/p&gt;
&lt;p&gt;But that is just from the interceptors side, what about the selection of the appropriate rules? We can handle that using ISubDependencyResolver, where we can decide how we want to filter the rules that goes into IWarehouseService based on the context. For that matter, we might have a completely different warehouse implementations, VirtualWarehouseService and PhysicalWarehouseService. And we need to select between them based on some business criteria. We handle that using IHanlderSelector, that make the decision which component to create.&lt;/p&gt;
&lt;p&gt;Again, IHandlerSelector, IModelInterceptorsSelector and ISubDependencyResolvers are all implementations of Windsor extensibility mechanisms (my next two posts will cover them in details) that allows us to make it aware of the context that we have in the application.&lt;/p&gt;
&lt;p&gt;The purpose of the explicit notion of context is to allow us to deal with the variability in the application in an explicit manner. And that, in turn means that we get much better separation of concerns.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10543.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/10/05/components-implementations-and-contextual-decisions.aspx</guid>
            <pubDate>Sun, 05 Oct 2008 13:10:02 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/10/05/components-implementations-and-contextual-decisions.aspx#feedback</comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10543.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>