<?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 : Autofac</title><link>http://blogs.msdn.com/nblumhardt/archive/tags/Autofac/default.aspx</link><description>Tags: Autofac</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Hosting MEF Extensions in an IoC Container</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/03/16/hosting-mef-extensions-in-an-ioc-container.aspx</link><pubDate>Tue, 17 Mar 2009 01:14:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9482064</guid><dc:creator>niblumha</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9482064.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9482064</wfw:commentRss><description>&lt;p&gt;There has been a lot of discussion among IoC container users about the similarities between MEF and IoC containers. Most of this has been addressing the question - “should MEF and an IoC container be used in the same app, or are they exclusive?”&lt;/p&gt;  &lt;p&gt;One possible answer to this question is outlined below. It isn’t by any means the definitive answer, but if these are technologies you find interesting, hopefully this article will give you plenty of food for thought.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;An IoC container is not required in order to use MEF. Unless you’re building or using an IoC container, this article probably won’t apply to you :).&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;h2&gt;Defining Extensibility Points&lt;/h2&gt;  &lt;p&gt;The &lt;a href="http://codeplex.com/MEF"&gt;Managed Extensibility Framework&lt;/a&gt; (MEF) in &lt;a href="http://www.microsoft.com/NET/"&gt;.NET 4.0&lt;/a&gt; introduces a &lt;strong&gt;standard model for defining and consuming application extensibility points&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;Central to this model is the &lt;em&gt;ComposablePartCatalog&lt;/em&gt; type and its subclasses:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_2.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="111" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb.png" width="598" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;em&gt;AssemblyCatalog&lt;/em&gt; and &lt;em&gt;DirectoryCatalog&lt;/em&gt; can read set of attributes for defining extensions:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_4.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="45" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_1.png" width="257" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here &lt;em&gt;Export&lt;/em&gt; marks the &lt;em&gt;Square&lt;/em&gt; class as an extension providing the &lt;em&gt;IShape&lt;/em&gt; contract.&lt;/p&gt;  &lt;p&gt;.NET 4.0 also introduces a class for assembling extensions into complete object graphs. This class is called &lt;em&gt;CompositionContainer&lt;/em&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_6.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="45" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_2.png" width="397" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The role that &lt;em&gt;CompositionContainer&lt;/em&gt; plays in a .NET 4.0 application is very similar to the role and IoC container might play in a .NET application today.&lt;/p&gt;  &lt;p&gt;This has raised a lot of interest among IoC container users, who want to know how they can take advantage of MEF in their applications. Should &lt;em&gt;CompositionContainer&lt;/em&gt;, for instance, replace their current IoC container, or can the two containers work together?&lt;/p&gt;  &lt;p&gt;The answer to this question is trickier than it seems, because there are substantial problems associated with having more than one container in an IoC-driven application:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;How do the containers mutually decide that an instance’s &lt;strong&gt;lifetime&lt;/strong&gt; is over? &lt;/li&gt;    &lt;li&gt;How is &lt;strong&gt;context&lt;/strong&gt; maintained, so that the correct type gets its correct dependencies even when they’re provided by the other container? &lt;/li&gt;    &lt;li&gt;If the graph building operation moves backwards and forwards between two containers, how can &lt;strong&gt;circular dependencies&lt;/strong&gt; be detected (without &lt;em&gt;StackOverflowException&lt;/em&gt;!) &lt;/li&gt;    &lt;li&gt;How can &lt;strong&gt;diagnostic&lt;/strong&gt; messages provide the ‘full picture’ when each container only knows about a portion of the graph being constructed? &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;(Some of this is a general problem with composing &lt;em&gt;frameworks &lt;/em&gt;in general, and is felt today by IoC container users integrating with UI frameworks in particular. It might be called ‘competing for control’.)&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;good news&lt;/strong&gt; is that there is at least one way to &lt;strong&gt;get some benefits of MEF&lt;/strong&gt; in an application that uses an IoC container &lt;strong&gt;without using two containers&lt;/strong&gt;.&lt;/p&gt;  &lt;h2&gt;MEF/IoC Container Roles&lt;/h2&gt;  &lt;p&gt;Although similar in many respects, the goals of MEF and IoC containers are different, and this gives each technology unique strengths.&lt;/p&gt;  &lt;p&gt;MEF is built for &lt;strong&gt;open systems&lt;/strong&gt; – scenarios in which the complete set of components that make up an application is defined at execution-time.&lt;/p&gt;  &lt;p&gt;This means that &lt;strong&gt;MEF&lt;/strong&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;supports self-describing components&lt;strong&gt; &lt;/strong&gt;that carry all of the information required to use them &lt;/li&gt;    &lt;li&gt;makes strong guarantees about how components will be loaded and manipulated, regardless of the execution environment &lt;/li&gt;    &lt;li&gt;includes robust tools for finding and loading components in different media &lt;/li&gt;    &lt;li&gt;allows components to be queried and inspected without necessarily loading or executing them &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;MEF is also part of the .NET Framework, so developers can learn how to use it once and then apply these skills in all MEF-enabled applications.&lt;/p&gt;  &lt;p&gt;IoC containers, on the other hand, have focused on highly-decoupled yet primarily &lt;strong&gt;closed&lt;/strong&gt; systems. I take the definition of ‘closed’ to mean that all application components are present during integration and testing.&lt;/p&gt;  &lt;p&gt;This makes typical &lt;strong&gt;IoC containers&lt;/strong&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;highly customizable to suit the internal architecture of a specific application &lt;/li&gt;    &lt;li&gt;extremely flexible in the criteria for what can be used as a component &lt;/li&gt;    &lt;li&gt;easy to wrap around existing frameworks and legacy code &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;These requirements lead in different directions when designing composition technologies, but appear together in some applications.&lt;/p&gt;  &lt;p&gt;Both MEF and typical IoC containers are flexible enough to fit to most scenarios; however, their &lt;em&gt;ideal &lt;/em&gt;roles might be:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;define third-party extension points using MEF &lt;/li&gt;    &lt;li&gt;locate and load these extensions using MEF &lt;/li&gt;    &lt;li&gt;structure and manage the host application’s fixed architecture using an IoC container &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Hosting MEF Catalogs&lt;/h2&gt;  &lt;p&gt;There are several approaches to integrating MEF and IoC containers. The one described here is probably the most powerful/complete/complex, those interested in other perspectives might start with &lt;a href="http://ondevelopment.blogspot.com/2009/03/unleashing-modules.html"&gt;this recent article by Patrik Hägne&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Most IoC containers, and MEF, use a layered architecture:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_10.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="171" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_4.png" width="287" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;MEF’s composition functionality is wrapped up in &lt;em&gt;CompositionContainer, &lt;/em&gt;and its component model in &lt;em&gt;ComposablePartDefinition&lt;/em&gt; and related types like &lt;em&gt;ComposablePartCatalog&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://autofac.org"&gt;Autofac&lt;/a&gt;, an open-source IoC container [&lt;em&gt;disclaimer: written by the author&lt;/em&gt;] follows the same typical pattern, using different types: &lt;em&gt;Container&lt;/em&gt; and &lt;em&gt;IComponentRegistration&lt;/em&gt;. (&lt;a href="http://funq.codeplex.com/"&gt;Funq&lt;/a&gt;, &lt;a href="http://code.google.com/p/linfu/"&gt;Linfu&lt;/a&gt;, &lt;a href="http://ninject.org"&gt;Ninject&lt;/a&gt;, &lt;a href="http://castleproject.com"&gt;Windsor&lt;/a&gt;, &lt;a href="http://springsource.com"&gt;Spring.NET&lt;/a&gt;, &lt;a href="http://structuremap.sourceforge.net/"&gt;StructureMap&lt;/a&gt; and &lt;a href="http://codeplex.com/Unity"&gt;Unity&lt;/a&gt; use similar abstractions.)&lt;/p&gt;  &lt;p&gt;Autofac and MEF used in the same application, without any integration, look like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_12.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="231" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_5.png" width="501" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The crossed-out arrow in the centre reflects the barrier that we’re trying to cross: the Autofac composition process won’t include components that are composed by MEF, and vice-versa.&lt;/p&gt;  &lt;p&gt;This kind of cross-over is frequently desirable, e.g.:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Application components invoke extensions &lt;/li&gt;    &lt;li&gt;Extensions need to access services provided by the host application &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The diagram below shows how this issue can be addressed without introducing the ‘two container’ problem:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_14.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="314" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_6.png" width="504" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Rather than hosting the MEF components in a separate container, an adapter is used so that the MEF components are presented to the Autofac container as Autofac components.&lt;/p&gt;  &lt;p&gt;The adapter allows MEF and Autofac components to participate in the composition process at the same time – each can have dependencies satisfied by the other.&lt;/p&gt;  &lt;p&gt;The IoC container is chosen as the ‘host’ because:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The application components are the ones most likely to integrate deeply into the container, and since they’re IoC container components it makes sense that they are hosted in the IoC container &lt;/li&gt;    &lt;li&gt;IoC containers don’t provide as robust a component mode as MEF does, so hosting components from any particular container in MEF is difficult &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So how does this look in practice?&lt;/p&gt;  &lt;h2&gt;Example Application: IoC and MEF&lt;/h2&gt;  &lt;p&gt;You can download a &lt;strong&gt;very contrived &lt;/strong&gt;example that demonstrates many of the moving parts &lt;a href="http://cid-668630fe3ecd2791.skydrive.live.com/self.aspx/.Public/Watcher.zip"&gt;here&lt;/a&gt;. &lt;strong&gt;This is not production quality design, so please don’t flame me for it :)&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The example is a task scheduler – perhaps you can imagine it running as a Windows service. It has two extension points: the tasks that will be run, and an output device to which notification messages can be sent.&lt;/p&gt;  &lt;p&gt;The extension points are regular MEF parts.&lt;/p&gt;  &lt;p&gt;The tasks export an interface and specify the interval at which they should be invoked via metadata attributes:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_8.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="75" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_3.png" width="562" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Notification services are similar, exporting the IMessageNotifier interface:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_16.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="78" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_7.png" width="511" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The container is created and Autofac services registered as usual:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_18.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="74" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_8.png" width="413" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;An extension method, RegisterCatalog(), allows a MEF DirectoryCatalog to be supplied to the container:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_20.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="93" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_9.png" width="508" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Note the list of services that will be imported from the catalog into the application. Parts can still export and import other contracts, but these won’t be available to Autofac components. This keeps the extensibility points of the application clearly defined.&lt;/p&gt;  &lt;p&gt;Similarly, Autofac services have to be explicitly marked when they will be provided to MEF components:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_22.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="57" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_10.png" width="337" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;There’s an example of an Autofac component consuming a mix of Autofac and MEF services:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_24.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="92" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_11.png" width="269" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As well as MEF components consuming Autofac services:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_26.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="63" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/IntegratingMEFandIoCContainers_B514/image_thumb_12.png" width="425" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Everything feels natural, although there are a few awkward issues surrounding the consumption of MEF metadata in Autofac services. These should go away once the MEF Export type is a framework type (it won’t feel so leaky to inject exports into Autofac components.)&lt;/p&gt;  &lt;h2&gt;Limitations&lt;/h2&gt;  &lt;p&gt;MEF defines how individual components will be manipulated, &lt;em&gt;not&lt;/em&gt; how they will be assembled into complete object graphs.&lt;/p&gt;  &lt;p&gt;This makes sense for open systems, because regardless of how components are packaged, they should not make assumptions about their host environment beyond the contracts that their immediate dependencies supply.&lt;/p&gt;  &lt;p&gt;When designing components that can be used in multiple applications, each component needs to be treated independently. MEF discourages thinking about catalogs of components as subsystems with a fixed configuration.&lt;/p&gt;  &lt;p&gt;The biggest side-effect of these decisions is that while MEF &lt;strong&gt;components&lt;/strong&gt; behave consistently whether hosted in MEF or another container, &lt;strong&gt;groups of components &lt;/strong&gt;will probably behave differently.&lt;/p&gt;  &lt;p&gt;MEF’s container also offers a different set of features from the typical IoC container – these may be sorely missed in highly-extensible applications, so if high extensibility is the goal the MEF container alone is likely a better choice.&lt;/p&gt;  &lt;h2&gt;Status&lt;/h2&gt;  &lt;p&gt;It will be some time before there is much empirical data on the benefits and pitfalls of this technique – YMMV, so be sure to carefully evaluate your needs before starting down this path. &lt;strong&gt;Experimental&lt;/strong&gt; is probably the appropriate tag!&lt;/p&gt;  &lt;p&gt;.NET 4.0 and MEF are not yet released, so these techniques may not even apply to the production version.&lt;/p&gt;  &lt;p&gt;There is a &lt;a href="http://mef.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=62133"&gt;whitepaper on the MEF site&lt;/a&gt; describing some of the technical requirements of the approach.&lt;/p&gt;  &lt;p&gt;Some notes on the Autofac adapter are available on the &lt;a href="http://code.google.com/p/autofac/wiki/MefIntegration"&gt;Autofac wiki&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9482064" 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/Autofac/default.aspx">Autofac</category></item><item><title>IoC on Silverlight</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/01/04/ioc-on-silverlight.aspx</link><pubDate>Sun, 04 Jan 2009 20:48:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9271594</guid><dc:creator>niblumha</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9271594.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9271594</wfw:commentRss><description>&lt;p&gt;A number of IoC containers have &lt;a href="http://silverlight.net/"&gt;Silverlight&lt;/a&gt; versions - &lt;a href="http://kohari.org/2008/06/16/ninject-10-goes-gold/"&gt;Ninject&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/dd362339.aspx"&gt;Unity&lt;/a&gt; especially seem to have healthy Silverlight offerings. Silverlight 2.0 is a variant of the .NET runtime, so this isn't too surprising.&lt;/p&gt;  &lt;p&gt;Thanks to Tyson Stolarski and &lt;a href="http://rabdullin.com/"&gt;Rinat Abdullin&lt;/a&gt;, Autofac recently joined their ranks with a &lt;a href="http://code.google.com/p/autofac/downloads/list"&gt;Silverlight-compatible build&lt;/a&gt;. (This one is hot off the press, so test it thoroughly before you set out to build anything mission-critical :))&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.codeplex.com/MEF"&gt;MEF&lt;/a&gt; hasn't released any concrete plans for Silverlight, but experimenters can probably coerce the Codeplex source into a compatible build by hacking the .csproj file.&lt;/p&gt;  &lt;p&gt;If you're reading this blog and use IoC on Silverlight in one form or another:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;What have your experiences been like?&lt;/li&gt;    &lt;li&gt;Do you use IoC in the UI, the 'back-end' or both?&lt;/li&gt;    &lt;li&gt;Does loose coupling/runtime composition get in the way of XAML-based designers?&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I'd love to hear your thoughts!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9271594" 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/Autofac/default.aspx">Autofac</category><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/Silverlight/default.aspx">Silverlight</category></item><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>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>