<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Krzysztof Kozmic : IoC</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx</link><description>Tags: IoC</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>IoC container solves a problem you might not have but it's a nice problem to have</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/2012/10/23/ioc-container-solves-a-problem-you-might-not-have-but-it-s-a-nice-problem-to-have.aspx</link><pubDate>Mon, 22 Oct 2012 22:03:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:70408</guid><dc:creator>Krzysztof Koźmic</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/krzysztof_kozmic/rsscomments.aspx?PostID=70408</wfw:commentRss><comments>http://devlicio.us/blogs/krzysztof_kozmic/archive/2012/10/23/ioc-container-solves-a-problem-you-might-not-have-but-it-s-a-nice-problem-to-have.aspx#comments</comments><description>&lt;h3&gt;On frameworks and libraries&lt;/h3&gt;
&lt;p&gt;
A logging framework helps you log what&amp;#39;s happening in your application. A UI framework helps you render and animate UIs to the user. A communication library helps connecting parts of a distributed system. All of these tasks and concepts are pretty easy to understand. They are quite down to earth, and therefore, at a high level at least, easy to explain. They produce &lt;em&gt;tangible&lt;/em&gt;, &lt;em&gt;additive&amp;nbsp;&lt;/em&gt;difference in your application. Also the code of your application changes in order to use those frameworks and libraries. You add a logging framework, you write some code against its API and you end up with a file on your disk, or some text in your console. You add a communication library, inherit some base classes, implement some interfaces to designate communication endpoints of your application and you can see two processes exchanging information. I hope you get the picture and I don&amp;#39;t need do go on describing the tangible, additive and visible results of using a UI framework.
&lt;/p&gt;
&lt;h3&gt;What about IoC container?&lt;/h3&gt;
&lt;p&gt;
So what about inversion of control containers? There&amp;#39;s a lot of confusion around what they do, and why you should use one at all. Every now and then I meet a developer, who says they read all the definitions, introductions and basic examples, and they still don&amp;#39;t get why would they use a container. Other times I&amp;#39;m having discussions that can be summarised as (slightly exaggerated for dramatic effect):
&lt;/p&gt;
&lt;blockquote&gt;I got one of the IoC containers, put it in my application, and then all hell broke loose. We got memory leaks, things weren&amp;#39;t appearing where they should, users were seing other users&amp;#39; data, we had to recycle the pool every hour because we were running out of database connections, and the app got very slow. Ah and also it destroyed maintainability of our code, because we never knew when something is safe to refactor, since we never knew where and who would try to get them from the container.&lt;/blockquote&gt;
&lt;p&gt;
Let&amp;#39;s ignore the details for now and concentrate on the wider sentiment.
&lt;/p&gt;
&lt;h3&gt;So? Should I use it or not?&lt;/h3&gt;
&lt;p&gt;
The sentiment is one of confusion,&amp;nbsp;scepticism&amp;nbsp;and frustration. Was it a good idea to use the container? Should we have not used it? Would &lt;em&gt;I&lt;/em&gt; use it if I was leading the team? Truth is, those aren&amp;#39;t necessarily the right questions to ask. You can&amp;#39;t answer them in a vacuum. Should I use my phone to twitter when I&amp;#39;m on the bus? While the answer might be &amp;quot;sure, why not&amp;quot; if you&amp;#39;re a passenger, it would be unequivocally &amp;quot;don&amp;#39;t even think about reaching for your phone&amp;quot; if you&amp;#39;re the driver. I have seen applications where &amp;nbsp;introducing a container immediately, would only worsen things. I also haven&amp;#39;t worked with any .NET application where, if architected the way &lt;em&gt;I&amp;nbsp;&lt;/em&gt;like to do things, the container wouldn&amp;#39;t be beneficial in the long term. It all boils down to the fact that container solves certain problems that arise if and when you build your applications a certain way, and unless you do, you&amp;#39;re not going to see the benefits it brings, simply because the problems container solves will not be the main problems you&amp;#39;ll be facing, or will not appear in your application at all.
&lt;/p&gt;
&lt;h3&gt;What sort of architecture are we talking about?&lt;/h3&gt;
&lt;p&gt;
Container has certain requirements&amp;nbsp;in order to work smoothly. &amp;nbsp;First and foremost it requires structure and order. At a mechanical level, container takes over certain level of control over low level, mundane details of your &amp;nbsp;application&amp;#39;s infrastructure. It is however still a tool, and unless the environment where it operates is clean, with well defined limited number of abstractions it will struggle, and so will you, trying to get it to work.&lt;/p&gt;
&lt;p&gt;There&amp;#39;s a lot of content in the sentence above so let&amp;#39;s dig a bit deeper.&lt;/p&gt;
&lt;p&gt;The assumption containers are making is, that you do have abstractions. Getting a little bit ahead of ourselves, one of the main reason for using the container is to maintain lose coupling in our code. The way we achieve this is by constructing our types in such a way that they do not depend directly on other concrete classes. Regardless if it&amp;#39;s a high level type, or low level type, instead of depending on one another directly, container expects they will depend on abstractions.&lt;/p&gt;
&lt;p&gt;Another, related (pre)assumption is you will &amp;nbsp;build your classes to expose abstractions. And not just in any way, but keep them small and crisp (structure and order, remember). To maintain loose coupling, the abstractions are expected to provide just a single small task, and most classes will provide just a single abstraction.&lt;/p&gt;
&lt;p&gt;It is not uncommon for a concrete class to provide more than one abstraction (for example to bridge two parts of the system) but in that case the container assumes you will separate these two responsibilities into two abstractions.&lt;/p&gt;
&lt;p&gt;Large number of abstractions in your system will be reusable, that is a single abstraction will be exposed by more than one type. In cases like that, the container assumes all the implementations will obey the contract of the abstraction, that is they will all behave in a way that will not be surprising to anyone consuming the abstraction without knowing what concrete implementation is behind it.
&lt;/p&gt;
&lt;p&gt;The point above is especially important to provide class level extensibility. Certain types in your system will depend on a collection of dependencies, and you should not have to go back and change them, whenever you add a new type ending up in that collection. The assumption is, the code using the abstractions, as well as the abstractions themselves are built in such a way, that the type can be extended by swapping (or adding new) dependencies it has, without the type itself having to change.
&lt;/p&gt;
&lt;h3&gt;That&amp;#39;s a lot of assumptions, isn&amp;#39;t it?&lt;/h3&gt;
&lt;p&gt;
You may say the container makes quite a number of assumptions (or requirements!) about how your application is structured. You wouldn&amp;#39;t be wrong.
&lt;/p&gt;
&lt;p&gt;However if you look up and read the previous section again, forgetting that we are talking about container, does this feel like an architecture you should be striving for anyway? Does it feel like good, maintainable object oriented architecture? If it does, then that&amp;#39;s good, because that&amp;#39;s intentional.
&lt;/p&gt;
&lt;p&gt;A container doesn&amp;#39;t put any artificial or arbitrary cumbersome constraints onto how you should build your application. It doesn&amp;#39;t ask you to compromise any good practices aiming at maximising testability, maintainability and readability of your code and architecture. On the contrary, it&amp;nbsp;encourages&amp;nbsp;and rewards you for that.
&lt;/p&gt;
&lt;h3&gt;It&amp;#39;s the inversion!&lt;/h3&gt;
&lt;p&gt;
Finally we&amp;#39;re at a point where we can talk about some answers. The container makes a lot of assumptions about how your code is structured for two reasons.
&lt;/p&gt;
&lt;p&gt;That makes the container different from logging framework or communication framework. None of them cares about how your code is structured. As long as you implement correct interfaces or inherit correct base classes, and are able to call the right API they don&amp;#39;t care.
&lt;/p&gt;
&lt;p&gt;Inversion of control containers are&amp;nbsp;&lt;em&gt;fundamentally&lt;/em&gt; different. They do not require your code to implement or inherit anything and there&amp;#39;s no API to call (outside of single place called&amp;nbsp;&lt;em&gt;composition root&lt;/em&gt;). Just by looking at code of your application, you can&amp;#39;t tell if there is a container involved or not.
&lt;/p&gt;
&lt;p&gt;That&amp;#39;s the&amp;nbsp;&lt;em&gt;inversion of control&lt;/em&gt; working. This is what confuses so many people when they first start using containers. The difference is so fundamental from most other frameworks and libraries that it may be hard to comprehend and make the switch.&lt;/p&gt;
&lt;p&gt;
Inversion of control confuses people and makes it harder to see the value a container brings because the value is not tangible and it is not additive.
&lt;/p&gt;
&lt;p&gt;Contrast that with the examples from the first section. With logging framework you can point a finger at a piece of code and say &lt;em&gt;&amp;quot;There, here&amp;#39;s the logging framework doing its thing&amp;quot;&lt;/em&gt;. You can point a finger at your screen, entry in Windows event log, or a file on disk and say &lt;em&gt;&amp;quot;There, here&amp;#39;s the result of logging framework doing its thing&amp;quot;&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;Inversion of control container is just &amp;quot;out there&amp;quot;. You can&amp;#39;t see the work that it&amp;#39;s doing by inspecting your code. That ephemeralness and intangibility is what confuses many new users and makes it harder for them to see the value the container brings to the table.
&lt;/p&gt;
&lt;p&gt;To make things even slightly more confusing for newcomers, when you&amp;#39;re &lt;em&gt;using&lt;/em&gt; a container you don&amp;#39;t end up writing code to&amp;nbsp;&lt;em&gt;use&lt;/em&gt; the container (again, expect for a little bit of code to wire it up). You end up writing&amp;nbsp;&lt;em&gt;less&lt;/em&gt; code as a result of using the container, not more, which again is harder to measure and appreciate than code you can clearly see as a result of using other kinds of frameworks.
&lt;/p&gt;
&lt;h3&gt;What isn&amp;#39;t there...&lt;/h3&gt;
&lt;p&gt;
The second reason why container makes all of these assumptions about your code is very simple. Unless your architecture follows these rules, you simply will not have the problems that the containers are built to solve. And even if you do, they will be far from being your main concern.
&lt;/p&gt;
&lt;p&gt;Building your application according to the assumptions that the container is built upon (I hope by now you figured out that they are just &lt;a title="SOLID principles of object oriented design" href="http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)" target="_blank"&gt;SOLID principles&lt;/a&gt; of good object oriented C# code) your application will be composed of types exposing certain characteristics.
&lt;/p&gt;
&lt;p&gt;Following Single Responsibility Principle will lead you down the road of having plenty of small classes.
&lt;/p&gt;
&lt;p&gt;Open Close Principle will lead you down the way of encapsulating the core logic of each type in it, and then delegating all other work to its dependencies, causing most of those small classes to have multiple dependencies.
&lt;/p&gt;
&lt;p&gt;Interface Segregation Principle will cause you to abstract most of those big number of classes by exposing an interface or an abstract class, further multiplying the number of types in your application.
&lt;/p&gt;
&lt;p&gt;Liskov Substitution Principle will force you to clearly shape your abstractions so that they can be used in a uniform way.
&lt;/p&gt;
&lt;p&gt;Finally, if you follow Dependency Inversion Principle your types will not only expose abstractions themselves, but also depend on abstractions rather than concrete types.
&lt;/p&gt;
&lt;p&gt;Overall you will have a big number of small classes working with a number of abstractions. Each of those classes will be concentrated on its own job and largely ambivalent (and abstracted from) about who is using it, and details of who it is using.
&lt;/p&gt;
&lt;p&gt;This leaves you with a problem of putting together a group of those small types (let&amp;#39;s call them &lt;a href="http://kozmic.pl/2012/07/03/ioc-concepts-component/" target="_blank"&gt;Components&lt;/a&gt;) in order to support a full, real life, end to end scenario. Not only will you have to put them together, but also, maintain and manage them throughout the lifetime of your application.
&lt;/p&gt;
&lt;p&gt;Each component will be slightly different. For some of them you will want to have just a single instance, that is reused everywhere, for others, you&amp;#39;ll want to provide a new instance each time one is needed, or may be reuse instances within the scope of a web request, or any other arbitrary scope.
&lt;/p&gt;
&lt;p&gt;Some of them will have decommission logic, like Dispose method. Some of them will have ambient steps associated with their lifecycle, like &amp;quot;subscribe me to certain events in event aggregator when I&amp;#39;m created and them unsubscribe me when I&amp;#39;m about to be destroyed&amp;quot;.
&lt;/p&gt;
&lt;p&gt;Also thanks to the power of abstractions and guarantees put in place by following Liskov Substitution Principle you&amp;#39;ll want to compose some of the components using a further set of design patterns. You&amp;#39;ll want to create Decorators, Chains of Responsibility, Composites etc.
&lt;/p&gt;
&lt;p&gt;Not to mention the fact that in addition to depending on abstractions exposed by other components (let&amp;#39;s call them &lt;a href="http://kozmic.pl/2012/07/02/ioc-concepts-service/" target="_blank"&gt;Services&lt;/a&gt;) your components may also have some configuration, or other &amp;quot;primitive&amp;quot; dependencies (like connection strings).
&lt;/p&gt;
&lt;p&gt;I hope you can see that the complexity of it all can quickly grow immensely. If you want to maintain loose coupling between your components, you will have to write a lot of code to put them together and maintain all the aforementioned qualities. The code to do that can quickly grow as well, both in size and complexity.
&lt;/p&gt;
&lt;h3&gt;Container to save the day&lt;/h3&gt;
&lt;p&gt;
Inversion of control containers exist to replace that code. I know it took me a while to get here, and I did it in a very roundabout way, but there you have it - that&amp;#39;s what IoC containers do - they help you stay sane, and reduce friction of development when adhering to good Object Oriented Design principles in language like C#.
&lt;/p&gt;
&lt;p&gt;I know it&amp;#39;s not a sexy and concise definition, and some of you will probably need to re-read this post for it to &amp;quot;click&amp;quot;. Don&amp;#39;t worry. It didn&amp;#39;t click for me at first either. The value and goal of IoC container is abstract and intangible, and the nomenclature doesn&amp;#39;t help either.
&lt;/p&gt;
&lt;h3&gt;The i-word&lt;/h3&gt;
&lt;p&gt;
Some people feel that a lot of misconceptions and misunderstanding about containers comes from their very name - inversion of control container. If you just hear someone say those words, and leave it to your imagination, you&amp;#39;ll most likely see... blank. Nothing. It&amp;#39;s so abstract, that unless you know what it means, it&amp;#39;s pretty much meaningless.
&lt;/p&gt;
&lt;p&gt;To alleviate that some people started referring to containers as Dependency Injection container. At first this sounds like a better option. We pretty much understand what dependency in context of relation between two objects is. Injection is also something that you can have a lot associations with, software related or otherwise. Sounds simpler, more down-to-earth, doesn&amp;#39;t it?
&lt;/p&gt;
&lt;p&gt;It does &lt;em&gt;sound&lt;/em&gt; like a better option, but in my experience it ends being a medicine that&amp;#39;s worse than the disease. While it doesn&amp;#39;t leave you with blank stare, it projects a wrong image of what a container does. By using the word &lt;em&gt;injection&lt;/em&gt; it emphasises the process of constructing the graph of dependencies, ignoring everything else the container does, especially the fact that it manages the whole lifetime of the object, not just its construction. As a result of that, in my experience, people who think about containers in terms of DI end up misusing the container and facing problems that arise from that.
&lt;/p&gt;
&lt;p&gt;That&amp;#39;s why, even though I&amp;#39;m quite aware Inversion of Control Container is a poor name, that doesn&amp;#39;t help much in understanding those tools, at least it doesn&amp;#39;t end up causing people to misunderstand them.
&lt;/p&gt;
&lt;h3&gt;In closing&lt;/h3&gt;
&lt;p&gt;
So there you have it. I admire your persistence if you made it that far. I hope this post will help people undersand what a container does, and why putting it in an application that&amp;#39;s not architected for it, may end up causing more problems than benefits.
&lt;/p&gt;
&lt;p&gt;I hope it made it clearer what having a container in a well designed application brings to the table. In fact I only spoke about the most basic and general usages scenario that every container supports. Beyond that particular containers have additional, sometimes very powerful features that will add additional benefit to your development process.
&lt;/p&gt;
&lt;p&gt;So even if you feel you don&amp;#39;t have a need for a container right now, having the problems that the container is meant to solve is often a sign of a good, object oriented architecture, and those are good problems to have.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=70408" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Design/default.aspx">Design</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx">IoC</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/architecture/default.aspx">architecture</category></item><item><title>Getting closer… Castle Windsor 3 RC 1</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/2011/11/20/getting-closer-castle-windsor-3-rc-1.aspx</link><pubDate>Sun, 20 Nov 2011 12:10:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:68406</guid><dc:creator>Krzysztof Koźmic</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/krzysztof_kozmic/rsscomments.aspx?PostID=68406</wfw:commentRss><comments>http://devlicio.us/blogs/krzysztof_kozmic/archive/2011/11/20/getting-closer-castle-windsor-3-rc-1.aspx#comments</comments><description>&lt;p&gt;Few weeks later than originally expected but here it is &amp;ndash; Castle Windsor 3.0 (along with its facilities and Castle.Core) achieved release candidate status.&lt;/p&gt;
&lt;p&gt;There is one major new feature in this release: registration API gained ability to specify properties to ignore/require. There are some scenarios where that&amp;rsquo;s useful, for example where integrating with some 3rd party framework that forces you to inherit from a base class which exposes its dependencies as properties. Creating pass-through constructors for each inherited class can be mundane. In those cases you can simply mark those base class properties as required, in which case Windsor will not allow them to be resolved unless all base property dependencies are satisfied. PropertyFilter enum supports several other most common scenarios, and for advanced cases there&amp;rsquo;s an overload that gives you more control.&lt;/p&gt;
&lt;div style="text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;border:1px solid silver;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;Container.Register(&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    Classes.FromThisAssembly()&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        .BasedOn&amp;lt;ICommon&amp;gt;()&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        .Configure(c =&amp;gt; c.Properties(PropertyFilter.RequireBase)));&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;To address performance hit at startup Windsor no longer enables &lt;a href="http://docs.castleproject.org/Windsor.Performance-Counters.ashx" target="_blank"&gt;performance counters&lt;/a&gt; by default. Now, you have to do it explicitly:&lt;/p&gt;
&lt;div style="text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;border:1px solid silver;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;var container = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; WindsorContainer();&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;var diagnostic = LifecycledComponentsReleasePolicy.GetTrackedComponentsDiagnostic(container.Kernel);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;var counter = LifecycledComponentsReleasePolicy.GetTrackedComponentsPerformanceCounter(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; PerformanceMetricsFactory());&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;container.Kernel.ReleasePolicy = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; LifecycledComponentsReleasePolicy(diagnostic, counter);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Full changelog is included in the packages. Please, if possible, take the time to upgrade to this version and if you find any issues report them so that the final release is rock solid. If no major issues are found, the final release will be published in two weeks.&lt;/p&gt;
&lt;p&gt;The binaries are available on Nuget right now, and soon &lt;a href="http://castleproject.org/castle/projects.html" target="_blank"&gt;on our website&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=68406" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Castle/default.aspx">Castle</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx">IoC</category></item><item><title>Windsor 3 beta 1 is released (along with other goodies)</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/2011/08/15/windsor-3-beta-1-is-released-along-with-other-goodies.aspx</link><pubDate>Mon, 15 Aug 2011 10:49:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:68106</guid><dc:creator>Krzysztof Koźmic</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/krzysztof_kozmic/rsscomments.aspx?PostID=68106</wfw:commentRss><comments>http://devlicio.us/blogs/krzysztof_kozmic/archive/2011/08/15/windsor-3-beta-1-is-released-along-with-other-goodies.aspx#comments</comments><description>&lt;p&gt;&lt;img src="http://castleproject.org/images/wc_logo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The title says it all. If you can&amp;rsquo;t wait grab the binaries &lt;a href="http://sourceforge.net/projects/castleproject/files/Windsor/3.0/Castle.Windsor.3.0.0.beta1.zip/download%20" target="_blank"&gt;here (Windsor with Castle Core and facilities)&lt;/a&gt; and &lt;a href="http://sourceforge.net/projects/castleproject/files/Core/3.0/Castle.Core.3.0.0.beta1.zip/download" target="_blank"&gt;here (just Core)&lt;/a&gt;. Nuget package will hopefully follow soon.&lt;/p&gt;
&lt;h3&gt;What&amp;rsquo;s in it?&lt;/h3&gt;
&lt;p&gt;This is a major release and there&amp;rsquo;s quite a lot of new features, as well as some significant changes under the covers. &lt;a href="http://docs.castleproject.org/Windsor.Whats-New-In-Windsor-3.ashx" target="_blank"&gt;There&amp;rsquo;s a high level overview of highlights for the release in the wiki&lt;/a&gt;, so please have a look there. More detailed changelog, and list of breaking changes is available as part of the package.&lt;/p&gt;
&lt;p&gt;Remember it&amp;rsquo;s a beta release. Documentation and samples are still being updated and some features are not yet implemented or not fully baked. However it is very important that you don&amp;#39;t wait for the final release to upgrade, so that we can find and fix all the issues before final release. I&amp;#39;m not asking you to use it in production, but I&amp;#39;d really appreciate if you&amp;#39;d take the time to branch your apps, try to upgrade, run your unit tests, play with new version, and report any issues you may find. And ask your colleagues, friends and random people on the street to do the same!&lt;/p&gt;
&lt;p&gt;And if you find any issues, &lt;a href="http://issues.castleproject.org" target="_blank"&gt;do report them&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;   &lt;br /&gt;Have fun and happy coding,     &lt;br /&gt;I hope you like it&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=68106" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Castle/default.aspx">Castle</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/.NET/default.aspx">.NET</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx">IoC</category></item><item><title>Point point one release for Windsor and Castle.Core</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/09/21/point-point-one-release-for-windsor-and-castle-core.aspx</link><pubDate>Tue, 21 Sep 2010 11:23:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:62343</guid><dc:creator>Krzysztof Koźmic</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/krzysztof_kozmic/rsscomments.aspx?PostID=62343</wfw:commentRss><comments>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/09/21/point-point-one-release-for-windsor-and-castle-core.aspx#comments</comments><description>&lt;p&gt;Exactly one month after release 2.5.0 we released first minor update to this release for Windsor and Castle.Core. It contains some minor improvements and fixes for issues that were identified after the 2.5 release. Complete changelog for Windsor contains 20 items, and 9 for Castle.Core, including single breaking change, which is deletion of web logger so that Castle.Core has no dependency on System.Web and we no longer need to provide two versions for .NET 4 (one with Client profile support, and one with full profile).&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s one relatively major feature in the release that you should be aware of &amp;ndash; &lt;a href="http://kozmic.pl/archive/2010/07/02/new-castle-windsor-feature-ndash-debugger-diagnostics.aspx"&gt;debugger views in Windsor&lt;/a&gt; were extended with detection of potential lifestyle mismatches. When you step over your configured container in the debugger the following item may now appear in the list:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/mismatches_5F00_2.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;margin:5px 0px;display:inline;border-top:0px;border-right:0px;" title="mismatches" alt="mismatches" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/mismatches_5F00_thumb.png" border="0" height="213" width="634" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;This lets you detect situations where you have a singleton component that depends on a transient or per-web-request component, which is usually a bug (although there are rare cases when singleton depending on transient is a valid solution).&lt;/p&gt;
&lt;p&gt;In addition to the list, the Description will give you&amp;hellip; well &amp;ndash; description of the situation:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/mismatches_5F00_text_5F00_2.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;margin:5px 0px;display:inline;border-top:0px;border-right:0px;" title="mismatches_text" alt="mismatches_text" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/mismatches_5F00_text_5F00_thumb.png" border="0" height="213" width="634" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Notice that this is only a helper and due to dynamic nature of Windsor it can only detect a statically known subset of possible dependencies. As such you should not assume that if the feature does not detect any issues there aren&amp;rsquo;t any.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You can download the projects from Sourceforge as usual:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://sourceforge.net/projects/castleproject/files/Windsor/2.5/Castle.Windsor.2.5.1.zip/download"&gt;Windsor, Logging Facility, Synchronize Facility and &lt;b&gt;Core&lt;/b&gt;&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://sourceforge.net/projects/castleproject/files/Core/2.5/Castle.Core.2.5.1.zip/download"&gt;Core (incl. DynamicProxy and Dictionary Adapter), Logging Services&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=62343" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Castle/default.aspx">Castle</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx">IoC</category></item><item><title>Must I release everything when using Windsor?</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/08/27/must-i-release-everything-when-using-windsor.aspx</link><pubDate>Fri, 27 Aug 2010 01:52:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:61703</guid><dc:creator>Krzysztof Koźmic</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/krzysztof_kozmic/rsscomments.aspx?PostID=61703</wfw:commentRss><comments>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/08/27/must-i-release-everything-when-using-windsor.aspx#comments</comments><description>&lt;p&gt;This is a follow up to &lt;a href="http://kozmic.pl/archive/2010/08/19/must-windsor-track-my-components.aspx"&gt;my previous post&lt;/a&gt;. If you haven’t yet – go and read that one first. I’ll wait.&lt;/p&gt;  &lt;h3&gt;So where were we? Aha…&lt;/h3&gt;  &lt;p&gt;In the last post I said, that Windsor (any container in general) creates objects for you, hence it owns them, ergo its responsibility is to properly end their lifetime when they’re no longer needed.&lt;/p&gt;  &lt;p&gt;Since as I mentioned in my previous post, Windsor will track your component, it’s a common misconception held by users that in order to properly release all components they must call Release method on the container.&lt;/p&gt;  &lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;container.Release(myComponent);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;How will Windsor know I no longer need an object?&lt;/h3&gt;

&lt;p&gt;That’s the most important part of this post really, so pay attention.&lt;/p&gt;

&lt;p&gt;In Windsor (every container in general) every component has a lifestyle associated with it. In short lifestyle defines the context in which an instance should be reused. If you want to have just single instance of a component in the context of the container, you give the object a singleton lifestyle (which is the default in Windsor). If you want to reuse your object in the context of a web request, you give it a lifestyle of a per-web-request and so on. Now the word context (though overloaded) is important here.&lt;/p&gt;

&lt;h3&gt;&lt;/h3&gt;

&lt;h3&gt;When the context ends?&lt;/h3&gt;

&lt;p&gt;While some contexts, like web request have a well defined ends that Windsor can detect, that’s not the case for every of them. This brings us to the Release method.&lt;/p&gt;

&lt;p&gt;Windsor has a Release method that you use to explicitly tell it &lt;i&gt;“hey, I’m not gonna use this this object anymore.” &lt;/i&gt;Although the method is named very imperatively, which would suggest it takes immediate action, it’s not often the case. Quite a lot of time may pass between you call the method and Windsor will get the object destroyed. When the object gets really destroyed is up to its lifestyle manager, and they act differently.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;b&gt;Singleton&lt;/b&gt; will ignore your call to &lt;i&gt;Release&lt;/i&gt; because the instance is global in the scope of the container that created it, and the fact that you don’t need the object in one place does not mean you won’t need it moments later somewhere else. The scope of the singleton lifestyle has a well defined end. Windsor will destroy the singletons when the container itself is being disposed. This also means that you don’t have to Release your singletons – disposing of the container will take care of that. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;Per-web-request&lt;/b&gt; will ignore your call to Release for similar reasons – the object is global in the scope of a web-request. Web request also has a well defined end, so it will wait with destroying the object till the end of the web request and then do all the work necessary. This also means that you don’t have to release your per-web-request components – Windsor can detect end of a web request and release the per-web-request components without and intervention from your side. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;Per-thread&lt;/b&gt; is like singleton in the scope of a thread. Releasing Per-thread components does nothing, they will be released when the container is disposed. This means that in this case as well you don’t have to do anything explicitly about them (except for remembering to dispose the container obviously) .. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;Pooled&lt;/b&gt; components are different. They don’t really have a well defined &lt;i&gt;“end”&lt;/i&gt;&amp;#160; so you need to return a component to the pool explicitly (that is pass them to container’s Release method), and Windsor (depending on the component and configuration of the pool) may either return the object to the pool, recycle it, or destroy and let it go. Thing is - it matters, and it usually makes sense to return the object to the pool, as soon as possible (think connection pooling in ADO.NET). &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;Transient&lt;/b&gt; components are similar to pooled, because there’s no well known end to transient component’s lifetime, and Windsor will not know if you still want to use a component or not, unless you explicitly tell it (by calling Release). Since transient components are by definition non-shared Windsor will immediately destroy the component when you Release it. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;And then it gets interesting&lt;/h3&gt;

&lt;p&gt;If you’re now scratching your head thinking “Does Windsor really puts all the burden of releasing stuff on me?” don’t despair. The reality is – &lt;b&gt;you never have to call Release explicitly in your application code&lt;/b&gt;. Here’s why.&lt;/p&gt;

&lt;h3&gt;You never have to call Release…&lt;/h3&gt;

&lt;p&gt;Releasing a component releases entire graph. As outlined in previous section, Windsor can detect end of scope for components with most lifetimes. In that case, if you have a per-web-request ShoppingCard component that depends on transient PaymentCalculationService and singleton AuditWriter, when the request ends Windsor will release the shopping card along with both of its dependencies. Since auditwriter is a singleton releasing it will not have any effect (which is what we want) but the transient payment calculator will be releases without you having to do anything explicitly.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;You obviously &lt;a href="http://stw.castleproject.org/Windsor.Three-Calls-Pattern.ashx"&gt;need to structure your application properly&lt;/a&gt; for this to work. If you’re &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;abusing the container as service locator&lt;/a&gt; than you’re cutting a branch you’re sitting on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Explicitly…&lt;/h3&gt;

&lt;p&gt;This also &lt;a href="http://stw.castleproject.org/Windsor.Typed-Factory-Facility.ashx"&gt;works with typed factories&lt;/a&gt; – when a factory is released, all the components that you pulled via the factory will be released as well. However you need to be cautious here – if you’re expecting to be pulling many components during factory’s lifetime (especially if the factory is a singleton) you may end up needlessly holding on to the components for much too long, causing a memory leak.&lt;/p&gt;

&lt;p&gt;Imagine you’re writing a web browser, and you have a TabFactory, that creates tabs to display in your browser. The factory would be a singleton in your application, but the tabs it produces would be transient – user can open many tabs, then close them, then open some more, and close them as well. Being a web savvy person you probably know firsthand how quickly memory consumption in a web browser can go up so you certainly don’t want to wait until you dispose of your container to release the factory and release all the tabs it ever created, even the ones that were closed days ago.&lt;/p&gt;

&lt;p&gt;More likely you’d want to tell Windsor to release your transient tabs as soon as the user closes it. Easy – just make sure your TabFactory &lt;a href="http://stw.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx#Releasing_methods_12"&gt;has a releasing method&lt;/a&gt; that you call when the tab is closed. The important piece here is that you’d be calling a method on a factory interface that is part of your application code, not method on the container (well – ultimately that will be the result, but you don’t do this explicitly).&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;As Mark pointed out in the comment there are certain low level components that act as factories for the root object in our application (IControllerFactory in MVC, IInstanceProvider in WCF etc). If you’re not using typed factories to provide these service and implement them manually pulling stuff from the container, than the other rule (discussed below) applies - release anything you explicitly resolve&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;In your application code&lt;/h3&gt;

&lt;p&gt;There are places where you do need to call Release explicitly – in code that extends or modifies the container. For example if you’re using a factory method to create a component by resolving another component first, you should release the other component.&lt;/p&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;container.Register(&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;   Component.For&amp;lt;ITaxCalculator&amp;gt;()&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;      .UsingFactoryMethod(&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;      k =&amp;gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;      {&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;         var country = k.Resolve&amp;lt;ICountry&amp;gt;(user.CountryCode);&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;         var taxCalculator = country.GetTaxCalculator();&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;         k.ReleaseComponent(country);&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;         &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; taxCalculator;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;      }&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;      )&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;   );&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This is a code you could place in one of your installers. It is completely artificial but that’s not the point. Point is, we’re using a factory method to provide instances of a component (tax calculator) and for this we’re using another component (country). Remember the rule – You have to release anything you explicitly resolve. We did resolve the country, so unless we are sure that the country is and always will be a singleton or have one of the other self-releasing lifestyles, we should release it before returning the tax calculator.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=61703" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Castle/default.aspx">Castle</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Design/default.aspx">Design</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx">IoC</category></item><item><title>Upgrading to Windsor 2.5 (Northwind)</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/08/24/upgrading-to-windsor-2-5-northwind.aspx</link><pubDate>Tue, 24 Aug 2010 13:31:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:61666</guid><dc:creator>Krzysztof Koźmic</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/krzysztof_kozmic/rsscomments.aspx?PostID=61666</wfw:commentRss><comments>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/08/24/upgrading-to-windsor-2-5-northwind.aspx#comments</comments><description>&lt;p&gt;I was looking at &lt;a href="http://www.sharparchitecture.net/"&gt;Sharp Architecture&lt;/a&gt; project and as I went through the codebase (the sample application in particular) I found several spots that weren&amp;rsquo;t using Windsor in a optimal way, and few other that could really benefit from some of the new improvements in version 2.5. So instead of keeping that knowledge all to myself I though I might as well use it as an example and show the process of migration I went though with it. The guide is based on current source from SA repository, and its Northwind sample application.&lt;/p&gt;
&lt;h3&gt;So here we go&lt;/h3&gt;
&lt;p&gt;We start off by copying Windsor 2.5 binaries to bin folder of SharpArchitecture.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/1_5F00_copy_5F00_binaries_5F00_2.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="1_copy_binaries" alt="1_copy_binaries" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/1_5F00_copy_5F00_binaries_5F00_thumb.png" border="0" height="386" width="508" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Windsor 2.5 consists of only 2 assemblies, as compared to 4 in previous versions. Castle.MicroKernel.dll and Castle.DynamicProxy2.dll are no longer needed (the classes from these two assemblies were integrated into the two other assemblies).&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/2_5F00_remove_5F00_old_5F00_binaries_5F00_2.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="2_remove_old_binaries" alt="2_remove_old_binaries" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/2_5F00_remove_5F00_old_5F00_binaries_5F00_thumb.png" border="0" height="333" width="615" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Since SharpArchitecture users NHibernate which has dependency on DynamicProxy I also needed to rebuild the NHibernate.ByteCode.Castle.dll for the new DynamicProxy (which now lives in Castle.Core.dll). It may seem complicated but really was just a matter of fixing some namespaces.&lt;/p&gt;
&lt;h3&gt;SharpArch project&lt;/h3&gt;
&lt;p&gt;Fixing Sharp Architecture was quite simple. I opened the solution, built it, watched it fail, and stared to fix references (removing references to defunct, Castle.DynamicProxy2.dll, and removing or replacing with Castle.Windsor.dll references to Castle.MicroKernel.dll)&lt;/p&gt;
&lt;h3&gt;Breaking change&lt;/h3&gt;
&lt;p&gt;While updating the code I also stumbled upon one breaking change in new Windsor.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/3_5F00_breaking_5F00_change_5F00_2.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="3_breaking_change" alt="3_breaking_change" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/3_5F00_breaking_5F00_change_5F00_thumb.png" border="0" height="358" width="719" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;ServiceSelector delegate (used in WithService.Select calls) changed signature so that its 2nd parameter is now an array of Types, not a single type. If you look at BreakingChanges.txt distributed with Windsor 2.5, you&amp;rsquo;ll find that it documents this breaking change along with suggestion how to upgrade your old code.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;fix - depending on the scenario. You would either ignore it, or wrap your current method&amp;#39;s body     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; in foreach(var baseType in baseTypes)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In our case the former applies, so we just update the signature and move on. The project now compiles just fine.&lt;/p&gt;
&lt;h3&gt;Northwind project&lt;/h3&gt;
&lt;p&gt;With that done we can shift focus to the sample Northwind application. We don&amp;rsquo;t need to do anything other than upgrading references to Windsor, NHibernate bytecode provider and SharpArch to get it to compile. This does not mean that we&amp;rsquo;re done though.&lt;/p&gt;
&lt;h3&gt;Obsolete API&lt;/h3&gt;
&lt;p&gt;The project will compile but will give us 18 errors. That&amp;rsquo;s something most users upgrading older apps will see. If you take a look at the errors you&amp;rsquo;ll see something like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/4_5F00_obsolete_5F00_api_5F00_4.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="4_obsolete_api" alt="4_obsolete_api" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/4_5F00_obsolete_5F00_api_5F00_thumb_5F00_1.png" border="0" height="297" width="701" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;In Windsor 2.5 all the old registration API (all AddComponent and friends methods) became obsolete, as first step towards cleaning the API of the container. All the obsolete methods points us towards alternative &amp;ndash; supported API that we can use to achieve the same thing so we can quite easily migrate the old calls. We won&amp;rsquo;t follow the suggestions from the error messages. Instead we&amp;rsquo;ll take a step back, to look at how the registration is being done.&lt;/p&gt;
&lt;h3&gt;Installers&lt;/h3&gt;
&lt;p&gt;All the obsolete calls come from a single class &amp;ndash; ComponentRegistrar, which looks like this:&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ComponentRegistrar&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;{&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; AddComponentsTo(IWindsorContainer container)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    {&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        AddGenericRepositoriesTo(container);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        AddCustomRepositoriesTo(container);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        AddApplicationServicesTo(container);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        AddWcfServiceFactoriesTo(container);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&amp;nbsp;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        container.AddComponent(&lt;span style="color:#006080;"&gt;&amp;quot;validator&amp;quot;&lt;/span&gt;,&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;                                &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (IValidator), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Validator));&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    }&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    &lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    &lt;span style="color:#008000;"&gt;// private registration methods&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Windsor has (and had for a very long time) a better &amp;ndash; &amp;ldquo;official&amp;rdquo; way of doing this, using installers. To take advantage of that we&amp;rsquo;ll start by moving code from the Add*To methods, to dedicated installer types.&lt;/p&gt;
&lt;p&gt;For example we could take the AddGenericRepositoriesTo method&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; AddGenericRepositoriesTo(IWindsorContainer container)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;{&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    container.AddComponent(&lt;span style="color:#006080;"&gt;&amp;quot;repositoryType&amp;quot;&lt;/span&gt;,&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;                            &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (IRepository&amp;lt;&amp;gt;), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Repository&amp;lt;&amp;gt;));&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    container.AddComponent(&lt;span style="color:#006080;"&gt;&amp;quot;nhibernateRepositoryType&amp;quot;&lt;/span&gt;,&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;                            &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (INHibernateRepository&amp;lt;&amp;gt;), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (NHibernateRepository&amp;lt;&amp;gt;));&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    container.AddComponent(&lt;span style="color:#006080;"&gt;&amp;quot;repositoryWithTypedId&amp;quot;&lt;/span&gt;,&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;                            &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (IRepositoryWithTypedId&amp;lt;,&amp;gt;), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (RepositoryWithTypedId&amp;lt;,&amp;gt;));&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    container.AddComponent(&lt;span style="color:#006080;"&gt;&amp;quot;nhibernateRepositoryWithTypedId&amp;quot;&lt;/span&gt;,&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;                            &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (INHibernateRepositoryWithTypedId&amp;lt;,&amp;gt;), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (NHibernateRepositoryWithTypedId&amp;lt;,&amp;gt;));&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and extract it to an installer:&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenericRepositoriesInstaller : IWindsorInstaller&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;{&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Install(IWindsorContainer container, IConfigurationStore store)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    {&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        container.Register(&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            Add(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (IRepository&amp;lt;&amp;gt;), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Repository&amp;lt;&amp;gt;)),&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            Add(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (INHibernateRepository&amp;lt;&amp;gt;), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (NHibernateRepository&amp;lt;&amp;gt;)),&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            Add(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (IRepositoryWithTypedId&amp;lt;,&amp;gt;), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (RepositoryWithTypedId&amp;lt;,&amp;gt;)),&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            Add(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (INHibernateRepositoryWithTypedId&amp;lt;,&amp;gt;), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (NHibernateRepositoryWithTypedId&amp;lt;,&amp;gt;)));&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    }&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&amp;nbsp;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; IRegistration Add(Type service, Type implementation)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    {&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; Component.For(service).ImplementedBy(implementation);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    }&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;I dropped the name of the component since it&amp;rsquo;s never used in the application anyway, and I extracted common code to a helper method.&lt;/p&gt;
&lt;h3&gt;Factories and parameters&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s have a look at another one of these methods:&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; AddWcfServiceFactoriesTo(IWindsorContainer container)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;{&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    container.AddFacility(&lt;span style="color:#006080;"&gt;&amp;quot;factories&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; FactorySupportFacility());&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    container.AddComponent(&lt;span style="color:#006080;"&gt;&amp;quot;standard.interceptor&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (StandardInterceptor));&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&amp;nbsp;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    var factoryKey = &lt;span style="color:#006080;"&gt;&amp;quot;territoriesWcfServiceFactory&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    var serviceKey = &lt;span style="color:#006080;"&gt;&amp;quot;territoriesWcfService&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&amp;nbsp;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    container.AddComponent(factoryKey, &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (TerritoriesWcfServiceFactory));&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    var config = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; MutableConfiguration(serviceKey);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    config.Attributes[&lt;span style="color:#006080;"&gt;&amp;quot;factoryId&amp;quot;&lt;/span&gt;] = factoryKey;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    config.Attributes[&lt;span style="color:#006080;"&gt;&amp;quot;factoryCreate&amp;quot;&lt;/span&gt;] = &lt;span style="color:#006080;"&gt;&amp;quot;Create&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    container.Kernel.ConfigurationStore.AddComponentConfiguration(serviceKey, config);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    container.Kernel.AddComponent(serviceKey, &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (ITerritoriesWcfService),&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;                                    &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (TerritoriesWcfServiceClient), LifestyleType.PerWebRequest);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s quite a lot going on here. It&amp;rsquo;s using FactorySupportFacility to register a service as well as a factory that will provide instances of this service. Why does it use a factory?&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; TerritoriesWcfServiceFactory&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;{&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ITerritoriesWcfService Create()&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    {&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        var address = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; EndpointAddress(&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            &lt;span style="color:#008000;"&gt;// I see the below as a magic string; I typically like to move these to a &lt;/span&gt;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            &lt;span style="color:#008000;"&gt;// web.config reader to consolidate the app setting names&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            ConfigurationManager.AppSettings[&lt;span style="color:#006080;"&gt;&amp;quot;territoryWcfServiceUri&amp;quot;&lt;/span&gt;]);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        var binding = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; WSHttpBinding();&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&amp;nbsp;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; TerritoriesWcfServiceClient(binding, address);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    }&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The factory provides arguments to the service, and one of these arguments is also depending on a value from the config file. How can we do this better? We have two options here. We either register the binding and endpoint address in our container as services, so that Windsor itself can provide them to the TerritoriesWcfServiceClient or we register them as inline dependencies of the service. I don&amp;rsquo;t think it makes much sense to register them as services, so we&amp;rsquo;ll go with the latter option.&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; TerritoriesWcfServiceClientInstaller:IWindsorInstaller&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;{&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Install(IWindsorContainer container, IConfigurationStore store)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    {&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        var address = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; EndpointAddress(ConfigurationManager.AppSettings[&lt;span style="color:#006080;"&gt;&amp;quot;territoryWcfServiceUri&amp;quot;&lt;/span&gt;]);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        container.Register(Component.For&amp;lt;ITerritoriesWcfService&amp;gt;()&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;                            .ImplementedBy&amp;lt;TerritoriesWcfServiceClient&amp;gt;()&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;                            .DependsOn(Property.ForKey&amp;lt;Binding&amp;gt;().Eq(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; WSHttpBinding()),&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;                                           Property.ForKey&amp;lt;EndpointAddress&amp;gt;().Eq(address))&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;                            .LifeStyle.PerWebRequest);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    }&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;re not using a factory here, letting Windsor create the instance for us. We&amp;rsquo;re also passing the binding and address as typed dependencies, which is a new option in version 2.5.&lt;/p&gt;
&lt;p&gt;The last method I want to look at (as this post is already enormously big) is this:&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; AddApplicationServicesTo(IWindsorContainer container)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;{&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    container.Register(&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        AllTypes.Pick()&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            .FromAssemblyNamed(&lt;span style="color:#006080;"&gt;&amp;quot;Northwind.ApplicationServices&amp;quot;&lt;/span&gt;)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            .WithService.FirstInterface());&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;There are two issues with it. First it calls Pick() before FromAssembly*. That&amp;rsquo;s not a big deal but in Windsor 2.5 we tried to unify the API so that the order always should be: Specify assembly &amp;ndash;&amp;gt; specify components &amp;ndash;&amp;gt; configure.&lt;/p&gt;
&lt;p&gt;The other issue is that it uses FirstInterface to pick a service for a type. Problem with that is, that if type implements more than one interface, which one is &amp;ldquo;first&amp;rdquo; is undefined. It can be one on Thursdays, and the other one on Fridays. Good luck chasing issues caused by this.&lt;/p&gt;
&lt;h3&gt;Default service&lt;/h3&gt;
&lt;p&gt;Windsor 2.5 adds new option that first the purpose much better in this case &amp;ndash; &lt;a href="http://stw.castleproject.org/Windsor.Registering-components-by-conventions.ashx#codeDefaultInterfacecode_7"&gt;default interface&lt;/a&gt;. It performs matching based on type/interface name. Since we have actually just single class and interface in that assembly: DashboardService/IDashboardService they are perfect match for this. So that our installer would look like this:&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ApplicationServicesInstaller : IWindsorInstaller&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;{&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Install(IWindsorContainer container, IConfigurationStore store)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    {&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;        container.Register(&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            AllTypes.FromAssemblyNamed(&lt;span style="color:#006080;"&gt;&amp;quot;Northwind.ApplicationServices&amp;quot;&lt;/span&gt;)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;            .Pick().WithService.DefaultInterface());&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;    }&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Installing the installers&lt;/h3&gt;
&lt;p&gt;Now having all registration enclosed in installers in our project we can change this:&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;ComponentRegistrar.AddComponentsTo(container);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;to this:&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;container.Install(FromAssembly.This());&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and Windsor will take care of all the rest.&lt;/p&gt;
&lt;h3&gt;In closing&lt;/h3&gt;
&lt;p&gt;That&amp;rsquo;s pretty much all it takes to upgrade the app to run on top of latest and greatest version of Windsor. In addition we introduced some new features that you likely are going to take advantage of when upgrading your apps (or starting new ones as well). I suspect there is some room for improvement in the Northwind app, and place for some other Windsor features but that perhaps should be left for another post.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=61666" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Castle/default.aspx">Castle</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx">IoC</category></item><item><title>Must Windsor track my components?</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/08/19/must-windsor-track-my-components.aspx</link><pubDate>Thu, 19 Aug 2010 13:07:31 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:61545</guid><dc:creator>Krzysztof Koźmic</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/krzysztof_kozmic/rsscomments.aspx?PostID=61545</wfw:commentRss><comments>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/08/19/must-windsor-track-my-components.aspx#comments</comments><description>&lt;p&gt;Probably the single most misunderstood feature of Castle Windsor is regarding its lifetime management of components. Hopefully in this post (and the next one) I’ll be able to clear all the misconceptions.&lt;/p&gt;  &lt;h3&gt;Why is Windsor tracking components in the first place?&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/lifecycle_5F00_simplified_5F00_2.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="lifecycle_simplified" border="0" alt="lifecycle_simplified" align="right" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/lifecycle_5F00_simplified_5F00_thumb.png" width="184" height="240" /&gt;&lt;/a&gt;One of the core responsibilities of a container is to manage lifecycle of components.&amp;#160; At a very high level it looks like in the picture on the right. The container creates the object, sets it up, then when it’s ready to go, it gives it to you, so that you can use it, and when it’s no longer needed it carefully tears it apart and sends it to &lt;a href="http://en.wikipedia.org/wiki/Happy_hunting_ground"&gt;happy hunting ground&lt;/a&gt;. Many people forget about that last part, and indeed some containers lack support for this crucial element at all.&lt;/p&gt;  &lt;p&gt;In order to be able to identify objects it created and destroy them when their time has come, Windsor obviously needs to have access to them and that’s why it keeps reference to the objects it needs to destroy.&lt;/p&gt;  &lt;h3&gt;Destroy objects. What does that even mean?&lt;/h3&gt;  &lt;p&gt;I’ve been pretty abstract until now, let’s look at an actual (trivialized) example .&lt;/p&gt;  &lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; UnitOfWork&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;{&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Init()&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;   {&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;      &lt;span style="color:#008000;"&gt;// some initialization logic...&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;   }&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Commit()&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;   {&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;      &lt;span style="color:#008000;"&gt;// commit or rollback the UoW&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;   }&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:&amp;#39;Courier New&amp;#39;, courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:8pt;border-left-style:none;overflow:visible;padding-top:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;We want to create the instance of our UnitOfWork, then before we use it, we want to initialize it, then use it for a while to do the work, and then when we’re done, commit all the work that we’ve done. Usually in a web-app the lifetime of the unit or work would be bound to a single web request, in a client app it could be bound to a screen.&lt;/p&gt;

&lt;p&gt;It is container’s work to create the object. It’s container’s work to initialize it. So that I don’t need to remember to call the Init method myself. Especially that within a web request (let’s stick to the web example) I can (and likely will) have multiple components depending on my unit of work. Which one of them should call Init?&lt;/p&gt;

&lt;p&gt;None. It’s not their job. Their job is to make use of it. Be lazy and outsource this to the container.&amp;#160; Which of my components should Commit the unit of work? Also none. &lt;/p&gt;

&lt;p&gt;None of my components is responsible for the unit of work, since none of them created it. The container did, so it’s container’s job to clean up after the object when I’m done using it.&lt;/p&gt;

&lt;p&gt;The destroy part is just it – do all the things with the components that need to be done after the component is no longer being used by the application code, but before the object can be let go. The most common example of that is the IDisposable interface and Dispose method.&lt;/p&gt;

&lt;p&gt;The rule in .NET framework is very simple when it comes to Disposing objects.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Dispose what you’ve created, when you’re done using it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hence clearly since the container is creating the object it’s its responsibility to dispose of them. In Windsor lingo, you’d call the Init method on the UnitOfWork a &lt;em&gt;commission concern&lt;/em&gt; and the Commit method a &lt;em&gt;decommission concern&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;What if my object requires no clean up? Will Windsor still track it?&lt;/h3&gt;

&lt;p&gt;This is a very important question. The answer is also important. In short.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;It depends.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Windsor by default will track objects that themselves &lt;strong&gt;have any decommission concerns, are pooled, or any of their dependencies is tracked.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will discuss this in more details in a future post, since it is a big and important topic. In short though – you as a user of a component should not know all these things (especially that they can change), so you should treat all components as being tracked..&lt;/p&gt;

&lt;h3&gt;What do you mean by default?&lt;/h3&gt;

&lt;p&gt;Windsor is very modular and unsurprisingly tracking of components is contained in a single object, called release policy. The behavior described above is the behavior of default, LifecycledComponentsReleasePolicy. Windsor comes also with alternative implementation called NoTrackingReleasePolicy which as its name implies will never ever track your components, hence you never want to use it.&lt;/p&gt;

&lt;p&gt;Now seriously – often people see that Windsor holds on to the components it creates as a memory leak (it often is, when used inappropriately which I’ll talk about in the next post), and they go – &lt;em&gt;Windsor holding on to the objects causes memory leaks, so lets use the NoTrackingReleasePolicy and the problem is solved.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This reasoning is flawed, because the actual reason is you not using this feature correctly, not the feature itself. By switching to no tracking, you end up out of the frying pan and into the fire, since by not destroying your objects properly you’ll be facing much more subtle, harder to find and potentially more severe in results bugs (like units of works not being committed, hence losing work of your users). So in closing – it’s there when you need it, but even when you thing you need it, you really don’t.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=61545" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Castle/default.aspx">Castle</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Design/default.aspx">Design</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx">IoC</category></item><item><title>IoC patterns – partitioning registration</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/08/10/ioc-patterns-partitioning-registration.aspx</link><pubDate>Tue, 10 Aug 2010 09:44:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:61441</guid><dc:creator>Krzysztof Koźmic</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/krzysztof_kozmic/rsscomments.aspx?PostID=61441</wfw:commentRss><comments>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/08/10/ioc-patterns-partitioning-registration.aspx#comments</comments><description>&lt;p&gt;I&amp;rsquo;ve blogged a bit in the past, more or less explicitly, about patterns and antipatterns of Inversion of Control usage. This is yet another post that will (possibly) spawn a series. We&amp;rsquo;ll see about that. Note that this post is not talking about any particular IoC container and what I&amp;rsquo;m talking about is generic and universally applicable to all of them.&lt;/p&gt;
&lt;p&gt;Historically we started to register and configure our components in XML (example is in pseudo syntax, not specific to any particular framework).&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&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;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;   &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;register&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;foo&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Acme.Crm.Foo, Acme.Crm&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;as&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Acme.Crm.IFoo, Acme.Crm&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;   &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;register&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;bar&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Acme.Crm.Bar, Acme.Crm&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;as&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Acme.Crm.IBar, Acme.Crm&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;parameter&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;bla&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;42&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;   &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;register&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;   &lt;span style="color:#008000;"&gt;&amp;lt;!--... many, many more components like this...--&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&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;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Later we learned that it&amp;rsquo;s massive pain in the neck to write and manage, and we moved to code:&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;container.Register&amp;lt;Foo&amp;gt;().As&amp;lt;IFoo&amp;gt;().WithName(&lt;span style="color:#006080;"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;container.Register&amp;lt;Bar&amp;gt;().As&amp;lt;IBar&amp;gt;().WithName(&lt;span style="color:#006080;"&gt;&amp;quot;bar&amp;quot;&lt;/span&gt;)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;   .WithParameter(&lt;span style="color:#006080;"&gt;&amp;quot;bla&amp;quot;&lt;/span&gt;, 42);&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;&lt;span style="color:#008000;"&gt;/* ...many, many more components like this... */&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This had the advantage over&amp;nbsp; XML of being strongly typed, refactoring friendly, but still shared the biggest drawback of the previous approach. For each and every new component you added to your application you would have to go back and explicitly register it.&lt;/p&gt;
&lt;p&gt;To alleviate that, many containers support conventions &amp;ndash; where naming the type in the &amp;ldquo;right&amp;rdquo; way or putting it in the &amp;ldquo;right&amp;rdquo; place would be enough to make that type available as a component.&lt;/p&gt;
&lt;div style="border:1px solid silver;text-align:left;padding:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;max-height:700px;font-size:8pt;overflow:auto;cursor:text;" id="codeSnippetWrapper"&gt;
&lt;div style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;" id="codeSnippet"&gt;
&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;container.RegisterAllTypesInNamespace(&lt;span style="color:#006080;"&gt;&amp;quot;Acme.Crm&amp;quot;&lt;/span&gt;)&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;   .AsInterfaceTheyImplement()&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:white;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;   .NamedAsLowercaseTypeName()&lt;/pre&gt;

&lt;pre style="border-style:none;text-align:left;padding:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;overflow:visible;"&gt;   .WithParameterFor&amp;lt;Bar&amp;gt;(&lt;span style="color:#006080;"&gt;&amp;quot;bla&amp;quot;&lt;/span&gt;, 42);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You would still need to do some finetuning with some of the components, but for bulk of it, it would just work.&lt;/p&gt;
&lt;h3&gt;So where&amp;rsquo;s the problem?&lt;/h3&gt;
&lt;p&gt;While the last approach is evolutionally the most advanced it too comes with its own set of drawbacks, that can bite you if you don&amp;rsquo;t pay attention to them.&lt;/p&gt;
&lt;p&gt;If you partition your application abstracting implementation detains with interfaces and registering classes via conventions you will end up with highly decoupled application. So decoupled in fact, that you will have no explicit reference to your classes anywhere outside your tests. This can complicate things when a new person comes to the project and tries to figure out how stuff works. Simply following ReSharper&amp;rsquo;s &amp;ldquo;Go to definition&amp;rdquo;/&amp;rdquo;Go to usage&amp;rdquo; won&amp;rsquo;t cut it. Often you can infer from the interface or usage which class is used under which interface, but if you have generic abstraction (like ICommandHandler) and multiple implementations that get wired in a non-trivial way it may quickly become much more complicated to find out how the stuff gets wired and why it works the way it does.&lt;/p&gt;
&lt;h3&gt;Partition your registration&lt;/h3&gt;
&lt;p&gt;&amp;nbsp;&lt;img style="border-right-width:0px;margin:5px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="Installers" alt="Installers" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/krzysztof_5F00_kozmic/Installers_5F00_3.png" border="0" height="158" width="235" /&gt;&lt;/p&gt;
&lt;p&gt;To minimize problems like this its crucial that you partition your registration correctly. What do I mean by that?&lt;/p&gt;
&lt;p&gt;First of all make it granular. Most containers have some means of dividing your registration code into pieces. &lt;a href="http://stw.castleproject.org/Windsor.Installers.ashx"&gt;Windsor has installers&lt;/a&gt;, Autofac and Ninject have modules, StructureMap has registries&amp;hellip; Take advantage of them. Don&amp;rsquo;t just create one and put all your registration in one. You will regret it when later on you try to change something and you find yourself scrolling through this monstrous class. Make it granular. Partition is applying Single Responsibility Principle. Even if your installer (I will stick to Windsor lingo &amp;ndash; replace with module/registry if you&amp;rsquo;re using any other container) ends up registering just a single type &amp;ndash; that&amp;rsquo;s fine.&lt;/p&gt;
&lt;p&gt;Remember to name them correctly so that it&amp;rsquo;s very quick and obvious having a type, to find which installer is responsible for its registration/configuration.&lt;/p&gt;
&lt;p&gt;To do that &amp;ndash; also pay attention to the name you give your installers. If the next developer has to stop and think, or search several to find the right one for a particular service you&amp;rsquo;ve failed.&lt;/p&gt;
&lt;p&gt;Last but not least &amp;ndash; keep them all in one visible place. I generally create folder/namespace dedicated to holding my installers, so that I can find them quickly and scan the entire list at once. It&amp;rsquo;s really very frustrating having to chase down installers all across the solution.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=61441" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Design/default.aspx">Design</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx">IoC</category></item><item><title>Semi final release – Windsor 2.5 beta 2 (now with Silverlight support)</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/07/21/semi-final-release-windsor-2-5-beta-2-now-with-silverlight-support.aspx</link><pubDate>Wed, 21 Jul 2010 00:21:42 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:61156</guid><dc:creator>Krzysztof Koźmic</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/krzysztof_kozmic/rsscomments.aspx?PostID=61156</wfw:commentRss><comments>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/07/21/semi-final-release-windsor-2-5-beta-2-now-with-silverlight-support.aspx#comments</comments><description>&lt;p&gt;A bit later than expected (ah, work) &lt;a href="https://sourceforge.net/projects/castleproject/files/InversionOfControl/2.5.0-beta2/Castle.Windsor.2.5.beta2.zip/download"&gt;I published beta 2 of Windsor 2.5 today&lt;/a&gt;. The release has the following changes as compared to beta 1.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Silverlight version (for Silverlight 3 and Silverlight 4) is now included in the package. &lt;/li&gt;    &lt;li&gt;Synchronize Facility is now included in the package (.NET only) &lt;/li&gt;    &lt;li&gt;The following code changes and fixes were made (incl. one breaking change)      &lt;p&gt;- added support for selecting components based on custom attributes and their properties. See Component.HasAttribute&amp;lt;T&amp;gt;() methods &lt;/p&gt;      &lt;p&gt;- added WithService.DefaultInterface() to fluent API. It matches Foo to IFoo, SuperFooExtended to IFoo and IFooExtended etc. If you know how DefaultConvention works in StructureMap, this is pretty similar&lt;/p&gt;      &lt;p&gt;- added support for CastleComponentAttribute in fluent API. Also added helper filter method Component.IsCastleComponent &lt;/p&gt;      &lt;p&gt;- added ability to specify interceptors selector as a service, not just as instance&lt;/p&gt;      &lt;p&gt;- added ability to specify proxy hook in fluent API&lt;/p&gt;      &lt;p&gt;- indexers on IKernel are now obsolete. &lt;/p&gt;      &lt;p&gt;- added WithAppConfig() method to logging facility to point to logging configuration in AppDomain&amp;#39;s config file (web.config or app.config) &lt;/p&gt;      &lt;p&gt;- &lt;strong&gt;BREAKING CHANGE:&lt;/strong&gt; Restructured lifecycle concerns - introduced ICommissionConcern and IDecommissionConcern and favors them over old enum driven style. &lt;/p&gt;      &lt;p&gt;- Fixed how contextual arguments are handled. Null is no longer considered a valid value (That would cause an exception later on, now it&amp;#39;s ignored). &lt;/p&gt;      &lt;p&gt;- Changed method DeferredStart on StartableFacility. It now does not take a bool parameter. A DeferredTryStart() method was introduced instead.&lt;/p&gt;      &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This is probably the last pre-release for 2.5 and if no critical issues are found, we’ll release final release in 2, 3 weeks. Go grab the bits, see if it works for you and if it does not report back. I’m also looking for people who want to contribute sample applications for the final release. Ping me if you’d like to contribute to that.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=61156" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Castle/default.aspx">Castle</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx">IoC</category></item><item><title>How I use Inversion of Control containers – pulling from the container</title><link>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/06/22/how-i-use-inversion-of-control-containers-pulling-from-the-container.aspx</link><pubDate>Tue, 22 Jun 2010 11:52:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:60706</guid><dc:creator>Krzysztof Koźmic</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/krzysztof_kozmic/rsscomments.aspx?PostID=60706</wfw:commentRss><comments>http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/06/22/how-i-use-inversion-of-control-containers-pulling-from-the-container.aspx#comments</comments><description>&lt;p&gt;As I expected my previous post prompted a few questions regarding the Three Container Calls pattern I outlined. Major one of them is how to handle the following scenario:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://kozmic.pl/images/kozmic_pl/WindowsLiveWriter/HowIuseInversionofControlcontainerspulli_11122/Container_pulling_2.png" rel="lightbox"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="Container_pulling" border="0" alt="Container_pulling" src="http://kozmic.pl/images/kozmic_pl/WindowsLiveWriter/HowIuseInversionofControlcontainerspulli_11122/Container_pulling_thumb.png" width="743" height="341" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;We create our container, install all the components in it.&amp;#160; Moments later we resolve the root component, container goes and creates its entire tree of dependencies for us, does all the configuration and other bookkeeping, injects all the dependencies and gives us back the new fresh ready to use objects graph that we then use to start off the application.&lt;/p&gt;  &lt;p&gt;More often than not, this will not be enough though. At some point later in the lifecycle of the application, a user comes in and wants &lt;em&gt;something&lt;/em&gt; from the application. Lets say the user clicks a button saying “Open a file” and at this point a “OpenFile” view should be loaded along with all of its dependencies and presented to the user. But wait – it gets worse – Because now if a user selects a .txt file a “DisplayText” view should be loaded, but when a .png file gets selected, we should load a “DisplayImage” view.&lt;/p&gt;  &lt;p&gt;Generalizing – how to handle situations where you need to request some dependencies unavailable at registration or root resolution time, potentially passing some parameters to the component to be resolved, or some contextual information affecting which component will be resolved (or both).&lt;/p&gt;  &lt;h3&gt;Factory alive and kicking&lt;/h3&gt;  &lt;p&gt;The way to tackle this is to use a very old design patterns (from the original GOF patterns book) called &lt;a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern"&gt;Abstract factory&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Factory_method"&gt;Factory method&lt;/a&gt;.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Nitpickers&amp;#39; corners – as I know I will be called out by some purists that what I’m describing here is not purely this or that pattern according to this or that definition&amp;#160; – I don’t care, so don’t bother. What’s important is the concept.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;It gives us exactly what we need – provides us with some components on demand, allowing us to pass some inputs in, and on the same time encapsulating and abstracting how the object is constructed. The encapsulating part means that I totally don’t need to care about how, and where the components get created – it’s completely offloaded to the container. The abstracting part means that I can still adhere to the Matrix Principle – the &lt;strike&gt;spoon&lt;/strike&gt; container does not exist.&lt;/p&gt;  &lt;h3&gt;How does this work?&lt;/h3&gt;  &lt;p&gt;Using Castle Windsor, this is very simple – all you have to do is to &lt;a href="http://stw.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx"&gt;declare the factory interface&lt;/a&gt;, and Windsor itself will provide the implementation. It is very important. Your interface lives in your domain assembly, and knows nothing about Windsor. The implementation is a detail, which depends on the abstraction you provided, not the other way around. This also means that most of the time all of the work you have to do is simply declaring the interface, and leave all the heavy lifting to Windsor, which has some pretty smart defaults that it will use to figure this stuff out by itself. For non-standard cases, like deciding which component to load based on the extension of the file provided, you can easily extend/customize the way factory works by using &lt;a href="http://stw.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx#Custom_codeITypedFactoryComponentSelectorcodes_15"&gt;custom selector&lt;/a&gt;. With well thought out naming convention, customizing Windsor to support this scenario is literally one line of meaningful code.&lt;/p&gt;  &lt;p&gt;In addition to that (although I prefer the interface-driven approach) &lt;a href="http://stw.castleproject.org/Windsor.Typed-Factory-Facility-delegate-based-factories.ashx"&gt;Windsor (upcoming version 2.5)&lt;/a&gt;, and some other containers, like Autofac (which first implemented the concept), and StructureMap (don’t really know about Ninject, perhaps someone can clarify this in the comments) support delegate based factories, where just the fact of having a dependency on &lt;em&gt;Func&amp;lt;IFoo&amp;gt;&lt;/em&gt; is enough for the container to figure out that it should inject for that dependency a delegate that when invoked will call back to the container to resolve the &lt;em&gt;IFoo&lt;/em&gt; service. The container will even implement the delegate for you, and it also obviously means that the code you have has no idea (nor does it care) that the actual dependency was created by a container.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;This is the beauty of this approach – it has all the pros and none of the cons of &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator&lt;/a&gt;, and shift towards supporting it was one of the major changes in the .NET IoC backyard in the last couple of months, so if you are still using Service Locator you’re doing it wrong.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=60706" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/Design/default.aspx">Design</category><category domain="http://devlicio.us/blogs/krzysztof_kozmic/archive/tags/IoC/default.aspx">IoC</category></item></channel></rss>