<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.noop.se/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:coop="http://www.google.com/coop/namespace" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
    <channel>
        <title>Techie.notepad</title>
        <link>http://blog.noop.se/Default.aspx</link>
        <description>The notepad of a .NET Techie!</description>
        <language>en-US</language>
        <copyright>Magnus Mårtensson</copyright>
        <managingEditor>noopman@hotmail.com</managingEditor>
        <generator>Subtext Version 2.0.0.43</generator>
        <image>
            <title>Techie.notepad</title>
            <url>http://blog.noop.se/images/RSS2Image.gif</url>
            <link>http://blog.noop.se/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.noop.se/Techienotepad" /><feedburner:info uri="techienotepad" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><geo:lat>55.602857</geo:lat><geo:long>12.998852</geo:long><creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.noop.se%2FTechienotepad" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.noop.se%2FTechienotepad" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.noop.se%2FTechienotepad" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare href="http://www.bloglines.com/sub/http://feeds.noop.se/Techienotepad" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.noop.se%2FTechienotepad" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.noop.se%2FTechienotepad" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.noop.se%2FTechienotepad" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:browserFriendly>I promise you to keep it really technical and in the space of what it means to be a professional .NET developer today! Have fun reading!</feedburner:browserFriendly><item>
            <title>Orleans: The new Cloud Development Platform for Windows Azure?</title>
            <category>Architecture</category>
            <category>Microsoft</category>
            <category>Azure</category>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/MmgmO1rxaMs/orleans-the-new-cloud-development-platform-for-windows-azure.aspx</link>
            <description>&lt;p&gt;There is some rumors that the Orleans software platform will actually be a product coming out from Microsoft that will empower the &lt;a href="http://www.microsoft.com/windowsazure/"&gt;Windows Azure&lt;/a&gt; platform and make it more easy to architecture and manage. This is just a research project but I for one am liking the hopeful outlook on life. ;~)&lt;/p&gt;  &lt;p&gt;Somewhere among the few references out there on the web I am grasping for an explanation on what this will be. How about this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;Orleans is to Azure as the CLR is to Windows&lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;It appears Orleans is designed to create a unified programming model for the client and the server. It offers greater abstraction so that applications can be easily ported across different platforms.&lt;/p&gt;  &lt;p&gt;In Rob Knies article “&lt;a href="http://research.microsoft.com/en-us/news/features/ccf-022409.aspx"&gt;Peering into Future of Cloud Computing&lt;/a&gt;” from way back when on February 24, 2009 we can read the following:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;b&gt;Orleans software platform:&lt;/b&gt; The software that runs in the data center is a complicated, distributed system. It must handle a vast number of requests from across the globe, and the computers on which the software runs fail regularly—but the service itself should not fail, even though the software is continually changing as the service evolves and new features are added. Orleans is a new software platform that runs on Microsoft’s &lt;a href="http://www.microsoft.com/azure/windowsazure.mspx"&gt;Windows® Azure&lt;/a&gt;™ system and provides the abstractions, programming languages, and tools that make it easier to build cloud services.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;James Larus has posted some Power Point slides from &lt;a href="http://research.microsoft.com/en-us/people/sriram/larus.pptx"&gt;the Mysore-Park Workshop on Cloud Computing&lt;/a&gt;, Mysore India on January 13, 2010. There we can see some nice slides about this concept:&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/29e2a1a44969_68D1/Orleans1.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Orleans1" border="0" alt="Orleans1" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/29e2a1a44969_68D1/Orleans1_thumb.png" width="244" height="184" /&gt;&lt;/a&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/29e2a1a44969_68D1/Orleans2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Orleans2" border="0" alt="Orleans2" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/29e2a1a44969_68D1/Orleans2_thumb.png" width="244" height="185" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/29e2a1a44969_68D1/Orleans3.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Orleans3" border="0" alt="Orleans3" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/29e2a1a44969_68D1/Orleans3_thumb.png" width="244" height="184" /&gt;&lt;/a&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/29e2a1a44969_68D1/Orleans4.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Orleans4" border="0" alt="Orleans4" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/29e2a1a44969_68D1/Orleans4_thumb.png" width="244" height="184" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I must say this looks really interesting! “Grains” of apps that can migrate between data centers. This is very nice indeed for software architects. Me likey a LOT!&lt;/p&gt;  &lt;p&gt;In an &lt;a href="http://www.eweek.com/c/a/Application-Development/Microsoft-Pumps-Windows-Azure-as-Top-Cloud-Choice-for-Developers-280192/"&gt;interview with eWEEK at Microsoft’s Worldwide Partner Conference in July&lt;/a&gt;, Amitabh Srivastava, senior vice president of Microsoft’s Server and Cloud Division, said Microsoft &lt;a href="http://www.eweekeurope.co.uk/news/news-it-infrastructure/microsoft-floats-new-server-and-cloud-division-2700"&gt;built its Windows Azure cloud platform&lt;/a&gt; with developers in mind from the start.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;When we were developing Azure from day one it was done for developers,” Srivastava said. “You have to allow developers to bring their skills, their current set of skills, to the cloud. So we said developers should get to choose the language they want to use. You can use any environment you want. You can use Visual Studio or you can do the entire development in Eclipse. You can’t pigeonhole developers into one or two languages or one or two frameworks. Just because our lineage is Windows Server doesn’t mean we will restrict you to using C# or a Microsoft language.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Unfortunately not all good ideas are more than &lt;a href="http://en.wikipedia.org/wiki/Vaporware"&gt;vaporware&lt;/a&gt; but I am very curious to see about this project. Let’s keep our fingers and toes crossed for Orleans and stay tuned for more on this as it comes out because I certainly intend to dip my fingers in that honey jar once it is available.&lt;/p&gt;  &lt;p&gt;Cheers,&lt;/p&gt;  &lt;p&gt;M.&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:d5462db1-a379-489b-a2f8-467c3e0f8eb5" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Windows+Azure" rel="tag"&gt;Windows Azure&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Orleans" rel="tag"&gt;Orleans&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/316.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/r2SLxcxG3iWcUyNaahPZJM5WO2U/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/r2SLxcxG3iWcUyNaahPZJM5WO2U/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/r2SLxcxG3iWcUyNaahPZJM5WO2U/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/r2SLxcxG3iWcUyNaahPZJM5WO2U/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Techienotepad/~4/MmgmO1rxaMs" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/09/06/orleans-the-new-cloud-development-platform-for-windows-azure.aspx</guid>
            <pubDate>Mon, 06 Sep 2010 11:00:00 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/316.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/09/06/orleans-the-new-cloud-development-platform-for-windows-azure.aspx#feedback</comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/316.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/316.aspx</trackback:ping>
        <coop:keyword>Architecture</coop:keyword><coop:keyword>Microsoft</coop:keyword><coop:keyword>Azure</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/09/06/orleans-the-new-cloud-development-platform-for-windows-azure.aspx</feedburner:origLink></item>
        <item>
            <title>It's time for IoC Container Configuration Detente</title>
            <category>.NET</category>
            <category>Architecture</category>
            <category>C#</category>
            <category>Patterns &amp; Practices</category>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/8k930L317Vs/its-time-for-ioc-container-configuration-detente.aspx</link>
            <description>&lt;p&gt;Want an easy way to configure your &lt;a href="http://en.wikipedia.org/wiki/Inversion_of_control"&gt;Inversion of Control&lt;/a&gt; (IoC) container using an API? Don’t care one iota about which specific container you actually are using you just want to get the work done? Want to configure your IoC in a type safe manner? Read on and find out how!&lt;/p&gt;  &lt;p&gt;Scroll passed the background and rationale musings for the good stuff. ;~)&lt;/p&gt;  &lt;h3&gt;Background and rationale musings&lt;font style="font-weight: bold" /&gt;&lt;/h3&gt;  &lt;p&gt;Looking at a great initiative; the &lt;a href="http://commonservicelocator.codeplex.com/"&gt;Common Service Locator&lt;/a&gt; and using it because it’s a good design practice to use abstractions one can’t help but to hunger for more! There was one more piece I felt myself wanting to sink my teeth into. After some testing, re-working and re-factoring and re-re-factoring ;~) an embryo for a common simple API begun to emerge. We have found in our team that there is a smallish set of classic configuration tasks involved in setting up your IoC:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Register Instance&lt;/strong&gt; (with or without a key) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Register Type&lt;/strong&gt; (with or without a key) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Marking a registration a Singleton&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Selecting a constructor for injection&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Selecting properties for injection&lt;/strong&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This is it really!&lt;/p&gt;  &lt;p&gt;Really? Yes pretty much. This is likely to fill about 95% of the needs for configuring an IoC.&lt;/p&gt;  &lt;p&gt;We’ve tested it in our project and it works really well. We started out using the API of the IoC we chose but felt it imposed a lot of presence in our code making our coders have to know and care a lot about how and what makes the underlying container tick. All they wanted to do was configure how their code would fit together.&lt;/p&gt;  &lt;p&gt;That seems quite easy to create and API for. Indeed. ;~)&lt;/p&gt;  &lt;p&gt;But wait; one single API can’t possibly fulfill all of the IoC configuration needs out there for ever! True that - it can’t pretend to be a &lt;a href="http://www.bing.com/Dictionary/search?q=define+silver+bullet&amp;amp;FORM=DTPDIA&amp;amp;qpvt=Silver+Bullet+definition"&gt;silver bullet&lt;/a&gt;. Well let’s make it easy to extend then.&lt;/p&gt;  &lt;p&gt;Hmmm... If not all IoC can implement this API; wouldn’t that make the API a little pointless? Well, not really. The API would not be &lt;em&gt;intended &lt;/em&gt;to allow you to replace one container with another without changing or even recompiling the code. Though it &lt;em&gt;could &lt;/em&gt;be used that way. Just because you can replace one IoC with another doesn’t mean you will! Having a fitting simplification for an API – and adapter if you will – still makes a lot of sense to your code maintainability!&lt;/p&gt;  &lt;p&gt;What if all of the existing IoC creators out there couldn’t care less about this little pesky API? Well I sure can’t blame them who has time to read each and every blog post. Fortunately it doesn’t matter. Let’s make it so that anyone who implements the API for their favorite IoC container and contribute.&lt;/p&gt;  &lt;p&gt;That sound’s fair! Not all IoC’s might even have a configuration API for that matter and the ones that do certainly do not have to have one that is suitable for such an API we’re talking of. Besides those that like code abstractions over hard references would use the API because it makes good design sense. Those who don’t care about such things would not use the API.&lt;/p&gt;  &lt;p&gt;OK – I’m still interested; let’s have a closer look.&lt;/p&gt;  &lt;h3&gt;Features we like&lt;/h3&gt;  &lt;p&gt;What kind of features would we like to have in our API?&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;It is annoying when configuration code breaks when you refactor. &lt;strong&gt;Type safety&lt;/strong&gt;? &lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/ItstimeforIoCContainerConfigurationDeten_125E6/checksmall%5B13%5D_2.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="checksmall[13]" border="0" alt="checksmall[13]" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/ItstimeforIoCContainerConfigurationDeten_125E6/checksmall%5B13%5D_thumb_2.png" width="20" height="20" /&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;It would be nice if it was easy to use and that means easy to read. A &lt;strong&gt;fluent interface&lt;/strong&gt;? &lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/ItstimeforIoCContainerConfigurationDeten_125E6/checksmall_4.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="checksmall" border="0" alt="checksmall" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/ItstimeforIoCContainerConfigurationDeten_125E6/checksmall_thumb_1.png" width="20" height="20" /&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;If I need to add my own favorite feature it should be easy to do so. Open source with an &lt;strong&gt;easily extensible API&lt;/strong&gt;? &lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/ItstimeforIoCContainerConfigurationDeten_125E6/checksmall%5B11%5D.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="checksmall[11]" border="0" alt="checksmall[11]" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/ItstimeforIoCContainerConfigurationDeten_125E6/checksmall%5B11%5D_thumb.png" width="20" height="20" /&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;It should be easy to implement for your own IoC. Good use of a &lt;strong&gt;base class implementation&lt;/strong&gt;? &lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/ItstimeforIoCContainerConfigurationDeten_125E6/checksmall%5B13%5D_1.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="checksmall[13]" border="0" alt="checksmall[13]" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/ItstimeforIoCContainerConfigurationDeten_125E6/checksmall%5B13%5D_thumb_1.png" width="20" height="20" /&gt;&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Sample code&lt;/h3&gt;  &lt;p&gt;It’s time to look at some sample code for this thing! There is more sample code on the project site and also a listing of the API in full.&lt;/p&gt;  &lt;p&gt;This is what the code would look like when you use the API to configure your code; easy to understand, fluent, type safe!&lt;/p&gt; &lt;font style="font-weight: bold"&gt;   &lt;pre class="code"&gt;ServiceConfigurator.RegisterType&amp;lt;&lt;span style="color: #2b91af"&gt;IFoo&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;&amp;gt;()
                   .InjectionConstructor(() =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;())
                   .InjectionProperties(f =&amp;gt; f.Bar, f =&amp;gt; f.Baz)
                   .AsSingleton();&lt;/pre&gt;
