<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/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>Nicholas Blumhardt : Container Managed Application Design</title><link>http://blogs.msdn.com/nblumhardt/archive/tags/Container+Managed+Application+Design/default.aspx</link><description>Tags: Container Managed Application Design</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Declarative Context Adapters</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/01/04/declarative-context-adapters.aspx</link><pubDate>Sun, 04 Jan 2009 19:59:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9271437</guid><dc:creator>niblumha</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9271437.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9271437</wfw:commentRss><description>&lt;p&gt;The &lt;a href="http://blogs.msdn.com/nblumhardt/archive/2008/12/27/container-managed-application-design-prelude-where-does-the-container-belong.aspx"&gt;first post&lt;/a&gt; in this series introduced the problem of accessing IoC container features dynamically.&lt;/p&gt;  &lt;p&gt;We brushed over two common patterns:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;em&gt;Global Container &lt;/em&gt;(or &lt;em&gt;Static Service Locator&lt;/em&gt;) &lt;/li&gt;    &lt;li&gt;&lt;em&gt;Injected Context&lt;/em&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Before we move on from this topic, I'd like to look at one more pattern in this theme that may not be in common use, but deserves some attention anyway.&lt;/p&gt;  &lt;p&gt;This pattern I've labeled &lt;em&gt;Declarative Context Adapter&lt;/em&gt;, and it builds on the strengths of &lt;em&gt;Injected Context&lt;/em&gt; while moving from imperative implementation to declarative configuration.&lt;/p&gt;  &lt;h2&gt;Problem&lt;/h2&gt;  &lt;p&gt;After composition, a component requires dynamic access to features of the container that hosts it (the &lt;em&gt;context&lt;/em&gt;.)&lt;/p&gt;  &lt;p&gt;Commonly, this involves dynamically creating or locating instances from the container (e.g. see &lt;strong&gt;&lt;em&gt;ControllerProvider &lt;/em&gt;&lt;/strong&gt;in &lt;a href="http://blogs.msdn.com/nblumhardt/archive/2008/12/27/container-managed-application-design-prelude-where-does-the-container-belong.aspx"&gt;this article&lt;/a&gt;.)&lt;/p&gt;  &lt;h2&gt;Solution&lt;/h2&gt;  &lt;ul&gt;   &lt;li&gt;Provide a domain-specific interface to the required functionality.&lt;/li&gt;    &lt;li&gt;Describe how this interface should interact with the container using metadata or configuration.&lt;/li&gt;    &lt;li&gt;A compliant implementation is supplied based on the metadata.&lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Example&lt;/h2&gt;  &lt;p&gt;One example of a &lt;em&gt;Declarative Context Adapter &lt;/em&gt;is the &lt;a href="http://code.google.com/p/autofac/wiki/DelegateFactories"&gt;generated factories&lt;/a&gt; feature of Autofac.&lt;/p&gt;  &lt;p&gt;This feature uses .NET delegate types as the 'domain-specific interfaces'. The &lt;strong&gt;&lt;em&gt;Shareholding.Factory &lt;/em&gt;&lt;/strong&gt;delegate type below plays this role:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ContainerManagedApplicationDesign1Declar_A407/image_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="219" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ContainerManagedApplicationDesign1Declar_A407/image_thumb.png" width="490" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In this example, we want invocations of the &lt;strong&gt;&lt;em&gt;Factory&lt;/em&gt;&lt;/strong&gt; delegate to return instances of the &lt;strong&gt;&lt;em&gt;Shareholding &lt;/em&gt;&lt;/strong&gt;class created and wired up by the container. This example is a bit contrived, since usually there is some abstraction involved, but hey, it is just an example :)&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;&lt;em&gt;ContainerBuilder&lt;/em&gt;&lt;/strong&gt;'s method-chaining API allows the mapping between the delegate to the container's &lt;strong&gt;&lt;em&gt;Resolve()&lt;/em&gt;&lt;/strong&gt; method to be specified:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ContainerManagedApplicationDesign1Declar_A407/image_4.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="159" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ContainerManagedApplicationDesign1Declar_A407/image_thumb_1.png" width="676" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This is specified via the &lt;strong&gt;&lt;em&gt;RegisterGeneratedFactory() &lt;/em&gt;&lt;/strong&gt;method.&lt;/p&gt;  &lt;p&gt;The only declarative configuration in this example apart from the delegate type itself, is the &lt;strong&gt;&lt;em&gt;TypedService&lt;/em&gt;&lt;/strong&gt; parameter that tells Autofac which service from the container should be returned whenever the delegate is invoked.&lt;/p&gt;  &lt;p&gt;(Additional configuration options can describe parameter mappings for the delegate parameters, however these aren't available in the current release builds.)&lt;/p&gt;  &lt;p&gt;Other components that need to instantiate &lt;strong&gt;&lt;em&gt;Shareholding&lt;/em&gt;&lt;/strong&gt;s can now have instances of &lt;strong&gt;&lt;em&gt;Shareholding.Factory&lt;/em&gt;&lt;/strong&gt; injected into them.&lt;/p&gt;  &lt;h2&gt;Discussion&lt;/h2&gt;  &lt;p&gt;To call this technique a 'pattern' is a bit premature, since the Autofac feature is the only implementation you can put your hands on.&lt;/p&gt;  &lt;p&gt;The same functionality could&amp;#160; be implemented on different containers, perhaps via AOP. It would also be preferable to support interfaces as well as delegate types.&lt;/p&gt;  &lt;p&gt;While the examples have been related to instantiation or service lookup, context adapters could be defined for other container features like lifetime management or even component registration. Anywhere that &lt;em&gt;Injected Context &lt;/em&gt;is used, it should be possible to generate a configuration-driven alternative.&lt;/p&gt;  &lt;h2&gt;Source Code&lt;/h2&gt;  &lt;p&gt;The tiny example from above can be downloaded &lt;a href="http://cid-668630fe3ecd2791.skydrive.live.com/self.aspx/.Public/DeclarativeContextAdapterSample.zip"&gt;here&lt;/a&gt; if you'd like to experiment with it.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9271437" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/Container+Managed+Application+Design/default.aspx">Container Managed Application Design</category><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/Autofac/default.aspx">Autofac</category></item><item><title>Explicit Context Dependencies</title><link>http://blogs.msdn.com/nblumhardt/archive/2008/12/29/explicit-context-dependencies.aspx</link><pubDate>Mon, 29 Dec 2008 21:34:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9256012</guid><dc:creator>niblumha</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9256012.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9256012</wfw:commentRss><description>&lt;p&gt;As a follow up from my last post about &lt;a href="http://blogs.msdn.com/nblumhardt/archive/2008/12/27/container-managed-application-design-prelude-where-does-the-container-belong.aspx#comments"&gt;&lt;em&gt;Global Container&lt;/em&gt; vs.&lt;em&gt; Injected Context&lt;/em&gt;&lt;/a&gt;, I would like to share this snippet from &lt;a href="http://www.amazon.com/Component-Software-Beyond-Object-Oriented-Programming/dp/0201745720/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1230573309&amp;amp;sr=8-1"&gt;Component Software&lt;/a&gt;, which casts the difference between the two approaches in another light:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;quot;A software component is a unit of composition with contractually specified interfaces and explicit context dependencies only. ...&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Under this definition, the &lt;strong&gt;&lt;em&gt;ControllerProvider &lt;/em&gt;&lt;/strong&gt;based upon an &lt;em&gt;Injected Context &lt;/em&gt;is a component, because it publishes its (admittedly broad) context dependency explicitly, via its constructor parameter.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;BadControllerProvider, &lt;/em&gt;&lt;/strong&gt;based upon a &lt;em&gt;Global Container, &lt;/em&gt;is not a component under this definition because its dependency on the global container is not explicit (you need to have source code in order to find it.)&lt;/p&gt;  &lt;p&gt;In my opinion, the distinction is significant because &amp;quot;components&amp;quot; and &amp;quot;wiring&amp;quot; are a higher-level abstraction than &amp;quot;classes&amp;quot; and &amp;quot;references&amp;quot;.&lt;/p&gt;  &lt;p&gt;In composite applications, an architecture will be more robust and easier to understand if these layers of abstraction are respected, and not tangled up in classes like &lt;strong&gt;&lt;em&gt;BadControllerProvider&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;(Component Software is a very worthwhile read and contains a lot of relevant material for those interested in IoC containers and other composition technologies. Clemens Szyperski is a mentor and highly influential voice for the &lt;/em&gt;&lt;a href="http://codeplex.com/MEF"&gt;&lt;em&gt;MEF&lt;/em&gt;&lt;/a&gt;&lt;em&gt; team.)&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9256012" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/Container+Managed+Application+Design/default.aspx">Container Managed Application Design</category></item><item><title>Container-Managed Application Design, Prelude: Where does the Container Belong?</title><link>http://blogs.msdn.com/nblumhardt/archive/2008/12/27/container-managed-application-design-prelude-where-does-the-container-belong.aspx</link><pubDate>Sat, 27 Dec 2008 03:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9253987</guid><dc:creator>niblumha</dc:creator><slash:comments>17</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9253987.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9253987</wfw:commentRss><description>&lt;P&gt;There is surprisingly little information out there in cyberspace on how IoC containers should fit into application architectures.&lt;/P&gt;
&lt;P&gt;In parallel with my MEF/Ruby series, I'm going to discuss some of the principles that I believe should be applied to "container-managed" application design.&lt;/P&gt;
&lt;H2&gt;The Enigmatic Ideal&lt;/H2&gt;
&lt;P&gt;In the world of systems built using &lt;I&gt;dependency injection&lt;/I&gt;, you hear plenty of mysterious statements like:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;“Except for when bootstrapping, don’t access the container directly!”&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This kind of thing caused me to scratch my head for the best part of a year before I finally caught on to what was meant.&lt;/P&gt;
&lt;H2&gt;Basic Mental Models&lt;/H2&gt;
&lt;P&gt;When confronted with the task of designing an application around an IoC container, the place that the container sits in that architecture usually seems to be influenced by the designer's mental model of what, exactly, an IoC container &lt;EM&gt;is&lt;/EM&gt;.&lt;/P&gt;
&lt;H3&gt;Model 1: a Dictionary of Services&lt;/H3&gt;
&lt;P&gt;The &lt;I&gt;dictionary &lt;/I&gt;or &lt;I&gt;associative array&lt;/I&gt; is one of the first constructs we learn about in software engineering. It is easy to see the analogy between a dictionary and an IoC container that composes objects using dependency injection:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_10.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_10.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=93 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_4.png" width=584 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_4.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Maps to:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_12.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=94 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_5.png" width=572 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_5.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;All that appears to differ is:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Some fancy new terms ‘register’ and ‘resolve’ are used, instead of indexer access&lt;/LI&gt;
&lt;LI&gt;The logger is created via reflection, saving some typing and configuration code in the case of inter-dependencies&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;If you come onto a project with established usage of a dependency injection container and had to figure out how to write the code you need to interact with the rest of the system, chances are this is the first mental model you’ll apply.&lt;/P&gt;
&lt;H3&gt;Model 2: an Abstract new() Operator&lt;/H3&gt;
&lt;P&gt;Dependency injection containers are often praised for eliminating &lt;I&gt;implementation type dependencies&lt;/I&gt;. Going back to our example, we’ve made our request for an &lt;STRONG&gt;&lt;EM&gt;ILogger&lt;/EM&gt;&lt;/STRONG&gt; independent of the implementation type that provides it (&lt;STRONG&gt;&lt;EM&gt;ConsoleLogger&lt;/EM&gt;&lt;/STRONG&gt; in this case.)&lt;/P&gt;
&lt;P&gt;Web frameworks like Monorail and ASP.NET MVC use our dependency injection container to create &lt;STRONG&gt;&lt;EM&gt;Controller&lt;/EM&gt;&lt;/STRONG&gt; instances in a similar way.&lt;/P&gt;
&lt;P&gt;Perhaps it makes more sense to think of the container as an &lt;I&gt;abstract new() operator&lt;/I&gt;?&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_14.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=160 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_6.png" width=454 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_6.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Becomes:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_16.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=125 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_7.png" width=508 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_7.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Here our container usage has not only abstracted away the concrete type of our MailTransport but also taken care of configuring its Host property. (Configuration must be a feature of our abstract new() operator since the configuration parameters on an instance depend on its concrete type rather than the service it provides.)&lt;/P&gt;
&lt;H2&gt;Design Outcome: the Global Container&lt;/H2&gt;
&lt;P&gt;Following either of these mental models encourages you to think about the container as something you &lt;EM&gt;retrieve things from&lt;/EM&gt;. Even the name 'container' pushes us in this direction.&lt;/P&gt;
&lt;P&gt;From this perspective, there isn't much of a challenge in the application architecture design assignment!&lt;/P&gt;
&lt;P&gt;We'll just create a static property &lt;STRONG&gt;&lt;EM&gt;Container&lt;/EM&gt;&lt;/STRONG&gt; on a class, say, &lt;STRONG&gt;&lt;EM&gt;Global&lt;/EM&gt;&lt;/STRONG&gt; and get instances from it using &lt;STRONG&gt;&lt;EM&gt;Global.Container.Resolve&amp;lt;MyService&amp;gt;().&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Easy!&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_2.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=146 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb.png" width=461 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;This snippet should be familiar as a pattern with another name: the static &lt;EM&gt;Service Locator&lt;/EM&gt;. &lt;/P&gt;
&lt;H3&gt;Problems with the Global Container as a Service Locator&lt;/H3&gt;
&lt;P&gt;There are good reasons why this pattern can rightly be called an anti-pattern:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Fragility&lt;/STRONG&gt;. The container acts like a bucket of global variables. All of the robustness that is achieved by forcing components to publicly declare dependencies is lost. Unexpected dependencies can appear between seemingly unrelated parts of an application, complicating maintenance.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Reduced Composability&lt;/STRONG&gt;. When dependencies are injected, the container can be configured to provide different implementations to different consumers. This is often necessary when combining components that were built independently. When dependencies are retrieved from a global container, the container does not know the identity of the requestor, and is forced to return the same implementation every time.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Limited Reuse&lt;/STRONG&gt;. Where the global container resides can limit the reusability of components that depend on it. Migrating components to a WPF application will require code changes if they depend on the global container attached to the HttpApplication class.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Implementation Issues&lt;/STRONG&gt;. Concurrency, re-entrancy, circular reference detection, and component lifetime management are much harder/messier in a global container based solution.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Resumption of Control: &lt;/STRONG&gt;Generally speaking, these issues arise because calling directly into a global container is a resumption of control. In a container-managed application, the container is given the responsibility of getting the right instances into the right places in order for work to be done. Calling into a global container takes over some of this responsibility in a much less manageable way.&lt;/P&gt;
&lt;P&gt;Sometimes, because of the design decisions made in many current-generation frameworks, the &lt;EM&gt;Global Container&lt;/EM&gt; is a necessity, but in order to advance the state of software engineering we need to look beyond this.&lt;/P&gt;
&lt;H2&gt;Thinking Inside the Box&lt;/H2&gt;
&lt;P&gt;The mental models listed earlier should have rung some alarm bells:&lt;/P&gt;
&lt;P&gt;The container is a dictionary of services? Hang on! If I ask for the same service twice, why do I sometimes get different instances? &lt;/P&gt;
&lt;P&gt;Good question. It is easy to configure a dependency injection container to return a new instance of the &lt;STRONG&gt;&lt;EM&gt;ConsoleLogger&lt;/EM&gt;&lt;/STRONG&gt; each time one is &lt;I&gt;resolved&lt;/I&gt;. Not very dictionary-like, is it?&lt;/P&gt;
&lt;P&gt;So the container is really an abstract &lt;EM&gt;new &lt;/EM&gt;operator? Wait! If I sometimes get the same shared instance back, when can I &lt;STRONG&gt;&lt;EM&gt;Dispose()&lt;/EM&gt;&lt;/STRONG&gt; it?&lt;/P&gt;
&lt;P&gt;These two models aren't even compatible. Sometimes the container hands out new instances, but sometimes what comes back from a call to &lt;I&gt;resolve&lt;/I&gt; is a singleton, or an instance that will be shared within some kind of context like the current transaction.&lt;/P&gt;
&lt;P&gt;So things are now confusing: who owns these instances, and where do they ‘live’?&lt;/P&gt;
&lt;H2&gt;Revised Mental Model: An Interconnected System of Objects&lt;/H2&gt;
&lt;P&gt;Here’s the problem: look at the example on the Autofac homepage. It shows how &lt;STRONG&gt;&lt;EM&gt;Register()&lt;/EM&gt;&lt;/STRONG&gt; and &lt;STRONG&gt;&lt;EM&gt;Resolve()&lt;/EM&gt;&lt;/STRONG&gt; are used in Autofac. This is a pretty common kind of introductory example to dependency injection. Notice what’s missing?&lt;/P&gt;
&lt;P&gt;The most important classes in this example are not &lt;STRONG&gt;&lt;EM&gt;ContainerBuilder&lt;/EM&gt;&lt;/STRONG&gt; or &lt;STRONG&gt;&lt;EM&gt;Container&lt;/EM&gt;&lt;/STRONG&gt;. From an application architecture perspective, we need to see the &lt;STRONG&gt;&lt;EM&gt;Straight6TwinTurbo&lt;/EM&gt;&lt;/STRONG&gt; and &lt;STRONG&gt;&lt;EM&gt;Car&lt;/EM&gt;&lt;/STRONG&gt; classes!&lt;/P&gt;
&lt;P&gt;The &lt;I&gt;resolve&lt;/I&gt; operation is just a way of finding an entry point into a self-contained system.&lt;/P&gt;
&lt;P&gt;The entire application resides within the container. There is no ‘outside’ except for whatever entry point gets the ball rolling. An understanding of IoC from this perspective does not revolve around its external APIs like &lt;STRONG&gt;&lt;EM&gt;Register()&lt;/EM&gt;&lt;/STRONG&gt; and &lt;STRONG&gt;&lt;EM&gt;Resolve()&lt;/EM&gt;&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_4.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=214 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_1.png" width=548 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Using this model, the container to gets component instances to the right place at the right time, and application design re-focuses on the implementation of the components themselves.&lt;/P&gt;
&lt;H2&gt;Design Outcome: Injected Context&lt;/H2&gt;
&lt;P&gt;This new perspective on the world leaves us with a challenge when implementing our revised &lt;STRONG&gt;&lt;EM&gt;IControllerProvider&lt;/EM&gt;&lt;/STRONG&gt;. This service needs to return (potentially) new, parameterised instances of the &lt;STRONG&gt;&lt;EM&gt;Controller &lt;/EM&gt;&lt;/STRONG&gt;type.&lt;/P&gt;
&lt;P&gt;The solution, commonly implemented in applications today, is to allow the container to provide context to our controller provider:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_6.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=262 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_2.png" width=467 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;We assume here that &lt;STRONG&gt;&lt;EM&gt;ControllerProvider&lt;/EM&gt;&lt;/STRONG&gt; is itself hosted within the container. &lt;STRONG&gt;&lt;EM&gt;IContext &lt;/EM&gt;&lt;/STRONG&gt;is an interface that Autofac automatically provides to any components that require it. You can create and use an equivalent interface in any other popular IoC container (there's a MEF example linked from below.)&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;IContext &lt;/EM&gt;&lt;/STRONG&gt;provides the instance resolution features of the container to the application, but in a controlled manner. The object implementing &lt;STRONG&gt;&lt;EM&gt;IContext&lt;/EM&gt;&lt;/STRONG&gt; provides the container's services, but may not be the container itself.&lt;/P&gt;
&lt;P&gt;Because this pattern gives the container or application developer an opportunity to customise or proxy the &lt;STRONG&gt;&lt;EM&gt;IContext&lt;/EM&gt;&lt;/STRONG&gt; implementation handed out to any particular component, most of the problems associated with a &lt;STRONG&gt;&lt;EM&gt;Global Container&lt;/EM&gt;&lt;/STRONG&gt; are mitigated.&lt;/P&gt;
&lt;H2&gt;Container-Managed Application Design&lt;/H2&gt;
&lt;P&gt;If you feel like this article and its explanations are somewhat incomplete, you're right. As the title of this post suggests, it is an introduction to a series on the topic. I don't have all the answers, and I don't even have all the questions, so the installments may come in fits and starts. Nevertheless, if you're out to design robust architectures that make use of an component-based composition technology there should be something of interest for you.&lt;/P&gt;
&lt;P&gt;I hope this article has whet your appetite!&lt;/P&gt;
&lt;H2&gt;Sample Code&lt;/H2&gt;
&lt;P&gt;The &lt;A href="http://cid-668630fe3ecd2791.skydrive.live.com/self.aspx/.Public/InjectedContext.zip" mce_href="http://cid-668630fe3ecd2791.skydrive.live.com/self.aspx/.Public/InjectedContext.zip"&gt;sample code, based on MEF preview 3&lt;/A&gt;, is very, very rough, and is really only worth consulting if you need to step through some running code in order to clarify the concepts in this article. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9253987" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/Container+Managed+Application+Design/default.aspx">Container Managed Application Design</category><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/Autofac/default.aspx">Autofac</category></item></channel></rss>