<?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/b/nblumhardt/</link><description /><dc:language>en-US</dc:language><generator>Telligent Community 5.6.583.17018 (Build: 5.6.583.17018)</generator><item><title>Back at Microsoft, new blog location...</title><link>http://blogs.msdn.com/b/nblumhardt/archive/2011/10/05/back-at-microsoft-new-blog-location.aspx</link><pubDate>Wed, 05 Oct 2011 17:37:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10220628</guid><dc:creator>Nicholas Blumhardt</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=10220628</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2011/10/05/back-at-microsoft-new-blog-location.aspx#comments</comments><description>&lt;p&gt;I've moved back to Redmond and again work on the .NET framework.&lt;/p&gt;
&lt;p&gt;I don't currently plan on reviving this blog. You can find me online at &lt;a title="my personal blog" href="http://nblumhardt.com"&gt;my personal blog&lt;/a&gt; or keep up with our team via &lt;a title="the BCL team blog" href="http://blogs.msdn.com/b/bclteam/"&gt;the BCL team blog&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thanks for dropping by!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10220628" width="1" height="1"&gt;</description></item><item><title>Homeward Bound</title><link>http://blogs.msdn.com/b/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><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/b/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>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9899106</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/09/24/debug-composition-from-within-visual-studio.aspx#comments</comments><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/b/nblumhardt/archive/tags/MEF/">MEF</category></item><item><title>Parameterised Triggers and Re-Entrant States in Stateless v2</title><link>http://blogs.msdn.com/b/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>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9889032</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/08/30/parameterised-triggers-and-re-entrant-states-in-stateless-v2.aspx#comments</comments><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/b/nblumhardt/archive/tags/Stateless/">Stateless</category></item><item><title>Analyze MEF Assemblies from the Command Line</title><link>http://blogs.msdn.com/b/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><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9888696</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/08/28/analyze-mef-assemblies-from-the-command-line.aspx#comments</comments><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/b/nblumhardt/archive/tags/MEF/">MEF</category></item><item><title>Dynamic Part Instantiation in MEF</title><link>http://blogs.msdn.com/b/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><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9887690</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/08/28/dynamic-part-instantiation-in-mef.aspx#comments</comments><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/b/nblumhardt/archive/tags/MEF/">MEF</category></item><item><title>Implementing Optional Exports with MEF Stable Composition</title><link>http://blogs.msdn.com/b/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><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9837689</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/07/17/light-up-or-mef-optional-exports.aspx#comments</comments><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/b/nblumhardt/archive/tags/MEF/">MEF</category></item><item><title>MEF Preview 6 Available</title><link>http://blogs.msdn.com/b/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><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9827085</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/07/09/mef-preview-6-available.aspx#comments</comments><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/b/nblumhardt/archive/tags/MEF/">MEF</category></item><item><title>Appearance on .NET Rocks!</title><link>http://blogs.msdn.com/b/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><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9655805</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/05/29/appearance-on-dotnet-rocks.aspx#comments</comments><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/b/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>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9552345</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/04/16/state-machines-in-domain-models.aspx#comments</comments><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/b/nblumhardt/archive/tags/DDD/">DDD</category><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/Stateless/">Stateless</category></item><item><title>MEF: Dependencies are Queries?</title><link>http://blogs.msdn.com/b/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><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9551727</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/04/16/mef-dependencies-are-queries.aspx#comments</comments><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/b/nblumhardt/archive/tags/MEF/">MEF</category></item><item><title>Hosting MEF Extensions in an IoC Container</title><link>http://blogs.msdn.com/b/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>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9482064</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/03/16/hosting-mef-extensions-in-an-ioc-container.aspx#comments</comments><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/b/nblumhardt/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/Autofac/">Autofac</category></item><item><title>IoC Open Space at ALT.NET Seattle</title><link>http://blogs.msdn.com/b/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><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9451532</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/03/01/ioc-open-space-at-alt-net-seattle.aspx#comments</comments><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;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9451532" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/altnetconf/">altnetconf</category></item><item><title>IoC on Silverlight</title><link>http://blogs.msdn.com/b/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>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9271594</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/01/04/ioc-on-silverlight.aspx#comments</comments><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/b/nblumhardt/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/Autofac/">Autofac</category><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/Silverlight/">Silverlight</category></item><item><title>Declarative Context Adapters</title><link>http://blogs.msdn.com/b/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><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9271437</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2009/01/04/declarative-context-adapters.aspx#comments</comments><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/b/nblumhardt/archive/tags/Container+Managed+Application+Design/">Container Managed Application Design</category><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/Autofac/">Autofac</category></item><item><title>Explicit Context Dependencies</title><link>http://blogs.msdn.com/b/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><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9256012</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2008/12/29/explicit-context-dependencies.aspx#comments</comments><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/b/nblumhardt/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/Container+Managed+Application+Design/">Container Managed Application Design</category></item><item><title>Container-Managed Application Design, Prelude: Where does the Container Belong?</title><link>http://blogs.msdn.com/b/nblumhardt/archive/2008/12/27/container-managed-application-design-prelude-where-does-the-container-belong.aspx</link><pubDate>Sat, 27 Dec 2008 03:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9253987</guid><dc:creator>niblumha</dc:creator><slash:comments>17</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9253987</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2008/12/27/container-managed-application-design-prelude-where-does-the-container-belong.aspx#comments</comments><description>&lt;P&gt;There is surprisingly little information out there in cyberspace on how IoC containers should fit into application architectures.&lt;/P&gt;
&lt;P&gt;In parallel with my MEF/Ruby series, I'm going to discuss some of the principles that I believe should be applied to "container-managed" application design.&lt;/P&gt;
&lt;H2&gt;The Enigmatic Ideal&lt;/H2&gt;
&lt;P&gt;In the world of systems built using &lt;I&gt;dependency injection&lt;/I&gt;, you hear plenty of mysterious statements like:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;“Except for when bootstrapping, don’t access the container directly!”&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This kind of thing caused me to scratch my head for the best part of a year before I finally caught on to what was meant.&lt;/P&gt;
&lt;H2&gt;Basic Mental Models&lt;/H2&gt;
&lt;P&gt;When confronted with the task of designing an application around an IoC container, the place that the container sits in that architecture usually seems to be influenced by the designer's mental model of what, exactly, an IoC container &lt;EM&gt;is&lt;/EM&gt;.&lt;/P&gt;
&lt;H3&gt;Model 1: a Dictionary of Services&lt;/H3&gt;
&lt;P&gt;The &lt;I&gt;dictionary &lt;/I&gt;or &lt;I&gt;associative array&lt;/I&gt; is one of the first constructs we learn about in software engineering. It is easy to see the analogy between a dictionary and an IoC container that composes objects using dependency injection:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_10.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_10.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=93 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_4.png" width=584 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_4.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Maps to:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_12.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=94 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_5.png" width=572 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_5.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;All that appears to differ is:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Some fancy new terms ‘register’ and ‘resolve’ are used, instead of indexer access&lt;/LI&gt;
&lt;LI&gt;The logger is created via reflection, saving some typing and configuration code in the case of inter-dependencies&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;If you come onto a project with established usage of a dependency injection container and had to figure out how to write the code you need to interact with the rest of the system, chances are this is the first mental model you’ll apply.&lt;/P&gt;
&lt;H3&gt;Model 2: an Abstract new() Operator&lt;/H3&gt;
&lt;P&gt;Dependency injection containers are often praised for eliminating &lt;I&gt;implementation type dependencies&lt;/I&gt;. Going back to our example, we’ve made our request for an &lt;STRONG&gt;&lt;EM&gt;ILogger&lt;/EM&gt;&lt;/STRONG&gt; independent of the implementation type that provides it (&lt;STRONG&gt;&lt;EM&gt;ConsoleLogger&lt;/EM&gt;&lt;/STRONG&gt; in this case.)&lt;/P&gt;
&lt;P&gt;Web frameworks like Monorail and ASP.NET MVC use our dependency injection container to create &lt;STRONG&gt;&lt;EM&gt;Controller&lt;/EM&gt;&lt;/STRONG&gt; instances in a similar way.&lt;/P&gt;
&lt;P&gt;Perhaps it makes more sense to think of the container as an &lt;I&gt;abstract new() operator&lt;/I&gt;?&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_14.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=160 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_6.png" width=454 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_6.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Becomes:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_16.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=125 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_7.png" width=508 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_7.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Here our container usage has not only abstracted away the concrete type of our MailTransport but also taken care of configuring its Host property. (Configuration must be a feature of our abstract new() operator since the configuration parameters on an instance depend on its concrete type rather than the service it provides.)&lt;/P&gt;
&lt;H2&gt;Design Outcome: the Global Container&lt;/H2&gt;
&lt;P&gt;Following either of these mental models encourages you to think about the container as something you &lt;EM&gt;retrieve things from&lt;/EM&gt;. Even the name 'container' pushes us in this direction.&lt;/P&gt;
&lt;P&gt;From this perspective, there isn't much of a challenge in the application architecture design assignment!&lt;/P&gt;
&lt;P&gt;We'll just create a static property &lt;STRONG&gt;&lt;EM&gt;Container&lt;/EM&gt;&lt;/STRONG&gt; on a class, say, &lt;STRONG&gt;&lt;EM&gt;Global&lt;/EM&gt;&lt;/STRONG&gt; and get instances from it using &lt;STRONG&gt;&lt;EM&gt;Global.Container.Resolve&amp;lt;MyService&amp;gt;().&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Easy!&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_2.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=146 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb.png" width=461 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;This snippet should be familiar as a pattern with another name: the static &lt;EM&gt;Service Locator&lt;/EM&gt;. &lt;/P&gt;
&lt;H3&gt;Problems with the Global Container as a Service Locator&lt;/H3&gt;
&lt;P&gt;There are good reasons why this pattern can rightly be called an anti-pattern:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Fragility&lt;/STRONG&gt;. The container acts like a bucket of global variables. All of the robustness that is achieved by forcing components to publicly declare dependencies is lost. Unexpected dependencies can appear between seemingly unrelated parts of an application, complicating maintenance.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Reduced Composability&lt;/STRONG&gt;. When dependencies are injected, the container can be configured to provide different implementations to different consumers. This is often necessary when combining components that were built independently. When dependencies are retrieved from a global container, the container does not know the identity of the requestor, and is forced to return the same implementation every time.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Limited Reuse&lt;/STRONG&gt;. Where the global container resides can limit the reusability of components that depend on it. Migrating components to a WPF application will require code changes if they depend on the global container attached to the HttpApplication class.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Implementation Issues&lt;/STRONG&gt;. Concurrency, re-entrancy, circular reference detection, and component lifetime management are much harder/messier in a global container based solution.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Resumption of Control: &lt;/STRONG&gt;Generally speaking, these issues arise because calling directly into a global container is a resumption of control. In a container-managed application, the container is given the responsibility of getting the right instances into the right places in order for work to be done. Calling into a global container takes over some of this responsibility in a much less manageable way.&lt;/P&gt;
&lt;P&gt;Sometimes, because of the design decisions made in many current-generation frameworks, the &lt;EM&gt;Global Container&lt;/EM&gt; is a necessity, but in order to advance the state of software engineering we need to look beyond this.&lt;/P&gt;
&lt;H2&gt;Thinking Inside the Box&lt;/H2&gt;
&lt;P&gt;The mental models listed earlier should have rung some alarm bells:&lt;/P&gt;
&lt;P&gt;The container is a dictionary of services? Hang on! If I ask for the same service twice, why do I sometimes get different instances? &lt;/P&gt;
&lt;P&gt;Good question. It is easy to configure a dependency injection container to return a new instance of the &lt;STRONG&gt;&lt;EM&gt;ConsoleLogger&lt;/EM&gt;&lt;/STRONG&gt; each time one is &lt;I&gt;resolved&lt;/I&gt;. Not very dictionary-like, is it?&lt;/P&gt;
&lt;P&gt;So the container is really an abstract &lt;EM&gt;new &lt;/EM&gt;operator? Wait! If I sometimes get the same shared instance back, when can I &lt;STRONG&gt;&lt;EM&gt;Dispose()&lt;/EM&gt;&lt;/STRONG&gt; it?&lt;/P&gt;
&lt;P&gt;These two models aren't even compatible. Sometimes the container hands out new instances, but sometimes what comes back from a call to &lt;I&gt;resolve&lt;/I&gt; is a singleton, or an instance that will be shared within some kind of context like the current transaction.&lt;/P&gt;
&lt;P&gt;So things are now confusing: who owns these instances, and where do they ‘live’?&lt;/P&gt;
&lt;H2&gt;Revised Mental Model: An Interconnected System of Objects&lt;/H2&gt;
&lt;P&gt;Here’s the problem: look at the example on the Autofac homepage. It shows how &lt;STRONG&gt;&lt;EM&gt;Register()&lt;/EM&gt;&lt;/STRONG&gt; and &lt;STRONG&gt;&lt;EM&gt;Resolve()&lt;/EM&gt;&lt;/STRONG&gt; are used in Autofac. This is a pretty common kind of introductory example to dependency injection. Notice what’s missing?&lt;/P&gt;
&lt;P&gt;The most important classes in this example are not &lt;STRONG&gt;&lt;EM&gt;ContainerBuilder&lt;/EM&gt;&lt;/STRONG&gt; or &lt;STRONG&gt;&lt;EM&gt;Container&lt;/EM&gt;&lt;/STRONG&gt;. From an application architecture perspective, we need to see the &lt;STRONG&gt;&lt;EM&gt;Straight6TwinTurbo&lt;/EM&gt;&lt;/STRONG&gt; and &lt;STRONG&gt;&lt;EM&gt;Car&lt;/EM&gt;&lt;/STRONG&gt; classes!&lt;/P&gt;
&lt;P&gt;The &lt;I&gt;resolve&lt;/I&gt; operation is just a way of finding an entry point into a self-contained system.&lt;/P&gt;
&lt;P&gt;The entire application resides within the container. There is no ‘outside’ except for whatever entry point gets the ball rolling. An understanding of IoC from this perspective does not revolve around its external APIs like &lt;STRONG&gt;&lt;EM&gt;Register()&lt;/EM&gt;&lt;/STRONG&gt; and &lt;STRONG&gt;&lt;EM&gt;Resolve()&lt;/EM&gt;&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_4.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=214 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_1.png" width=548 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Using this model, the container to gets component instances to the right place at the right time, and application design re-focuses on the implementation of the components themselves.&lt;/P&gt;
&lt;H2&gt;Design Outcome: Injected Context&lt;/H2&gt;
&lt;P&gt;This new perspective on the world leaves us with a challenge when implementing our revised &lt;STRONG&gt;&lt;EM&gt;IControllerProvider&lt;/EM&gt;&lt;/STRONG&gt;. This service needs to return (potentially) new, parameterised instances of the &lt;STRONG&gt;&lt;EM&gt;Controller &lt;/EM&gt;&lt;/STRONG&gt;type.&lt;/P&gt;
&lt;P&gt;The solution, commonly implemented in applications today, is to allow the container to provide context to our controller provider:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_6.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=262 alt=image src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_2.png" width=467 border=0 mce_src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/WheredoestheContainerBelong_D0B1/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;We assume here that &lt;STRONG&gt;&lt;EM&gt;ControllerProvider&lt;/EM&gt;&lt;/STRONG&gt; is itself hosted within the container. &lt;STRONG&gt;&lt;EM&gt;IContext &lt;/EM&gt;&lt;/STRONG&gt;is an interface that Autofac automatically provides to any components that require it. You can create and use an equivalent interface in any other popular IoC container (there's a MEF example linked from below.)&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;IContext &lt;/EM&gt;&lt;/STRONG&gt;provides the instance resolution features of the container to the application, but in a controlled manner. The object implementing &lt;STRONG&gt;&lt;EM&gt;IContext&lt;/EM&gt;&lt;/STRONG&gt; provides the container's services, but may not be the container itself.&lt;/P&gt;
&lt;P&gt;Because this pattern gives the container or application developer an opportunity to customise or proxy the &lt;STRONG&gt;&lt;EM&gt;IContext&lt;/EM&gt;&lt;/STRONG&gt; implementation handed out to any particular component, most of the problems associated with a &lt;STRONG&gt;&lt;EM&gt;Global Container&lt;/EM&gt;&lt;/STRONG&gt; are mitigated.&lt;/P&gt;
&lt;H2&gt;Container-Managed Application Design&lt;/H2&gt;
&lt;P&gt;If you feel like this article and its explanations are somewhat incomplete, you're right. As the title of this post suggests, it is an introduction to a series on the topic. I don't have all the answers, and I don't even have all the questions, so the installments may come in fits and starts. Nevertheless, if you're out to design robust architectures that make use of an component-based composition technology there should be something of interest for you.&lt;/P&gt;
&lt;P&gt;I hope this article has whet your appetite!&lt;/P&gt;
&lt;H2&gt;Sample Code&lt;/H2&gt;
&lt;P&gt;The &lt;A href="http://cid-668630fe3ecd2791.skydrive.live.com/self.aspx/.Public/InjectedContext.zip" mce_href="http://cid-668630fe3ecd2791.skydrive.live.com/self.aspx/.Public/InjectedContext.zip"&gt;sample code, based on MEF preview 3&lt;/A&gt;, is very, very rough, and is really only worth consulting if you need to step through some running code in order to clarify the concepts in this article. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9253987" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/Container+Managed+Application+Design/">Container Managed Application Design</category><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/Autofac/">Autofac</category></item><item><title>Ruby on MEF: Hybrid Application</title><link>http://blogs.msdn.com/b/nblumhardt/archive/2008/12/23/ruby-on-mef-hybrid-application.aspx</link><pubDate>Tue, 23 Dec 2008 03:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9248779</guid><dc:creator>niblumha</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9248779</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2008/12/23/ruby-on-mef-hybrid-application.aspx#comments</comments><description>&lt;p&gt;Since the &lt;a href="http://blogs.msdn.com/nblumhardt/archive/2008/12/14/ruby-on-mef-imports-and-exports.aspx"&gt;last installment&lt;/a&gt; in this little series, I've started to consider how Ruby/C# hybrid MEF applications might look.&lt;/p&gt;  &lt;p&gt;The result is yet another component-based calculator:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="224" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_thumb_1.png" width="304" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Besides the &lt;a href="http://www.youtube.com/watch?v=lstDdzedgcE"&gt;Radiohead arithmetic&lt;/a&gt;, there &lt;em&gt;is&lt;/em&gt; one reason to get excited...&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_10.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="193" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_thumb_4.png" width="218" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Ruby parts! (I bet you hadn't guessed.)&lt;/p&gt;  &lt;p&gt;The Ruby-based export and import features are heading towards a fairly natural syntax. &lt;strong&gt;&lt;em&gt;IOperation&lt;/em&gt;&lt;/strong&gt;, for example, is a regular .NET interface type:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_14.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="87" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_thumb_6.png" width="305" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let's take a look at how the Ruby implementation is woven into the app.&lt;/p&gt;  &lt;h2&gt;Configuration&lt;/h2&gt;  &lt;p&gt;This is still a work-in-progress, so configuration is basic. All of the Ruby parts are loaded from a single file that is fed into the &lt;strong&gt;&lt;em&gt;RubyCatalog&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_12.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="59" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_thumb_5.png" width="493" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;&lt;em&gt;calculator_ops.rb &lt;/em&gt;&lt;/strong&gt;file contains part definitions like &lt;strong&gt;&lt;em&gt;Multiply &lt;/em&gt;&lt;/strong&gt;from above.&lt;/p&gt;  &lt;p&gt;An additional catalog adds all of the C# parts to the composition as well:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_16.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="37" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_thumb_7.png" width="585" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h2&gt;Main Window&lt;/h2&gt;  &lt;p&gt;The main window is a typical WPF &lt;strong&gt;&lt;em&gt;Window&lt;/em&gt;&lt;/strong&gt; that &lt;strong&gt;&lt;em&gt;Imports&lt;/em&gt;&lt;/strong&gt; a list of operations:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_8.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="115" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_thumb_3.png" width="501" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Because the &lt;strong&gt;&lt;em&gt;MainWindow&lt;/em&gt;&lt;/strong&gt; is instantiated and composed by MEF, all known exporters of &lt;strong&gt;&lt;em&gt;IOperation &lt;/em&gt;&lt;/strong&gt;will be provided, regardless of the language they're implemented in.&lt;/p&gt;  &lt;h2&gt;Bi-Directional Composition&lt;/h2&gt;  &lt;p&gt;You'll notice that the &lt;strong&gt;&lt;em&gt;MainWindow &lt;/em&gt;&lt;/strong&gt;class implements the &lt;strong&gt;&lt;em&gt;IErrorLog &lt;/em&gt;&lt;/strong&gt;contract. This allows messages from the parts to be presented to the user:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_6.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="224" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_thumb_2.png" width="304" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Parts that wish to access the &lt;strong&gt;&lt;em&gt;IErrorLog &lt;/em&gt;&lt;/strong&gt;service from Ruby can import it:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_18.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="312" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_thumb_8.png" width="413" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The integration (and IronRuby in general) treats interface contracts as Ruby &lt;strong&gt;&lt;em&gt;Module&lt;/em&gt;&lt;/strong&gt; objects, so the &lt;strong&gt;&lt;em&gt;IErrorLog &lt;/em&gt;&lt;/strong&gt;used by the &lt;strong&gt;&lt;em&gt;Divide&lt;/em&gt;&lt;/strong&gt; part could be implemented by a Ruby object, although I haven't tested that case.&lt;/p&gt;  &lt;p&gt;Fanatics take note: I did attempt to use IronRuby's case-transforming features to allow &lt;strong&gt;&lt;em&gt;Symbol&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;Apply() &lt;/em&gt;&lt;/strong&gt;to be specified in their natural Ruby forms (&lt;strong&gt;&lt;em&gt;symbol &lt;/em&gt;&lt;/strong&gt;and &lt;strong&gt;&lt;em&gt;apply()&lt;/em&gt;&lt;/strong&gt;) but had no success. Hopefully I'll resolve this in a future version.&lt;/p&gt;  &lt;h2&gt;Monkeypatching the Export Type&lt;/h2&gt;  &lt;p&gt;I can't say whether this will turn out to be a good move or not, but right now it seems reasonable.&lt;/p&gt;  &lt;p&gt;Notice this method call:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_20.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="25" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_thumb_9.png" width="379" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In order to support MEF's lazy-instantiation feature, the &lt;strong&gt;&lt;em&gt;@error_log &lt;/em&gt;&lt;/strong&gt;attribute needs to be of type &lt;strong&gt;&lt;em&gt;System::ComponentModel::Composition::Export&lt;/em&gt;&lt;/strong&gt;, which will supply the actual instance when it is required through the &lt;strong&gt;&lt;em&gt;get_exported_object() &lt;/em&gt;&lt;/strong&gt;method. Calling &lt;strong&gt;&lt;em&gt;@error_log.get_exported_object.add_message&lt;/em&gt;&lt;/strong&gt; felt decidedly unnatural, so I've added method forwarding to the Ruby version of the &lt;em&gt;&lt;strong&gt;Export &lt;/strong&gt;class:&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_22.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="125" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFHybridApplication_E50A/image_thumb_10.png" width="397" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I've had to do some type aliasing to disambiguate &lt;strong&gt;&lt;em&gt;Export &lt;/em&gt;&lt;/strong&gt;from &lt;strong&gt;&lt;em&gt;Export&amp;lt;T&amp;gt;&lt;/em&gt;&lt;/strong&gt;, but otherwise this is straightforward. Rubyists, please weigh in and let me know if this implementation is less-than-desirable :)&lt;/p&gt;  &lt;h2&gt;Source Code&lt;/h2&gt;  &lt;p&gt;Once again you can download the full &lt;a href="http://cid-668630fe3ecd2791.skydrive.live.com/self.aspx/.Public/MefDlr-2008-12-22.zip"&gt;source code for this article&lt;/a&gt; from SkyDrive.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9248779" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/Ruby/">Ruby</category></item><item><title>Ruby on MEF: Imports and Exports</title><link>http://blogs.msdn.com/b/nblumhardt/archive/2008/12/14/ruby-on-mef-imports-and-exports.aspx</link><pubDate>Sun, 14 Dec 2008 04:18:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9212570</guid><dc:creator>niblumha</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9212570</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2008/12/14/ruby-on-mef-imports-and-exports.aspx#comments</comments><description>&lt;p&gt;Well, the &amp;quot;Ruby Parts&amp;quot; implementation is slowly taking shape. If you haven't read &lt;a href="http://blogs.msdn.com/nblumhardt/archive/2008/12/09/hosting-ruby-parts-in-mef.aspx" target="_blank"&gt;the first article&lt;/a&gt; you probably should before you read on.&lt;/p&gt;  &lt;h2&gt;Exports&lt;/h2&gt;  &lt;p&gt;An &lt;em&gt;export&lt;/em&gt; is an object that a part will hand to the outside world to fulfil a &lt;em&gt;contract&lt;/em&gt;. Sometimes, this is the part itself. Other times, the part will expose a property or a method as an export.&lt;/p&gt;  &lt;p&gt;MEF requires us to publish the information about what exports are available (our &lt;strong&gt;&lt;em&gt;ComposablePartDefinition&lt;/em&gt;&lt;/strong&gt;) before any object that provides the export has been created.&lt;/p&gt;  &lt;p&gt;We can achieve this in Ruby because class definitions are &lt;em&gt;executed&lt;/em&gt;. The &lt;strong&gt;&lt;em&gt;export*&lt;/em&gt;&lt;/strong&gt; methods can run arbitrary code and have access to the class definition that they're running within.&lt;/p&gt;  &lt;p&gt;Without any syntactic sugar, a part definition that exports the 'answer' contract looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFImportsandExports_EAF8/image_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="93" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFImportsandExports_EAF8/image_thumb_1.png" width="210" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Other parts that import the 'answer' contract will get the integer value 42.&lt;/p&gt;  &lt;p&gt;Our technique is:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Keep a global table of 'part definitions' that can be updated as a Ruby script executes &lt;/li&gt;    &lt;li&gt;When a static &lt;strong&gt;&lt;em&gt;export*&lt;/em&gt;&lt;/strong&gt; method executes, create a function that can retrieve the export from a part instance (the 'export accessor') &lt;/li&gt;    &lt;li&gt;Store the export accessor along with the part's class in the table of part definitions &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Each part definition ends up mapping a Ruby class to a table of contract name/function pairs.&lt;/p&gt;  &lt;p&gt;When an export is requested from our part, the value returned is the result of executing the function that we stored for that export in the part definition.&lt;/p&gt;  &lt;p&gt;The function can return either the object itself, an attribute, or a &lt;strong&gt;&lt;em&gt;Proc&lt;/em&gt;&lt;/strong&gt; object representing a method, which seems like a good generic foundation to build our different kinds of exports on top of.&lt;/p&gt;  &lt;p&gt;Because &lt;strong&gt;&lt;em&gt;self&lt;/em&gt;&lt;/strong&gt; or a &lt;strong&gt;&lt;em&gt;Proc&lt;/em&gt;&lt;/strong&gt; can be returned from a property, we'll implement property exports first.&lt;/p&gt;  &lt;p&gt;In the example, to get things started the export accessor function is explicitly specified in a block argument to the &lt;strong&gt;&lt;em&gt;export_attr &lt;/em&gt;&lt;/strong&gt;method.&lt;/p&gt;  &lt;h3&gt;The export_attr Method&lt;/h3&gt;  &lt;p&gt;This method is defined as a class method on &lt;strong&gt;&lt;em&gt;PartDefinition&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFImportsandExports_EAF8/image_8.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="145" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFImportsandExports_EAF8/image_thumb_3.png" width="462" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So, while the &lt;strong&gt;&lt;em&gt;MyPart&lt;/em&gt;&lt;/strong&gt; class is being defined in the Ruby runtime, &lt;strong&gt;&lt;em&gt;export_attr &lt;/em&gt;&lt;/strong&gt;will create an export accessor that runs the provided block in the context of the part instance.&lt;/p&gt;  &lt;h3&gt;RubyCatalog&lt;/h3&gt;  &lt;p&gt;The meat of the &lt;strong&gt;&lt;em&gt;add_export&lt;/em&gt;&lt;/strong&gt; method creates or finds a &lt;strong&gt;&lt;em&gt;RubyPartDefinition&lt;/em&gt;&lt;/strong&gt; instance and adds an export definition to it.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFImportsandExports_EAF8/image_5.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="283" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/RubyonMEFImportsandExports_EAF8/image_thumb.png" width="512" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The mysterious &lt;strong&gt;&lt;em&gt;MefPartsCollection&lt;/em&gt;&lt;/strong&gt; variable that appears in the above code is a named instance provided by the MEF &lt;strong&gt;&lt;em&gt;RubyCatalog &lt;/em&gt;&lt;/strong&gt;to the IronRuby runtime.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;RubyCatalog &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;ComposablePartCatalog
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ScriptRuntime &lt;/span&gt;_runtime;
    &lt;span style="color: blue"&gt;readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ScriptEngine &lt;/span&gt;_ruby;
    &lt;span style="color: blue"&gt;readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ComposablePartDefinition&lt;/span&gt;&amp;gt; _parts;

    &lt;span style="color: blue"&gt;public &lt;/span&gt;RubyCatalog(&lt;span style="color: blue"&gt;string &lt;/span&gt;script)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(script == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;script&amp;quot;&lt;/span&gt;);

        &lt;span style="color: blue"&gt;var &lt;/span&gt;rubySetup = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;LanguageSetup&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;RubyContext&lt;/span&gt;).AssemblyQualifiedName);
        rubySetup.FileExtensions.Add(&lt;span style="color: #a31515"&gt;&amp;quot;rb&amp;quot;&lt;/span&gt;);
        rubySetup.Names.Add(&lt;span style="color: #a31515"&gt;&amp;quot;IronRuby&amp;quot;&lt;/span&gt;);

        &lt;span style="color: blue"&gt;var &lt;/span&gt;setup = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ScriptRuntimeSetup&lt;/span&gt;();
        setup.LanguageSetups.Add(rubySetup);

        _runtime = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ScriptRuntime&lt;/span&gt;(setup);
        _runtime.LoadAssembly(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;RubyPartDefinition&lt;/span&gt;).Assembly);
        _runtime.LoadAssembly(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ExportDefinition&lt;/span&gt;).Assembly);
        _runtime.LoadAssembly(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;).Assembly);

        _ruby = _runtime.GetEngine(&lt;span style="color: #a31515"&gt;&amp;quot;IronRuby&amp;quot;&lt;/span&gt;);

        &lt;span style="color: blue"&gt;var &lt;/span&gt;parts = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Hashtable&lt;/span&gt;();
        _runtime.Globals.SetVariable(&lt;span style="color: #a31515"&gt;&amp;quot;MefPartsCollection&amp;quot;&lt;/span&gt;, parts);
        _ruby.Execute(&lt;span style="color: #2b91af"&gt;Script&lt;/span&gt;.PartDefinition);
        _ruby.Execute(script);

        _parts = parts.Values.Cast&amp;lt;&lt;span style="color: #2b91af"&gt;ComposablePartDefinition&lt;/span&gt;&amp;gt;().AsQueryable();
    }

    &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ComposablePartDefinition&lt;/span&gt;&amp;gt; Parts
    {
        &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_parts; }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;You can see how the &lt;strong&gt;&lt;em&gt;&amp;quot;MefPartsCollection&amp;quot;&lt;/em&gt;&lt;/strong&gt; variable is being set to a &lt;strong&gt;&lt;em&gt;Hashtable &lt;/em&gt;&lt;/strong&gt;before the script executes, and afterwards the values are retrieved as a list of &lt;strong&gt;&lt;em&gt;ComposablePartDefinition&lt;/em&gt;&lt;/strong&gt; instances.&lt;/p&gt;