&lt;/font&gt;

&lt;p&gt;That looks just awesome! Yes indeed! Awesome and quite useful. It’s easy to see how this may be extended for specific purposes. (Doh – sorry I’ll try not to sound like a TV chef or a TV advertisement any more – sorry.)&lt;/p&gt;

&lt;p&gt;The code above tells your IoC container that you want to register the implementation Foo as the target for requests for instances of the contract IFoo. It will tell the IoC container to use the parameterless constructor (it can use any constructor). It will also tell the IoC container to use the properties Bar and Baz for &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;Dependency Injection&lt;/a&gt;. Finally it will tell the IoC container to treat the instance of Foo as a &lt;a href="http://en.wikipedia.org/wiki/Singleton_pattern"&gt;singleton&lt;/a&gt; instance once it is created.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://lambdas"&gt;lambdas&lt;/a&gt; look messy! Do they? We don’t even notice any more in our project. They are there to ensure type safety and if you implement this API using the base class the expressions that make up this “mess” are parsed once and for all and converted into more easily handled parameters such as types and strings. If you change any of your configured classes and try to compile the configuration code it will not even compile.&lt;/p&gt;

&lt;h3&gt;The what and the where&lt;/h3&gt;

&lt;p&gt;If you feel this API would be pretty darn useful and nice to get your hands on you’re in luck! It’s already created and published under open source license on &lt;a href="http://www.codeplex.com"&gt;CodePlex&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So without further ado… Introducing&lt;/p&gt;

&lt;p&gt;&lt;a href="http://commonservicemanager.codeplex.com/"&gt;&lt;strong&gt;&lt;font size="3"&gt;The Common Service Manager&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All the information you need about this project is up there on the site so this post does not need to go any further. If you hurry YOU can &lt;a href="http://commonservicemanager.codeplex.com/wikipage?title=Contributing"&gt;contribute&lt;/a&gt; with the implementation for your favorite IoC and claim the fame!&lt;/p&gt;

&lt;p&gt;If you feel this project is missing one key feature please don’t hesitate to &lt;a href="http://commonservicemanager.codeplex.com/discussions"&gt;provide feedback&lt;/a&gt;!&lt;/p&gt;

&lt;h3&gt;Final words&lt;/h3&gt;

&lt;p&gt;Thanks to the great guys of coding for the inspirational &lt;a href="http://commonservicelocator.codeplex.com/"&gt;Common Service Locator&lt;/a&gt; project that set our team off in the direction to create a piece of code that extends your work. Together these two pieces of API make our every day much more easy and that is what code by developers and for developers is all about! (The title of this post is a paraphrase from &lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2008/08/16/it-s-time-for-ioc-container-detente.aspx"&gt;Jeremy Miller&lt;/a&gt;s classic post.)&lt;/p&gt;

&lt;p&gt;Cheers,&lt;/p&gt;

&lt;p&gt;M.&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:c686b1a5-fd25-44de-afcb-68cab906319e" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Arcitecture" rel="tag"&gt;Arcitecture&lt;/a&gt;,&lt;a href="http://technorati.com/tags/API" rel="tag"&gt;API&lt;/a&gt;,&lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/IoC" rel="tag"&gt;IoC&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/315.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/o7H9px-wkF0l46Vi2jnSMxZebf0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/o7H9px-wkF0l46Vi2jnSMxZebf0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/o7H9px-wkF0l46Vi2jnSMxZebf0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/o7H9px-wkF0l46Vi2jnSMxZebf0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Techienotepad/~4/8k930L317Vs" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/09/05/its-time-for-ioc-container-configuration-detente.aspx</guid>
            <pubDate>Sun, 05 Sep 2010 20:30:07 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/315.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/09/05/its-time-for-ioc-container-configuration-detente.aspx#feedback</comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/315.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/315.aspx</trackback:ping>
        <coop:keyword>.NET</coop:keyword><coop:keyword>Architecture</coop:keyword><coop:keyword>C#</coop:keyword><coop:keyword>Patterns &amp; Practices</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/09/05/its-time-for-ioc-container-configuration-detente.aspx</feedburner:origLink></item>
        <item>
            <title>Entity Framework and Show &amp;amp; Tell .NET community event in Swenug Sk&amp;aring;ne in Malmo, Sweden</title>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/vB_xx7BITYU/entity-framework-and-show-and-tell-.net-community-event.aspx</link>
            <description>&lt;p align="left"&gt;If you are nearby or live here don’t be square! First NUG event in Skåne, Sweden for the fall is one with two agenda entries; Entity Framework and Show &amp;amp; Tell raffle.&lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://buzzfrog.blogs.com/"&gt;Dag&lt;/a&gt; &lt;a href="http://twitter.com/buzzfrog"&gt;König&lt;/a&gt; from Microsoft DPE Sweden will join us to give the latest info &lt;a href="http://msdn.microsoft.com/en-us/data/aa937723.aspx"&gt;Entity Framework&lt;/a&gt;.&lt;/p&gt;  &lt;p align="left"&gt;We will have a raffle on who will get to present their latest cool/interesting/noteworthy thing from .NET development with a prize raffled among the participants.&lt;/p&gt;  &lt;p align="left"&gt;Join Swenug and sigh up for the event here: &lt;a title="http://www.swenug.se/events/skaane-entity-framework-show" href="http://www.swenug.se/events/skaane-entity-framework-show"&gt;http://www.swenug.se/events/skaane-entity-framework-show&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;Sponsor is &lt;a href="http://jayway.com"&gt;Jayway&lt;/a&gt;.&lt;/p&gt;  &lt;p align="left"&gt;Cheers,&lt;/p&gt;  &lt;p align="left"&gt;M.&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:b847f993-ecef-4eb4-938a-fad09e331ecd" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Swenug" rel="tag"&gt;Swenug&lt;/a&gt;,&lt;a href="http://technorati.com/tags/NUG" rel="tag"&gt;NUG&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Entity+Framework" rel="tag"&gt;Entity Framework&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/314.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/SrQfQMEum1ycAzYNt4XbF5KV5cA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SrQfQMEum1ycAzYNt4XbF5KV5cA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/SrQfQMEum1ycAzYNt4XbF5KV5cA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SrQfQMEum1ycAzYNt4XbF5KV5cA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Techienotepad/~4/vB_xx7BITYU" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/08/20/entity-framework-and-show-and-tell-.net-community-event.aspx</guid>
            <pubDate>Fri, 20 Aug 2010 05:48:14 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/314.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/08/20/entity-framework-and-show-and-tell-.net-community-event.aspx#feedback</comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/314.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/314.aspx</trackback:ping>
        <coop:keyword>Entity Framework and Show &amp;amp; Tell .NET community event in Swenug Sk&amp;aring;ne in Malmo, Sweden</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/08/20/entity-framework-and-show-and-tell-.net-community-event.aspx</feedburner:origLink></item>
        <item>
            <title>Lazy&amp;lt;T&amp;gt; with implicit cast to T can give you some pretty code</title>
            <category>.NET</category>
            <category>C#</category>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/5y1M81J5uOI/lazy-t-with-implicit-cast-to-t.aspx</link>
            <description>&lt;p&gt;Adding an implicit cast functionality to &lt;a href="http://msdn.microsoft.com/en-us/library/dd642331.aspx"&gt;Lazy&amp;lt;T&amp;gt;&lt;/a&gt; can give you an option to writing null checkers in your delayed instantiation properties.&lt;/p&gt;  &lt;h3&gt;The problem&lt;/h3&gt;  &lt;p&gt;Today in our project a colleague was cussing over the fact that he kept writing the same block of code over and over again when null checking a property with a backing field.&lt;/p&gt;  &lt;p&gt;Given the class &lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo &lt;/span&gt;oldFooProp;
&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo &lt;/span&gt;OldFooProp
{
    &lt;span style="color: blue"&gt;get 
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(oldFooProp == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
        {
            oldFooProp = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;();
        }
        &lt;span style="color: blue"&gt;return &lt;/span&gt;oldFooProp;
    }
}&lt;/pre&gt;

&lt;p&gt;Have any of you written something like this before? It’s a bunch of lines of code that you tend to write (in small variations) over and over again. Sure you can compact it a little but it in the order of 5 – 10 lines of annoying repetitive code.&lt;/p&gt;

&lt;p&gt;Some use the &lt;a href="http://msdn.microsoft.com/en-us/library/ms173224.aspx"&gt;null coalescing operator (??)&lt;/a&gt; version that looks like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo &lt;/span&gt;OldAlternativeFooProp
{
    &lt;span style="color: blue"&gt;get
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;return &lt;/span&gt;oldFooProp ?? (oldFooProp = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;());
    }
}&lt;/pre&gt;

&lt;p&gt;This version is a little easier to compact down to even a single line.&lt;/p&gt;

&lt;p&gt;But we (my colleague and I) can do you one better:&lt;/p&gt;

&lt;h3&gt;Here is an optional approach&lt;/h3&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;&amp;gt; foo = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;&amp;gt;();

&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo &lt;/span&gt;Foo
{
    &lt;span style="color: blue"&gt;get
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;return &lt;/span&gt;foo;
    }
}&lt;/pre&gt;

&lt;p&gt;Actually I’d prefer to do that like this but then you have to know the solution below and be comfortable with it:&lt;/p&gt;

&lt;p&gt;&lt;span style="color: blue"&gt;public readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;&amp;gt; Foo = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;&amp;gt;();&lt;/p&gt;

&lt;p&gt;But wait a minute: That doesn’t work you say, right? You can’t return a &lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;&amp;gt; as a &lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt; you have to write theFoo.Value.&lt;/p&gt;

&lt;h3&gt;The Trick&lt;/h3&gt;

&lt;p&gt;The secret trick is to inherit Lazy&amp;lt;T&amp;gt; and implement an &lt;a href="http://msdn.microsoft.com/en-us/library/z5z9kes2%28VS.71%29.aspx"&gt;implicit operator&lt;/a&gt; for T on it!&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Jayway.com.Sys
{
    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;A version of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="Sys.Lazy{T}"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;with the twist of being implicitly castable to &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;typeparamref name="T"/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of lazy instance we are creating.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;T&amp;gt; : System.&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;T&amp;gt;
    {
&lt;span style="color: blue"&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;&lt;font color="#000000"&gt;        &lt;/font&gt;// Ctors removed for brevity
&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;font color="#008000" /&gt;
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Cast the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="Lazy{T}"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;to it's contained &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;typeparamref name="T"/&amp;gt;
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="lazyT"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The lazy to cast.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The instance of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="Lazy{T}.Value"/&amp;gt;&amp;lt;/returns&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public static implicit operator &lt;/span&gt;T(&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;T&amp;gt; lazyT)
        {
            &lt;span style="color: blue"&gt;return &lt;/span&gt;lazyT.Value;
        }
    }
}&lt;/pre&gt;

