<?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</title><link>http://blogs.msdn.com/nblumhardt/default.aspx</link><description /><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Homeward Bound</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/09/29/homeward-bound.aspx</link><pubDate>Wed, 30 Sep 2009 00:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9900994</guid><dc:creator>niblumha</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9900994.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9900994</wfw:commentRss><description>&lt;p align="left"&gt;The last year in Redmond has been truly amazing. Besides working with the great people in the .NET team and elsewhere at Microsoft – an experience that can’t be done justice in a brief blog post like this – I’ve also had a chance to take in Seattle and to explore the incredible Pacific Northwest.&lt;/p&gt;  &lt;p align="left"&gt;It might come as a bit of a surprise then to hear that I’m leaving the US to return to Brisbane, Australia. Despite the many attractions of being here, I’ve found that it is too far to be away from my family and friends.&lt;/p&gt;  &lt;p align="left"&gt;This Friday will be my last day here, and this blog will shortly be going into ‘read-only’ mode, so please &lt;a mce_href="http://twitter.com/nblumhardt" href="http://twitter.com/nblumhardt"&gt;find me on Twitter&lt;/a&gt; if you’d like to keep in touch.&lt;/p&gt;  &lt;p align="left"&gt;Nick&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9900994" width="1" height="1"&gt;</description></item><item><title>Debug Composition from within Visual Studio</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/09/24/debug-composition-from-within-visual-studio.aspx</link><pubDate>Thu, 24 Sep 2009 22:12:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9899106</guid><dc:creator>niblumha</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9899106.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9899106</wfw:commentRss><description>&lt;p&gt;This morning &lt;a href="http://davesbox.com"&gt;Dave&lt;/a&gt; came up with a great suggestion for finding MEF composition problems from within Visual Studio.&lt;/p&gt;  &lt;p&gt;By registering the composition analyzer as an external tool, the &lt;a href="http://blogs.msdn.com/nblumhardt/archive/2009/08/28/analyze-mef-assemblies-from-the-command-line.aspx"&gt;root cause of a composition problem&lt;/a&gt; can be located in a flash!&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;Step 1 - Obtain MEFX.EXE&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;First, you’ll need to build &lt;strong&gt;mefx.exe&lt;/strong&gt; from the &lt;a href="http://mef.codeplex.com/Release/ProjectReleases.aspx"&gt;MEF Codeplex Drop&lt;/a&gt; (at this time, in the non-Silverlight zip files under /Samples/CompositionDiagnostics).&lt;/p&gt;  &lt;p&gt;For demo purposes I’ve placed this (and Microsoft.ComponentModel.Composition.Diagnostics.dll) in C:\Tools.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;Step 2 – Register MEFX.EXE as an External Tool&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In Visual Studio, select the &lt;em&gt;Tools &amp;gt; External Tools&lt;/em&gt; menu item. This will bring up a dialog like the following:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DebugCompositionfromwithinVisualStudio_9913/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DebugCompositionfromwithinVisualStudio_9913/image_thumb.png" width="488" height="486" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here you’ll need to click &lt;em&gt;Add&lt;/em&gt; and enter the information above. Importantly, select the &lt;em&gt;&lt;strong&gt;Use Output Window&lt;/strong&gt;&lt;/em&gt; option.&lt;/p&gt;  &lt;p&gt;Note the &lt;em&gt;/causes&lt;/em&gt; and &lt;em&gt;/verbose &lt;/em&gt;switches. This tells mefx to find parts with missing dependencies that aren’t simply the result of &lt;a href="http://mef.codeplex.com/Wiki/View.aspx?title=Debugging%20and%20Diagnostics"&gt;‘cascading’ failures&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;Step 3 – Run Composition Diagnostics&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Now that the tooling is set up, finding those composition problems is a breeze!&lt;/p&gt;  &lt;p&gt;In my example application, I’m unable to resolve instances of &lt;em&gt;Extensions.Abc&lt;/em&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DebugCompositionfromwithinVisualStudio_9913/image_10.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DebugCompositionfromwithinVisualStudio_9913/image_thumb_4.png" width="238" height="25" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The exception message tells me that no instances of &lt;em&gt;Abc &lt;/em&gt;are available, but I &lt;strong&gt;know&lt;/strong&gt; that it is there in my catalog, so something else must be wrong…&lt;/p&gt;  &lt;p&gt;In Solution Explorer, I select the executable project:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DebugCompositionfromwithinVisualStudio_9913/image_8.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DebugCompositionfromwithinVisualStudio_9913/image_thumb_3.png" width="228" height="117" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Then, under the &lt;em&gt;Tools&lt;/em&gt; menu, I click the &lt;em&gt;Debug Composition&lt;/em&gt; item that we added in Step 2:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DebugCompositionfromwithinVisualStudio_9913/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DebugCompositionfromwithinVisualStudio_9913/image_thumb_1.png" width="442" height="484" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This will analyze all of the assemblies in the output directory of the application, to find the root cause of the composition failure.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;NOTE: Currently, only .DLL files are searched, so parts in .EXE files won’t be included in analysis. In a future version of MEFX this will change, but for now, I recommend keeping all parts in .DLL files.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The output of the tool is piped to the &lt;em&gt;Output&lt;/em&gt; window:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DebugCompositionfromwithinVisualStudio_9913/image_6.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DebugCompositionfromwithinVisualStudio_9913/image_thumb_2.png" width="644" height="219" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Let’s break down this output line-by-line.&lt;/p&gt;  &lt;p&gt;First, the name of the problematic part is displayed:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;[Part] Extensions.Def from: DirectoryCatalog (Path=&amp;quot;c:\Workspace\ExampleExtensibleApp\Extensions.dll\bin\Debug\&amp;quot;)       &lt;br /&gt;&amp;#160; [Primary Rejection]&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The text &lt;em&gt;Primary Rejection &lt;/em&gt;indicates that the analysis identified this as a root cause.&lt;/p&gt;  &lt;p&gt;We then see a bit more information about the part, and the status of each of its imports. The problematic import is shown here:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;&amp;#160; [Import] Extensions.Def.Ghi (ContractName=&amp;quot;c&amp;quot;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; [Exception] System.ComponentModel.Composition.ImportCardinalityMismatchException: No valid exports were found that match the constraint '(((exportDefinition.ContractName == &amp;quot;c&amp;quot;) AndAlso (exportDefinition.Metadata.ContainsKey(&amp;quot;ExportTypeIdentity&amp;quot;) AndAlso &amp;quot;Extensions.Ghi&amp;quot;.Equals(exportDefinition.Metadata.get_Item(&amp;quot;ExportTypeIdentity&amp;quot;)))) AndAlso ((Not(exportDefinition.Metadata.ContainsKey(&amp;quot;System.ComponentModel.Composition.CreationPolicy&amp;quot;)) OrElse Any.Equals(exportDefinition.Metadata.get_Item(&amp;quot;System.ComponentModel.Composition.CreationPolicy&amp;quot;))) OrElse Shared.Equals(exportDefinition.Metadata.get_Item(&amp;quot;System.ComponentModel.Composition.CreationPolicy&amp;quot;))))', invalid exports may have been rejected.        &lt;br /&gt;&amp;#160;&amp;#160; at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)        &lt;br /&gt;&amp;#160;&amp;#160; at Microsoft.ComponentModel.Composition.Diagnostics.CompositionInfo.AnalyzeImportDefinition(ExportProvider host, IEnumerable`1 availableParts, ImportDefinition id) in C:\Users\niblumha\Desktop\Microsoft.ComponentModel.Composition.Diagnostics\CompositionInfo.cs:line 157&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;There’s a lot of information here, but the important thing to note is that the import is for property &lt;em&gt;Ghi&lt;/em&gt;, which imports contract “&lt;em&gt;c”&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;We now know which import in the composition is problematic, and can possibly determine our root cause manually from this information.&lt;/p&gt;  &lt;p&gt;More interestingly though, the tool provides some suggestions for fixing this error. Here we’re told that there is a part in the system that &lt;em&gt;looks like&lt;/em&gt; it could satisfy the import, but has some problems:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;&amp;#160;&amp;#160;&amp;#160; [Unsuitable] Extensions.Ghi (ContractName=&amp;quot;c&amp;quot;) from: Extensions.Ghi from: DirectoryCatalog (Path=&amp;quot;c:\Workspace\ExampleExtensibleApp\Extensions.dll\bin\Debug\&amp;quot;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Because] TypeIdentity, The export is a 'System.Object', but the import requires 'Extensions.Ghi'. These types must match exactly (conversions are not supported.)        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Because] CreationPolicy, The import requires creation policy 'Shared', but the exporting part only supports 'NonShared'.&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Diagnostics in MEF are a high priority for us. The declarative nature of MEF composition gives us a lot of interesting opportunities for tooling, so expect to see more work along these lines.&lt;/p&gt;  &lt;p&gt;As always, feedback is appreciated!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9899106" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/MEF/default.aspx">MEF</category></item><item><title>Parameterised Triggers and Re-Entrant States in Stateless v2</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/08/30/parameterised-triggers-and-re-entrant-states-in-stateless-v2.aspx</link><pubDate>Sun, 30 Aug 2009 04:34:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9889032</guid><dc:creator>niblumha</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9889032.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9889032</wfw:commentRss><description>&lt;p&gt;Since working on a &lt;em&gt;clinical outcomes review system&lt;/em&gt; a couple of years ago, I’ve been aware of the gap between simple hand-coded workflows and the full-blown workflow tools.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://stateless.googlecode.com"&gt;Stateless&lt;/a&gt; embodies the idea that a state machine can use closures to &lt;strong&gt;implement workflow without taking on persistence responsibilities&lt;/strong&gt;. So long as all of the data used to drive the state machine is external to it, a regular ORM or other persistence mechanism can deal with identity, persistence and transactional issues. This substantially reduces the technical complexity of the framework.&lt;/p&gt;  &lt;p&gt;As a ‘fun’ project, I haven’t done much other than maintain the site since the first version was finished. The second version extends the paradigm just a little, but in doing so makes a wider range of scenarios cleanly approachable.&lt;/p&gt;  &lt;p&gt;To illustrate the new features I’ll draw on &lt;a href="http://www.odetocode.com/Articles/460.aspx"&gt;Scott Allen’s nifty bug tracker example&lt;/a&gt; for Windows Workflow.&lt;/p&gt;  &lt;h2&gt;Parameterised Triggers&lt;/h2&gt;  &lt;p&gt;The triggers that drive transitions in a state machine are approximately analogous to events or commands. Like events and commands, triggers can be associated with parameters.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;When a bug is &lt;em&gt;assigned&lt;/em&gt;, the parameter may be the &lt;em&gt;assignee&lt;/em&gt;; &lt;/li&gt;    &lt;li&gt;When an order is &lt;em&gt;cancelled&lt;/em&gt;, the parameter may be the &lt;em&gt;reason&lt;/em&gt;; &lt;/li&gt;    &lt;li&gt;etc. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Modelling these details in Stateless, I adopted the some design goals and constraints:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Retain the existing simple syntax for non-parameterised triggers; &lt;/li&gt;    &lt;li&gt;Execute parameter-driven logic &lt;em&gt;during&lt;/em&gt; the transition, i.e. &lt;em&gt;after&lt;/em&gt; the exit events for the last state have fired; &lt;/li&gt;    &lt;li&gt;Make it clear to the user exactly when parameter-driven logic will execute; &lt;/li&gt;    &lt;li&gt;Control interference between parameter-driven logic and guard conditions; &lt;/li&gt;    &lt;li&gt;Ensure parameter type-safety at compile time. &lt;/li&gt; &lt;/ol&gt;  &lt;h3&gt;New APIs&lt;/h3&gt;  &lt;p&gt;To associate parameters with a trigger, &lt;em&gt;StateMachine &lt;/em&gt;provides the &lt;em&gt;SetTriggerParameters()&lt;/em&gt; generic methods:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ParameterisedTriggersandReentrantStatesi_9950/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="67" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ParameterisedTriggersandReentrantStatesi_9950/image_thumb_2.png" width="569" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;em&gt;SetTriggerParameters() &lt;/em&gt;accepts one generic parameter for each of the trigger’s parameters, so in this case the &lt;em&gt;Assign&lt;/em&gt; trigger is being associated with a single parameter of type &lt;em&gt;string&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;This method is called once, when the state machine is being configured.&lt;/p&gt;  &lt;p&gt;The return value in this case is a &lt;em&gt;TriggerWithParameters&amp;lt;string&amp;gt;&lt;/em&gt; object that lets us invoke the &lt;em&gt;Fire() &lt;/em&gt;method in a strongly-typed way:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ParameterisedTriggersandReentrantStatesi_9950/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="28" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ParameterisedTriggersandReentrantStatesi_9950/image_thumb_1.png" width="388" border="0" /&gt;&lt;/a&gt;The C# compiler will use inference to determine that the parameter to the trigger is of type &lt;em&gt;string&lt;/em&gt;. Attempting to fire &lt;em&gt;assignTrigger&lt;/em&gt; with any other parameters will be rejected at compile-time.&lt;/p&gt;  &lt;p&gt;Now, all of this would be fairly pointless if &lt;em&gt;Fire()&lt;/em&gt; was the only place that the parameters needed checking. However, the firing of the trigger is only half of the story.&lt;/p&gt;  &lt;p&gt;When the machine transitions into the new state, logic based on the trigger parameters will need to run:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ParameterisedTriggersandReentrantStatesi_9950/image_8.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="62" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ParameterisedTriggersandReentrantStatesi_9950/image_thumb_3.png" width="544" border="0" /&gt;&lt;/a&gt;Because &lt;em&gt;assignTrigger&lt;/em&gt; is provided to the &lt;em&gt;OnEntryFrom()&lt;/em&gt; method, the compiler knows that the provided entry action accepts a single parameter of type &lt;em&gt;string&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;Putting everything together behind the facade of a domain object leads to a natural, understandable design:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ParameterisedTriggersandReentrantStatesi_9950/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="73" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ParameterisedTriggersandReentrantStatesi_9950/image_thumb_4.png" width="353" border="0" /&gt;&lt;/a&gt; A more complete example is in the &lt;a href="http://code.google.com/p/stateless/source/browse/#hg/BugTrackerExample"&gt;Stateless Mercurial repository&lt;/a&gt;.&lt;/p&gt;  &lt;h3&gt;Alternatives&lt;/h3&gt;  &lt;p&gt;One alternative I considered but later discarded was to parameterise the &lt;em&gt;states&lt;/em&gt; rather than the triggers, something that seems to be more established in traditional state machine models. After a good deal of unsuccessful experimentation I concluded that state parameterisation isn’t as useful as the trigger-based version.&lt;/p&gt;  &lt;h2&gt;Re-Entrant States&lt;/h2&gt;  &lt;p&gt;Stateless encourages you to attach program logic to &lt;em&gt;entry &lt;/em&gt;and &lt;em&gt;exit &lt;/em&gt;events for the states in your state machine.&lt;/p&gt;  &lt;p&gt;One of the edge cases that the bug tracker example reveals is re-initialisation of an already active state. When the bug is assigned, an email might be sent to the assignee. If &lt;em&gt;Assign() &lt;/em&gt;is called again, to reassign the bug to someone else, it makes sense to re-initialise the &lt;em&gt;Assigned&lt;/em&gt; state by executing the exit actions then executing the entry actions again.&lt;/p&gt;  &lt;p&gt;To enable this behaviour for a state, the &lt;em&gt;PermitReentry() &lt;/em&gt;method is supplied:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ParameterisedTriggersandReentrantStatesi_9950/image_12.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="82" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/ParameterisedTriggersandReentrantStatesi_9950/image_thumb.png" width="536" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The previous version of Stateless considered a self-transition to be the same as an ignored trigger. Version 2 requires that self-transitions are either explicitly ignored, or configured as re-entrant.&lt;/p&gt;  &lt;h2&gt;Next?&lt;/h2&gt;  &lt;p&gt;These small additions were actually quite a challenge, but Stateless is still a very small library. One idea I’m toyed with is to use it as a ‘kernel’ for higher-level state machine functionality, e.g. an XML or DSL-driven framework. If you have ideas or requests, feel free to visit the &lt;a href="http://stateless.uservoice.com"&gt;UserVoice forum&lt;/a&gt;!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9889032" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/Stateless/default.aspx">Stateless</category></item><item><title>Analyze MEF Assemblies from the Command Line</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/08/28/analyze-mef-assemblies-from-the-command-line.aspx</link><pubDate>Fri, 28 Aug 2009 20:45:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9888696</guid><dc:creator>niblumha</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9888696.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9888696</wfw:commentRss><description>&lt;p&gt;In &lt;a href="http://blogs.msdn.com/nblumhardt/archive/2009/07/09/mef-preview-6-available.aspx"&gt;MEF Preview 6&lt;/a&gt; we shipped a sample assembly called &lt;em&gt;Microsoft.ComponentModel.Composition.Diagnostics&lt;/em&gt;, demonstrating the kinds of things that can be determined by (semi-) statically analyzing MEF catalogs.&lt;/p&gt;  &lt;p&gt;With &lt;a href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30259#ReleaseFiles"&gt;Preview 7&lt;/a&gt; we added a utility that makes use of the diagnostics routines to print information about parts directly from the command-line.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2" face="Courier New"&gt;C:\Users\...\CompositionDiagnostics&amp;gt; &lt;strong&gt;mefx /?&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /? &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Print usage. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /causes &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List root causes - parts with errors not related to the rejection of other parts. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /dir:C:\MyApp\Parts &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Specify directories to search for parts. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /exporters:MyContract &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List exporters of the given contract. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /exports &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Find exported contracts. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /file:MyParts.dll &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Specify assemblies to search for parts. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /importers:MyContract &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List importers of the given contract. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /imports &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Find imported contracts. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /parts &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List all parts found in the source assemblies. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /rejected &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List all rejected parts. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /type:MyNamespace.MyType &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Print details of the given part type. &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; /verbose &lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Print verbose information on each part.&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The &lt;em&gt;&lt;strong&gt;/parts&lt;/strong&gt;&lt;/em&gt; switch list all parts in a composition:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2" face="Courier New"&gt;C:\Users\...\CompositionDiagnostics&amp;gt;&lt;strong&gt; mefx /dir:..\MefStudio /parts&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;Designers.CSharp.Commands&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;Designers.BasicComponentFactory&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;Designers.CSharpFormFactory&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;...&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;While the&lt;em&gt; &lt;strong&gt;/rejected&lt;/strong&gt;&lt;/em&gt; and &lt;em&gt;&lt;strong&gt;/causes&lt;/strong&gt;&lt;/em&gt; switches will print information about rejected parts and suspected root causes respectively.&lt;/p&gt;  &lt;p&gt;By specifying the&lt;strong&gt; /verbose&lt;/strong&gt; switch, detailed information about parts can be retrieved:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2" face="Courier New"&gt;C:\Users\...\CompositionDiagnostics&amp;gt; &lt;/font&gt;&lt;font size="2" face="Courier New"&gt;&lt;strong&gt;mefx /dir:..\MefStudio /type:Designers.BasicComponentFactory /verbose&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;[Part] Designers.BasicComponentFactory from: DirectoryCatalog (Path=&amp;quot;..\MefStudio&amp;quot;)&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; [Export] Designers.BasicComponentFactory (ContractName=&amp;quot;Contracts.HostSurfaceFactory&amp;quot;)&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; [Import] Contracts.HostSurfaceFactory.propertyGrid (ContractName=&amp;quot;Contracts.IPropertyGrid&amp;quot;)&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; [Actual] ToolWindows.PropertyGridWindow (ContractName=&amp;quot;Contracts.IPropertyGrid&amp;quot;) from: ToolWindows.PropertyGridWindow from: DirectoryCatalog (Path=&amp;quot;..\MefStudio&amp;quot;)&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160; [Import] Contracts.HostSurfaceFactory.Commands (ContractName=&amp;quot;System.Void(Contracts.HostSurface)&amp;quot;)&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; [Actual] Designers.CSharp.Commands.Cut (ContractName=&amp;quot;System.Void(Contracts.HostSurface)&amp;quot;) from: Designers.CSharp.Commands from: DirectoryCatalog (Path=&amp;quot;..\MefStudio&amp;quot;)&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; [Actual] Designers.CSharp.Commands.Copy (ContractName=&amp;quot;System.Void(Contracts.HostSurface)&amp;quot;) from: Designers.CSharp.Commands from: DirectoryCatalog (Path=&amp;quot;..\MefStudio&amp;quot;)&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font size="2" face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; [Actual] Designers.CSharp.Commands.Paste (ContractName=&amp;quot;System.Void(Contracts.HostSurface)&amp;quot;) from: Designers.CSharp.Commands from: DirectoryCatalog (Path=&amp;quot;..\MefStudio&amp;quot;)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;There are a few more goodies in there for the curious.&lt;/p&gt;  &lt;p&gt;It is important to realise that the utility can only analyze MEF assemblies built against a compatible version of MEF; for example, &lt;em&gt;mefx.exe &lt;/em&gt;built against the CodePlex drops will not be able to analyze assemblies built against the signed .NET Framework version of MEF, and vice-versa.&lt;/p&gt;  &lt;p&gt;We call these “samples” right because they’re for the purpose of our own exploration. Some experience with applying these techniques will hopefully lead to some great production-quality tools in a future milestone.&lt;/p&gt;  &lt;p&gt;One particular direction I’d like to venture is towards tooling suitable for continuous integration (CI), for example, a build task capable of validating component status.&lt;/p&gt;  &lt;p&gt;If you have ideas for improvement or an opportunity to try &lt;em&gt;mefx&lt;/em&gt;, we’d love to hear from you.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9888696" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/MEF/default.aspx">MEF</category></item><item><title>Dynamic Part Instantiation in MEF</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/08/28/dynamic-part-instantiation-in-mef.aspx</link><pubDate>Fri, 28 Aug 2009 03:57:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9887690</guid><dc:creator>niblumha</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9887690.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9887690</wfw:commentRss><description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;Disclaimer: &lt;/strong&gt;This functionality is &lt;strong&gt;not&lt;/strong&gt; shipping in .NET 4 (but is available in the &lt;a href="http://codeplex.com/mef"&gt;recent CodePlex drops&lt;/a&gt;, see below for more details.)&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;The golden rule of using composition to simplify your architecture is to &lt;a href="http://blogs.msdn.com/nblumhardt/archive/2008/12/27/container-managed-application-design-prelude-where-does-the-container-belong.aspx"&gt;avoid calling the container directly&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Until now, MEF users have had to jump through hoops to observe this rule in some circumstances. The most common of these is dynamic part creation – creating instances of parts on-the-fly in response to application events.&lt;/p&gt;  &lt;p&gt;To alleviate this, the latest &lt;strong&gt;Silverlight version of MEF&lt;/strong&gt; in “Preview 7” introduces a&lt;em&gt; &lt;/em&gt;&lt;a href="http://blogs.msdn.com/nblumhardt/archive/2009/01/04/declarative-context-adapters.aspx"&gt;&lt;em&gt;Declarative Context Adapter&lt;/em&gt;&lt;/a&gt;&lt;em&gt; &lt;/em&gt;called &lt;em&gt;PartCreator&amp;lt;T&amp;gt;&lt;/em&gt;. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_12.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_thumb_5.png" width="557" height="337" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h3&gt;PartCreator&amp;lt;T&amp;gt;&lt;/h3&gt;  &lt;p&gt;Imagine we’re writing a pluggable text editor. The application uses a part to represent the document being edited:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_thumb.png" width="309" height="127" /&gt;&lt;/a&gt; Whenever the user clicks the ‘New’ button, the editor window creates a new instance:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_thumb_1.png" width="381" height="286" /&gt;&lt;/a&gt; There are a few important things to notice here:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Every time &lt;em&gt;OnNewDocument()&lt;/em&gt; executes, a new instance supporting &lt;em&gt;IDocument&lt;/em&gt; is created (in this case the actual object will be of type &lt;em&gt;TextDocument,&lt;/em&gt;) &lt;/li&gt;    &lt;li&gt;When MEF sees an &lt;em&gt;[Import]&lt;/em&gt; attribute on a member of type &lt;em&gt;PartCreator&amp;lt;T&amp;gt;&lt;/em&gt;, it will use the same rules as for &lt;em&gt;Lazy&amp;lt;T&amp;gt; &lt;/em&gt;to determine the appropriate contract, &lt;/li&gt;    &lt;li&gt;The parameter &lt;em&gt;T&lt;/em&gt; is the type of the exported value, not the type of the underlying part implementation, &lt;/li&gt;    &lt;li&gt;The return value from &lt;em&gt;CreatePart()&lt;/em&gt; is a wrapper class, &lt;em&gt;PartLifetimeContext&amp;lt;T&amp;gt;&lt;/em&gt;, that implements IDisposable and must be used to clean up the part when it is no longer required, &lt;/li&gt;    &lt;li&gt;&lt;em&gt;PartLifetimeContext&amp;lt;T&amp;gt; &lt;/em&gt;has a property &lt;em&gt;Value&lt;/em&gt; of type &lt;em&gt;T&lt;/em&gt;, in this case &lt;em&gt;T&lt;/em&gt; will be &lt;em&gt;IDocument&lt;/em&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;MEF supports &lt;em&gt;PartCreator&amp;lt;T&amp;gt;&lt;/em&gt; implicitly, in a very similar way to &lt;em&gt;Lazy&amp;lt;T&amp;gt;.&lt;/em&gt; There is no additional code required to get this behaviour.&lt;/p&gt;  &lt;h3&gt;PartCreator&amp;lt;T, TMetadata&amp;gt;&lt;/h3&gt;  &lt;p&gt;Many scenarios for dynamic instantiation need to select the appropriate part to instantiate from several different implementations.&lt;/p&gt;  &lt;p&gt;In this example, imagine we’re building a simple MVC web framework (don’t confuse this with ASP.NET MVC; this is just a made-up exercise, not a serious MVC framework design.)&lt;/p&gt;  &lt;p&gt;Developers create controllers to handle the various URLs that the web application responds to. Each controller exports the &lt;em&gt;IController&lt;/em&gt; contract, and adds some &lt;a href="http://mef.codeplex.com/Wiki/View.aspx?title=Exports%20and%20Metadata"&gt;metadata&lt;/a&gt; describing the route that the controller handles.&lt;/p&gt;  &lt;p&gt;Here’s part of the controller for the &lt;em&gt;/home &lt;/em&gt;page:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_thumb_2.png" width="336" height="128" /&gt;&lt;/a&gt; Down in the plumbing of our MVC framework, the request handler is going to have to decide which of the available controllers will be invoked by an incoming request.&lt;/p&gt;  &lt;p&gt;To do this, the &lt;em&gt;RequestHandler &lt;/em&gt;part imports an array of &lt;em&gt;PartCreator&amp;lt;IController, IControllerMetadata&amp;gt;&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;When a request comes in (see the &lt;em&gt;HandleRequest() &lt;/em&gt;method) the appropriate part creator is chosen and used to create a controller:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_thumb_3.png" width="528" height="441" /&gt;&lt;/a&gt; Notice that the &lt;em&gt;controller&lt;/em&gt; variable is used in a &lt;em&gt;using &lt;/em&gt;block. This ensures that the controller and any other resources that were used to construct it will be cleaned up after the request is handled.&lt;/p&gt;  &lt;h3&gt;Emulating PartCreator&amp;lt;T&amp;gt; on .NET 4&lt;/h3&gt;  &lt;p&gt;&lt;em&gt;PartCreator&amp;lt;T&amp;gt; &lt;/em&gt;is not included in the .NET 4 builds of MEF – it was developed as part of the Silverlight MEF support (where &lt;em&gt;CompositionContainer&lt;/em&gt; is much less accessible.) However, with a little work it is possible to emulate some of the &lt;em&gt;PartCreator&amp;lt;T&amp;gt; &lt;/em&gt;functionality.&lt;/p&gt;  &lt;p&gt;Included with the .NET beta-compatible version of MEF is a sample that supports a limited subset of the feature, under &lt;em&gt;Microsoft.ComponentModel.Composition.DynamicInstantiation&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;This assembly includes versions of the part creator types, and an &lt;em&gt;ExportProvider&lt;/em&gt; that handles requests for &lt;em&gt;PartCreator&amp;lt;T&amp;gt;&lt;/em&gt; when added to the container.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_10.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DynamicPartCreationinMEF_BFC9/image_thumb_4.png" width="572" height="76" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This sample doesn’t fully support all container features, and (as you can tell by the lack of unit tests) may blow up in unexpected ways. Instantiation performance is also not guaranteed, thanks to O(n^2) time complexity on the number of exporters of the contract being instantiated.&lt;/p&gt;  &lt;p&gt;We’re including this so that .NET 4 users can experiment with the part creator APIs; it is not yet recommended for production use.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9887690" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/MEF/default.aspx">MEF</category></item><item><title>Implementing Optional Exports with MEF Stable Composition</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/07/17/light-up-or-mef-optional-exports.aspx</link><pubDate>Fri, 17 Jul 2009 20:59:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9837689</guid><dc:creator>niblumha</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9837689.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9837689</wfw:commentRss><description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;Disclaimer: &lt;/strong&gt;As usual, this blog post is discussing pre-release software, which may differ from the final released version.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ayende.com/Blog/archive/2008/09/25/the-managed-extensibility-framework.aspx"&gt;Ayende once observed&lt;/a&gt; that &lt;a href="http://codeplex.com/mef"&gt;MEF&lt;/a&gt; is very focused on dependency management. It’s an accurate description of the driving force that has shaped MEF as it is today.&lt;/p&gt;  &lt;p&gt;Dependency management doesn’t end at build time with assembly references. Functional dependencies are more complex and harder to control than static compilation unit dependencies. MEF has some nifty tricks up its sleeve to help out with this task.&lt;/p&gt;  &lt;p&gt;Imagine the example of a retail management system:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="399"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="101"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="99"&gt;         &lt;p align="center"&gt;&lt;strong&gt;Standard&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="98"&gt;         &lt;p align="center"&gt;&lt;strong&gt;Pro Service&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="99"&gt;         &lt;p align="center"&gt;&lt;strong&gt;Platinum&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="101"&gt;&lt;strong&gt;Service Schedule&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="99"&gt;         &lt;p align="center"&gt;&lt;strong&gt;&amp;#160;&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="98"&gt;         &lt;p align="center"&gt;&lt;strong&gt;X&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="99"&gt;         &lt;p align="center"&gt;&lt;strong&gt;X&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="101"&gt;&lt;strong&gt;Stock Control&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="99"&gt;         &lt;p align="center"&gt;&lt;strong&gt;X&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="98"&gt;         &lt;p align="center"&gt;&lt;strong&gt;&amp;#160;&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="99"&gt;         &lt;p align="center"&gt;&lt;strong&gt;X&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="101"&gt;&lt;strong&gt;Client List&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="99"&gt;         &lt;p align="center"&gt;&lt;strong&gt;&amp;#160;&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="98"&gt;         &lt;p align="center"&gt;&lt;strong&gt;X&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="99"&gt;         &lt;p align="center"&gt;&lt;strong&gt;X&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="101"&gt;&lt;strong&gt;Marketing&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="99"&gt;&lt;strong&gt;&amp;#160;&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="98"&gt;&lt;strong&gt;&amp;#160;&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="100"&gt;         &lt;p align="center"&gt;&lt;strong&gt;X&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;The &lt;em&gt;AdventureWorks Store Manager &lt;/em&gt;application has many features. To support pay-for-play licensing, the company sells several versions (‘SKUs’) targeting different market segments, with different features enabled.&lt;/p&gt;  &lt;p&gt;All of these versions are subsets of the same essential codebase. Each SKU, or configuration, includes the relevant pieces.&lt;/p&gt;  &lt;h3&gt;Mapping Features to Components&lt;/h3&gt;  &lt;p&gt;The implementation of each feature can involve more than one component. Part of the system might look like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/LightuporMEFOptionalExports_AA58/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/LightuporMEFOptionalExports_AA58/image_thumb_1.png" width="394" height="329" /&gt;&lt;/a&gt; A quick description:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;em&gt;PointOfSale&lt;/em&gt; lets a storeperson process sales&lt;/li&gt;    &lt;li&gt;&lt;em&gt;PointOfSale&lt;/em&gt; uses a collection of &lt;em&gt;ItemLookup&lt;/em&gt;s representing each kind of thing that can be sold&lt;/li&gt;    &lt;li&gt;&lt;em&gt;ProductLookup&lt;/em&gt; provides &lt;em&gt;ItemLookup&lt;/em&gt; functionality for products, and requires exactly one &lt;em&gt;ProductRepository&lt;/em&gt; to do its job&lt;/li&gt;    &lt;li&gt;&lt;em&gt;ServiceLookup&lt;/em&gt; is like &lt;em&gt;ProductLookup&lt;/em&gt; but for services that may be performed for the client&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;An important aspect of the diagram is the cardinality of the relationships.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;PointOfSale’&lt;/em&gt;s one-to-many dependency on &lt;em&gt;ItemLookup&lt;/em&gt; is a MEF &lt;em&gt;[ImportMany]&lt;/em&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/LightuporMEFOptionalExports_AA58/image_6.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/LightuporMEFOptionalExports_AA58/image_thumb_2.png" width="456" height="140" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The one-to-one dependency between &lt;em&gt;ProductLookup &lt;/em&gt;and &lt;em&gt;ProductRepository&lt;/em&gt; correspond to MEF required imports. The same applies to &lt;em&gt;ServiceLookup &lt;/em&gt;and &lt;em&gt;ServiceRepository&lt;/em&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/LightuporMEFOptionalExports_AA58/image_8.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/LightuporMEFOptionalExports_AA58/image_thumb_3.png" width="458" height="137" /&gt;&lt;/a&gt; To configure the system, we need to make sure that &lt;em&gt;ServiceLookup &lt;/em&gt;doesn’t appear in &lt;em&gt;PointOfSale.ItemLookups &lt;/em&gt;when we’re in the “Standard” SKU.&lt;/p&gt;  &lt;h3&gt;Partitioning the System&lt;/h3&gt;  &lt;p&gt;‘Classic’ approaches to sub-setting used to revolve around conditional compilation (&lt;em&gt;#ifdef&lt;/em&gt;s) or runtime &lt;em&gt;“IsEnabled”&lt;/em&gt; checks. While effective, the results of these methods can be very difficult to maintain.&lt;/p&gt;  &lt;p&gt;Plug-in systems, or selective registration of components in an IoC container, provide a more intentional, centralized way of managing the contents of a SKU.&lt;/p&gt;  &lt;p&gt;Even here there is significant complexity and thus room for improvement. Functional dependencies usually span layers and revolve around multiple pieces of code. Some components will appear in multiple SKUs, while others will be available with different degrees of functionality.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;(Note that we ignore assembly-level partitioning here since market-driven functional partitioning does not always match architecture-driven partitioning.)&lt;/em&gt;&lt;/p&gt;  &lt;h3&gt;Enter Stable Composition&lt;/h3&gt;  &lt;p&gt;Tucked away in the recent &lt;a href="http://blogs.msdn.com/nblumhardt/archive/2009/07/09/mef-preview-6-available.aspx"&gt;announcement of MEF preview 6&lt;/a&gt; was a snippet on what we’ve been calling &lt;em&gt;Stable Composition&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;The cornerstone rule of Stable Composition is:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Parts that have missing &lt;em&gt;required&lt;/em&gt; dependencies are ‘rejected.’ They appear in the catalog, but the container never uses them.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now, the impact this has on a system as a whole is more interesting than it would first appear. Not only will parts with missing dependencies be rejected, but so will any parts that transitively depend on rejected parts. One missing dependency can cause a whole graph of components to be ignored.&lt;/p&gt;  &lt;p&gt;This isn’t accidental:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Utilising Stable Composition, one can manage just the parts that &lt;em&gt;define&lt;/em&gt; a SKU, and MEF will make sure that dependent parts are included or excluded as needed.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Instead of tracking dependency relationships manually to create a master set of components for each SKU, offload this responsibility to MEF.&lt;/p&gt;  &lt;h3&gt;Configuring &lt;em&gt;AdventureWorks Store Manager Standard &lt;/em&gt;SKU&lt;/h3&gt;  &lt;p&gt;This SKU is characterised by the absence of service-related functionality.&lt;/p&gt;  &lt;p&gt;Instead of analyzing all components to determine whether they play a part in service-related scenarios, we make the observation that all such functionality depends on the presence of a &lt;em&gt;ServiceRepository&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;When the MEF &lt;em&gt;ComposablePartCatalog &lt;/em&gt;is being configured, excluding the &lt;em&gt;ServiceRepository&lt;/em&gt; gives us the result we require.&lt;/p&gt;  &lt;p&gt;MEF itself will determine which other components depend on the missing functionality and automatically ignore (‘reject’) them:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/LightuporMEFOptionalExports_AA58/image_10.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/LightuporMEFOptionalExports_AA58/image_thumb_4.png" width="555" height="330" /&gt;&lt;/a&gt;This behaviour comes out-of-the-box with MEF – there is no need to opt-in or change the way that the &lt;em&gt;CompositionContainer&lt;/em&gt; is used.&lt;/p&gt;  &lt;h4&gt;Single Required Dependencies Only&lt;/h4&gt;  &lt;p&gt;Rejection typically applies to one-to-one dependencies. The &lt;em&gt;ServiceLookup&lt;/em&gt; screen is rejected because it requires exactly one &lt;em&gt;ServiceRepository&lt;/em&gt;, and by excluding it from the SKU we’ve effectively disabled the &lt;em&gt;ServiceLookup &lt;/em&gt;screen.&lt;/p&gt;  &lt;p&gt;The &lt;em&gt;PointOfSale&lt;/em&gt; screen’s dependency on many &lt;em&gt;ItemLookup&lt;/em&gt;s will never cause it to be rejected, since it can effectively function when the number of available &lt;em&gt;ItemLookup&lt;/em&gt;s is zero.&lt;/p&gt;  &lt;p&gt;The other common MEF dependency, represented by an import with &lt;em&gt;AllowDefault=true&lt;/em&gt;, is an optional dependency. Since by declaring an optional dependency, a component guarantees that it can operate without the dependency present, optional dependencies do not trigger rejection either.&lt;/p&gt;  &lt;p&gt;The special case to look out for is when too many implementations are present, e.g. two &lt;em&gt;ServiceRepository&lt;/em&gt;s. In this scenario components that depend on a single instance &lt;em&gt;will &lt;/em&gt;be rejected.&lt;/p&gt;  &lt;h3&gt;Debugging and Testing&lt;/h3&gt;  &lt;p&gt;The last drop of MEF included a sample (&lt;em&gt;Microsoft.ComponentModel.Composition.Diagnostics&lt;/em&gt;) capable of determining the rejection status of a part. This is useful for debugging at development time, but there’s also an opportunity to do integration-time testing of SKU configurations.&lt;/p&gt;  &lt;p&gt;For example, if I’m testing the “Pro Service” SKU and expect &lt;em&gt;ServiceLookup &lt;/em&gt;to be available, I might write:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/LightuporMEFOptionalExports_AA58/image_12.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/LightuporMEFOptionalExports_AA58/image_thumb_5.png" width="459" height="188" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now, this is a hypothetical test case; I haven’t had an opportunity to use this in anger. It is an interesting possibility though, and worth exploring if you’re going to make heavy use of Stable Composition.&lt;/p&gt;  &lt;h3&gt;Optional Exports&lt;/h3&gt;  &lt;p&gt;Why did the title of this post include the term ‘Optional Exports*’? This is a useful way to think about how rejection works. MEF components provide exports that are optional, on the condition that their mandatory imports can be satisfied. The &lt;em&gt;ServiceLookup &lt;/em&gt;component provides its exports on the condition that &lt;em&gt;ServiceRepository &lt;/em&gt;is available.&lt;/p&gt;  &lt;p&gt;You can use this way of looking at the feature to implement some other nifty tricks in the realm of ‘light up,’ but that is for another day!&lt;/p&gt;  &lt;p&gt;&lt;em&gt;(*One of the many subtle coinages of Blake Stone – Blake, where’s your blog?)&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9837689" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/MEF/default.aspx">MEF</category></item><item><title>MEF Preview 6 Available</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/07/09/mef-preview-6-available.aspx</link><pubDate>Thu, 09 Jul 2009 19:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9827085</guid><dc:creator>niblumha</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9827085.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9827085</wfw:commentRss><description>&lt;P mce_keep="true"&gt;In the three months that have passed since the last release, we’ve been very busy making final adjustments to get closer to RTM.&lt;/P&gt;
&lt;P&gt;In this post I’ve summarized the biggest changes between the Preview 5 and Preview 6 releases.&lt;/P&gt;
&lt;P&gt;The highlights are:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Silverlight support&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Lazy&amp;lt;T&amp;gt;&lt;/STRONG&gt; replaces &lt;STRONG&gt;Export&amp;lt;T&amp;gt;&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;Collection import changes &lt;/LI&gt;
&lt;LI&gt;Inheritance changes &lt;/LI&gt;
&lt;LI&gt;Export attribute is unsealed &lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Stable Composition &lt;/EM&gt;makes its debut &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;You can download MEF Preview 6 from &lt;A href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30098" mce_href="http://mef.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30098"&gt;the CodePlex site&lt;/A&gt;.&lt;/P&gt;
&lt;H2&gt;Silverlight Support&lt;/H2&gt;
&lt;P&gt;With this release comes an early preview of our Silverlight plans. The included solution &lt;EM&gt;MEFSL.sln&lt;/EM&gt; can be used to build MEF binaries that target the newly-released &lt;A href="http://microsoft.com/silverlight" mce_href="http://microsoft.com/silverlight"&gt;Silverlight 3&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image11.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image11.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; MARGIN-LEFT: 0px; BORDER-TOP: 0px; MARGIN-RIGHT: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image align=right src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image11_thumb.png" width=244 height=223 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image11_thumb.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Those familiar with the full-framework MEF won’t see many differences in the Silverlight build as it stands today. We aim to have some exciting new features in the eventual release, but for now the APIs are the same apart from changes required because of the different environment.&lt;/P&gt;
&lt;H3&gt;Silverlight Catalog&lt;/H3&gt;
&lt;P&gt;&lt;STRONG&gt;DirectoryCatalog&lt;/STRONG&gt; is not available in the Silverlight version. Instead, this is replaced by &lt;STRONG&gt;PackageCatalog&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;This API is tentative and will very probably change in a future preview.&lt;/P&gt;
&lt;H3&gt;Silverlight Picture Viewer&lt;/H3&gt;
&lt;P&gt;There’s a new sample called &lt;EM&gt;PictureViewer&lt;/EM&gt; that demonstrates some aspects composition in a Silverlight application.&lt;/P&gt;
&lt;H2&gt;Lazy&amp;lt;T&amp;gt;&lt;/H2&gt;
&lt;P&gt;One of the first things you’ll notice when moving to the new build is that the &lt;STRONG&gt;System.ComponentModel.Composition.Export&amp;lt;T&amp;gt;&lt;/STRONG&gt; and &lt;STRONG&gt;Export&amp;lt;T, TMetadata&amp;gt;&lt;/STRONG&gt; types are gone.&lt;/P&gt;
&lt;P&gt;In their place, MEF now recognizes the &lt;STRONG&gt;System.Lazy&amp;lt;T&amp;gt;&lt;/STRONG&gt; and &lt;STRONG&gt;Lazy&amp;lt;T, TMetadta&amp;gt;&lt;/STRONG&gt; types.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_18.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_18.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_8.png" width=346 height=46 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_8.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Functionally these types match the old ones, however:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The &lt;STRONG&gt;GetExportedObject()&lt;/STRONG&gt; method is replaced by a &lt;STRONG&gt;Value&lt;/STRONG&gt; property &lt;/LI&gt;
&lt;LI&gt;The raw metadata dictionary is gone (if you truly require this, you can use the &lt;STRONG&gt;IDictionary&amp;lt;string, object&amp;gt;&lt;/STRONG&gt; type as a metadata view interface) &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Collection Import Changes&lt;/H2&gt;
&lt;P&gt;The &lt;STRONG&gt;System.ComponentModel.Composition.&lt;/STRONG&gt;&lt;STRONG&gt;ExportCollection&amp;lt;T&amp;gt; &lt;/STRONG&gt;type has also been removed. In its place, use &lt;STRONG&gt;IEnumerable&amp;lt;Lazy&amp;lt;T&amp;gt;&amp;gt;&lt;/STRONG&gt;, or &lt;STRONG&gt;Lazy&amp;lt;T&amp;gt;[]&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_20.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_20.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_9.png" width=461 height=46 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_9.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;STRONG&gt;ImportManyAttribute&lt;/STRONG&gt; is now required for &lt;EM&gt;all&lt;/EM&gt; collection imports.&lt;/P&gt;
&lt;H2&gt;Inheritance Changes&lt;/H2&gt;
&lt;P&gt;&lt;STRONG&gt;PartExportsInheritedAttribute &lt;/STRONG&gt;has been removed. The new mechanism separates inheritable exports from non-inheritable exports.&lt;/P&gt;
&lt;P&gt;Exports declared using the &lt;STRONG&gt;ExportAttribute &lt;/STRONG&gt;are never inherited.&lt;/P&gt;
&lt;P&gt;Exports declared using the &lt;STRONG&gt;InheritedExportAttribute&lt;/STRONG&gt; will be exposed by sub-classes of the part type.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_22.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_22.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_10.png" width=289 height=198 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_10.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;H2&gt;Custom Export Attributes&lt;/H2&gt;
&lt;P&gt;In previous releases, &lt;STRONG&gt;ExportAttribute &lt;/STRONG&gt;was sealed.&lt;/P&gt;
&lt;P&gt;It is now possible to subclass &lt;STRONG&gt;ExportAttribute&lt;/STRONG&gt;, and this can be combined with the existing &lt;STRONG&gt;MetadataAttribute &lt;/STRONG&gt;functionality.&lt;/P&gt;
&lt;P&gt;This means that instead of:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_12.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_5.png" width=375 height=157 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_5.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;It is possible to define a custom attribute that completely describes the export:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_14.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_6.png" width=568 height=269 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_6.png"&gt;&lt;/A&gt;Thus users can declare both the exported contract and its metadata using only one attribute:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_16.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_7.png" width=396 height=114 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_7.png"&gt;&lt;/A&gt;We expect this will be very useful for authors of extensible frameworks. &lt;/P&gt;
&lt;P&gt;&lt;B&gt;Note:&lt;/B&gt; We have also unsealed &lt;STRONG&gt;ImportAttribute&lt;/STRONG&gt;, &lt;STRONG&gt;ImportManyAttribute&lt;/STRONG&gt; and &lt;STRONG&gt;ImportingConstructor&lt;/STRONG&gt; thus allowing these to be extended as well.&lt;/P&gt;
&lt;H2&gt;Stable Composition&lt;/H2&gt;
&lt;P&gt;As an extensibility framework, it is important that MEF applications start and execute reliably even when some extensions lack required dependencies.&lt;/P&gt;
&lt;P&gt;Stable Composition contributes to this goal by validating dependencies in advance.&lt;/P&gt;
&lt;H3&gt;Part Definition Rejection&lt;/H3&gt;
&lt;P&gt;In earlier releases, MEF would hand out lazy exports from parts with missing dependencies. Later, when the export was used an exception would be thrown because of problems creating the part.&lt;/P&gt;
&lt;P&gt;There were two major issues with this:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Side-effects of failures during composition could not always be cleaned up &lt;/LI&gt;
&lt;LI&gt;Light-up could only be supported via optional dependencies, which led to awkward component designs &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The solution to this problem is:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Parts that have missing &lt;U&gt;required&lt;/U&gt; dependencies are ‘rejected.’ They appear in the catalog, but the container never uses them.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;MEF remains dynamic – as parts are added and removed from the composition, the state of part definitions can change. Parts that may have been rejected will become available when their dependencies can be satisfied.&lt;/P&gt;
&lt;H3&gt;Atomic Changes&lt;/H3&gt;
&lt;P&gt;The container now treats lazy references as &lt;EM&gt;promises&lt;/EM&gt;. Once the container hands out a &lt;STRONG&gt;Lazy&amp;lt;T&amp;gt;&lt;/STRONG&gt; export, the value is guaranteed to be available, and to remain available for the life of the part that imports it.&lt;/P&gt;
&lt;P&gt;The parts available in a MEF container can change, either by adding or removing to the a &lt;STRONG&gt;ComposablePartCatalog&lt;/STRONG&gt;, or directly via &lt;STRONG&gt;CompositionContainer.ComposeBatch().&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;In order to keep promises, changes like these are examined carefully and verified not to break anything already instantiated.&lt;/P&gt;
&lt;P&gt;If such a change would cause an existing promise to be broken, the change is rejected by means of a &lt;STRONG&gt;CompositionException&lt;/STRONG&gt;.&lt;/P&gt;
&lt;H3&gt;Diagnosing Composition Problems&lt;/H3&gt;
&lt;P&gt;One of the implications of Stable Composition is that the rejected parts will simply never show up.&lt;/P&gt;
&lt;P&gt;Because parts are interdependent, one broken part can cause a chain of other dependent parts to be rejected as well.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_2.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb.png" width=542 height=320 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Finding the root cause of a ‘cascading rejection’ like this can be tricky.&lt;/P&gt;
&lt;P&gt;Included in the samples under &lt;EM&gt;/Samples/CompositionDiagnostics &lt;/EM&gt;is a prototype diagnostic library that can be used to track composition problems down.&lt;/P&gt;
&lt;P&gt;The two basic functions that are implemented allow the rejection state of a part to be inspected, and the contents of an entire catalog to be dumped along with status and root cause analysis information.&lt;/P&gt;
&lt;H4&gt;Dump Composition State&lt;/H4&gt;
&lt;P&gt;For comprehensive diagnostics, the complete composition can be dumped in text format:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_4.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_1.png" width=407 height=101 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;The output contains many interesting things, including ‘primary rejection’ guesses and analysis of common problems like mismatched type identity, mismatched creation policy, and missing required metadata:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_6.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_2.png" width=573 height=691 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;There’s enough information here to correctly diagnose most common issues.&lt;/P&gt;
&lt;H4&gt;Find Likely Root Causes&lt;/H4&gt;
&lt;P&gt;The dump technique above is comprehensive but verbose, and if you have access to the running process in a debugger, the following is more likely to be convenient:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_8.png"&gt;&lt;IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_3.png" width=382 height=30 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/MEFforSilverlightAvailableNow_90D1/image_thumb_3.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;The return value of &lt;STRONG&gt;CompositionInfo.GetPartDefinitionInfo()&lt;/STRONG&gt; is an object that gives quick access to all of the same analytical information as the text dump, but relating to the part Foo. The API exposes:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The part’s rejection state (&lt;STRONG&gt;IsRejected&lt;/STRONG&gt;) &lt;/LI&gt;
&lt;LI&gt;Whether it is a primary cause of rejection, or if it is rejected because of other cascading rejections (&lt;STRONG&gt;IsPrimaryRejection&lt;/STRONG&gt;) &lt;/LI&gt;
&lt;LI&gt;Which parts are likely to be the root causes of the part’s rejection (&lt;STRONG&gt;PossibleRootCauses&lt;/STRONG&gt;) &lt;/LI&gt;
&lt;LI&gt;The state of all imports (&lt;STRONG&gt;ImportDefinitions&lt;/STRONG&gt;) &lt;/LI&gt;
&lt;LI&gt;For each import, which exports would satisfy the imports (&lt;STRONG&gt;ImportDefinitions..Actuals&lt;/STRONG&gt;) &lt;/LI&gt;
&lt;LI&gt;For each import, which other exports with the same contract name were not matched, and the reason for each (&lt;STRONG&gt;ImportDefinitions..UnsuitableExportDefinitions&lt;/STRONG&gt;) &lt;/LI&gt;&lt;/UL&gt;
&lt;H3&gt;Implementing Export Providers&lt;/H3&gt;
&lt;P&gt;The &lt;STRONG&gt;ExportProvider.GetExportsCore()&lt;/STRONG&gt; method has gained a parameter to support Stable Composition. &lt;STRONG&gt;ExportProvider&lt;/STRONG&gt; also has a new event &lt;STRONG&gt;ExportsChanging&lt;/STRONG&gt; for the same purpose.&lt;/P&gt;
&lt;P&gt;Simple &lt;STRONG&gt;ExportProviders&lt;/STRONG&gt; that hand out the same values and do not recursively call into the container can ignore the parameter and event.&lt;/P&gt;
&lt;P&gt;If you implement a custom &lt;STRONG&gt;ExportProvider&lt;/STRONG&gt; that recursively calls into the container to satisfy dependencies of the exports it hands out, then you should participate in Stable Composition.&lt;/P&gt;
&lt;P&gt;Likewise, if the set of available exports in an &lt;STRONG&gt;ExportProvider&lt;/STRONG&gt; can change, you’ll need to fire the &lt;STRONG&gt;ExportsChanging&lt;/STRONG&gt; and &lt;STRONG&gt;ExportsChanged&lt;/STRONG&gt; events.&lt;/P&gt;
&lt;P&gt;See the API documentation for these types for more details.&lt;/P&gt;
&lt;H2&gt;Other API Changes&lt;/H2&gt;
&lt;P&gt;A number of other supporting API changes have been made, and a few obsolete or problematic items removed:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;CompositionContainer.GetExported&lt;EM&gt;Object&lt;/EM&gt;() &lt;/STRONG&gt;and related overloads have been renamed to &lt;STRONG&gt;GetExported&lt;EM&gt;Value&lt;/EM&gt;()&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;CompositionEngine &lt;/STRONG&gt;has been renamed &lt;STRONG&gt;ImportEngine&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;AttributedModelServices&lt;/STRONG&gt; 
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;AddExportedObject&amp;lt;T&amp;gt;()&lt;/STRONG&gt; renamed to &lt;STRONG&gt;AddExportedValue&amp;lt;T&amp;gt;()&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;ComposeExportedObject&amp;lt;T&amp;gt;()&lt;/STRONG&gt; renamed to&lt;STRONG&gt; ComposeExportedValue&amp;lt;T&amp;gt;()&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;New &lt;STRONG&gt;GetMetadataView&amp;lt;TMetadataView&amp;gt;()&lt;/STRONG&gt; method &lt;/LI&gt;
&lt;LI&gt;New &lt;STRONG&gt;SatisfyIImportsOnce&lt;/STRONG&gt;() method &lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;ICompositionService&lt;/STRONG&gt; changes 
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;SatisfyImports()&lt;/STRONG&gt; renamed&amp;nbsp; &lt;STRONG&gt;SatisfyImportsOnce()&lt;/STRONG&gt; and no longer registers the part for recomposition &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;UnregisterForRecomposition() &lt;/STRONG&gt;removed &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;CompositionServiceExtensions&lt;/STRONG&gt; removed &lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;ContractTypeAttribute&lt;/STRONG&gt; removed since exports on interfaces are now inherited &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;ComposablePartCatalogChangedEventArgs&lt;/STRONG&gt; renamed to &lt;STRONG&gt;ComposablePartCatalogChangeEventArgs&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;New &lt;STRONG&gt;AtomicComposition&lt;/STRONG&gt; class supports Stable Composition &lt;/LI&gt;
&lt;LI&gt;Contract adapters have been removed &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Help and Feedback&lt;/H2&gt;
&lt;P&gt;Enjoy the new release, and remember that the &lt;A href="http://mef.codeplex.com/Thread/List.aspx" mce_href="http://mef.codeplex.com/Thread/List.aspx"&gt;MEF discussion forum&lt;/A&gt; on CodePlex is a great place to get help if you find yourself stuck.&lt;/P&gt;
&lt;P&gt;The MEF Team is very grateful to everyone who’s provided feedback and input into the new release. I hope we’ve hit the right note, but as always your comments are welcome.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9827085" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/MEF/default.aspx">MEF</category></item><item><title>Appearance on .NET Rocks!</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/05/29/appearance-on-dotnet-rocks.aspx</link><pubDate>Fri, 29 May 2009 20:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9655805</guid><dc:creator>niblumha</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9655805.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9655805</wfw:commentRss><description>&lt;P&gt;This week I took some time out to talk to Richard and Carl on &lt;EM&gt;&lt;A href="http://dotnetrocks.com/" mce_href="http://dotnetrocks.com"&gt;.NET Rocks!&lt;/A&gt;&lt;/EM&gt; about &lt;A href="http://www.dotnetrocks.com/default.aspx?showNum=450" mce_href="http://www.dotnetrocks.com/default.aspx?showNum=450"&gt;Autofac, MEF and component-oriented software&lt;/A&gt; in general.&lt;/P&gt;
&lt;P&gt;If you have any questions about material from the show, please feel free to post them in the comments.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9655805" width="1" height="1"&gt;</description></item><item><title>State Machines in Domain Models</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/04/16/state-machines-in-domain-models.aspx</link><pubDate>Thu, 16 Apr 2009 09:52:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9552345</guid><dc:creator>niblumha</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9552345.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9552345</wfw:commentRss><description>&lt;p&gt;I was reminded today of &lt;a href="http://stateless.googlecode.com"&gt;Stateless&lt;/a&gt;, a little project that I’m quite fond of.&lt;/p&gt;  &lt;p&gt;Stateless is a &lt;em&gt;hierarchical state machine framework&lt;/em&gt; based on &lt;a href="http://codeplex.com/simplestatemachine"&gt;Simple State Machine&lt;/a&gt; for &lt;a href="http://boo.codehaus.org"&gt;Boo&lt;/a&gt;, but configured using C# 3.0.&lt;/p&gt;  &lt;p&gt;When I &lt;a href="http://ubik.com.au/article/named/stateless"&gt;announced Stateless last year&lt;/a&gt;, I hardly even explained its purpose, let alone its tongue-in-cheek name. I think I owe it a better start in life!&lt;/p&gt;  &lt;p&gt;If you’re doing heavy duty state-machine based programming (writing parsers, radiotherapy machines or flight control systems) then Stateless is probably not what you’re looking for.&lt;/p&gt;  &lt;p&gt;If you’re doing domain-driven design, and anxious about the ugly nested ‘if’ and ‘switch’ statements building up around _state variables, Stateless can help.&lt;/p&gt;  &lt;p&gt;State-based behaviour is awkward because despite careful programming, it is difficult to see how the states relate to each other. Adding and removing states is error-prone, repetition creeps in, and refactoring gets tricky.&lt;/p&gt;  &lt;p&gt;There’s more than one way out. The &lt;a href="http://en.wikipedia.org/wiki/State_pattern"&gt;State&lt;/a&gt; &lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2006/04/30/143733.aspx"&gt;pattern&lt;/a&gt; can be one very elegant solution. &lt;/p&gt;  &lt;p&gt;Another solution that works nicely is to create a declarative model of the states, transitions and associated actions, and have a state machine framework ‘run’ it for you. This is the approach enabled by Stateless.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_2.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="551" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb.png" width="337" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The Telephone Call state chart is broken down into the following states and triggers:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_4.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="351" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_1.png" width="238" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You can use any types to represent states and triggers, but enumerations are convenient.&lt;/p&gt;  &lt;p&gt;After creating an instance of &lt;em&gt;StateMachine&lt;/em&gt;, each state is configured independently. Configuration for a state revolves around the triggers that the state can accept, the transitions that these triggers will cause (to new states) and the actions that will be performed when entering or leaving a state.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_6.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="385" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_2.png" width="546" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Once the state machine has been configured, the triggers can be ‘fired’. The side-effects from firing triggers drive the program.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/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="46" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_3.png" width="300" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You might wonder how this fits into a domain model. &lt;em&gt;StateMachine&lt;/em&gt; certainly doesn’t belong on the public surface area of your domain model classes. It’s an implementation detail – like, for example, &lt;em&gt;StringBuilder &lt;/em&gt;or &lt;em&gt;Regex.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_10.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="281" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_4.png" width="438" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Users of the class interact with the state machine indirectly, by calling public methods on the domain object.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_12.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="178" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_5.png" width="349" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The state machine is configured to call two methods when entering and leaving the Connected state (see the configuration above.)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_14.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="197" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/StateMachinesinDDD_12E24/image_thumb_6.png" width="452" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Stateless does all of the heavy lifting. For example, the state machine understands that because &lt;em&gt;OnHold&lt;/em&gt; is a sub-state of &lt;em&gt;Connected&lt;/em&gt;, the &lt;em&gt;Connected &lt;/em&gt;entry/exit actions aren’t called when moving &lt;em&gt;between&lt;/em&gt; these two states.&lt;/p&gt;  &lt;p&gt;Now for a justification of the silly name…&lt;/p&gt;  &lt;p&gt;Notice the way that the &lt;em&gt;_state&lt;/em&gt; field belongs to the &lt;em&gt;PhoneCall &lt;/em&gt;object, rather than the state machine? This allows the field to be easily mapped to a database column by NHibernate or your ORM of choice. The state machine reads and writes the &lt;em&gt;_state&lt;/em&gt; value using the pair of lambdas provided to its constructor. In that sense you can &lt;em&gt;almost &lt;/em&gt;say that the state machine itself is ‘stateless’. Almost.&lt;/p&gt;  &lt;p&gt;There’s some more basic documentation and a source download &lt;a href="http://code.google.com/p/stateless/"&gt;at the site&lt;/a&gt;. I hope you’ll try Stateless and be pleasantly surprised by how expressive it can make your code. Search your domain model for “State” or “Status” and see what Stateless can do!&lt;/p&gt;  &lt;p&gt;&lt;em&gt;This post also comes with a challenge: Stateless is fairly simple and very hackable - if you’re a fluent-interface aficionado and think you can improve the configuration API, join the project!&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9552345" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/DDD/default.aspx">DDD</category><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/Stateless/default.aspx">Stateless</category></item><item><title>MEF: Dependencies are Queries?</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/04/16/mef-dependencies-are-queries.aspx</link><pubDate>Thu, 16 Apr 2009 02:01:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9551727</guid><dc:creator>niblumha</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9551727.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9551727</wfw:commentRss><description>&lt;p&gt;Start to schematically represent any component system and you’re likely to come up with a diagram like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DependenciesareQueries_B15F/image_6.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="114" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DependenciesareQueries_B15F/image_thumb_2.png" width="532" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So what do the &lt;em&gt;dependency &lt;/em&gt;and the &lt;em&gt;service &lt;/em&gt;actually &lt;em&gt;mean&lt;/em&gt;?&lt;/p&gt;  &lt;p&gt;In most implementations, each is associated with a key:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DependenciesareQueries_B15F/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="120" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DependenciesareQueries_B15F/image_thumb_3.png" width="530" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here the Screen Renderer component needs the Typesetter service, which the Fast Typesetter component can provide.&lt;/p&gt;  &lt;p&gt;Dependency resolution is a done by matching keys – resolving the Screen Renderer’s dependency boils down to doing a dictionary lookup with Typesetter as the key, to find the Fast Typesetter implementation.&lt;/p&gt;  &lt;p&gt;This is the case whether the dependencies are expressed as strings, types, or some combination of the two.&lt;/p&gt;  &lt;p&gt;While this model is very powerful and can be taken a long way, ‘open’ systems can benefit from an alternative approach.&lt;/p&gt;  &lt;p&gt;Imagine a system in which multiple renderers and typesetters exist, each with distinct capabilities:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DependenciesareQueries_B15F/image_10.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="229" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DependenciesareQueries_B15F/image_thumb_4.png" width="533" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The dependencies above are no longer evaluated using simple equality comparisons.&lt;/p&gt;  &lt;p&gt;On the right, the capabilities of each component are represented as structured data: the service provided, plus a collection of key-value pairs bearing more detailed information. Together these are a &lt;em&gt;service definition&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;On the left, the dependencies of each component are represented as predicates over the possible service definitions. The predicate evaluates to &lt;em&gt;true&lt;/em&gt; if the service definition can satisfy the dependencies.&lt;/p&gt;  &lt;p&gt;So, the Print Renderer’s requirements, &lt;em&gt;{ Quality == High }&lt;/em&gt;, are &lt;em&gt;true &lt;/em&gt;when evaluated against the Accurate Typesetter’s service definition, and &lt;em&gt;false &lt;/em&gt;when executed against the Fast Typesetter’s.&lt;/p&gt;  &lt;p&gt;In effect, components express their dependencies as &lt;em&gt;queries&lt;/em&gt; over the entire set of available services.&lt;/p&gt;  &lt;p&gt;To get an idea of where the power of this technique comes from, consider the way the example will compose: the Screen Renderer gets a fast Typesetter and the Print Renderer gets an accurate one. In a system with a better Typesetter – one that is both fast and accurate – a single Typesetter implementation could fulfill both dependencies.&lt;/p&gt;  &lt;p&gt;Constraints aren’t just limited to equality comparisons either – if speed is expressed in characters per second, the Screen Renderer could specify &lt;em&gt;{ Speed &amp;gt; 500 }&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;If you haven’t guessed already, this is the model that the MEF Primitives use. The service definitions are exposed with the type &lt;em&gt;ExportDefinition&lt;/em&gt;. The predicate for testing &lt;em&gt;ExportDefinitions&lt;/em&gt; for suitability is encoded in &lt;em&gt;ImportDefinition.Constraint&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DependenciesareQueries_B15F/image_12.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="112" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DependenciesareQueries_B15F/image_thumb_5.png" width="543" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;One of the constraints in the example might be expressed as:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/DependenciesareQueries_B15F/image_14.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/DependenciesareQueries_B15F/image_thumb_6.png" width="425" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The system is elegant, but there are a few things to be aware of. The first that comes to mind is how the constraint is Boolean, so ‘falling back’ to a slow Typesetter when no fast one is available means expressing two separate constraints&lt;/p&gt;  &lt;p&gt;Because &lt;em&gt;ImportDefinition.Constraint&lt;/em&gt; is a Linq expression, and thanks to some special-case logic, MEF is able to resolve many dependencies without doing the linear scan that the query implies. This is only the case when constraints take the typical forms expressible in MEF’s Attributed Programming Model. Arbitrary constraints &lt;strong&gt;can&lt;/strong&gt; result in a linear scan (although the way is clear to optimizing these in the future.)&lt;/p&gt;  &lt;p&gt;One other interesting note – it is technically &lt;em&gt;possible &lt;/em&gt;to transform an import constraint into a format that could be evaluated by a database-backed Linq provider… If for some reason you’d like to do that :)&lt;/p&gt;  &lt;p&gt;You might be wondering why code snippets like the predicate above don’t appear in the MEF examples you typically see. The answer is that this is the key to understanding &lt;em&gt;programming models&lt;/em&gt;. A programming model, in the MEF sense, translates some easy-to-type format like the &lt;em&gt;Import&lt;/em&gt; and &lt;em&gt;Export&lt;/em&gt; attributes, into constraints under-the-hood.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Constraint-based dependency resolution is one of the foundations that future versions of MEF will build upon.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9551727" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/MEF/default.aspx">MEF</category></item><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 Open Space at ALT.NET Seattle</title><link>http://blogs.msdn.com/nblumhardt/archive/2009/03/01/ioc-open-space-at-alt-net-seattle.aspx</link><pubDate>Sun, 01 Mar 2009 03:08:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9451532</guid><dc:creator>niblumha</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/nblumhardt/comments/9451532.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nblumhardt/commentrss.aspx?PostID=9451532</wfw:commentRss><description>&lt;P&gt;We had a great little open discussion on building IoC-driven apps today at ALT.NET Seattle.&lt;/P&gt;
&lt;P&gt;A&amp;nbsp;handful&amp;nbsp;of&amp;nbsp;good points came up:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Building aggregate object graphs for the UI layer is giving people headaches&lt;/LI&gt;
&lt;LI&gt;Dynamic instantiation patterns like 'injected context' and 'generated context adapter' are starting to gain more recognition as basic buildling blocks&lt;/LI&gt;
&lt;LI&gt;Service locators can be a useful step&amp;nbsp;in the process of refactoring-to-IoC (seems like there is some work to be done in developing a body of knowledge in this area)&lt;/LI&gt;
&lt;LI&gt;The time is right for some better tooling, e.g. dependency graph exploration&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Thanks everyone who showed up and got involved. (Extra thanks to &lt;A class="" title="Glenn Block" href="http://blogs.msdn.com/gblock" mce_href="http://blogs.msdn.com/gblock"&gt;Glenn Block&lt;/A&gt; for setting up the session - kudos!)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9451532" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nblumhardt/archive/tags/altnetconf/default.aspx">altnetconf</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>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></channel></rss>