&lt;p&gt;The DLR setup in the catalog is a bit tentative. We'll refactor and extend it to be a little more user-friendly in the future.&lt;/p&gt;

&lt;p&gt;(This catalog design assumes a single catalog per IronRuby runtime. This probably isn't going to be the best outcome for pure-Ruby applications, but for C# applications hosting Ruby parts this will work for now.)&lt;/p&gt;

&lt;h3&gt;Ruby Programming Model&lt;/h3&gt;

&lt;p&gt;The items in the &lt;strong&gt;&lt;em&gt;MefPartsCollection&lt;/em&gt;&lt;/strong&gt; are instances of the &lt;strong&gt;&lt;em&gt;RubyPartDefinition &lt;/em&gt;&lt;/strong&gt;class, indexed by Ruby part class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;RubyPartDefinition&lt;/em&gt;&lt;/strong&gt; collaborates with &lt;strong&gt;&lt;em&gt;RubyPart&lt;/em&gt;&lt;/strong&gt;,&lt;strong&gt;&lt;em&gt; RubyExportDefinition&lt;/em&gt;&lt;/strong&gt; and&lt;strong&gt;&lt;em&gt; RubyImportDefinition&lt;/em&gt;&lt;/strong&gt; to create what MEF terms a 'programming model'.&lt;/p&gt;

&lt;p&gt;I've implemented the programming model classes in C#, but they should be able to be implemented equally well in Ruby. Porting them might be an interesting exercise for the reader :)&lt;/p&gt;

&lt;h2&gt;Imports&lt;/h2&gt;

&lt;p&gt;Imports are defined using a similar technique. You can see how they work in the example code.&lt;/p&gt;

&lt;h2&gt;The Unit Tests&lt;/h2&gt;

&lt;p&gt;The solution you'll find attached to this article doesn't include an application - just a meagre selection of unit tests for the catalog.&lt;/p&gt;

&lt;p&gt;The most interesting is this one:&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;ImportsValues()
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;script = &lt;span style="color: #a31515"&gt;@&amp;quot;
        class MyPart &amp;lt; PartDefinition
            import 'input_value' do |input|
                @input_value = input
            end

            export_attr 'output_value' do
                @input_value.get_exported_object
            end
        end
    &amp;quot;&lt;/span&gt;;

    &lt;span style="color: #2b91af"&gt;ComposablePart &lt;/span&gt;part = CreateMyPart(script).CreatePart();
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(1, part.ImportDefinitions.Count());

    &lt;span style="color: blue"&gt;var &lt;/span&gt;inputImport = part.ImportDefinitions.Single(
        i =&amp;gt; ((&lt;span style="color: #2b91af"&gt;ContractBasedImportDefinition&lt;/span&gt;)i).ContractName == &lt;span style="color: #a31515"&gt;&amp;quot;input_value&amp;quot;&lt;/span&gt;);
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(1, part.ExportDefinitions.Count());

    &lt;span style="color: blue"&gt;var &lt;/span&gt;outputExport = part.ExportDefinitions.Single();
    &lt;span style="color: blue"&gt;var &lt;/span&gt;testValue = &lt;span style="color: #a31515"&gt;&amp;quot;Hello, world&amp;quot;&lt;/span&gt;;
    part.SetImport(inputImport, &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Export&lt;/span&gt;(
        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ExportDefinition&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;input_value&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;null&lt;/span&gt;),
        () =&amp;gt; testValue)});
    &lt;span style="color: blue"&gt;var &lt;/span&gt;output = part.GetExportedObject(outputExport);
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(testValue, output);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;font face="Courier New"&gt;&lt;/font&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here you can see a complete roundtrip from import to export. Not groundbreaking, but I think this puts us on the right track.&lt;/p&gt;