&lt;p&gt;Now you can implicitly cast a Lazy&amp;lt;T&amp;gt; to it’s contained type T.&lt;/p&gt;

&lt;p&gt;A few alternative constructions for it might look like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;LazyTests()
{
    &lt;span style="color: green"&gt;// With parameter
    &lt;/span&gt;theFoo = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;&amp;gt;(() =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"bar"&lt;/span&gt;));

    &lt;span style="color: green"&gt;// With &lt;a href="http://martinfowler.com/articles/injection.html"&gt;Inversion Of Control&lt;/a&gt;
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IServiceLocator &lt;/span&gt;serviceLocator = GetServiceLocator();
    theFoo = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;&amp;gt;(() =&amp;gt; serviceLocator.GetInstance());
}&lt;/pre&gt;

&lt;p&gt;I kind of like this code. It’s compact. It’s easy to read. It’s pretty!&lt;/p&gt;

&lt;p&gt;Hope you like it too – HTH!&lt;/p&gt;

&lt;p&gt;Cheers,&lt;/p&gt;

&lt;p&gt;M.&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:2491b7a2-a81b-491e-b8b9-700e5514443a" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/312.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/gZTQDMn4b-E3pzLqVTxJuU-S2Rg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gZTQDMn4b-E3pzLqVTxJuU-S2Rg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/gZTQDMn4b-E3pzLqVTxJuU-S2Rg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gZTQDMn4b-E3pzLqVTxJuU-S2Rg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Techienotepad/~4/5y1M81J5uOI" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/08/05/lazy-t-with-implicit-cast-to-t.aspx</guid>
            <pubDate>Thu, 05 Aug 2010 19:57:34 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/312.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/08/05/lazy-t-with-implicit-cast-to-t.aspx#feedback</comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/312.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/312.aspx</trackback:ping>
        <coop:keyword>.NET</coop:keyword><coop:keyword>C#</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/08/05/lazy-t-with-implicit-cast-to-t.aspx</feedburner:origLink></item>
        <item>
            <title>Handling Lazy + Metadata instances in Managed Extensibility Framework (MEF)</title>
            <category>.NET</category>
            <category>C#</category>
            <category>Architecture</category>
            <category>Patterns &amp; Practices</category>
            <category>Windows Azure</category>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/4knbh4r9NYc/handling-lazy-metadata-instances-in-managed-extensibility-framework-mef.aspx</link>
            <description>&lt;p&gt;Have you ever wanted to dynamically choose from many potential implementations of some code? Would you like to have the power of metadata filtering and lazy instantiation of the selected implementation? Here is how to do just that using Managed Extensibility Framework (MEF) and also a neat trick to handle your implementation/metadata pairs that I recently put to good use. Code samples and optimizations follow so don’t worry; I won’t remain all theoretical in this post. (&lt;a href="http://msdn.microsoft.com/en-us/library/dd460648.aspx"&gt;MEF&lt;/a&gt; is part of .NET Framework 4.0 but is available also on CodePlex for other releases – &lt;a href="http://mef.codeplex.com/"&gt;MEF Community Site&lt;/a&gt;.)&lt;/p&gt;  &lt;p&gt;Up front I want to show what I am aiming for so you can evaluate if this is a worth while read: Using the more advanced features of MEF suddenly I find I use these pretty gruesome variable and parameter declarations: &lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt;&amp;gt;. After applying the fix in this post I am left with the exact same behavior and this much improved declaration: &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWork&lt;/span&gt;&amp;gt;.&lt;/p&gt;  &lt;h3&gt;Background on MEF&lt;/h3&gt;  &lt;p&gt;Using MEF you can easily juggle the concept of lazily (using &lt;a href="http://msdn.microsoft.com/en-us/library/dd642331.aspx"&gt;System.Lazy&amp;lt;T&amp;gt;&lt;/a&gt;) importing an open ended set of instances of a given contract. This is built into the library for you. It takes some getting used to the concept but it is quite powerful. As if this wasn’t enough you can also add metadata to the contracts (with the MEF version of Lazy&amp;lt;T&amp;gt; – &lt;a href="http://msdn.microsoft.com/en-us/library/dd986615%28VS.95%29.aspx"&gt;System.Lazy&amp;lt;T, TMetadata&amp;gt;&lt;/a&gt;) to enable you to select which of the - potentially many - implementations of the contract that you want to lazily load into your app domain and instantiate. Does this all sound like geek speech? Well it sure is but as I said this is a very powerful concept. Given that you have a good requirement for this scenario you never have to code extensibility code yourself ever again. I would go so far as to say that &lt;strong&gt;MEF makes the extensibility scenario a solved problem!&lt;/strong&gt;&lt;/p&gt;  &lt;h3&gt;&lt;strong&gt;My Scenario&lt;/strong&gt;&lt;/h3&gt;  &lt;p&gt;Working with improving the Worker role concept in &lt;a href="http://www.microsoft.com/windowsazure/"&gt;Windows AzureI&lt;/a&gt; I recently had a use for an open ended set of implementations of what I called “work methods”. I wound up making my work methods implementations of &lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;. The metadata I added to my work method classes I decorated with &lt;span style="color: #2b91af"&gt;IWorkItemView &lt;/span&gt;metadata contract. This is what the two contracts look like:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IWorkItem&lt;br /&gt;&lt;/span&gt;{
    &lt;span style="color: blue"&gt;bool &lt;/span&gt;HasWork { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;void &lt;/span&gt;DoWork();
}&lt;br /&gt;
&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IWorkItemView
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;int &lt;/span&gt;Order { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
}&lt;/pre&gt;

&lt;p&gt;When using these contracts lazily in MEF I get the ugly instances of &lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt;&amp;gt;. There is some really cool functionality here for sure but I just can’t bare the ugliness of the statement as I begin to pass these instances around in method calls etc. The cool thing with MEF is that I don’t know beforehand how many implementations I have. All I need to know is that I can get them all:&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;ImportMany&lt;/span&gt;]
&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt;&amp;gt;&amp;gt; Work { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;

&lt;p&gt;This gives me a whole enumeration of lazy instances that are combined with the metadata of the metadata contract. On each instance of the lazy works you call .Metadata to access the &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt; contract and .Value to instantiate and return the instance of the &lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;. Now I don’t know about you but this is beginning to look kind of hairy to me. After all in my code I am not really concerned with any laziness or metadata contracts. Not only that – provided you are a coder that believes in separation of concerns there is one more issue. To explain the last statement let’s go back to my Windows Azure scenario:&lt;/p&gt;

&lt;p&gt;I import my many lazy instances with metadata into my code. Then I’d like to begin to use those instances. I wrote some code to filter out which instance I wanted to use. I wrote code to execute the selected instance. I wanted to separate these two code blocks from each other and I also wanted to be able to exchange filtering methods on the fly. I could continue but some of you have certainly begun to see the problem here. I was now passing around parameters of type &lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt;&amp;gt;. That’s not exactly pretty! What I found that I was conceptually doing was rather using an instance of something that had the combined contract of my two work-method, and work-metadata contracts:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IWork &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWorkItemView &lt;/span&gt;{ }&lt;/pre&gt;

&lt;p&gt;Passing around parameters of type &lt;span style="color: #2b91af"&gt;IWork&lt;/span&gt; is a lot better than &lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt;&amp;gt;. You have to all agree to that, right?&lt;/p&gt;

&lt;p&gt;So now – finally – the issue becomes that of transforming one instance into the other. I also want to keep the Lazy capability. The transformation technique is mostly built into an abstract base class I’ve written which you can download here:&lt;/p&gt;

&lt;p&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:F60BB8FA-6F02-4999-8F5E-9DD4E92C4DA7:3e443eaf-0bc2-4650-8cec-1edd01486f4b" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/e7a8e409b46d_E8F7/MEFContrib.LazyDomainCompactor.zip" target="_self"&gt;MEFContrib.LazyDomainCompactor.zip&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;

  &lt;br /&gt;I have also put the code in the &lt;a href="http://github.com/mefcontrib"&gt;MEF Contrib project at Github&lt;/a&gt;.

&lt;p&gt;Using the base class comes with a little bit of custom implementation per conversion you want to do from from “lazy with metadata” to “other domain object”. The alternative would have been for me to use a dynamic code generation approach for the implementation but I felt that dynamic code in this instance is a serious overkill. Besides we also need to make sure MEF picks up our implementation which in this case means we have to apply an attribute to our implementation.&lt;/p&gt;

&lt;h3&gt;The solution&lt;/h3&gt;

&lt;p&gt;To re-iterate: We had the &lt;span style="color: #2b91af"&gt;ImportMany &lt;/span&gt;statement above which is a bit cumbersome. Instead we are aiming for this:&lt;/p&gt;

&lt;p&gt;[&lt;span style="color: #2b91af"&gt;Import&lt;/span&gt;] 

  &lt;br /&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWork&lt;/span&gt;&amp;gt; Work { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/p&gt;

&lt;p&gt;Please notice the two different attributes &lt;span style="color: #2b91af"&gt;ImportMany &lt;/span&gt;and &lt;span style="color: #2b91af"&gt;Import&lt;/span&gt;. There is an important difference. The first one will find a lot of implementations of the contract (zero to many) while the latter will find only one exact match to the contract. In the first case we want MEF to find all &lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt; implementations that are decorated with the &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt; metadata. In the second case we want to find one single collection of &lt;span style="color: #2b91af"&gt;IWork&lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;The trick is to collect all the instances (&lt;span style="color: #2b91af"&gt;ImportManyAttribute&lt;/span&gt;) and hand them back out (&lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.exportattribute.aspx"&gt;ExportAttribute&lt;/a&gt;) as a single collection. What we do in the base class is import the many lazy with metadata instances and then immediately export a whole collection of the new contract to be picked up somewhere in the code as needed.&lt;/p&gt;

&lt;h3&gt;Details&lt;/h3&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public abstract class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ValueMetadataCompactorBase&lt;/span&gt;&amp;lt;TValue, TMetadata, TValueMetadata&amp;gt; :
    &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TValueMetadata&amp;gt;
    &lt;span style="color: blue"&gt;where &lt;/span&gt;TValueMetadata : TValue, TMetadata
{
&lt;span style="color: gray"&gt;    &lt;/span&gt;&lt;span style="color: blue"&gt;protected abstract &lt;/span&gt;TValueMetadata Compact(&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;TValue, TMetadata&amp;gt; lazyValueMetadata);

&lt;span style="color: gray"&gt;    &lt;/span&gt;[&lt;span style="color: #2b91af"&gt;ImportMany&lt;/span&gt;(AllowRecomposition = &lt;span style="color: blue"&gt;true&lt;/span&gt;)]
    &lt;span style="color: blue"&gt;internal &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;TValueMetadata&amp;gt;&amp;gt; ValueMetadatas { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

&lt;span style="color: gray"&gt;    &lt;/span&gt;[&lt;span style="color: #2b91af"&gt;ImportMany&lt;/span&gt;(AllowRecomposition = &lt;span style="color: blue"&gt;true&lt;/span&gt;)]
    &lt;span style="color: blue"&gt;internal &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;TValue, TMetadata&amp;gt;&amp;gt; ValuesWithMetadata { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;

&lt;pre class="code"&gt;     &lt;span style="color: green"&gt;// Cliped IEnumerable implementation for brevity [...]
&lt;/span&gt;}&lt;/pre&gt;

&lt;p&gt;There are a few things to notice here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;We &lt;span style="color: #2b91af"&gt;ImportMany &lt;/span&gt;instances of the &lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;TValue, TMetadata&amp;gt; and also for good measure we &lt;span style="color: #2b91af"&gt;ImportMany &lt;/span&gt;istances of &lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;TValueMetadata&amp;gt;. The reason for the latter is that since we offer the contract &lt;span style="color: #2b91af"&gt;IWork &lt;/span&gt;for any developer to pick up an implement we can’t be 100% certain some one doesn’t use it in a direct implementation. It is no crime to do so even though the intent is to implement &lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt; and decorate it with the metadata &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt;. The real reason for not implementing the &lt;span style="color: #2b91af"&gt;IWork&lt;/span&gt; direct is because we can remain lazy with the metadata if it is real metadata. If we want to start looking at the members of an implementation that does not have metadata we need to load that implementations assembly into the &lt;a href="http://msdn.microsoft.com/en-us/library/system.appdomain.aspx"&gt;AppDomain&lt;/a&gt;.&lt;/li&gt;

  &lt;li&gt;The class implements &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TValueMetadata&amp;gt;. This is used in the derived classes to &lt;span style="color: #2b91af"&gt;Export&lt;/span&gt; the behavior and make the transformation accessible to our needy develoeprs. ;~) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To use this base class you have to do two things:&lt;/p&gt;

&lt;p&gt;1) Implement the base class which constitutes implementing one method for compacting together a TValue and TMetadata into a TValueMetadata. 
  &lt;br /&gt;2) Apply the &lt;span style="color: #2b91af"&gt;ExportAttribute&lt;/span&gt; with the parameter &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&amp;lt;your type&amp;gt;).&lt;/p&gt;

&lt;p&gt;Let me show you my implementation of the compactor for compacting &lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;s with &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt; into instances of &lt;span style="color: #2b91af"&gt;IWork&lt;/span&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Export&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWork&lt;/span&gt;&amp;gt;))]
&lt;span style="color: blue"&gt;internal class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WorkCompactor &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;ValueMetadataCompactorBase&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWork&lt;/span&gt;&amp;gt;
{
    &lt;span style="color: blue"&gt;protected override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IWork &lt;/span&gt;Compact(&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt;&amp;gt; lazyValueMetadata)
    {
        &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WorkWrapper&lt;/span&gt;(lazyValueMetadata);
    }

    &lt;span style="color: blue"&gt;private class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WorkWrapper &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IWork
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt;&amp;gt; lazyWorkWithMetadata;

        &lt;span style="color: blue"&gt;public &lt;/span&gt;WorkWrapper(&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWorkItem&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IWorkItemView&lt;/span&gt;&amp;gt; lazyWorkWithMetadata)
        {
            &lt;span style="color: blue"&gt;this&lt;/span&gt;.lazyWorkWithMetadata = lazyWorkWithMetadata;
        }

        &lt;span style="color: blue"&gt;public bool &lt;/span&gt;HasWork
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;lazyWorkWithMetadata.Value.HasWork; }
        }

        &lt;span style="color: blue"&gt;public void &lt;/span&gt;DoWork()
        {
            lazyWorkWithMetadata.Value.DoWork();
        }

        &lt;span style="color: blue"&gt;public int &lt;/span&gt;Order
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;lazyWorkWithMetadata.Metadata.Order; }
        }

        &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;lazyWorkWithMetadata.Metadata.Name; }
        }
    }
}&lt;/pre&gt;

