<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.microsoft.co.il/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>Bnaya Eshet : extension</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx</link><description>Tags: extension</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Immutable Collections</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/02/17/immutable-collections.aspx</link><pubDate>Sun, 17 Feb 2013 19:45:12 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1751972</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Immutable Collections &lt;/h2&gt;  &lt;p&gt;&lt;strong&gt;Immutability&lt;/strong&gt; is a pattern which is suit well &lt;strong&gt;parallel programming&lt;/strong&gt;,     &lt;br /&gt;but you have to be aware of a potential &lt;strong&gt;memory pressure risk&lt;/strong&gt; when it&amp;#39;s not implemented right or used wisely.&lt;/p&gt;  &lt;p&gt;this post will cover a new &lt;strong&gt;BCL library&lt;/strong&gt; (still in its preview stage) which is targeting&lt;strong&gt; immutable collections&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0382_692BD55A.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Immutable, Parallel, Task, BCL, collection, Thread-safe" border="0" alt="Immutable, Parallel, Task, BCL, collection, Thread-safe" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0382_thumb_6CE97A2A.jpg" width="423" height="308" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;.NET&lt;/strong&gt; is already having &lt;strong&gt;Concurrent implementation&lt;/strong&gt; for Queue, Stack, Bug and Dictionary, which is &lt;strong&gt;thread-safe&lt;/strong&gt;, but other type of collection like List is missing.     &lt;br /&gt;another type of collection are &lt;strong&gt;read only collection&lt;/strong&gt;, but those type of collection are &lt;strong&gt;not truly thread-safe&lt;/strong&gt; because it is just a wrapper upon other collection which can be mutate by it&amp;#39;s owner.     &lt;br /&gt;so &lt;strong&gt;iterating on a read only collection doesn&amp;#39;t guarantee thread-safety&lt;/strong&gt; because the collection can be mutate during the iteration by it&amp;#39;s original owner.     &lt;br /&gt;currently we do have one option to create an &lt;strong&gt;immutable collection&lt;/strong&gt; by using the &lt;strong&gt;ToArray()&lt;/strong&gt; extension method, but as I mentioned before it &lt;strong&gt;can lead to a memory pressure&lt;/strong&gt; while using a large collection.&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;BCL team&lt;/strong&gt; was targeting this issue and are working on a new kind of collections which are immutable.     &lt;br /&gt;those collection is having a cool concept which guarantee that an immutable sequence will not changed, but it can be part of another immutable sequence. this mean that you can take an immutable collection and add an item. this will keep the immutable part as is and you will get a new collection that include your addition and the original collection, but if someone is using the original collection he won&amp;#39;t be affected by your mutation.     &lt;br /&gt;as you can see the &lt;strong&gt;BCL immutable collection&lt;/strong&gt; &lt;strong&gt;can be&lt;/strong&gt; &lt;strong&gt;mutate&lt;/strong&gt; but the mutation will return a new collection and leave the original collection untouched.&lt;/p&gt;  &lt;h5&gt;Start using the immutable collection&lt;/h5&gt;  &lt;p&gt;you can start using it though &lt;a href="https://nuget.org/packages/Microsoft.Bcl.Immutable"&gt;NuGet&lt;/a&gt;.     &lt;br /&gt;on the current preview version it includes:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ImmutableStack&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableQueue&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableList&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableHashSet&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableSortedSet&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableDictionary&amp;lt;K, V&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableSortedDictionary&amp;lt;K, V&amp;gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h5&gt;Fluent API&lt;/h5&gt;  &lt;p&gt;I will speak about 3 APIs:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Add &lt;/li&gt;    &lt;li&gt;AddRange &lt;/li&gt;    &lt;li&gt;Builder &lt;/li&gt; &lt;/ul&gt;  &lt;h6&gt;Add&lt;/h6&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6e44ba97-9c59-49e9-8793-4d1ed33706a6" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Test&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; List_AddItem_Test()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImmutableList&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;.Empty;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col1 = col.Add(1);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col2 = col1.Add(2);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(0, col.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(1, col1.Count);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(2, col2.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;whenever you want an &lt;strong&gt;empty instance&lt;/strong&gt; of immutable collection you should use the &lt;strong&gt;static Empty property&lt;/strong&gt; (there is no public constructor available).&lt;/p&gt;  &lt;p&gt;you should always remember that Adding an item will &lt;strong&gt;not affect the original collection&lt;/strong&gt; therefore you must &lt;strong&gt;handle the return value&lt;/strong&gt; (is is the same concept as string.Replace).&lt;/p&gt;  &lt;p&gt;finally you can notice at lines 8-10 that the above code was actually construct 3 different immutable collections.&lt;/p&gt;  &lt;h6&gt;AddRange&lt;/h6&gt;  &lt;p&gt;both from &lt;strong&gt;performance&lt;/strong&gt; and &lt;strong&gt;usability&lt;/strong&gt; it is better using the &lt;strong&gt;AddRange&lt;/strong&gt; API when you intend to add multiple items.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4a59b070-234d-40da-af88-f5149f6ae735" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Test&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; List_AddRange_Test()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImmutableList&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;.Empty;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col1 = col.AddRange(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Enumerable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Range(0, 1000));&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(0, col.Count);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(1000, col1.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h6&gt;Builder&lt;/h6&gt;  &lt;p&gt;if you want to &lt;strong&gt;efficiently&lt;/strong&gt; &lt;strong&gt;add multiple items&lt;/strong&gt; and AddRange doesn&amp;#39;t do the job for you, you can use a &lt;strong&gt;Builder&lt;/strong&gt; (the concept is quit similar to StringBuilder).     &lt;br /&gt;you should remember that &lt;strong&gt;unlike the immutable collection a builder is not a thread-safe&lt;/strong&gt; structure, therefore you shouldn&amp;#39;t share its state between threads.     &lt;br /&gt;when you complete the mutation, you can &lt;strong&gt;extract an immutable collection from the builder&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:74e71650-2eaf-46b2-b766-eed18f1569aa" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Test&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; List_Builder_Test()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Enumerable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Range(0, 1000).ToImmutableList();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; builder = col.ToBuilder();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;for&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; i = 0; i &amp;lt; 100; i++)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;builder.Remove(i * 10);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col1 = builder.ToImmutable();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(1000, col.Count);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(900, col1.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;line 5&lt;/strong&gt;: getting a builder from immutable collection.     &lt;br /&gt;&lt;strong&gt;line 10&lt;/strong&gt;: getting an immutable back from the builder (after the mutation).&lt;/p&gt;  &lt;h5&gt;Migration&lt;/h5&gt;  &lt;p&gt;if you are using a read only collection though the IReadOnlyCollection, IReadOnlyList or IReadOnlyDictionary, migration will be easy because the immutable collections are implementing those interfaces.&lt;/p&gt;  &lt;h5&gt;Optimization&lt;/h5&gt;  &lt;p&gt;we already spoke about the memory pressure optimization, but what is the characterize of those collection in compare with the well known BCL mutable collection?    &lt;br /&gt;as the BCL team argue that &lt;strong&gt;immutable collections have been heavily tuned for maximum performance and minimum GC pressure&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the following table was published by the &lt;strong&gt;BCL team&lt;/strong&gt;.     &lt;br /&gt;it &lt;strong&gt;compare the performance behavior of the BCL immutable and immutable&lt;/strong&gt; &lt;strong&gt;collection&lt;/strong&gt; (and remember that this is only a preview version which mean that further optimization can be applied).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/immutable-table_3C72F5B4.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Immutable, Parallel, Task, BCL, collection, Thread-safe" border="0" alt="Immutable, Parallel, Task, BCL, collection, Thread-safe" src="http://blogs.microsoft.co.il/blogs/bnaya/immutable-table_thumb_33A2ED68.png" width="492" height="179" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;you can read more on this at &lt;a href="http://blogs.msdn.com/b/bclteam/archive/2012/12/18/preview-of-immutable-collections-released-on-nuget.aspx"&gt;this&lt;/a&gt; post.&lt;/p&gt;  &lt;h5&gt;summary&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;Immutability&lt;/strong&gt; is a great pattern for &lt;strong&gt;parallelism&lt;/strong&gt;.     &lt;br /&gt;the BCL team are giving us a robust and &lt;strong&gt;well design immutable collection&lt;/strong&gt;.     &lt;br /&gt;the potential memory pressure has reduced by the internal data-structure design.     &lt;br /&gt;so it is definitely something to wait for its release.&lt;/p&gt;  &lt;p&gt;you can read more about it at &lt;a href="http://blogs.msdn.com/b/bclteam/archive/2012/12/18/preview-of-immutable-collections-released-on-nuget.aspx"&gt;here&lt;/a&gt;.     &lt;br /&gt;and if you want more background and comparison to the read only collection, you can check out &lt;a href="http://blogs.msdn.com/b/andrewarnottms/archive/2011/08/22/read-only-frozen-and-immutable-types-and-collections.aspx"&gt;this&lt;/a&gt; post.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/Immutable-Collections-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F02%2F17%2Fimmutable-collections.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1751972" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/collection/default.aspx">collection</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/BCL/default.aspx">BCL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Immutable/default.aspx">Immutable</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread-safe/default.aspx">Thread-safe</category></item><item><title>MEF 2.0 - mini series: Part 7 (Catalog filter and Deep hierarchic scoping)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/24/mef-2-0-mini-series-part-7-catalog-filter-and-deep-hierarchic-scoping.aspx</link><pubDate>Thu, 24 Jan 2013 16:31:18 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1696250</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF 2.0 - mini series: Part 7 (Catalog filter and Deep hierarchic scoping)&lt;/h2&gt;  &lt;p&gt;this is the 7th post in the &lt;strong&gt;MEF 2.0&lt;/strong&gt; mini series.     &lt;br /&gt;you can see the following &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx"&gt;TOC&lt;/a&gt; for other posts in this series.&lt;/p&gt;  &lt;p&gt;in the &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/16/mef-2-0-mini-series-part-6-composition-scoping-and-lifetime-management.aspx" target="_blank"&gt;previous&lt;/a&gt; post I was talking about &lt;strong&gt;composition scoping &lt;/strong&gt;and &lt;strong&gt;lifetime management.&lt;/strong&gt;     &lt;br /&gt;on this one, I will extend the &lt;strong&gt;composition scoping&lt;/strong&gt; topic toward &lt;strong&gt;hierarchic&lt;/strong&gt; along with &lt;strong&gt;catalog filtering&lt;/strong&gt; capability.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6354_3E65F039.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" border="0" alt="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6354_thumb_05F7C961.jpg" width="373" height="368" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;hierarchic scoping&lt;/strong&gt; is not trivial, you must understand the hierarchic behavior and what it was design for.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF hierarchic&lt;/strong&gt; was &lt;strong&gt;design&lt;/strong&gt; to enable &lt;strong&gt;parts &lt;/strong&gt;from &lt;strong&gt;higher scope &lt;/strong&gt;to access &lt;strong&gt;lower scope&amp;#39;s part&lt;/strong&gt; without re-instantiation, lower part do not target higher scope&amp;#39;s part directly.&lt;/p&gt;  &lt;p&gt;each hierarchic scope construct by importing a &lt;strong&gt;ExportFactory&amp;lt;T&amp;gt;&lt;/strong&gt; (which we saw in the previous post).&lt;/p&gt;  &lt;p&gt;I will try to make it as simple as I can.&lt;/p&gt;  &lt;p&gt;the following diagram demonstrate the idea of &lt;strong&gt;sub scope&lt;/strong&gt; that &lt;strong&gt;interact with its parent scope&amp;#39;s items&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/SubScope_113DDFA4.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" border="0" alt="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/SubScope_thumb_76251095.png" width="371" height="459" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;let try to have a similar code structures.&lt;/p&gt;  &lt;p&gt;I will use a plug-in base class to trace the instantiations:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:25b11936-2884-4ae7-b73d-702f0d714feb" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;abstract&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; _globalId = 0;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; _id;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; PluginBase()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;_id = _globalId++; &lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;// not thread-safe&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;derived class&amp;#39;s ids will be incremented per instantiation. &lt;/p&gt;  &lt;p&gt;the following snippet include the classes which will be used for our hierarchic demonstration:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b4a6d890-4976-4cfd-b8c1-4a8ed9535156" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ScopeRoot&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ItemA { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ItemB { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ItemZ { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SharedItem { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportFactory&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubScopeRoot&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; SubScope { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubScopeRoot&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItem1&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SubItem1 { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItem2&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SubItem2 { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItem1&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level1ItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ItemShared { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SubItemShared { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItem2&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; SubItem { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Level2SubItemShared&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { }&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;all the classes prefixed with the scoping &amp;#39;&lt;strong&gt;Level&lt;/strong&gt;&amp;#39;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;referring a sub-scope&lt;/strong&gt; required using of &lt;strong&gt;ExportFactory&amp;lt;T&amp;gt; &lt;/strong&gt;(line 23).&lt;/p&gt;  &lt;p&gt;the following diagram is showing their hierarchic and dependencies graph:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/SubScope1_0D03E212.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" border="0" alt="MEF, extension, Composition, ImportMany, Export, Import, .NET 4.5, egistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/SubScope1_thumb_23E2B38E.png" width="487" height="457" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;our final step is to define a &lt;strong&gt;catalog per scope&amp;#39;s hierarchic&lt;/strong&gt; and to &lt;strong&gt;set hierarchic structure&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:cb185349-f5b2-4c6e-a386-5622630e500b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; picker = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RegistrationBuilder&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForTypesDerivedFrom&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;// add ScopeLevel metadata &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.AddMetadata(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;ScopeLevel&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, t =&amp;gt; t.Name.StartsWith(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Level1&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;) ? 1 : 2)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.Export&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginBase&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;).Assembly, picker);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalogL0 = catalog.Filter(p =&amp;gt; p.ContainsPartMetadata(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;ScopeLevel&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, 1));&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalogL1 = catalog.Filter(p =&amp;gt; p.ContainsPartMetadata(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;ScopeLevel&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, 2));&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; scopeL1 = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionScopeDefinition&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalogL1, &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; scopeL0 = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionScopeDefinition&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalogL0, &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] { scopeL1 });&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(scopeL0);&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;at line 2-5, I &lt;strong&gt;export&lt;/strong&gt; &lt;strong&gt;everything inherits from &amp;#39;PluginBase&amp;#39;&lt;/strong&gt; and &lt;strong&gt;adding a &amp;#39;ScopeLevel&amp;#39; metadata&lt;/strong&gt; with the right scope leveling value.&lt;/p&gt;  &lt;p&gt;line 7, define a general &lt;strong&gt;assembly catalog&lt;/strong&gt; (which will be the base for the filtered catalogs).&lt;/p&gt;  &lt;p&gt;lines 9,10, are defining a &lt;strong&gt;filtered catalog&lt;/strong&gt; for each scoping level (filtering an existing assembly catalog).&lt;/p&gt;  &lt;p&gt;lines 12,13, &lt;strong&gt;defines&lt;/strong&gt; the &lt;strong&gt;hierarchic&lt;/strong&gt; between the scoped catalogs.&lt;/p&gt;  &lt;p&gt;line 15, create a &lt;strong&gt;container&lt;/strong&gt; with the top level scope.&lt;/p&gt;  &lt;p&gt;now we&amp;#39;re ready for a composition.&lt;/p&gt;  &lt;p&gt;&lt;a href="https://skydrive.live.com/?cid=9bf7c1a515d76a9a&amp;amp;id=9BF7C1A515D76A9A%216037" target="_blank"&gt;here&lt;/a&gt; you can download a &lt;a href="https://skydrive.live.com/?cid=9bf7c1a515d76a9a&amp;amp;id=9BF7C1A515D76A9A%216037" target="_blank"&gt;sample code&lt;/a&gt;.&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;I consider this feature as &lt;strong&gt;MEF 2&lt;/strong&gt;&amp;#39;s most complex one.&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Hierarchic&lt;/strong&gt; can become complex, so use it with caution.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-Part-7-Catalog-filter-and-Deep-hierarchic-scoping-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F24%2Fmef-2-0-mini-series-part-7-catalog-filter-and-deep-hierarchic-scoping.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1696250" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Metadata/default.aspx">Metadata</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/exception/default.aspx">exception</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/egistrationBuilder/default.aspx">egistrationBuilder</category></item><item><title>MEF 2.0 - mini series: part 6 (Composition scoping and lifetime management)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/16/mef-2-0-mini-series-part-6-composition-scoping-and-lifetime-management.aspx</link><pubDate>Wed, 16 Jan 2013 06:21:01 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1667233</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h3&gt;MEF 2.0 - mini series: part 6 &lt;/h3&gt;  &lt;h3&gt;(Composition scoping and lifetime management)&lt;/h3&gt;  &lt;p&gt;this is the 6th post in the &lt;strong&gt;MEF 2.0&lt;/strong&gt; mini series.     &lt;br /&gt;you can see the following &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx"&gt;TOC&lt;/a&gt; for other posts in this series.&lt;/p&gt;  &lt;p&gt;in this post I will cover a new concept of &lt;strong&gt;scoping&lt;/strong&gt; and &lt;strong&gt;part lifetime management&lt;/strong&gt;, which is a great improvement over MEF 1.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_2080_614BDBED.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="DEV, MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog" border="0" alt="DEV, MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_2080_thumb_3A9D4FA0.jpg" width="471" height="324" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF 1&lt;/strong&gt; was coming with a fairly &lt;strong&gt;naïve lifetime management&lt;/strong&gt;.     &lt;br /&gt;part&amp;#39;s lifetime could be either shared or non-shared (&lt;font color="#666666"&gt;you could also apply &amp;#39;any&amp;#39; but eventually &amp;#39;any&amp;#39; will be created as shared or non-shared&lt;/font&gt;).     &lt;br /&gt;shared is a singleton instantiation, while non-shared will create a new instance each time.&lt;/p&gt;  &lt;p&gt;MEF 1&amp;#39;s instantiation model doesn&amp;#39;t support a complex scenario where some dependency&amp;#39;s lifetime should be dictate by the lifetime of other unites.    &lt;br /&gt;you can conceder a UI window that is having plug-ins that should be dispose while the UI window is closing. &lt;/p&gt;  &lt;p&gt;for example consider you have the following components (parts) dependency flow:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/Presentation1_4CBB76C0.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="DEV, MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog" border="0" alt="DEV, MEF, SELA, Export, Import, CompositionContainer, RegistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/Presentation1_thumb_36A4E561.jpg" width="418" height="235" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;the application can have multiple processes.    &lt;br /&gt;each process is having some plugins, but each view should have different instantiation of the plugins.     &lt;br /&gt;until now you can argue that a non-shared instantiation will do the job.     &lt;br /&gt;the next dependency level (DAL) &lt;strong&gt;should be shared under the boundary of a process&lt;/strong&gt; but &lt;strong&gt;shouldn&amp;#39;t be shared across processes&lt;/strong&gt;.&amp;#160; &lt;br /&gt;this one can neither handle by the share nor by non-shared instantiation.     &lt;br /&gt;single instance of DAL should be created under each view scope.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;MEF 2&lt;/strong&gt; added a &lt;strong&gt;scoped lifetime management&lt;/strong&gt;.     &lt;br /&gt;I will use the&lt;strong&gt; attribute based model&lt;/strong&gt; for this sample, but it will work in the same way in the &lt;strong&gt;fluent model&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bbded519-b190-4131-a26d-a23dc52527e3" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Application&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportFactory&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; ProcAFactory { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportFactory&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; ProcBFactory { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; PlugA { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; PlugB { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; PlugA { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; PlugB { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DAL&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Dal { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginB&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DAL&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Dal { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DAL&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;at line 4-7 you can see a &lt;strong&gt;new type of importing target&lt;/strong&gt; called &lt;strong&gt;ExportFactory&amp;lt;T&amp;gt;&lt;/strong&gt; this type will initialize the scope.     &lt;br /&gt;It is having a &lt;strong&gt;CreateExport&lt;/strong&gt; method that return a &lt;strong&gt;ExportLifetimeContext&amp;lt;T&amp;gt;&lt;/strong&gt; can control the part life time.&lt;/p&gt;  &lt;p&gt;the full implementation of the Application class is:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:28b41930-c6ff-4a1b-ad02-fbbd478b7d27" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;  &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;  &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Application&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportFactory&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; ProcAFactory { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Import&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportFactory&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; ProcBFactory { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; WriteLayoutA()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;using&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportLifetimeContext&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; lifeOfA = ProcAFactory.CreateExport())&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; a = lifeOfA.Value;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Proc A&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;tPlug A: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, a.PlugA.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;t&amp;#92;tDal: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, a.PlugA.Dal.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;tPlug B: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, a.PlugB.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;t&amp;#92;tDal: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, a.PlugB.Dal.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; WriteLayoutB()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;using&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ExportLifetimeContext&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; lifeOfB = ProcBFactory.CreateExport())&lt;/span&gt;&lt;/li&gt; &lt;li&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;                &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;b = lifeOfB.Value;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Proc B&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;tPlug A: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, b.PlugA.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;t&amp;#92;tDal: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, b.PlugA.Dal.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;tPlug B: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, b.PlugB.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li&gt;              &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;&amp;#92;t&amp;#92;tDal: {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, b.PlugB.Dal.GetHashCode());&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;          &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;      &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;  &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;you can see the usage of &lt;strong&gt;ExportFactory&amp;lt;T&amp;gt;&lt;/strong&gt; at lines 11 and 24.&lt;/p&gt;  &lt;p&gt;having a &lt;strong&gt;lifetime handled part&lt;/strong&gt; is the &lt;strong&gt;first step for the scoping&lt;/strong&gt;.     &lt;br /&gt;now we can define which part will be managed by the scope.     &lt;br /&gt;for each scope we except to find a single instantiation of each scoped part.     &lt;br /&gt;as you can see in the diagram below, the Plugins and the DAL should be instantiate once for each scope.     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/Presentation1_4D1783E8.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Presentation1" border="0" alt="Presentation1" src="http://blogs.microsoft.co.il/blogs/bnaya/Presentation1_thumb_376D257E.jpg" width="429" height="241" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;in order to define what&amp;#39;s goes within the scope we should use the &lt;strong&gt;CompositionScopeDefinition&lt;/strong&gt; as shown in the following code snippet:&lt;/p&gt;    &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c29f7814-3e2a-4939-9ad9-13a2a6f52938" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Main(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] args)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; scopeDependentCatalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TypeCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ProcessB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;), &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginA&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;PluginB&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;),&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DAL&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;));&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; scopeDefDependent = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionScopeDefinition&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(scopeDependentCatalog, &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; appCatalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TypeCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Application&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;));&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; scopeDefRoot = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionScopeDefinition&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(appCatalog, &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] { scopeDefDependent });&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(scopeDefRoot);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; app = container.GetExportedValue&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Application&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;app.WriteLayoutA();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;------------------------------------&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;app.WriteLayoutB();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;p&gt;you can see the &lt;strong&gt;scope dependent definition&lt;/strong&gt; at line 9     &lt;br /&gt;and the &lt;strong&gt;hierarchic&lt;/strong&gt; between the scope and the application at line 12.&lt;/p&gt;  &lt;p&gt;as you may notice it is possible to have deeper hierarchic, but this will be shown in future posts.&lt;/p&gt;  &lt;p&gt;the output is:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/scopeoutput_652AC876.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="scopeoutput" border="0" alt="scopeoutput" src="http://blogs.microsoft.co.il/blogs/bnaya/scopeoutput_thumb_2ACF5295.png" width="337" height="235" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;remember that we was printing the &lt;strong&gt;part&amp;#39;s hash code&lt;/strong&gt;.     &lt;br /&gt;you can easily see that each process is having different plugin and DAL&amp;#39;s instantiations, but even those the DAL was consumed by both plugins it has a single instantiation per scope.&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;scoping is a very powerful instantiation model which can solve some of the real-life scenario which wasn&amp;#39;t fall into the shared or non-shared models.&lt;/p&gt;  &lt;p&gt;in future post I will present a deeper hierarchic and the catalog filtering capability.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-part-6-Composition-scoping-and-lifetime-management-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F16%2Fmef-2-0-mini-series-part-6-composition-scoping-and-lifetime-management.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1667233" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF 2.0 - mini series: part 5 (Fluent export properties)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/15/mef-2-0-mini-series-part-5-fluent-export-properties.aspx</link><pubDate>Tue, 15 Jan 2013 06:04:54 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1663793</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF 2.0 - mini series: part 5 (Fluent export properties)&lt;/h2&gt;  &lt;p&gt;this is the 5th post in the &lt;strong&gt;MEF 2.0&lt;/strong&gt; mini series.     &lt;br /&gt;you can see the following &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx"&gt;TOC&lt;/a&gt; for other posts in this series.&lt;/p&gt;  &lt;p&gt;in this post I will cover the &lt;strong&gt;fluent property&amp;#39;s export&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_8047_6004ACAC.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog" border="0" alt="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_8047_thumb_441377B4.jpg" width="533" height="420" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Exporting properties&lt;/strong&gt; is a less known feature of &lt;strong&gt;MEF&lt;/strong&gt;.     &lt;br /&gt;MEF 1 was supporting this feature by using the attribute model.     &lt;br /&gt;you could decorate a property with a &lt;strong&gt;[Export]&lt;/strong&gt; attribute and then it become available for imports.&lt;/p&gt;  &lt;p&gt;the following code demonstrate property exporting in &lt;strong&gt;MEF 1&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;the Foo class is importing multiple SymmetricAlgorithm:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b0602047-a06b-4f28-b86b-e676c745744e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImportMany&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] CryptoProviders { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;and the CryptoComposer class is exporting a few symmetric algorithms:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:31387df1-d35c-4c93-a7ee-5e2c44cf671b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CryptoComposer&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Aes { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AesManaged&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; TripleDES { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TripleDESCryptoServiceProvider&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; RC2 { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RC2CryptoServiceProvider&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;with the following composition the Foo instance will have the CryptoComposer&amp;#39;s symmetric algorithms:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3d1ef620-a957-4e7e-86d9-27990ba28cb0" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;).Assembly);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalog);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foo = container.GetExportedValue&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;so, how do we do it with the fluent export API?&lt;/h5&gt;  &lt;p&gt;to keep it simple I will leave the Foo class with the attribute model (we learned how to use &lt;strong&gt;fluent import&lt;/strong&gt; in the previous &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/12/mef-2-0-mini-series-part-4-fluent-import.aspx" target="_blank"&gt;post&lt;/a&gt;):&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e2fcb7bc-b2db-49d9-ab32-ddbedd73912a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Export&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImportMany&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] CryptoProviders { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CryptoComposer&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Aes { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AesManaged&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; TripleDES { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TripleDESCryptoServiceProvider&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; RC2 { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RC2CryptoServiceProvider&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); } }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;in order to export the CryptoComposer&amp;#39;s properties you should use the following code:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3e993dad-1510-4ea9-a111-a7325b56d7ca" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; picker = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;RegistrationBuilder&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;picker.ForType&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CryptoComposer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.ExportProperties(p =&amp;gt; p.PropertyType == &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;SymmetricAlgorithm&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;));&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; catalog = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;).Assembly, picker);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; container = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(catalog);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; foo = container.GetExportedValue&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Foo&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;summary&lt;/h5&gt;  &lt;p&gt;exporting properties using the &lt;strong&gt;fluent API&lt;/strong&gt; is fairly straightforward.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-mini-series-part-5-Fluent-export-properties-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F15%2Fmef-2-0-mini-series-part-5-fluent-export-properties.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1663793" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF 2.0 TOC</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-toc.aspx</link><pubDate>Sun, 06 Jan 2013 14:30:49 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1634514</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF 2.0 TOC&lt;/h2&gt;  &lt;p&gt;&lt;strong&gt;MEF 2.0&lt;/strong&gt; is a reflection of a community requests.     &lt;br /&gt;features like &lt;strong&gt;&lt;em&gt;Open Generics&lt;/em&gt;&lt;/strong&gt;, &lt;em&gt;&lt;strong&gt;fluent&lt;/strong&gt; and &lt;strong&gt;conventional discovery&lt;/strong&gt;&lt;/em&gt;, &lt;em&gt;&lt;strong&gt;lifetime handling&lt;/strong&gt; of the part&lt;/em&gt;, &lt;strong&gt;&lt;em&gt;better exception handling &lt;/em&gt;&lt;/strong&gt;and more, was all requested by the community.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_8436_0F867DDB.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog, Generics" border="0" alt="MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog, Generics" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_8436_thumb_46805C14.jpg" width="501" height="359" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/04/mef-2-0-mini-series-part-1.aspx" target="_blank"&gt;part 1: Open Generics&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/06/mef-2-0-mini-series-part-2-fluent-and-conventional-export.aspx" target="_blank"&gt;Part 2: Fluent and Conventional Export&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/09/mef-2-0-mini-series-part-3-fluent-import-constructor.aspx" target="_blank"&gt;Part 3: Fluent import constructor&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/12/mef-2-0-mini-series-part-4-fluent-import.aspx" target="_blank"&gt;Part 4: Fluent import&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/15/mef-2-0-mini-series-part-5-fluent-export-properties.aspx" target="_blank"&gt;Part 5: Fluent export properties.&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/16/mef-2-0-mini-series-part-6-composition-scoping-and-lifetime-management.aspx" target="_blank"&gt;Part 6: Composition scoping and lifetime management&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/24/mef-2-0-mini-series-part-7-catalog-filter-and-deep-hierarchic-scoping.aspx" target="_blank"&gt;Part 7: Catalog filter and Deep hierarchic scoping.&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/02/16/mef-2-0-mini-series-part-8-composition-options-and-exception-handling.aspx"&gt;Part 8: Composition options and exception handling&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;you can read more about MEF 1 series:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2009/11/28/mef-for-beginner-concept-part-1.aspx"&gt;Concept&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2009/12/05/mef-for-beginner-part-2-vs-2010.aspx"&gt;How To build your first MEF application&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2009/12/19/mef-for-beginner-part-3-hello-silverlight.aspx"&gt;Hello Silverlight&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2009/12/26/mef-for-beginner-part-4-a-sync-silverlight-loading.aspx"&gt;A-sync Silverlight loading&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/02/mef-for-beginner-part-5-import.aspx"&gt;Import&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-part-creation-policy-part-6.aspx"&gt;Part Creation policy&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/15/mef-for-beginner-recomposition-policy-part-7.aspx"&gt;Recomposition policy&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/20/mef-for-beginner-metadata-part-8.aspx"&gt;Metadata (part 1)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/29/mef-for-beginner-repeatable-metadata-part-9.aspx"&gt;Metadata (part 2 – repeatable)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/02/13/mef-for-beginner-catalogs-part-10.aspx"&gt;Catalogs&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/03/20/mef-for-beginner-import-from-xaml-part-11.aspx"&gt;Import from Xaml&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/04/01/mef-for-beginner-deployment-catalog-part-12.aspx"&gt;Deployment Catalog&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/MEF-20-TOC-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F06%2Fmef-2-0-toc.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1634514" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Generics/default.aspx">Generics</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/RegistrationBuilder/default.aspx">RegistrationBuilder</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Catalog/default.aspx">Catalog</category></item><item><title>MEF - Best Practice</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/07/27/mef-best-practice.aspx</link><pubDate>Thu, 28 Jul 2011 02:32:22 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:876925</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF - Best Practice&lt;/h2&gt;  &lt;p&gt;In this post I will offer &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt;&amp;#160;&lt;strong&gt;practice&lt;/strong&gt; that may&lt;strong&gt; ease your MEF life&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;one of the &lt;strong&gt;MEF&lt;/strong&gt; &lt;strong&gt;headache&lt;/strong&gt; is to understand what&amp;#39;s goes wrong, &lt;/p&gt;  &lt;p&gt;when &lt;strong&gt;MEF &lt;/strong&gt;&lt;strong&gt;reject the composition&lt;/strong&gt;, you can read more about &lt;/p&gt;  &lt;p&gt;MEF diagnostic in &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/03/18/sdp-mef-real-life-patterns.aspx" target="_blank"&gt;this&lt;/a&gt; post.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/iStock_000005438250VerySmall_17C5917B.jpg"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="MEF, Import, Export, Import Many, Best practice" border="0" alt="MEF, Import, Export, Import Many, Best practice" src="http://blogs.microsoft.co.il/blogs/bnaya/iStock_000005438250VerySmall_thumb_563AB954.jpg" width="240" height="121" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Most of the obscure loading failure happens when your &lt;strong&gt;Export dependencies&lt;/strong&gt; &lt;strong&gt;doesn&amp;#39;t &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;satisfied&lt;/strong&gt;. in those case you solely relay on the MEF error message which is not enough &lt;/p&gt;  &lt;p&gt;in cases of deep dependencies graph.&lt;/p&gt;  &lt;p&gt;It will normally occurs when you are using the &lt;strong&gt;Import&lt;/strong&gt; attribute &lt;font color="#808080"&gt;(not ImportMany)&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;let take the following test case:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:78b28f31-127f-4d43-a90a-acef23661a6c" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Write();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;))]&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{ &lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    [&lt;span style="color:#2b91af;"&gt;Import&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt; Foo { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;class Bar importing IFoo, which mean that the composition will fail whenever&lt;/p&gt;  &lt;p&gt;MEF find no valid IFoo export or find more than single IFoo export.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;we can use 2 patterns:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;[ImportMany]&lt;/strong&gt; which will handle both the case of no export and too many export. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;[Import(AllowDefault=true)] &lt;/strong&gt;which cover the case of no export.       &lt;br /&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;using &lt;strong&gt;ImportMany&lt;/strong&gt; and the &lt;strong&gt;IPartImportsSatisfiedNotification&lt;/strong&gt; pattern.&lt;/p&gt;  &lt;p&gt;we can &lt;strong&gt;validate the composition&lt;/strong&gt; right after the composition stage.     &lt;br /&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b3b0a025-255b-4825-8e61-88075dad9419" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IPartImportsSatisfiedNotification&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt; Foo { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    [&lt;span style="color:#2b91af;"&gt;ImportMany&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt;[] FoosInternal { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; OnImportsSatisfied()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (FoosInternal == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt; || FoosInternal.Length == 0)&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MEF composition find no IFoo&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (FoosInternal.Length &amp;gt; 1)&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MEF composition find to many IFoo&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;at the OnImportsSatisfied method you can check whether &lt;/p&gt;  &lt;p&gt;you got the composition right &lt;font color="#808080"&gt;(instead of letting the MEF infrastructure &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;doing this job)&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;in most cases it is enough to ignore the to many IFoo composition case&lt;/p&gt;  &lt;p&gt;and to apply the &lt;strong&gt;[Import(AllowDefault=true)] &lt;/strong&gt;pattern.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:de4c27f3-7f68-4618-99bb-4260c31496e9" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IPartImportsSatisfiedNotification&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    [&lt;span style="color:#2b91af;"&gt;Import&lt;/span&gt;(AllowDefault=&lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IFoo&lt;/span&gt; Foo { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; OnImportsSatisfied()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (Foo == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MEF composition find no IFoo&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;after the composition we can validate whether IFoo composition goes right.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;MEF failure at the composition phase can be frustrating.&lt;/p&gt;  &lt;p&gt;in this post I have tried to offer pattern to ease that pain.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://dotnetshoutout.com/MEF-Best-Practice-Export-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2011%2F07%2F27%2Fmef-best-practice.aspx" /&gt;&lt;/a&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2011%2f07%2f27%2fmef-best-practice.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2011%2f07%2f27%2fmef-best-practice.aspx&amp;amp;title=MEF+-+Best+Practice"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=876925" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Debug/default.aspx">Debug</category></item><item><title>SDP - MEF (Real-Life patterns)</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/03/18/sdp-mef-real-life-patterns.aspx</link><pubDate>Fri, 18 Mar 2011 13:36:51 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:808581</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;SDP - MEF (Real-Life patterns)&lt;/h2&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_6FD6A3D0.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="MEF, Compose, Import, ImportMany, Import, .net" border="0" alt="MEF, Compose, Import, ImportMany, Import, .net" align="left" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_5C85C764.png" width="63" height="84" /&gt;&lt;/a&gt;On the last day of the SDP at Crown Plaza Hotel, Tel Aviv.&lt;/p&gt;  &lt;p&gt;I was lecturing about &lt;strong&gt;Real-Life&lt;/strong&gt; scenario of &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; failures&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the lecture was focusing on &lt;strong&gt;diagnostic&lt;/strong&gt; ,&lt;strong&gt;solutions&lt;/strong&gt; and &lt;strong&gt;testing&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;I want to thanks all attendant&amp;#39;s, It was the after lunch session and &lt;/p&gt;  &lt;p&gt;you were bravely kept your eyes open.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the presentation &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Sela%20public/SDP%202011" target="_blank"&gt;pdf&lt;/a&gt; can be found &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Sela%20public/SDP%202011" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;you can use the following check-list as &lt;strong&gt;recommended diagnostic process&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Diagnostic Check-List &lt;/h5&gt;  &lt;ul&gt;   &lt;li&gt;Is the instance included in the composition &lt;/li&gt;    &lt;li&gt;Are the plug-in assemblies included in the deployment path &lt;/li&gt;    &lt;li&gt;Are the plug-in dependencies included in the probing path &lt;/li&gt;    &lt;li&gt;Is the Silverlight XAP included in the application &lt;/li&gt;    &lt;li&gt;Use Strong Name and versioning policy &lt;/li&gt;    &lt;li&gt;Do not refer to imported properties during construction time, use IPartImportsSatisfiedNotification &lt;/li&gt;    &lt;li&gt;Check that IEnumerable&amp;lt;T&amp;gt;’s are decorated with [ImportMany] as long as they come from multiple exports. &lt;/li&gt;    &lt;li&gt;Remember that errors in nested dependencies are sometimes obscured &lt;/li&gt;    &lt;li&gt;Use AllowDefault=true when nulls are permitted &lt;/li&gt;    &lt;li&gt;Use ComposeExportedValue for including mocks of your dependencies &lt;/li&gt; &lt;/ul&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2011%2f03%2f18%2fsdp-mef-real-life-patterns.aspx&amp;amp;title=SDP+-+MEF+(Real-Life+patterns)"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=808581" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/VS2010/default.aspx">VS2010</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Debug/default.aspx">Debug</category></item><item><title>Dynamic MEF isn't thread safe by nature</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/12/04/dynamic-mef-isn-t-thread-safe-by-nature.aspx</link><pubDate>Sat, 04 Dec 2010 17:05:54 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:752599</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Dynamic MEF isn&amp;#39;t thread safe by nature&lt;/h2&gt;  &lt;p&gt;this post focus a pitfall which you should be aware in order to avoid sporadic crashes.&lt;/p&gt;  &lt;p&gt;In general most of &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt; operation are operate once at the application initialization stage,&lt;/p&gt;  &lt;p&gt;and it normally happens on a single thread.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;this may not be true when you need to dynamically load &lt;strong&gt;MEF&lt;/strong&gt; parts (using &lt;strong&gt;GetExportedValue&lt;/strong&gt; or &lt;strong&gt;SatisfyImportsOnce&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;unfortunately &lt;strong&gt;MEF&lt;/strong&gt; underline is using &lt;strong&gt;non concurrent dictionary&lt;/strong&gt; which is &lt;strong&gt;not thread safe&lt;/strong&gt; for the above operation implementation.&lt;/p&gt;  &lt;p&gt;therefore the following code will &lt;strong&gt;crash sporadically&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:456d762d-6216-4361-b71f-e55bace18916" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;] &lt;span style="color:#008000;"&gt;// exported type which will instantiate using MEF&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ExportedType&lt;/span&gt; {}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Program&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Main(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;[] args)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; catalog = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Program&lt;/span&gt;).Assembly);&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; container = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;(catalog);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 100; i++)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Thread&lt;/span&gt;(() =&amp;gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                {   &lt;span style="color:#008000;"&gt;// get the exported part dynamically (instantiate by MEF)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    container.GetExportedValue&amp;lt;&lt;span style="color:#2b91af;"&gt;ExportedType&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                }).Start();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;line 15: &lt;strong&gt;GetExportedValue&lt;/strong&gt; call is a &lt;strong&gt;non protected critical section&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;whenever using &lt;strong&gt;dynamic MEF&lt;/strong&gt; techniques you should put a &lt;strong&gt;critical section synchronization&lt;/strong&gt; over it.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the code sample can be found &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Code%20Samples/MEF/Thread%20safe?uc=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Credits&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/stiller/" target="_blank"&gt;Eran Stiller&lt;/a&gt;&lt;/strong&gt; was the first one who&amp;#39;s point my intention to this issue.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f12%2f04%2fdynamic-mef-isn-t-thread-safe-by-nature.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt; &lt;a href="http://dotnetshoutout.com/Dynamic-MEF-isnt-thread-safe-by-nature-Export-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2010%2F12%2F04%2Fdynamic-mef-isn-t-thread-safe-by-nature.aspx" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f12%2f04%2fdynamic-mef-isn-t-thread-safe-by-nature.aspx&amp;amp;title=Dynamic+MEF+isn&amp;#39;t+thread+safe+by+nature"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=752599" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/expert/default.aspx">expert</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category></item><item><title>Exporting non Exportable types</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/10/30/exporting-non-exportable-types.aspx</link><pubDate>Sat, 30 Oct 2010 19:23:56 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:734376</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Exporting non Exportable types&lt;/h2&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/1288445311_Startup_Wizard_7DC77FDF.jpg"&gt;&lt;img style="border-right-width:0px;margin:0px 10px 0px 0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="MEF, Compose, Import, Export, Poco" border="0" alt="MEF, Compose, Import, Export, Poco" align="left" src="http://blogs.microsoft.co.il/blogs/bnaya/1288445311_Startup_Wizard_thumb_6BBB3C52.jpg" width="65" height="65" /&gt;&lt;/a&gt; this post extend &lt;strong&gt;Glenn Block&lt;/strong&gt;&amp;#39;s post about &lt;/p&gt;  &lt;p&gt;&amp;quot;&lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/04/27/poco-mef-and-custom-type-systems-are-you-ready-to-take-the-red-pill.aspx" target="_blank"&gt;Poco, Mef, and custom type systems. Are you ready to take the red pill?&amp;quot;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;the post is adding &lt;font color="#000000"&gt;a &lt;strong&gt;compile time attribute export model&lt;/strong&gt;, &lt;strong&gt;Directory catalog&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000000"&gt;and migrate Glenn code to &lt;strong&gt;VS 2010&lt;/strong&gt;&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;It is very recommended to read &lt;strong&gt;Glenn Block&lt;/strong&gt;&amp;#39;s &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2009/04/27/poco-mef-and-custom-type-systems-are-you-ready-to-take-the-red-pill.aspx" target="_blank"&gt;post&lt;/a&gt; before reading &lt;/p&gt;  &lt;p&gt;this one.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;code sample&lt;/strong&gt; for this post can be download from &lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Code%20Samples/MEF/TypeExtensions" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summarizing Glenn&amp;#39;s post&lt;/h5&gt;  &lt;p&gt;in general Glenn show how to add attributes (like Export) to 3rd party type&lt;/p&gt;  &lt;p&gt;which doesn&amp;#39;t support it (&lt;em&gt;&lt;font color="#808080"&gt;it is cool technique which doesn&amp;#39;t related directly to &lt;/font&gt;&lt;/em&gt;&lt;a href="http://mef.codeplex.com/" target="_blank"&gt;&lt;em&gt;&lt;font color="#808080"&gt;MEF&lt;/font&gt;&lt;/em&gt;&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;Glenn show how to write catalog which can be aware of types extension.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;What was added?&lt;/h5&gt;  &lt;p&gt;I took Glenn&amp;#39;s source, fix it to run against the CLR 4.0,&lt;/p&gt;  &lt;p&gt;then I added a &lt;strong&gt;directory catalog&lt;/strong&gt; which aware of the &lt;/p&gt;  &lt;p&gt;type extension and I also add a nice clean way of exposing the type &lt;/p&gt;  &lt;p&gt;by using &lt;strong&gt;assembly level attribute&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;the directory catalog is very straight forward so I wont cover it in the post &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;(you can download the &lt;/font&gt;&lt;a href="http://cid-9bf7c1a515d76a9a.office.live.com/browse.aspx/Code%20Samples/MEF/TypeExtensions" target="_blank"&gt;&lt;font color="#808080"&gt;source&lt;/font&gt;&lt;/a&gt;&lt;font color="#808080"&gt; and see the implementation).&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the following code show how to use the assembly level attribute:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e8842f66-b6a0-430a-927c-44d416a8d343" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#0000ff;"&gt;assembly&lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;ComposablePartRegistry&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;MyNoneExportedType&lt;/span&gt;), &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IMyNoneExportedType&lt;/span&gt;))]&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;as you can see, all you have to do is to declare the &lt;strong&gt;exported type&lt;/strong&gt; followed by the &lt;strong&gt;exported contract&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the attribute snippet:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f7f9f1e4-2e8d-401d-a5d9-6730fedd2073" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;AttributeUsage&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;AttributeTargets&lt;/span&gt;.Assembly)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ComposablePartRegistryAttribute&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ComposablePartRegistryAttribute(&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt; implementation, &lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt; contract, &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;CreationPolicy&lt;/span&gt; policy = &lt;span style="color:#2b91af;"&gt;CreationPolicy&lt;/span&gt;.Any)&lt;/li&gt; &lt;li&gt;    { &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt; t = &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;ComposablePartType&lt;/span&gt;&amp;lt;,&amp;gt;);&lt;/li&gt; &lt;li&gt;        t = t.MakeGenericType(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[] {implementation, contract});&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        Type = &lt;span style="color:#2b91af;"&gt;Activator&lt;/span&gt;.CreateInstance(t, policy) &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TypeDelegator&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TypeDelegator&lt;/span&gt; Type { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;line 8-10: creating &lt;strong&gt;ComposablePartType&lt;/strong&gt; type which later be used by the &lt;strong&gt;catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;this was small extension to Glenn&amp;#39;s concept which can be very useful &lt;/p&gt;  &lt;p&gt;in cases where you want to embrace 3rs party component into your &lt;strong&gt;MEF&lt;/strong&gt; model.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f10%2f30%2fexporting-non-exportable-types.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt; &lt;a href="http://dotnetshoutout.com/Exporting-non-Exportable-types-Export-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2010%2F10%2F30%2Fexporting-non-exportable-types.aspx" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f10%2f30%2fexporting-non-exportable-types.aspx&amp;amp;title=Exporting+non+Exportable+types"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=734376" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Poco/default.aspx">Poco</category></item><item><title>Post recommendation</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/05/31/post-recommendation.aspx</link><pubDate>Tue, 01 Jun 2010 01:17:28 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:645687</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;if you want to learn more about &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;MEF&lt;/a&gt; backstage you can find &lt;/p&gt;  &lt;p&gt;more information under the following post:&lt;/p&gt;  &lt;h5&gt;MEF: What and why&lt;/h5&gt;  &lt;p&gt;&lt;a title="http://blogs.msdn.com/b/hammett/archive/2010/05/29/mef-what-and-why.aspx" href="http://blogs.msdn.com/b/hammett/archive/2010/05/29/mef-what-and-why.aspx"&gt;http://blogs.msdn.com/b/hammett/archive/2010/05/29/mef-what-and-why.aspx&lt;/a&gt;&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f05%2f31%2fpost-recommendation.aspx&amp;amp;title=Post+recommendation"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=645687" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category></item><item><title>C# IntelliSense extension for VS 2010 - UPDATE</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/04/05/c-intellisense-extension-for-vs-2010-update.aspx</link><pubDate>Tue, 06 Apr 2010 00:33:13 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:575266</guid><dc:creator>bnaya</dc:creator><slash:comments>2</slash:comments><description>&lt;h5&gt;C# IntelliSense extension for VS 2010 – UPDATE&lt;/h5&gt;  &lt;p&gt;the extension has update and it is include the description.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_5722478F.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="VS2010, extension, intellisense" border="0" alt="VS2010, extension, intellisense" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_5CECC55B.png" width="371" height="348" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;enjoy :)&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:0px 0px 0px 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=575266" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.net/default.aspx">.net</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/VS2010/default.aspx">VS2010</category></item><item><title>C# IntelliSense extension for VS 2010</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/04/02/c-intellisense-extension-for-vs-2010.aspx</link><pubDate>Fri, 02 Apr 2010 21:07:41 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:573134</guid><dc:creator>bnaya</dc:creator><slash:comments>2</slash:comments><description>&lt;h2&gt;C# IntelliSense extension for VS 2010&lt;/h2&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;C#&lt;/strong&gt; &lt;strong&gt;IntelliSense&lt;/strong&gt; &lt;strong&gt;extension&lt;/strong&gt; is now available at the &lt;strong&gt;&lt;a href="http://visualstudiogallery.msdn.microsoft.com/en-us/c3eaa4fc-f2de-43ad-92ee-f0f257b79005" target="_blank"&gt;Visual Studio Gallery&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the extension is adding filtering capability to the VS IntelliSense, &lt;/p&gt;  &lt;p&gt;so for example when you are looking for methods you can filter out the namespace, fields, events and properties.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/CSharp_IntelliSense_24FAF328.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="CSharp,IntelliSense,VS,2010,plug-in,extension" border="0" alt="CSharp,IntelliSense,VS,2010,plug-in,extension" src="http://blogs.microsoft.co.il/blogs/bnaya/CSharp_IntelliSense_thumb_2AC570F4.png" width="379" height="341" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Credits&lt;/h5&gt;  &lt;p&gt;this work is heavily based on the &lt;strong&gt;Xaml IntelliSense&lt;/strong&gt; extension that was written by&amp;#160; &lt;a href="http://karlshifflett.wordpress.com/" target="_blank"&gt;Karl Shifflett&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Download&lt;/h5&gt;  &lt;p&gt;you can either &lt;a href="http://visualstudiogallery.msdn.microsoft.com/en-us/c3eaa4fc-f2de-43ad-92ee-f0f257b79005" target="_blank"&gt;download&lt;/a&gt; the extension from Visual Studio 2010 (tool-&amp;gt;Add-in manage…&lt;/p&gt;  &lt;p&gt;and search for CSharp IntelliSense).&lt;/p&gt;  &lt;p&gt;or &lt;a href="http://visualstudiogallery.msdn.microsoft.com/en-us/c3eaa4fc-f2de-43ad-92ee-f0f257b79005" target="_blank"&gt;directly&lt;/a&gt; from Visual Studio Gallery site &lt;a href="http://visualstudiogallery.msdn.microsoft.com/en-us/c3eaa4fc-f2de-43ad-92ee-f0f257b79005" target="_blank"&gt;here&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Point of interest&lt;/h5&gt;  &lt;p&gt;you may also want to check the &lt;a href="http://blogs.msdn.com/noahric/archive/2010/01/28/new-extension-spell-checker.aspx" target="_blank"&gt;Spell Checker&lt;/a&gt; extension.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f04%2f02%2fc-intellisense-extension-for-vs-2010.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f04%2f02%2fc-intellisense-extension-for-vs-2010.aspx" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:0px 0px 0px 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=573134" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.net/default.aspx">.net</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/VS2010/default.aspx">VS2010</category></item><item><title>MEF for Beginner (Deployment Catalog) - part 12</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/04/01/mef-for-beginner-deployment-catalog-part-12.aspx</link><pubDate>Fri, 02 Apr 2010 01:20:55 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:572185</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF for Beginner (Deployment Catalog) - part 12&lt;/h2&gt;  &lt;p&gt;&lt;img style="border-right-width:0px;margin:0px 15px 0px 0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="deployment catalog,MEF,Import,Importmany,export,compose,composition" border="0" alt="deployment catalog,MEF,Import,Importmany,export,compose,composition" align="left" src="http://blogs.microsoft.co.il/blogs/bnaya/deploymentcatalog_2D5B1AB5.jpg" width="131" height="156" /&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;this is the 12th post of the &lt;a href="http://mef.codeplex.com/"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt; for Beginner series, the series &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-toc.aspx"&gt;TOC&lt;/a&gt; is available &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-toc.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;this post will focus on&lt;strong&gt; Deployment Catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the code sample for this post can be found &lt;a href="http://cid-9bf7c1a515d76a9a.skydrive.live.com/browse.aspx/Code%20Samples/MEF/MEF%20for%20Beginners/Part%2012?uc=1&amp;amp;isFromRichUpload=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;&amp;#160;&lt;/h5&gt;  &lt;h5&gt;What is MEF Deployment Catalogs?&lt;/h5&gt;  &lt;p&gt;the &lt;strong&gt;deployment catalog&lt;/strong&gt; is actually a redesign of the older&lt;strong&gt; package catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;it enable to &lt;strong&gt;load parts&lt;/strong&gt; from &lt;strong&gt;xap&lt;/strong&gt; packages a-synchronically.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Code sample&lt;/h5&gt;  &lt;p&gt;the following code sample depend on 2 assembly that should be added to the project:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;System.ComponentModel.Composition &lt;/li&gt;    &lt;li&gt;System.ComponentModel.Composition.Initialization &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;at the startup (&lt;strong&gt;app.xaml&lt;/strong&gt;)&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1445eb1a-5ba2-480c-82bd-bd8ae0324ac5" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Application_Startup(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; sender,&lt;span style="color:#2b91af;"&gt;StartupEventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;InitializeContainer();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.RootVisual = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MainPage&lt;/span&gt;();&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; InitializeContainer()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; catalogs = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;AggregateCatalog&lt;/span&gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; catalog = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TypeCatalog&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;PlugIns&lt;/span&gt;));&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;catalogs.Catalogs.Add(catalog);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#008000;"&gt;// Create deployment catalog&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; deployCatalog = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DeploymentCatalog&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;PlugIns.xap&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;deployCatalog.DownloadCompleted += OnDownloadCompletedHandler;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;deployCatalog.DownloadProgressChanged += OnDownloadProgressChangedHandler;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;deployCatalog.DownloadAsync();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;catalogs.Catalogs.Add(deployCatalog);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#008000;"&gt;// initialize the main application composition host (container)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#2b91af;"&gt;CompositionHost&lt;/span&gt;.Initialize(catalogs);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;line 3&lt;/strong&gt;, &lt;strong&gt;initialize&lt;/strong&gt; &lt;strong&gt;MEF&lt;/strong&gt; before the creation of any windows that depend on MEF.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 9&lt;/strong&gt;, define &lt;strong&gt;aggregate catalog&lt;/strong&gt; (this is basically a catalog collection). &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 11-12&lt;/strong&gt;, create &lt;strong&gt;type catalog&lt;/strong&gt; for plug-ins within the &lt;strong&gt;main&lt;/strong&gt; &lt;strong&gt;xap&lt;/strong&gt; (plug-in within the main xap&lt;/p&gt;  &lt;p&gt;can be assess directly) and add the catalog to the &lt;strong&gt;aggregate catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 15&lt;/strong&gt;, create &lt;strong&gt;deployment catalog&lt;/strong&gt; (it get the xap relative path as parameter).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 16-17&lt;/strong&gt;, register for &lt;strong&gt;xap&lt;/strong&gt; &lt;strong&gt;download process&lt;/strong&gt; events (&lt;strong&gt;optional&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 18&lt;/strong&gt;, start &lt;strong&gt;downloading&lt;/strong&gt; the xap.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 20&lt;/strong&gt;, add the &lt;strong&gt;deployment catalog&lt;/strong&gt; to the &lt;strong&gt;aggregate catalog&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 23&lt;/strong&gt;, &lt;strong&gt;initialize&lt;/strong&gt; the &lt;strong&gt;composition host&lt;/strong&gt; with the &lt;strong&gt;aggregate catalog&lt;/strong&gt; (so &lt;/p&gt;  &lt;p&gt;latter using &lt;strong&gt;CompositionInitializer.SatisfyImports&lt;/strong&gt; will operate against the &lt;strong&gt;aggregate catalog&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;at the &lt;strong&gt;MainPage.xaml&lt;/strong&gt; the code will look as follow:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ac24aa17-da47-4416-b1c8-eca20f21e845" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MainPage&lt;/span&gt;:&lt;span style="color:#2b91af;"&gt;UserControl&lt;/span&gt;,&lt;span style="color:#2b91af;"&gt;INotifyPropertyChanged&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; MainPage()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;InitializeComponent();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.DataContext = &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#2b91af;"&gt;CompositionInitializer&lt;/span&gt;.SatisfyImports(&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt; _plugIns;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;ImportMany&lt;/span&gt;(AllowRecomposition = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt; PlugIns&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _plugIns;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;_plugIns = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(PropertyChanged != &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;PropertyChanged(&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;,&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;PropertyChangedEventArgs&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;PlugIns&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;event&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;PropertyChangedEventHandler&lt;/span&gt; PropertyChanged;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 7&lt;/strong&gt;, asking &lt;strong&gt;MEF&lt;/strong&gt; to &lt;strong&gt;satisfy&lt;/strong&gt; any &lt;strong&gt;import&lt;/strong&gt; of the current instance (in our case the PlugIns property).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 11&lt;/strong&gt;, decorate the &lt;strong&gt;PlugIns&lt;/strong&gt; property for being assigned by the &lt;strong&gt;MEF&lt;/strong&gt; composition.&lt;/p&gt;  &lt;p&gt;notice that the decoration define &lt;strong&gt;AllowRecomposition = true, &lt;/strong&gt;this is very important&lt;/p&gt;  &lt;p&gt;because the &lt;strong&gt;deployment catalog&lt;/strong&gt; is &lt;strong&gt;a-synchronic,&lt;/strong&gt; therefore we do not control the exact time of the assignment.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;deployment catalog&lt;/strong&gt; is the current way for &lt;strong&gt;delay composition&lt;/strong&gt; from &lt;strong&gt;xap&lt;/strong&gt; packages.&lt;/p&gt;  &lt;p&gt;it is a-synchronic and it will notify the catalog upon the download completion.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;the code sample for this post can be found &lt;a href="http://cid-9bf7c1a515d76a9a.skydrive.live.com/browse.aspx/Code%20Samples/MEF/MEF%20for%20Beginners/Part%2012?uc=1&amp;amp;isFromRichUpload=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Learn more&lt;/h5&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="http://codebetter.com/blogs/glenn.block/archive/2010/03/07/building-hello-mef-part-iv-deploymentcatalog.aspx" href="http://codebetter.com/blogs/glenn.block/archive/2010/03/07/building-hello-mef-part-iv-deploymentcatalog.aspx"&gt;http://codebetter.com/blogs/glenn.block/archive/2010/03/07/building-hello-mef-part-iv-deploymentcatalog.aspx&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a title="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/02/24/mef-silverlight-and-the-deploymentcatalog.aspx" href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/02/24/mef-silverlight-and-the-deploymentcatalog.aspx"&gt;http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/02/24/mef-silverlight-and-the-deploymentcatalog.aspx&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f04%2f01%2fmef-for-beginner-deployment-catalog-part-12.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f04%2f01%2fmef-for-beginner-deployment-catalog-part-12.aspx" /&gt;&lt;/a&gt; &lt;a href="http://dotnetshoutout.com/MEF-for-Beginner-Deployment-Catalog-part-12-Export-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2010%2F04%2F01%2Fmef-for-beginner-deployment-catalog-part-12.aspx" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:0px 0px 0px 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=572185" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Beginner/default.aspx">Beginner</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CLR+4/default.aspx">CLR 4</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.net/default.aspx">.net</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/IoC/default.aspx">IoC</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category></item><item><title>recommending VS 2010 XAML IntelliSense Extension</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/03/26/recommending-vs-2010-xaml-intellisense-extension.aspx</link><pubDate>Fri, 26 Mar 2010 18:12:38 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:563202</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h5&gt;recommending VS 2010 XAML IntelliSense Extension&lt;/h5&gt;  &lt;p&gt;a pal named Karl had published a very useful &lt;strong&gt;plug-in&lt;/strong&gt; to &lt;strong&gt;VS 2010&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;this &lt;strong&gt;plug-in&lt;/strong&gt; is making &lt;strong&gt;Xaml&lt;/strong&gt; writing so much better experience with enhanced &lt;strong&gt;IntelliSense&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;if you doing some &lt;strong&gt;Xaml&lt;/strong&gt; work using &lt;strong&gt;VS 2010&lt;/strong&gt; you should consider to download this plug-in.&lt;/p&gt;  &lt;p&gt;the download is available from the following link:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://karlshifflett.wordpress.com/2010/03/21/visual-studio-2010-xaml-editor-intellisense-presenter-extension/#comment-2228" href="http://karlshifflett.wordpress.com/2010/03/21/visual-studio-2010-xaml-editor-intellisense-presenter-extension/#comment-2228"&gt;http://karlshifflett.wordpress.com/2010/03/21/visual-studio-2010-xaml-editor-intellisense-presenter-extension/#comment-2228&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h6&gt;Features&lt;/h6&gt;  &lt;ul&gt;   &lt;li&gt;Pascal case lookup &lt;/li&gt;    &lt;li&gt;Optional narrowing list filter &lt;/li&gt;    &lt;li&gt;Filtering based on item type &lt;/li&gt;    &lt;li&gt;xmlns IntelliSense options      &lt;ul&gt;       &lt;li&gt;Show/hide only solution assemblies &lt;/li&gt;        &lt;li&gt;Show/hide schemas &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Filter settings persist for the current Visual Studio session &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;img style="display:inline;margin-left:0px;margin-right:0px;" title="FullControls" border="0" alt="FullControls" align="left" src="http://karlshifflett.files.wordpress.com/2010/03/fullcontrols.png?w=308&amp;amp;h=304" width="308" height="304" /&gt;&lt;img style="display:inline;margin-left:0px;margin-right:0px;" title="FullProperties" border="0" alt="FullProperties" align="left" src="http://karlshifflett.files.wordpress.com/2010/03/fullproperties.png?w=305&amp;amp;h=305" width="305" height="305" /&gt;&lt;img title="FullNamespace" border="0" alt="FullNamespace" src="http://karlshifflett.files.wordpress.com/2010/03/fullnamespace.png?w=509&amp;amp;h=282" width="509" height="282" /&gt;     &lt;br /&gt;&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:0px 0px 0px 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=563202" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/WPF/default.aspx">WPF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.net/default.aspx">.net</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/IntelliSense/default.aspx">IntelliSense</category></item><item><title>MEF for Beginner (Import from Xaml) - part 11</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/03/20/mef-for-beginner-import-from-xaml-part-11.aspx</link><pubDate>Sat, 20 Mar 2010 21:28:23 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:552959</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;MEF for Beginner (Import from Xaml) - part 11&lt;/h2&gt;  &lt;p&gt;this is the 11th post of the &lt;a href="http://mef.codeplex.com/"&gt;&lt;strong&gt;MEF&lt;/strong&gt;&lt;/a&gt; for Beginner series, the series &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-toc.aspx"&gt;TOC&lt;/a&gt; is available &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-toc.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;this post will focus on &lt;strong&gt;Importing mef parts &lt;/strong&gt;directly from the &lt;strong&gt;Xaml&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/iStock_000005438250VerySmallXmXorl_0339C5C9.jpg"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="Import, ImportMany, MEF, composition, CLR 4" border="0" alt="Import, ImportMany, MEF, composition, CLR 4" src="http://blogs.microsoft.co.il/blogs/bnaya/iStock_000005438250VerySmallXmXorl_thumb_2E6E3703.jpg" width="303" height="77" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;the code sample for this post is available &lt;a href="http://cid-9bf7c1a515d76a9a.skydrive.live.com/browse.aspx/Code%20Samples/MEF/MEF%20for%20Beginners/Part%2011?uc=1&amp;amp;isFromRichUpload=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;assuming that we have the following &lt;strong&gt;exports&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3c19987e-7cb2-4576-8452-1bb91342eeae" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DemoStrings&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MyTag&amp;quot;&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Text1 { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;Hello world&amp;quot;&lt;/span&gt;; } }&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MyTag&amp;quot;&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Text2 { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;export using custom attribute&amp;quot;&lt;/span&gt;; } }&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;NotMyTag&amp;quot;&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Text3 { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;not included (Not My Tag contract)&amp;quot;&lt;/span&gt;; } }&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MyTag&amp;quot;&lt;/span&gt;)]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Text4 { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;Wpf markup extension&amp;quot;&lt;/span&gt;; } }&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af;"&gt;Export&lt;/span&gt;]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Text5 { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#a31515;"&gt;&amp;quot;not included (have no contract)&amp;quot;&lt;/span&gt;; } }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;we may want to have it &lt;strong&gt;import&lt;/strong&gt; into the &lt;strong&gt;Xaml&lt;/strong&gt; as follow:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8f4b3bb2-cced-4c46-9881-b1d7f42810a4" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Window&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; x&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;Class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Bnaya.Samples.MainWindow&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;x&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;mef&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;clr-namespace:Bnaya.Samples&amp;quot;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; Title&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;MainWindow&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; Height&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;350&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; Width&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;525&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#a31515;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Grid&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#a31515;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ListBox&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; DataContext&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color:#a31515;"&gt;mef&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ImportManyStrings&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; MyTag}&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;quot;&lt;/span&gt; &lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#ff0000;"&gt; ItemsSource&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Binding}&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;quot;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#a31515;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Grid&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Window&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;as you can see at &lt;strong&gt;line 8&lt;/strong&gt; the &lt;strong&gt;data context&lt;/strong&gt; is using special definition for &lt;strong&gt;importing&lt;/strong&gt; all &lt;strong&gt;exported&lt;/strong&gt; &lt;strong&gt;strings&lt;/strong&gt; that     &lt;br /&gt;define the their &lt;strong&gt;contract&lt;/strong&gt; as &lt;strong&gt;MyTag&lt;/strong&gt; (&lt;font color="#808080"&gt;looking at the &lt;strong&gt;exports&lt;/strong&gt; it will &lt;strong&gt;import&lt;/strong&gt; the exports at&lt;strong&gt; lines 4,6,10&lt;/strong&gt;&lt;/font&gt;).&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;what should we have in order to achieve that functionality?&lt;/h5&gt;  &lt;p&gt;actually very little, all we have to do is to define a&lt;strong&gt; markup extension&lt;/strong&gt; that return the &lt;strong&gt;exported&lt;/strong&gt; &lt;strong&gt;parts&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the following code is how the &lt;strong&gt;markup extension&lt;/strong&gt; should be define:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ce4d674f-f189-46ff-82bc-0b4ea7447c79" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;[&lt;span style="color:#2b91af;"&gt;MarkupExtensionReturnType&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;))]&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ImportManyStringsExtension&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;MarkupExtension&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt; _dataSource;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ImportManyStringsExtension(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; contract)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;_dataSource = &lt;span style="color:#2b91af;"&gt;CompositionHost&lt;/span&gt;.Provider.GetExportedValues&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;(contract);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; ProvideValue(&lt;span style="color:#2b91af;"&gt;IServiceProvider&lt;/span&gt; serviceProvider)&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _dataSource;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&amp;#160;&amp;#160;&amp;#160;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&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;strong&gt;line 1&lt;/strong&gt;, decorate that our &lt;strong&gt;markup extension&lt;/strong&gt; for returning &lt;strong&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 2&lt;/strong&gt;, derive from &lt;strong&gt;MarkupExtension&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 7&lt;/strong&gt;, at the construction time, dynamically ask &lt;strong&gt;mef&lt;/strong&gt; for any &lt;strong&gt;discoverable parts&lt;/strong&gt; that follow the &lt;strong&gt;contract&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;the CompositionHost is a very small helper class that was taken from the &lt;strong&gt;MEF Silverlight&lt;/strong&gt; implementation &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#808080"&gt;with some very minor enhancements&lt;/font&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 10-13&lt;/strong&gt;, returning the &lt;strong&gt;discoverable parts&lt;/strong&gt; to the &lt;strong&gt;Xaml&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;One last thing&lt;/h5&gt;  &lt;p&gt;we should remove the &lt;strong&gt;StartupUri&lt;/strong&gt; from the &lt;strong&gt;App.Xaml&lt;/strong&gt; and using the &lt;strong&gt;OnStartup override&lt;/strong&gt; instead:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a18e7edc-77f1-43b8-8db2-338a4a86aea3" class="wlWriterSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; OnStartup(&lt;span style="color:#2b91af;"&gt;StartupEventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.OnStartup(e);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (!&lt;span style="color:#2b91af;"&gt;Utils&lt;/span&gt;.IsInDesignTool)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; asmCatalog = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;AssemblyCatalog&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;App&lt;/span&gt;).Assembly);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; catalogs = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;AggregateCatalog&lt;/span&gt;(asmCatalog);&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; container = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CompositionContainer&lt;/span&gt;(catalogs);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#2b91af;"&gt;CompositionHost&lt;/span&gt;.Initialize(container);&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;container.Compose(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CompositionBatch&lt;/span&gt;());&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; root = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MainWindow&lt;/span&gt;(); &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;root.ShowDialog();&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;line 5&lt;/strong&gt;, preventing &lt;strong&gt;MEF&lt;/strong&gt; operation during&lt;strong&gt; design time&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 7-9&lt;/strong&gt;, setting the&lt;strong&gt; MEF container&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 10&lt;/strong&gt;, setting the &lt;strong&gt;MEF container&lt;/strong&gt; as the &lt;strong&gt;main container&lt;/strong&gt; of the application &lt;/p&gt;  &lt;p&gt;(this way it can be &lt;strong&gt;reachable anywhere&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;line 11&lt;/strong&gt;, do the &lt;strong&gt;composition&lt;/strong&gt;, &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;IMPOERANT&lt;/font&gt;&lt;/strong&gt;: the &lt;strong&gt;composition&lt;/strong&gt; should happens &lt;strong&gt;before&lt;/strong&gt; the &lt;strong&gt;window instantiation&lt;/strong&gt;, &lt;/p&gt;  &lt;p&gt;because that when the &lt;strong&gt;markup extension&lt;/strong&gt; will be invoke.&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;exported part&lt;/strong&gt; should be available at this time. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;lines 13-14&lt;/strong&gt;, starting the application.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;we can extend the &lt;strong&gt;Xaml&lt;/strong&gt; very easily in order to get better &lt;strong&gt;Xaml&lt;/strong&gt;-&amp;gt;&lt;strong&gt;Mef&lt;/strong&gt; experience.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;the code sample for this post is available &lt;a href="http://cid-9bf7c1a515d76a9a.skydrive.live.com/browse.aspx/Code%20Samples/MEF/MEF%20for%20Beginners/Part%2011?uc=1&amp;amp;isFromRichUpload=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:1bd718e5-fd3d-4d99-80e3-d80d7f7d4f74" class="wlWriterSmartContent"&gt;תגים של Technorati:‏ &lt;a href="http://technorati.com/tags/Import" rel="tag"&gt;Import&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ImportMany" rel="tag"&gt;ImportMany&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MEF" rel="tag"&gt;MEF&lt;/a&gt;,&lt;a href="http://technorati.com/tags/composition" rel="tag"&gt;composition&lt;/a&gt;,&lt;a href="http://technorati.com/tags/CLR+4" rel="tag"&gt;CLR 4&lt;/a&gt;&lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f03%2f20%2fmef-for-beginner-import-from-xaml-part-11.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2010%2f03%2f20%2fmef-for-beginner-import-from-xaml-part-11.aspx" /&gt;&lt;/a&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px;padding:0px 0px 0px 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=552959" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MEF/default.aspx">MEF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/WPF/default.aspx">WPF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Beginner/default.aspx">Beginner</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.net/default.aspx">.net</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category></item></channel></rss>