&lt;h2&gt;Coming up...&lt;/h2&gt;

&lt;p&gt;We'll implement the basics of metadata, tidy up the syntax (I'm considering &lt;a href="http://davesbox.com/archive/2008/12/11/ruby-and-the-managed-extensiblity-framework.aspx" target="_blank"&gt;Dave's post&lt;/a&gt; a challenge ;)), and start to look at a hybrid application. Stay tuned!&lt;/p&gt;

&lt;h2&gt;Files&lt;/h2&gt;

&lt;p&gt;&lt;a title="MefDlr-2008-12-13.zip" href="http://cid-668630fe3ecd2791.skydrive.live.com/self.aspx/.Public/MefDlr-2008-12-13.zip"&gt;MefDlr-2008-12-13.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9212570" width="1" height="1"&gt;</description></item><item><title>Hosting Ruby Parts in MEF</title><link>http://blogs.msdn.com/b/nblumhardt/archive/2008/12/09/hosting-ruby-parts-in-mef.aspx</link><pubDate>Tue, 09 Dec 2008 22:29:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9187725</guid><dc:creator>niblumha</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/nblumhardt/rsscomments.aspx?WeblogPostID=9187725</wfw:commentRss><comments>http://blogs.msdn.com/b/nblumhardt/archive/2008/12/09/hosting-ruby-parts-in-mef.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://codeplex.com/MEF" target="_blank"&gt;MEF&lt;/a&gt; is fascinating because of the way some initial assumptions led to a different flavor of composition technology from the ones we've seen for .NET to date.&lt;/p&gt;  &lt;p&gt;In my opinion, the most exciting &amp;#8216;parameter variation&amp;#8217; in the design process for MEF was the idea that &amp;#8216;parts&amp;#8217; should support the full range of .NET languages. The recent popularity of &lt;a href="http://www.codeplex.com/IronPython" target="_blank"&gt;IronPython&lt;/a&gt; and &lt;a href="http://www.ironruby.net/" target="_blank"&gt;IronRuby&lt;/a&gt;, and the inclusion of the &lt;a href="http://codeplex.com/DLR" target="_blank"&gt;DLR&lt;/a&gt; in .NET 4.0, meant that dynamic language support got first-class consideration.&lt;/p&gt;  &lt;p&gt;As readers of my &lt;a href="http://ubik.com.au" target="_blank"&gt;other blog&lt;/a&gt; may know, I&amp;#8217;m a fan of Ruby in particular. For that reason I&amp;#8217;m going to spend some time exploring how Ruby can fit into a MEF application.&lt;/p&gt;  &lt;h3&gt;Disclaimer:&lt;/h3&gt;  &lt;p&gt;I'm by no means a Ruby expert. Let me know if you seem me off course!&lt;/p&gt;  &lt;h2&gt;The Role of Parts&lt;/h2&gt;  &lt;p&gt;MEF&amp;#8217;s unit of composition is the &lt;strong&gt;&lt;em&gt;ComposablePart&lt;/em&gt;&lt;/strong&gt;, or just &amp;#8216;part&amp;#8217;. Each part is self-contained and opaque to the outside world. The MEF &lt;strong&gt;&lt;em&gt;CompositionContainer&lt;/em&gt;&lt;/strong&gt; wires parts together according to the primitive &lt;strong&gt;&lt;em&gt;Export&lt;/em&gt;&lt;/strong&gt;s that each part exposes and consumes.&lt;/p&gt;  &lt;p&gt;The independence of the part implementation behind these abstractions makes it possible to build a part just about any way you please.&lt;/p&gt;  &lt;p&gt;The goal of the MEF/Ruby integration is to allow parts to be written in Ruby.&lt;/p&gt;  &lt;h2&gt;C# and the Attributed Part&lt;/h2&gt;  &lt;p&gt;In the default programming model, parts are defined in C# as classes, with attributes to describe their exports and imports:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/HostingRubyPartsinMEF_71AC/image_4.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="259" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/HostingRubyPartsinMEF_71AC/image_thumb_1.png" width="638" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This example is from the &lt;a href="http://www.codeplex.com/MEF/Wiki/View.aspx?title=Declaring%20Exports" target="_blank"&gt;MEF Programming Guide&lt;/a&gt;, which describes these and some other interesting export and import capabilities (like method exports, which we'll look at later in this series.)&lt;/p&gt;  &lt;p&gt;We call this the &lt;em&gt;attributed programming model &lt;/em&gt;because MEF is agnostic about how parts are defined. A good example of an alternative programming model for an earlier version of MEF is &lt;a href="http://www.managed-world.com/archive/2008/07/04/building-a-fluent-interface-for-mef.aspx" target="_blank"&gt;Jason Olson's fluent version&lt;/a&gt;.&lt;/p&gt;  &lt;h2&gt;Translating Part Definitions into Ruby&lt;/h2&gt;  &lt;p&gt;I'm going to assume that if you are interested enough to read this far, you probably have at least a basic familiarity with the Ruby language, or the motivation to &lt;a href="http://www.ruby-lang.org/en/documentation/quickstart/" target="_blank"&gt;learn it&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Just as there are multiple ways of defining MEF parts in C#, there are endless ways that MEF parts could be defined in Ruby. There are two obvious ways that I can see Ruby interacting with other .NET languages via MEF:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Ruby snippets fulfil a specific role in a C# application (e.g. as command macros); or,&lt;/li&gt;    &lt;li&gt;Composite applications are built using a mixture of Ruby and C#&lt;/li&gt; &lt;/ol&gt;  &lt;h3&gt;Ruby Parts as 'Macros' (1)&lt;/h3&gt;  &lt;h3&gt;&lt;/h3&gt;  &lt;p&gt;The first approach would point towards supporting a minimal subset of MEF in order to supply Ruby snippets, probably as functions, to C# components:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/HostingRubyPartsinMEF_71AC/image_6.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="88" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/HostingRubyPartsinMEF_71AC/image_thumb_2.png" width="403" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;For this imaginary example, a MEF integration would expose all similar functions from a script as &lt;strong&gt;&lt;em&gt;Action&amp;lt;Document&amp;gt;&lt;/em&gt;&lt;/strong&gt; delegates that could be imported by other parts. Export metadata could be used in order to describe the command as &amp;quot;delete selection&amp;quot; and this would be sufficient for including the command into user-defined toolbar buttons.&lt;/p&gt;  &lt;p&gt;A simple technique like this is appealing for the first kind of Ruby/MEF integration, and I'm sure it is going to be implemented widely (I've already seen something similar done in Python.)&lt;/p&gt;  &lt;h3&gt;Full-Fledged Ruby Parts (2)&lt;/h3&gt;  &lt;p&gt;The second approach - building composite applications - requires that Ruby parts support most of the key MEF programming model features. It makes more sense in this case to use Ruby classes as the primary definition of a Part, and to require that the Ruby programmer has some knowledge of MEF itself.&lt;/p&gt;  &lt;p&gt;This is the approach that I'm going to take in this series of posts.&lt;/p&gt;  &lt;h2&gt;Defining Ruby Parts&lt;/h2&gt;  &lt;p&gt;One of the wonderful things about Ruby is its flexibility. It is entirely possible to take a syntax-first approach and to work back from there towards an implementation.&lt;/p&gt;  &lt;p&gt;My 'ideal' translation of the C# sample above would look something like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/HostingRubyPartsinMEF_71AC/image_14.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="291" alt="image" src="http://blogs.msdn.com/blogfiles/nblumhardt/WindowsLiveWriter/HostingRubyPartsinMEF_71AC/image_thumb_6.png" width="516" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;It should be possible to interchange the Ruby &lt;strong&gt;&lt;em&gt;Configuration &lt;/em&gt;&lt;/strong&gt;part for the C# one in this example &lt;em&gt;without changing any further code in the application.&lt;/em&gt; This is a really exciting possibility enabled by the DLR and MEF, and we'll try to push our implementation as far as possible in that direction.&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h3&gt;Identifying Parts&lt;/h3&gt;  &lt;p&gt;I'm going to attempt to avoid a 'Part' base class, and instead regard any class that contains &lt;strong&gt;&lt;em&gt;export &lt;/em&gt;&lt;/strong&gt;statements as representing a part.&lt;/p&gt;  &lt;p&gt;One thing this seems to imply is that the helper methods will be on &lt;strong&gt;&lt;em&gt;Kernel&lt;/em&gt;&lt;/strong&gt;. This might not be a good thing (it's like adding extension methods to &lt;strong&gt;&lt;em&gt;System.Object&lt;/em&gt;&lt;/strong&gt; in C#) so the base class might appear in the future.&lt;/p&gt;  &lt;h3&gt;Exports&lt;/h3&gt;  &lt;p&gt;There are three kinds of exports we need to account for: exporting the entire 'part object', exporting a property value, and exporting a method. All three of these are going to appear in the body of our Ruby class definition.&lt;/p&gt;  &lt;p&gt;We'll need to use separate methods for each export type, so that we can differentiate between them. This is especially relevant in the case of properties/methods, which are the same under the hood in Ruby, but which need to be exported differently. A method export will be a delegate type, while a property export will be a value that is the result of invoking the property. Using &lt;strong&gt;&lt;em&gt;export_attr&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;export_method &lt;/em&gt;&lt;/strong&gt;would help to disambiguate this case clearly.&lt;/p&gt;  &lt;h3&gt;Imports&lt;/h3&gt;  &lt;p&gt;Imports are a bit simpler. I think that a single mechanism for importing values will suffice, and some hash parameters will allow the value to be imported into an attribute, an attribute writer (property) or perhaps via an optional block.&lt;/p&gt;  &lt;h2&gt;Where to Next?&lt;/h2&gt;  &lt;p&gt;There's no source code with this article today. I have a few unit tests working but things are a bit haphazard and not packaged up to distribute. I'm still experimenting with syntax so expect some significant changes of direction :)&lt;/p&gt;  &lt;p&gt;I'll try to keep installments in this series short - perhaps a single unit test each. We'll work from there towards an example application that pulls everything together.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9187725" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/MEF/">MEF</category><category domain="http://blogs.msdn.com/b/nblumhardt/archive/tags/Ruby/">Ruby</category></item></channel></rss>