&lt;p&gt;Notice:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The annoyance here is that I have to custom implement a boring &lt;span style="color: #2b91af"&gt;IWork &lt;/span&gt;wrapper ourselves since I am not using a dynamic code approach.&lt;/li&gt;

  &lt;li&gt;The implementation of said &lt;span style="color: #2b91af"&gt;IWork &lt;/span&gt;retains the lazy behavior in my implementation since I only forward calls to the members to the contained lazy instance.&lt;/li&gt;

  &lt;li&gt;The [&lt;span style="color: #2b91af"&gt;Export&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWork&lt;/span&gt;&amp;gt;))] attribute on the implementation is that which makes my &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IWork&lt;/span&gt;&amp;gt; available to the rest of my code through MEF.&lt;/li&gt;

  &lt;li&gt;The implementation is marked as &lt;span style="color: blue"&gt;internal&lt;/span&gt;. MEF doesn’t care – it finds &lt;span style="color: blue"&gt;internal&lt;/span&gt;, &lt;span style="color: blue"&gt;protected&lt;/span&gt; and &lt;span style="color: blue"&gt;private&lt;/span&gt; implementations as well as &lt;span style="color: blue"&gt;public&lt;/span&gt;.. This means that user coders do not even know that this conversion exists. All the handling of lazy etc. is removed from the “subscribing” code. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Note to self:&lt;/strong&gt; Write less epic blog posts. ;~)&lt;/p&gt;

&lt;p&gt;I hope this post gives you some insight into how MEF works as well as a neat trick to simplify some of your MEF-Import code and lazy-metadata craziness. The functionality is very good to have but I really do not want library specific concerns to affect my code too much. Do you?&lt;/p&gt;

&lt;p&gt;Cheers,&lt;/p&gt;

&lt;p&gt;M. (the NoOpMan)&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:e5f4039c-15d2-49b0-a8ba-a585f2731411" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Microsoft" rel="tag"&gt;Microsoft&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Arcitecture" rel="tag"&gt;Arcitecture&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Extensibility" rel="tag"&gt;Extensibility&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MEF" rel="tag"&gt;MEF&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/311.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/lhFb2qrFZ4lE8cAHGUbLYc_f9yM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lhFb2qrFZ4lE8cAHGUbLYc_f9yM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/lhFb2qrFZ4lE8cAHGUbLYc_f9yM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lhFb2qrFZ4lE8cAHGUbLYc_f9yM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Techienotepad/~4/4knbh4r9NYc" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/08/04/handling-lazy-metadata-instances-in-managed-extensibility-framework-mef.aspx</guid>
            <pubDate>Wed, 04 Aug 2010 19:21:32 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/311.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/08/04/handling-lazy-metadata-instances-in-managed-extensibility-framework-mef.aspx#feedback</comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/311.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/311.aspx</trackback:ping>
        <coop:keyword>.NET</coop:keyword><coop:keyword>C#</coop:keyword><coop:keyword>Architecture</coop:keyword><coop:keyword>Patterns &amp; Practices</coop:keyword><coop:keyword>Windows Azure</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/08/04/handling-lazy-metadata-instances-in-managed-extensibility-framework-mef.aspx</feedburner:origLink></item>
        <item>
            <title>Common Service Locator MEF Adapter now on .NET Framework 4.0</title>
            <category>.NET</category>
            <category>vs2010</category>
            <category>Patterns &amp; Practices</category>
            <category>Visual Studio</category>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/UKX_jmYey0Y/common-service-locator-mef-adapter-now-on-.net-framework-4.0.aspx</link>
            <description>&lt;p&gt;I like the &lt;a href="http://commonservicelocator.codeplex.com/"&gt;Common Service Locator&lt;/a&gt;. It’s nice and clean. Abstractions are good.&lt;/p&gt;  &lt;p&gt;This is why I bothered &lt;a href="http://blogs.msdn.com/gblock/"&gt;Glenn&lt;/a&gt; &lt;a href="http://twitter.com/gblock"&gt;Block&lt;/a&gt; about updating the &lt;a href="http://commonservicelocator.codeplex.com/wikipage?title=MEF%20Adapter&amp;amp;referringTitle=Home"&gt;MEF Adapter&lt;/a&gt; all the time as each version of &lt;a href="http://mef.codeplex.com/"&gt;Managed Extensibility Framework MEF&lt;/a&gt; came out. Finally he gave in and made me a contributor to the CSL project. – Fine – Do it your self, kind of! ;~) Glenn and I are good mates. I didn’t mind updating it just to have one thing that was in order. Hmmm… sounding bitter and disgruntled on a Saturday night, sorry.&lt;/p&gt;  &lt;p&gt;Well now the MEF release is finally here as part of &lt;a href="http://www.microsoft.com/net/"&gt;.NET Framework 4.0&lt;/a&gt; and I’ve updated the MEF Adapter for &lt;a href="http://msdn.microsoft.com/en-us/vstudio/default.aspx"&gt;Visual Studio 2010&lt;/a&gt; and .NET 4.0.&lt;/p&gt;  &lt;p&gt;Hope you like it and use it!&lt;/p&gt;  &lt;p&gt;Cheers,&lt;/p&gt;  &lt;p&gt;M.&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:b7a0b254-e055-4018-a759-3317ebbbcfac" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET+4.0" rel="tag"&gt;.NET 4.0&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Inversion+of+Control" rel="tag"&gt;Inversion of Control&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Depencency+Injection" rel="tag"&gt;Depencency Injection&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/310.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ErsAEnyv3Ul5C96zAlF2UD1RSSM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ErsAEnyv3Ul5C96zAlF2UD1RSSM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ErsAEnyv3Ul5C96zAlF2UD1RSSM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ErsAEnyv3Ul5C96zAlF2UD1RSSM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Techienotepad/~4/UKX_jmYey0Y" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/05/08/common-service-locator-mef-adapter-now-on-.net-framework-4.0.aspx</guid>
            <pubDate>Sat, 08 May 2010 20:25:01 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/310.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/05/08/common-service-locator-mef-adapter-now-on-.net-framework-4.0.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/310.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/310.aspx</trackback:ping>
        <coop:keyword>.NET</coop:keyword><coop:keyword>vs2010</coop:keyword><coop:keyword>Patterns &amp; Practices</coop:keyword><coop:keyword>Visual Studio</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/05/08/common-service-locator-mef-adapter-now-on-.net-framework-4.0.aspx</feedburner:origLink></item>
        <item>
            <title>MEF + Azure + Visual Studio 2010 = Tricky but doable (part 2)</title>
            <category>.NET</category>
            <category>TDD</category>
            <category>Windows Azure</category>
            <category>vs2010</category>
            <category>Visual Studio</category>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/0O-g-u9RvCs/mef-azure-visual-studio-2010-tricky-but-again.aspx</link>
            <description>&lt;p&gt;In order to make &lt;a href="http://mef.codeplex.com"&gt;Managed Extensibility Framework (MEF)&lt;/a&gt; a part of your Windows Azure project in Visual Studio 2010 while having a Test Project to test your code there is a nasty reference issue you have to resolve. I posted all the steps to resolving this issue in my recent post &lt;a href="http://blog.noop.se/archive/2010/04/28/mef-azure-visual-studio-2010-tricky-but.aspx"&gt;MEF + Azure + Visual Studio 2010 = Tricky but doable&lt;/a&gt;. but I forgot one step! Here is the final piece of the puzzle.&lt;/p&gt;  &lt;h3&gt;The remaining problem&lt;/h3&gt;  &lt;p&gt;Following the steps in my previous post (link above) you are good to go with your project in all respects save one. You can compile. You can run your Windows Azure application in the Development Fabric. You can run your tests in your Test Project.&lt;/p&gt;  &lt;p&gt;The only thing you cannot do is refer from your tests to the type Tuple&amp;lt;T1, T2&amp;gt; (or Lazy&amp;lt;T&amp;gt;) in your Test Project. This will cause an “Ambiguous reference” error at compile time in your code:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/MEFAzureVisualStudio2010Trickybutdoablep_EF95/Ambiguous%20Reference%202.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Ambiguous Reference 2" border="0" alt="Ambiguous Reference 2" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/MEFAzureVisualStudio2010Trickybutdoablep_EF95/Ambiguous%20Reference%202_thumb.png" width="342" height="156" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Test Projects in Visual Studio 2010 have to run on .NET Framework 4.0. This new version of the framework includes the new Tuple class. However you cannot use .NET Framework 4.0 components on Windows Azure (until something like next month when Windows Azure upgrades).&lt;/p&gt;  &lt;p&gt;Your Windows Azure project has to run on .NET Framework 3.5. Which means you have to download and run Preview 9 of MEF from the CodePlex site (link above) and use that. In my first post on this topic I showed how to get your preview (.NET 3.5) version into your (.NET 4.0) Test Project. If you follow these instructions you can compile and run your application.&lt;/p&gt;  &lt;p&gt;However the final problem in this matter is the following: The type "’System.Tuple&amp;lt;T1, T2&amp;gt; exists in both ‘c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll’ and &amp;lt;your MEF assembly&amp;gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/MEFAzureVisualStudio2010Trickybutdoablep_EF95/Ambiguous%20Reference%201.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Ambiguous Reference 1" border="0" alt="Ambiguous Reference 1" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/MEFAzureVisualStudio2010Trickybutdoablep_EF95/Ambiguous%20Reference%201_thumb.png" width="1381" height="62" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;There is a way to fix this issue so that you can refer to System.Tuple from your Test Project. &lt;/p&gt;  &lt;h3&gt;The Solution&lt;/h3&gt;  &lt;p&gt;What you need to do is eliminate one of the references. Since you can’t really, in any easy way, eliminate the .NET 4.0 version of mscorlib it’s perhaps best to try with the other one – the MEF (.NET 3.5) assembly.&lt;/p&gt;  &lt;p&gt;Here are the steps to follow:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Download &lt;em&gt;the source code &lt;/em&gt;to MEF Preview 9 from the CodePlex site. &lt;/li&gt;    &lt;li&gt;Open up the ComponentModel Project. (Make sure you don’t get it converted to .NET Framework 4.0!) &lt;/li&gt;    &lt;li&gt;Open the three files Tuple, Lazy and LazyOfTTMetadata. The first two are part of mscorlib 4.0 and the last one is part of the 4.0 version of MEF (System.ComponentModel.Composition.dll). &lt;/li&gt;    &lt;li&gt;Rename the namespace to something you like; I chose System2 – why not? &lt;/li&gt;    &lt;li&gt;Make the ComponentModel Project compile again. This is done by resolving all of the references from the old System.Tuple&amp;lt;T1, T2&amp;gt;, System.Lazy&amp;lt;T&amp;gt; and System.Lazy&amp;lt;T, TMetadata&amp;gt; to the new System2.Tuple&amp;lt;T1, T2&amp;gt;, System2.Lazy&amp;lt;T&amp;gt; and System2.Lazy&amp;lt;T, TMetadata&amp;gt;. &lt;/li&gt;    &lt;li&gt;Once the project compiles; take this modified version of MEF (the System.ComponentModel.Composition.dll) and reference it to your solution instead of the real MEF Preview 9 version. &lt;/li&gt;    &lt;li&gt;Now of course you have to resolve the usages of the three types that have moved again. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;That’s it! Now your Windows Azure Project will be able to run MEF Preview 9 on .NET Framework 3.5 together with your Visual Studio 2010, .NET Framework 4.0, Test Project and both will be able to reference your new version of Tuple, and the two Lazies.&lt;/p&gt;  &lt;h3&gt;Summary&lt;/h3&gt;  &lt;p&gt;Sure this is a fix but it enables you to run MEF for open ended extensibility scenarios in Windows Azure right now. When Windows Azure upgrades to .NET Framework 4.0 you can simply drop this fix and refer to the .NET Framework 4.0 version of System.ComponentModel.Composition.dll.&lt;/p&gt;  &lt;p&gt;Cheers,&lt;/p&gt;  &lt;p&gt;M.&lt;/p&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&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:0767317B-992E-4b12-91E0-4F059A8CECA8:92395355-ce94-4b4c-af80-baf89b5c6947" class="wlWriterSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/.NET+Framework" rel="tag"&gt;.NET Framework&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Extensibility" rel="tag"&gt;Extensibility&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/309.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/kCV5ZblnOmt5iMXImVItX3lBkrw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/kCV5ZblnOmt5iMXImVItX3lBkrw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/kCV5ZblnOmt5iMXImVItX3lBkrw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/kCV5ZblnOmt5iMXImVItX3lBkrw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Techienotepad/~4/0O-g-u9RvCs" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/04/29/mef-azure-visual-studio-2010-tricky-but-again.aspx</guid>
            <pubDate>Thu, 29 Apr 2010 20:19:28 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/309.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/04/29/mef-azure-visual-studio-2010-tricky-but-again.aspx#feedback</comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/309.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/309.aspx</trackback:ping>
        <coop:keyword>.NET</coop:keyword><coop:keyword>TDD</coop:keyword><coop:keyword>Windows Azure</coop:keyword><coop:keyword>vs2010</coop:keyword><coop:keyword>Visual Studio</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/04/29/mef-azure-visual-studio-2010-tricky-but-again.aspx</feedburner:origLink></item>
        <item>
            <title>MEF + Azure + Visual Studio 2010 = Tricky but doable</title>
            <category>.NET</category>
            <category>Windows Azure</category>
            <category>TDD</category>
            <category>vs2010</category>
            <category>Visual Studio</category>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/ZWE-rhYhETk/mef-azure-visual-studio-2010-tricky-but.aspx</link>
            <description>&lt;p&gt;Putting Managed Extensibility Framework (MEF) in your Windows Azure Application while trying to test your code using Visual Studio 2010 does not work OOTB. It is quite easy to modify – in this case MEF – to enable this scenario. The same issue might result from using other libraries as well.&lt;/p&gt;  &lt;p&gt;Now that Visual Studio 2010 is out you really do not want to remain on the old version. Sometimes there can be issues working with applications that need to support previous versions of .NET Framework – previous to .NET 4.0 that is. Here is one such issue and how you work around it.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; There was one more problem that I did not cover in this post. Once you’ve read this. Continue reading here &lt;a href="http://blog.noop.se/archive/2010/04/29/mef-azure-visual-studio-2010-tricky-but-again.aspx"&gt;MEF + Azure + Visual Studio 2010 = Tricky but doable (Part 2)&lt;/a&gt;  in order to get the final piece of the puzzle.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This issue will go away within a couple of months when .NET Framework 4.0 is deployed to Windows Azure. But right now this may be an issue. Guess the blogging window for this topic is right now. ;~)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note to self:&lt;/strong&gt; Stop doing epic refactoring and development tasks on your small(ish) projects. This will halt your blogging.&lt;/p&gt;  &lt;h3&gt;Prologue&lt;/h3&gt;  &lt;p&gt;Testing in Visual Studio 2010 requires you to run the new version of the Microsoft.VisualStudio.QualityTools.UnitTestFramework. This artifact is built using .NET Framework 4.0.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/MEFAzureVisualStudio2010Trickybutdoable_72B4/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/MEFAzureVisualStudio2010Trickybutdoable_72B4/image_thumb.png" width="394" height="211" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;If you go File –&amp;gt; New –&amp;gt; Windows Azure Cloud Service project and select an ASP.NET MVC 2.0 Web Role and choose to create also a Test Project for this solution you will get one Web App on .NET Framework 3.5 and one Test Project on .NET Framework 4.0. If you want to develop your Windows Azure Application using Visual Studio 2010 your Azure app requires .NET Framework 3.5 because Windows Azure is on .NET Framework 3.5.&lt;/p&gt;  &lt;p&gt;Can you see a versioning conflict a’ brewin’? ;~)&lt;/p&gt;  &lt;h3&gt;The Problem&lt;/h3&gt;  &lt;p&gt;Some parts of Windows Azure Applications is a classic extensibility scenario. My colleague &lt;a href="http://twitter.com/vonlochow"&gt;Peter&lt;/a&gt; and I used a standard .NET Framework library in preview called &lt;a href="http://mef.codeplex.com"&gt;Managed Extensibility Framework (MEF)&lt;/a&gt; to handle extensibility. This worked fine for the old version of Visual Studio but results in a conflict in Visual Studio 2010. MEF includes a couple of small utility classes from .NET Framework 4.0 as part of the preview. These are embedded in the System.ComponentModel.Composition assembly that is MEF. If you try to reference this assembly from your Visual Studio 2010 test project you will first fail. Visual Studio is smart enough to instead not do as you ask but rather go and reference the assembly that is part of your .NET Framework 4.0 installation:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/MEFAzureVisualStudio2010Trickybutdoable_72B4/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/MEFAzureVisualStudio2010Trickybutdoable_72B4/image_thumb_1.png" width="860" height="290" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;But if you use the preview version of MEF that is compiled against .NET Framework 3.5 in your Windows Azure Application you have to have this particular version referenced in your tests. If you run your tests using the 4.0 version of MEF in your Test Project and the 3.5 version in your Azure Role you will get this error:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/MEFAzureVisualStudio2010Trickybutdoable_72B4/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/MEFAzureVisualStudio2010Trickybutdoable_72B4/image_thumb_2.png" width="477" height="312" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;It appears you cannot load the 3.5 version of MEF when you have already loaded a newer (4.0) version when you run your test.&lt;/p&gt;  &lt;h3&gt;The Solution&lt;/h3&gt;  &lt;p&gt;How do I add the version I want (3.5) then? Let’s resort to good old hand editing of the project file:&lt;/p&gt;  &lt;p&gt;I opened up my Azure Role project file in notepad and copied my reference to MEF:&lt;/p&gt;  &lt;p&gt;&amp;lt;Reference Include="System.ComponentModel.Composition"&amp;gt;    &lt;br /&gt;  &amp;lt;HintPath&amp;gt;..\lib\System.ComponentModel.Composition.dll&amp;lt;/HintPath&amp;gt;     &lt;br /&gt;&amp;lt;/Reference&amp;gt;&lt;/p&gt;  &lt;p&gt;Then I opened up the Visual Studio 2010 Test Project file in notepad and replaced the following with the text above:&lt;/p&gt;  &lt;p&gt;&amp;lt;Reference Include="System.ComponentModel.Composition" /&amp;gt;&lt;/p&gt;  &lt;p&gt;Hey presto! It works.&lt;/p&gt;  &lt;h3&gt;Summary&lt;/h3&gt;  &lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; There was one more problem that I did not cover in this post. Once you’ve read this. Continue reading here &lt;a href="http://blog.noop.se/archive/2010/04/29/mef-azure-visual-studio-2010-tricky-but-again.aspx"&gt;MEF + Azure + Visual Studio 2010 = Tricky but doable (Part 2)&lt;/a&gt;  in order to get the final piece of the puzzle.&lt;/p&gt;  &lt;p&gt;If there is a will there is a way. Due to the collision between the MEF Preview that works in .NET Framework 3.5 and the Visual Studio 2010 Test Project type which defaults to the .NET Framework 4.0 version it was non-trivial to use the MEF library in the Cloud.&lt;/p&gt;  &lt;p&gt;Using MEF in Windows Azure is something I automatically turned to when I saw that my scenario had an open ended extensibility point. Since you know what it is you deploy to Windows Azure and if you are not using a bunch of third party libraries that might utilize extensibility through MEF it is unlikely that this problem will apply to you.&lt;/p&gt;  &lt;p&gt;That said if you are stringent about using the right tool for the right job and strive to make your code designs and software architecture adhere to a good yet for your domain appropriate number of design patterns and techniques then MEF will very likely find it’s way into Your Application in the Cloud. &lt;/p&gt;  &lt;p&gt;Cheers,&lt;/p&gt;  &lt;p&gt;M.&lt;/p&gt;  &lt;p /&gt;  &lt;p /&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:0767317B-992E-4b12-91E0-4F059A8CECA8:92395355-ce94-4b4c-af80-baf89b5c6947" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/.NET+Framework" rel="tag"&gt;.NET Framework&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Extensibility" rel="tag"&gt;Extensibility&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/308.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/QBStjyES2bc3ENsfScEEI5mZ0FI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/QBStjyES2bc3ENsfScEEI5mZ0FI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/QBStjyES2bc3ENsfScEEI5mZ0FI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/QBStjyES2bc3ENsfScEEI5mZ0FI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Techienotepad/~4/ZWE-rhYhETk" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/04/28/mef-azure-visual-studio-2010-tricky-but.aspx</guid>
            <pubDate>Wed, 28 Apr 2010 18:51:16 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/308.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/04/28/mef-azure-visual-studio-2010-tricky-but.aspx#feedback</comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/308.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/308.aspx</trackback:ping>
        <coop:keyword>.NET</coop:keyword><coop:keyword>Windows Azure</coop:keyword><coop:keyword>TDD</coop:keyword><coop:keyword>vs2010</coop:keyword><coop:keyword>Visual Studio</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/04/28/mef-azure-visual-studio-2010-tricky-but.aspx</feedburner:origLink></item>
        <item>
            <title>Neo4j .NET Client over HTTP using REST and json</title>
            <category>.NET</category>
            <category>Architecture</category>
            <category>Patterns &amp; Practices</category>
            <category>C#</category>
            <category>TDD</category>
            <category>Graph Databases</category>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/niYy-58AmZ8/neo4j-net-client-over-http-using-rest-and-json.aspx</link>
            <description>&lt;p&gt;Here it is; a &lt;a href="http://en.wikipedia.org/wiki/Proof_of_concept"&gt;Proof of Concept&lt;/a&gt; of the world’s first Neo4j .NET Client. In other words: Here follows a discussion on how to create a client library for communicating with a graph database over &lt;a href="http://en.wikipedia.org/wiki/REST"&gt;REST&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; There is now a live CodePlex project for the realization of this concept; A .NET Client Library for Neo4j over HTTP using REST and JSON; &lt;a title="http://neo4jrestsharp.codeplex.com/" href="http://neo4jrestsharp.codeplex.com/"&gt;http://neo4jrestsharp.codeplex.com/&lt;/a&gt;&lt;/p&gt; &lt;h3&gt;Intro&lt;/h3&gt; &lt;p&gt;&lt;a href="http://neotechnology.com/"&gt;Neo Technologies&lt;/a&gt; have come out with a &lt;a href="http://components.neo4j.org/neo4j-rest/"&gt;Neo4j REST API&lt;/a&gt; for their popular Neo4j graph database. Since I am employed at &lt;a href="http://jayway.com"&gt;Jayway&lt;/a&gt; and a couple of colleagues working with Neo are too a little bird by the name of &lt;a href="http://twitter.com/peterneubauer/status/11761202320"&gt;Peter Neubauer tweeted in my ear&lt;/a&gt; that there was a free T-Shirt involved in making a working sample for .NET that communicated with the Neo4j server. Not only was that a handsome offer – I wanted to check out what the &lt;em&gt;Architecture&lt;/em&gt; of such a client library for a graph database would look like.&lt;/p&gt; &lt;p&gt;I’ve recently been involved in client library abstractions and good architecture with the RESTful interface to &lt;a href="http://www.microsoft.com/windowsazure/windowsazure/"&gt;Windows Azure Storage&lt;/a&gt;. (More on Azure and storage on my blog &lt;a href="http://blog.noop.se/archive/2009/11/29/azure-contrib-codeplex-updated-to-the-november-ctp.aspx"&gt;here&lt;/a&gt;, &lt;a href="http://blog.noop.se/archive/2009/08/15/introducing-the-cloud-storage-api.aspx"&gt;here&lt;/a&gt;, &lt;a href="http://blog.noop.se/archive/2009/08/17/using-the-cloudstorage.api-the-message-queue.aspx"&gt;here&lt;/a&gt;, &lt;a href="http://blog.noop.se/archive/2009/08/19/using-the-cloudstorage.api-the-blob-storage.aspx"&gt;here&lt;/a&gt; and &lt;a href="http://blog.noop.se/archive/2009/08/20/using-the-cloudstorage.api-the-entity-storage.aspx"&gt;here&lt;/a&gt;.) Enough with the side tracking (sorry)!&lt;/p&gt; &lt;p&gt;Said and done; We set out to code up a working sample for .NET. I hacked the client code. The Neo-guys cheered me on and offered very helpful assistance when I ran into trouble or was just plane stumped on how to proceed with the API. A very nice team effort, including a remote team debugging session, later and we have a small and running POC&lt;/p&gt; &lt;p&gt;Tweeting on this effort it is notable how a bunch of people answer back with an interest in this idea for potential applications in the .NET world. I’ve been approached both on twitter (&lt;a href="http://twitter.com/noopman"&gt;@noopman&lt;/a&gt;) and on &lt;a href="http://se.linkedin.com/in/noopman"&gt;LinkedIn&lt;/a&gt; with questions on this.&lt;/p&gt; &lt;p&gt;This blog post is to submit our findings to the world and hope that someone will be inspired to get going with a real .NET client library for Neo4j.&lt;/p&gt; &lt;h3&gt;Architecture&lt;/h3&gt; &lt;p&gt;First let’s look at the architecture for creating a client library for a RESTful service. This is a more general discussion that leads to our client library POC below. If your main reason for reading is the actual sample; scroll down.&lt;/p&gt; &lt;p&gt;When doing a client library abstractions and domain thinking comes into play. What we want is for the forgotten users (developers) of your application to work with their normal domain objects in the business code and for them to use an API that makes sense to their domain while talking to a highly specific graph database backend store. This means you have to keep all the RESTfulness, HTTP and &lt;a href="http://json.org/"&gt;json&lt;/a&gt; away from the domain code. Let me show you a sketch and see if I can make this explanation a bit less wordy:&lt;/p&gt; &lt;p&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/Neo4j.NETClientoverHTTPusingRESTandjson_C9DC/image_3.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/Neo4j.NETClientoverHTTPusingRESTandjson_C9DC/image_thumb.png" width="563" height="551" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;At the other side of the Cloud from our .NET code is a Java based graph database. It is friendly enough to expose a RESTful API so that we may speak to it.&lt;/p&gt; &lt;p&gt;Doing so we want to create a client library that is capable of a couple of things:1) Taking care of serializing objects to json (and deserializing them back again). 2) Knowing about HTTP requests and how to handle them. In our sample we employed the power of the &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx"&gt;HttpWebRequest&lt;/a&gt; class to handle things like HTTP Headers, Content Types, URIs etc. 3) Finally this library must understand to expose an interface to your code that you can use from a specialized Data Access Component.&lt;/p&gt; &lt;p&gt;To protect the business layer from knowing anything about the specifics of the Neo4j API you really should use a specialized Data Access Component. There is now law being broken if you don’t do that. However a good rule of thumb is to add abstraction layers in your system to shield your domain code in your business components from any specifics of an underlying storage structure. The details of serialization of objects to json and making requests over HTTP are not a natural fit to the business problem you are solving. The code that does this separation is code you write yourself in your application. Only you know how to adapt a foreign library and technology to your domain. The sole purpose of this code, in other words, is to abstract away from your business logic the underlying technologies used to persist and query data used in the application. This component translates between the two worlds of your business logic and the techniques used to store data in a graph database. Most of the specific technical details of storage are wrapped up in the Client Lib mind you but the Data Access Layer DAL knows about this problem domain. It understands that the persistence indeed is a REST based service over HTTP with all that this entails. This component in your code can handle synchonisity and take care of specific errors or faults that may occur when using the storage. All of this is done in a language that is fairly close to the API that Neo4j exposes. In the end this component translates into terms useful to the domain in which your business operates. Instead of saying .PostNode() in your code perhaps you instead expose a .StorePerson() action.&lt;/p&gt; &lt;p&gt;What we achieve is a good and sound (and classic) layering model: Business Logic – DAL – Client Lib.&lt;/p&gt; &lt;p&gt;There is one more observation to make in the picture and that is the matter of &lt;a href="http://en.wikipedia.org/wiki/Inversion_of_control"&gt;IoC - Inversion of Control&lt;/a&gt;. In order to keep your code testable, maintainable and untangled from the implementations at lower layers you should always employ the method of Inversion of Control – making your business code for instance not reference the DAL direct but instead use a common contract to do so. The &lt;a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle"&gt;Depencency Inversion Principle&lt;/a&gt; states briefly that a higher layer should never depend direct on a lower layer but instead depend on an abstraction. If this is true you are able to test your business logic without using the actual underlying storage. This is for instance key in a &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development"&gt;TDD – Test Driven Development&lt;/a&gt; development style.&lt;/p&gt; &lt;p&gt;Code that depends on abstractions can be tested very quickly and does not depend on specific data in a data store to be correct in order to test the intended behavior. The Business Logic uses a contract for the DAL and the DAL implements it. Then you use something called an IoC container (read on at Wikipedia in the link above) to create the DAL instances and hand them to the Business Logic employing the technique of &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;DI -Dependency Injection&lt;/a&gt;. This separation enables you to modify, re-deploy or even exchange DAL components without having to touch your Business Layer at all. If separating the Business Logic from the DAL using contracts is important it is almost event more important to do so in the lower layer that in run time speaks direct to the data store! You never want to end up in the situation where in order to test your DAL code you have to have access to a specific data store with specific data in it. Not only is that an inconvenience; it also makes executing your tests run as slow as transferring data over the Internet compared to as fast as your CPU can execute code. From experience it is an obvious conclusion that tests that are slow to run are not in fact run. The conclusion from all of these IoC discussions can only be that it is imperative that your Client Library comes with an abstraction layer.&lt;/p&gt; &lt;h4&gt;What did we do with the Architecture in our POC?&lt;/h4&gt; &lt;p&gt;All of these architectural considerations are of course premature in a POC. This is why in our sample we did not really separate the DAL code from the Client Lib. However to prove the point of IoC and DI I did however separate out the Business Logic from the Data Access logic using a simple contract and an equally simple &lt;a href="http://en.wikipedia.org/wiki/Factory_method_pattern"&gt;Factory Method Pattern&lt;/a&gt;. Instead of new:ing up a DAL component in my code I called a factory method to access an abstraction of the DAL.&lt;/p&gt; &lt;h3&gt;Our POC sample&lt;/h3&gt; &lt;p&gt;Our code sample creates a small node graph with a few relationships. Then it makes a traversal of the graph to find all who “knows” someone according to the relationships. Finally it deletes the graph again.&lt;/p&gt; &lt;p&gt;Here is the graph we create in our sample: &lt;a title="http://wiki.neo4j.org/content/The_Matrix" href="http://wiki.neo4j.org/content/The_Matrix"&gt;http://wiki.neo4j.org/content/The_Matrix&lt;/a&gt; We only create names on nodes and types on relationships in our simple sample.&lt;/p&gt; &lt;p&gt;And here is also the code so that you may download and follow along with our explanation your self:&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:F60BB8FA-6F02-4999-8F5E-9DD4E92C4DA7:2ece3c0c-e960-4fb6-bcfc-b89358d619c9" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/Neo4j.NETClientoverHTTPusingRESTandjson_C9DC/Neo4j.POC.zip" target="_self"&gt;Neo4j.POC.zip&lt;/a&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;&lt;br /&gt;First, in accordance with architectural considerations (above) we get hold of our DAL using a factory method. We also create a new Person object and store him (in this case Thomas Anderson – Neo):&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;INeo4jStorageClient &lt;/span&gt;neo4JStorageClient = 
&lt;span style="color: #2b91af"&gt;Neo4jStorageFactory&lt;/span&gt;.GetNodeStorageClient(neo4jServerUri);

neo = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Person&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"Thomas Anderson"&lt;/span&gt;);
neo4JStorageClient.StoreNode(neo);&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;pre /&gt;
&lt;p /&gt;
&lt;p&gt;&lt;font color="#222222" face="Verdana"&gt;As you can see in our business code this is just standard .NET&lt;/font&gt; classes being instantiated and a “Neo” Person is stored in a very normal way. In the business logic code there is no real details on how this storage happens. There is of course a “node” concept and you can consider for your self if that should be hidden away from business code too.&lt;/p&gt;
&lt;p&gt;Since this is a POC the implementation of .StoreNode(neo) is very very trivial. If this were a real client library we would have included things like a library to transform (serialize) the neo-Person node into a json string. Now we just do it statically:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;StoreNode(&lt;span style="color: #2b91af"&gt;Person &lt;/span&gt;person)
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;name = person.Name;

    &lt;span style="color: blue"&gt;string &lt;/span&gt;jsonString = &lt;span style="color: #a31515"&gt;@"{""Name"":""" &lt;/span&gt;+ name + &lt;span style="color: #a31515"&gt;@"""}"&lt;/span&gt;;

    &lt;span style="color: blue"&gt;var &lt;/span&gt;nodeProtocol = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;NodeProtocol&lt;/span&gt;(uri);

    &lt;span style="color: blue"&gt;var &lt;/span&gt;nodeId = nodeProtocol.PostNode(jsonString);
    person.Id = nodeId;
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The interesting part here is the existence of a &lt;span style="color: #2b91af"&gt;NodeProtocol&lt;/span&gt; implementation. I’ve found it useful working with a remote API like Windows Azure Storage and also with this Neo4j REST API to implement methods in C# code that correlate very closely to the API methods available. In our case we have a PostNode operation on the REST API and consequently we have a .PostNode() method in the C# implementation of that HTTP method call.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public int &lt;/span&gt;PostNode(&lt;span style="color: blue"&gt;string &lt;/span&gt;nodeData)
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;uri = &lt;span style="color: #2b91af"&gt;HttpHelper&lt;/span&gt;.ConcatUri(baseUri, &lt;span style="color: #a31515"&gt;"node"&lt;/span&gt;);

    &lt;span style="color: #2b91af"&gt;HttpWebRequest &lt;/span&gt;request =
        &lt;span style="color: #2b91af"&gt;Request&lt;/span&gt;.CreateWebRequest(&lt;span style="color: #2b91af"&gt;HttpWebRequestMethod&lt;/span&gt;.POST, uri);

    &lt;span style="color: #2b91af"&gt;HttpHelper&lt;/span&gt;.AddBody(request, nodeData);

    &lt;span style="color: blue"&gt;var &lt;/span&gt;response = requestMaker.MakeRequest(request);

    &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HttpHelper&lt;/span&gt;.FindNodeId(response.Item3);
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;I won’t bore you with the exact details of all of the helper classes. To dig in to those it is better if you just download the code and look at the details your self.&lt;/p&gt;
&lt;p&gt;As you can see we create a HttpWebRequest, add some headers to it (in the CreateWebRequest method) specifying things like request.Accept = "application/json"; meaning we accept a json formatted response. Also we specify that the body we send out is formatted as json with request.ContentType = "application/json";. Finally we make the call and return the id of the node we just created.&lt;/p&gt;
&lt;p&gt;Again this is a solution bordering on triviality but still it is to the point of making a REST request and receiving a response. Also there is no code to handle any errors present here and the request is done synchronously.&lt;/p&gt;
&lt;p&gt;What does the actual HTTP REST and json request and response look like for this simple sample?&lt;/p&gt;
&lt;p&gt;Request:&lt;/p&gt;&lt;pre class="code"&gt;&lt;p&gt;POST &lt;a href="http://try.neo4j.org:9999/node"&gt;http://try.neo4j.org:9999/node&lt;/a&gt; HTTP/1.1
Content-Type: application/json
Host: try.neo4j.org:9999
Content-Length: 26
Expect: 100-continue
Connection: Keep-Alive&lt;/p&gt;&lt;p&gt;&lt;font style="background-color: #ececec" /&gt;
{"Name":"Thomas Anderson"}&lt;/p&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Response (note that the added line breaks in the response body were added by me):&lt;/p&gt;&lt;pre class="code"&gt;&lt;p&gt;HTTP/1.1 201 Created
server: grizzly/1.8.1
Content-Encoding: UTF-8
Location: &lt;a href="http://try.neo4j.org:9999/node/1"&gt;http://try.neo4j.org:9999/node/1&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Content-Type: application/json
Content-Length: 860
Date: Fri, 16 Apr 2010 08:36:45 GMT

{"incoming typed relationships":&lt;/p&gt;&lt;p&gt;&lt;a href="http://try.neo4j.org:9999/node/1/relationships/in/{-list|&amp;amp;|types}"&gt;http://try.neo4j.org:9999/node/1/relationships/in/{-list|&amp;amp;|types}&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;"incoming relationships":&lt;a href="http://try.neo4j.org:9999/node/1/relationships/in"&gt;http://try.neo4j.org:9999/node/1/relationships/in&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;"all relationships":&lt;a href="http://try.neo4j.org:9999/node/1/relationships/all"&gt;http://try.neo4j.org:9999/node/1/relationships/all&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;"create relationship":&lt;a href="http://try.neo4j.org:9999/node/1/relationships"&gt;http://try.neo4j.org:9999/node/1/relationships&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;"data":{"Name":"Thomas Anderson"},&lt;/p&gt;&lt;p&gt;"traverse":&lt;a href="http://try.neo4j.org:9999/node/1/traverse/{returnType}"&gt;http://try.neo4j.org:9999/node/1/traverse/{returnType}&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;"property":&lt;a href="http://try.neo4j.org:9999/node/1/properties/{key}"&gt;http://try.neo4j.org:9999/node/1/properties/{key}&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;"self":&lt;a href="http://try.neo4j.org:9999/node/1"&gt;http://try.neo4j.org:9999/node/1&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;"properties":&lt;a href="http://try.neo4j.org:9999/node/1/properties"&gt;http://try.neo4j.org:9999/node/1/properties&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;"all typed relationships":&lt;/p&gt;&lt;p&gt;&lt;a href="http://try.neo4j.org:9999/node/1/relationships/all/{-list|&amp;amp;|types}"&gt;http://try.neo4j.org:9999/node/1/relationships/all/{-list|&amp;amp;|types}&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;"outgoing typed relationships":&lt;/p&gt;&lt;p&gt;&lt;a href="http://try.neo4j.org:9999/node/1/relationships/out/{-list|&amp;amp;|types}"&gt;http://try.neo4j.org:9999/node/1/relationships/out/{-list|&amp;amp;|types}&lt;/a&gt;,&lt;/p&gt;&lt;p&gt;"outgoing relationships":&lt;a href="http://try.neo4j.org:9999/node/1/relationships/out"&gt;http://try.neo4j.org:9999/node/1/relationships/out&lt;/a&gt;}
&lt;/p&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;As you can see the Neo4j REST API is self explanatory. Creating a node gives you back a response with all the things you can now do with this new node over the API. Since this quite a lot of data when the only thing that really matters is in the header (Location: &lt;a href="http://try.neo4j.org:9999/node/1"&gt;http://try.neo4j.org:9999/node/1&lt;/a&gt;) I’ve already given feedback to the Neo team that I’d like a simple Uri switch (like APIDocumentation=0) to shut off the detailed response. But in fact I do like that you get so many Uris back that you may use direct. Depending on your needs this is either good or to verbose a payload to get back in your response. As you can see the node we created got the Id set to 1.&lt;/p&gt;
&lt;p&gt;Now we proceed to create all the other nodes and in a very similar fashion the relationships between nodes. Here is the relationship that says that “Neo knows Trinity” (who has a node id of 2). This is a POST operation to /node/1/relationships:&lt;/p&gt;&lt;pre class="code"&gt;&lt;p&gt;POST &lt;a href="http://try.neo4j.org:9999/node/1/relationships"&gt;http://try.neo4j.org:9999/node/1/relationships&lt;/a&gt; HTTP/1.1
Content-Type: application/json
Host: try.neo4j.org:9999
Content-Length: 56
Expect: 100-continue

{"type":"Knows","to":&lt;a href="http://try.neo4j.org:9999/node/2"&gt;http://try.neo4j.org:9999/node/2&lt;/a&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;p&gt;Once the node graph for the Matrix is created we proceed to make a traversal of the graph. We want to find anyone who knows someone throughout the graph.&lt;/p&gt;
&lt;p&gt;The request looks like this in REST (after a similar path through the code as above with a call from business logic to DAL and through to the client lib that makes the actual call using REST):&lt;/p&gt;&lt;pre class="code"&gt;&lt;p&gt;POST &lt;a href="http://try.neo4j.org:9999/node/1/traverse/node"&gt;http://try.neo4j.org:9999/node/1/traverse/node&lt;/a&gt; HTTP/1.1
Accept: application/json
Content-Type: application/json
Host: try.neo4j.org:9999
Content-Length: 57
Expect: 100-continue
Connection: Keep-Alive

{ "relationships": { "type": "Knows" }, "max depth": 10 }&lt;/p&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Again we simplified to a “max depth” of 10 relationships deep into the graph which works fine for our sample.&lt;/p&gt;
&lt;p&gt;The result is a list of all the nodes that match. Because of the self explanatory API mentioned above the result also gives you actions to perform on each node that comes back in the response thus making the response body quite long. Rather than pasting the whole thing here into the blog post you can try it out your self (by downloading the application) and you will get a result exactly like this from the app:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/Neo4j.NETClientoverHTTPusingRESTandjson_C9DC/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/Neo4j.NETClientoverHTTPusingRESTandjson_C9DC/image_thumb_1.png" width="221" height="222" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Neo knows Trinity and Morpheus, who knows Cypher, who in turn knows Agent Smith.&lt;/p&gt;
&lt;h3&gt;Difficulties&lt;/h3&gt;
&lt;p&gt;We had only two real challenges in coding this POC.&lt;/p&gt;
&lt;p&gt;The first one was an issue with streams and the optional so called &lt;a href="http://en.wikipedia.org/wiki/Byte_Order_Mark"&gt;Byte Order Mark (BOM)&lt;/a&gt; that you can put at the head of a stream. I’ve posted on this here: &lt;a href="http://blog.jayway.com/2010/04/14/would-you-like-a-byte-order-mark-to-go-with-that/"&gt;Would you like a Byte Order Mark to go with that?&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The other challenge was that I wrote a small app for Windows to show our results. But the guys on the Neo team are certainly not Windows users. The wanted to show the code on Mono. Just to be sure of compatibility I scaled back to a good old Windows Forms demo app in .NET Framework 3.0. This worked great on Mono which was our intent. But the drawback was that the library I’d found to handle object serialization didn’t respond well to that old version of the framework. That’s why I replaced my lib with static string creations (as you can see in the implementation of the .StoreNode() method above).&lt;/p&gt;
&lt;h3&gt;What have we learned from this?&lt;/h3&gt;
&lt;p&gt;First of all creating a client library for .NET to speak REST and json over HTTP to Neo4j is very doable; quite easy and straight forward.&lt;/p&gt;
&lt;p&gt;Second (and that’s just a word of warning); keep your layers separated. This goes for all applications I guess but if you start to put code to create instances of web requests with web methods and content types and such in your business code you will end up in a very tangled and messy situation very fast which will be a pain and a high cost to maintain.&lt;/p&gt;
&lt;p&gt;Finally we have considered looking into using &lt;a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;WCF data services&lt;/a&gt; (and &lt;a href="http://www.odata.org/"&gt;OData&lt;/a&gt; – the Open Data Protocol) and also possibly &lt;a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx"&gt;Linq&lt;/a&gt; – Language Integrated Query on the client side to make implementations in the library even more powerful and useful. And we do need a object to json serializer library.&lt;/p&gt;
&lt;p&gt;Oh and here is another post on using the Neo4j REST API by the team itself: &lt;a title="http://blog.neo4j.org/2010/04/neo4j-rest-server-part1-get-it-going.html" href="http://blog.neo4j.org/2010/04/neo4j-rest-server-part1-get-it-going.html"&gt;http://blog.neo4j.org/2010/04/neo4j-rest-server-part1-get-it-going.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That’s it!&lt;/p&gt;
&lt;p&gt;We hope this is inspirational enough for you to get going with your own samples or even client libraries in .NET to speak to Neo4j. If you do; please get back in touch with us!&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;M.&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:ab1ff037-9d61-416c-aebd-079d4d638e1f" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Graph+Databases" rel="tag"&gt;Graph Databases&lt;/a&gt;,&lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/REST" rel="tag"&gt;REST&lt;/a&gt;,&lt;a href="http://technorati.com/tags/json" rel="tag"&gt;json&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/307.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/f2-a7GTvL0T6Sc8qvrYiYIXQZGo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/f2-a7GTvL0T6Sc8qvrYiYIXQZGo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/f2-a7GTvL0T6Sc8qvrYiYIXQZGo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/f2-a7GTvL0T6Sc8qvrYiYIXQZGo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Techienotepad/~4/niYy-58AmZ8" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/04/16/neo4j-net-client-over-http-using-rest-and-json.aspx</guid>
            <pubDate>Fri, 16 Apr 2010 09:51:09 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/307.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/04/16/neo4j-net-client-over-http-using-rest-and-json.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/307.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/307.aspx</trackback:ping>
        <coop:keyword>.NET</coop:keyword><coop:keyword>Architecture</coop:keyword><coop:keyword>Patterns &amp; Practices</coop:keyword><coop:keyword>C#</coop:keyword><coop:keyword>TDD</coop:keyword><coop:keyword>Graph Databases</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/04/16/neo4j-net-client-over-http-using-rest-and-json.aspx</feedburner:origLink></item>
        <item>
            <title>Would you like a Byte Order Mark to go with that?</title>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/_xW-7f2kKxA/would-you-like-a-byte-order-mark-to-go-with-that.aspx</link>
            <description>&lt;p&gt;It is possible to encode a little bit of metadata at the beginning of your byte streams to let the stream itself carry information on how it has been encoded. This is known as a &lt;a href="http://en.wikipedia.org/wiki/Byte_Order_Mark"&gt;Byte Order Mark (BOM)&lt;/a&gt; and it is as far as we know completely optional. Some .NET Framework implementations add this BOM to the start of streams given that they are to have a specific &lt;a href="http://en.wikipedia.org/wiki/Encoding"&gt;encoding&lt;/a&gt;. Here is how to find the BOM, and if necessary, remove it from your stream.&lt;/p&gt;  &lt;h3&gt;Context&lt;/h3&gt;  &lt;p&gt;Recently &lt;a href="http://neo4j.org/"&gt;Neo4j&lt;/a&gt; graph database announced a &lt;a href="http://en.wikipedia.org/wiki/REST"&gt;REST&lt;/a&gt; interface. A couple of colleagues at &lt;a href="http://jayway.com"&gt;Jayway&lt;/a&gt; are working on this. This was (very much) a too intriguing temptation to resist. I naturally had to write a small &lt;a href="http://en.wikipedia.org/wiki/Proof_of_concept"&gt;POC&lt;/a&gt; .NET based REST client to be able to talk to that server. (Blog post on this is pending within a few days.) While trying to send REST data in HTTP requests to the server we came across something interesting that caused compatibility issues between the Java and .NET worlds. The culprit was that optional byte order mark that I unintentionally embedded in my streams of bytes. The server did not recognize them. In a joint effort from both sides of the world (.NET client and Java server) we were able to find and identify the problem. Not that this is a new problem - It is very well documented in Wikipedia (link above). However I feel that one more post on this topic with a good test to show what’s going on is warranted.&lt;/p&gt;  &lt;p&gt;A very good tool was also used to nail down the problem; &lt;a href="http://www.fiddler2.com/fiddler2/"&gt;Fiddler2&lt;/a&gt;. This little baby of a program is an http proxy that sits on your machine and listens to your http traffic. Using the tool you can inspect exactly what you are sending over the wire in your http requests and also view the responses coming back. Sweet stuff indeed. This is a MUST tool for you if you develop anything that sends data over HTTP.&lt;/p&gt;  &lt;p&gt;Using Fiddler2 we were able to observe that when sending a stream of bytes as a body for an http POST the byte length, when the request came from my code, was 108 bytes. However when I used the built in request builder in Fiddler2 to reproduce and resend the exact same request it was reported that the length was 105. Why was there a difference in three (3) bytes?&lt;/p&gt;  &lt;p&gt;Problem was that these tree bytes, as I stated above, caused the server to throw a fit. It could not recognize the body of the message to be the expected json string in UTF8 encoding. Json, I’ve learned, is always in UTF8 encoding.&lt;/p&gt;  &lt;p&gt;The java guys &lt;a href="http://www.google.se/#hl=sv&amp;amp;source=hp&amp;amp;q=byte+order+mark&amp;amp;meta=lr%3D&amp;amp;aq=f&amp;amp;aqi=g6&amp;amp;aql=&amp;amp;oq=&amp;amp;gs_rfai=&amp;amp;fp=8132a90bf14f85fd"&gt;googled&lt;/a&gt; and I &lt;a href="http://www.bing.com/search?q=byte+order+mark&amp;amp;go=&amp;amp;form=QBLH&amp;amp;filt=all&amp;amp;qs=n&amp;amp;sk=&amp;amp;sc=1-7"&gt;binged&lt;/a&gt; ;~) and we found that these three bytes is an optional “byte order mark” or BOM. It is not wrong to send these three bytes and it is not wrong not to send them. Guess what? .The .NET world tends to send these extra bytes and everyone else tends not to. Why not have a bit of more incompatibility, right? It’s not like we want to talk to each other anyway!&lt;/p&gt;  &lt;h3&gt;What’s up with the BOM? What is it’s purpose?&lt;/h3&gt;  &lt;p&gt;The BOM is a way to add a kind of metadata inside of a stream of bytes instead of sending some actual metadata along next to your stream. Option one is saying “Here is my byte stream and btw it is in UTF8 encoding”. The other way to do it is saying “Here is my byte stream and if you look at the first three bytes you can read the encoding of it”. Which is better? I can’t say I care all that much other than the fact that it caused us a problem when I tried to send requests from .NET code to the Neo4j server.&lt;/p&gt;  &lt;h3&gt;How can you then – finally – handle these three bytes?&lt;/h3&gt;  &lt;p&gt;Well that depends… on what you intend to do. What I can show you is a piece of code that tells you exactly what this is and then you can copy that behavior into your code and modify it to serve your purpose. The code below should be pretty self explanatory but just to be sure. I take the string &lt;span style="color: #a31515"&gt;"foo"&lt;/span&gt; and encode it into a &lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt; using a &lt;span style="color: #2b91af"&gt;StreamWriter&lt;/span&gt;. The issue is that I tell the stream writer to use &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8 and this is where .NET Framework adds the BOM. The resulting stream is not 3 bytes as perhaps expected. Instead it is 6 bytes long. What you have to do if you read this stream ‘raw’ – by hand – in some library, is skip over the first three bytes. A better way to do it is to use a reader that handles the BOM. Finally if you don’t want to add the BOM in the first place you can write your bytes yourself in a more ‘raw’ fashion byte by byte to the stream. As you can see .NET Framework is good enough to have a way to find an actual BOM for different encodings. The UTF8 encoding does it this way: &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetPreamble() (&lt;a href="http://msdn.microsoft.com/en-us/library/system.text.encoding.getpreamble.aspx"&gt;msdn library link to .GetPremable()&lt;/a&gt;).&lt;/p&gt;  &lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;StreamWriter_with_encoding_writes_ByteOrderMark()
{
    &lt;span style="color: blue"&gt;string &lt;/span&gt;str = &lt;span style="color: #a31515"&gt;"foo"&lt;/span&gt;;

    &lt;span style="color: blue"&gt;var &lt;/span&gt;memStream = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;();

    &lt;span style="color: green"&gt;// Test
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;writer = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StreamWriter&lt;/span&gt;(memStream, &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8);
    writer.Write(str);
    writer.Flush();

    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(6, memStream.Length);

    &lt;span style="color: blue"&gt;var &lt;/span&gt;buffer = &lt;span style="color: blue"&gt;new byte&lt;/span&gt;[3];

    memStream.Position = 0;
    memStream.Read(buffer, 0, 3);
    &lt;span style="color: blue"&gt;string &lt;/span&gt;result = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetString(buffer);

    &lt;span style="color: blue"&gt;var &lt;/span&gt;preamble = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetPreamble();
    &lt;span style="color: blue"&gt;var &lt;/span&gt;byteOrderMarkUtf8 = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetString(preamble);

    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(byteOrderMarkUtf8, result);

    memStream.Position = 3;
    memStream.Read(buffer, 0, 3);
    result = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetString(buffer);

    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(str, result);

    &lt;span style="color: green"&gt;// Alternative: Write the bytes yourself to skip the BOM

    &lt;/span&gt;memStream = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;();

    &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;@byte &lt;span style="color: blue"&gt;in &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.UTF8.GetBytes(str))
    {
        memStream.WriteByte(@byte);
    }

    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(3, memStream.Length);
}&lt;/pre&gt;

&lt;p&gt;You can also if you like compare bytes for the premable one by one rather than converting to a string comparison.&lt;/p&gt;

&lt;p&gt;As you can see this BOM can be handled easily if you like. The thing is I did not have a clue that it was there.&lt;/p&gt;

&lt;p&gt;Oh – and btw now the Neo4j server accepts REST json bodys that both can have and skip the BOM! Good ‘bug’ to solve or ‘feature’ to have.&lt;/p&gt;

&lt;p&gt;Cheers,&lt;/p&gt;

&lt;p&gt;M.&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:396e3ec0-d73d-4bb9-8c29-a94534e3608f" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Arcitecture" rel="tag"&gt;Arcitecture&lt;/a&gt;,&lt;a href="http://technorati.com/tags/json" rel="tag"&gt;json&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Java" rel="tag"&gt;Java&lt;/a&gt;,&lt;a href="http://technorati.com/tags/REST" rel="tag"&gt;REST&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/306.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/2bk3Reaq5d605ey4LBfxepY95sI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2bk3Reaq5d605ey4LBfxepY95sI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/2bk3Reaq5d605ey4LBfxepY95sI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2bk3Reaq5d605ey4LBfxepY95sI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Techienotepad/~4/_xW-7f2kKxA" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/04/14/would-you-like-a-byte-order-mark-to-go-with-that.aspx</guid>
            <pubDate>Wed, 14 Apr 2010 06:46:20 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/306.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/04/14/would-you-like-a-byte-order-mark-to-go-with-that.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/306.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/306.aspx</trackback:ping>
        <coop:keyword>Would you like a Byte Order Mark to go with that?</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/04/14/would-you-like-a-byte-order-mark-to-go-with-that.aspx</feedburner:origLink></item>
    </channel>
</rss>
