<?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>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>
        <item>
            <title>Effective and Infinite storage in the cloud session at TechDays 2010 Sweden</title>
            <category>Architecture</category>
            <category>Windows Azure</category>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/RFjJ4xwh2Kk/effective-and-infinite-storage-in-the-cloud-session-at-techdays-2010-sweden.aspx</link>
            <description>&lt;p&gt;The main points of our Effective and Infinite Storage in the Cloud session at &lt;a href="http://microsoft.se/techdays"&gt;TechDays 2010 in Sweden&lt;/a&gt; are listed below as well as links to all of our related material and the zip with our demos. The slides are here too but they are in Swedish only.&lt;/p&gt;  &lt;p&gt;Our session at TechDays 2010 gave an overview of what storage in the Cloud is, according to Microsoft at present date, and some of the goals for the future in this area. We intended our session to provide the basis for companies to be able to evaluate if and how moving storage to the Cloud can enhance their business for their own specific scenarios.&lt;/p&gt;  &lt;p&gt;Speaking about Cloud storage and Microsoft today means you need to give a session divided into two parts; &lt;a href="http://www.microsoft.com/windowsazure/sqlazure/"&gt;SQL Azure&lt;/a&gt; and &lt;a href="http://www.microsoft.com/windowsazure/windowsazure/"&gt;Windows Azure storage&lt;/a&gt;. And so our presentation had two distinct parts.&lt;/p&gt;  &lt;p&gt;As consultants working with different clients we see that companies in general are not yet ready to adopt Cloud storage on a broad scale. However we also see a continuous increase in interest in this area and we believe that this is directly related to the richness of the offerings and services now becoming available. The story matures in the Microsoft camp and the pace is rapid. Cloud storage from Microsoft is admittedly still very young. (Youngest in the business?) But now Microsoft has thrown the full weight of the company behind making the cloud story stick. Expect to see a lot of new offerings, services, products, frameworks etc. in the Cloud coming out from Microsoft in the near future.&lt;/p&gt;  &lt;p&gt;There are a few common concerns when it comes to storing data in the cloud. A quick (and perhaps not complete) listing follows:&lt;/p&gt;  &lt;h3&gt;Storage cost in the Cloud&lt;/h3&gt;  &lt;p&gt;One big concern we hear from clients and in the buzz at conferences that take place around now is a concern related to the cost of storage and data transfer in the Cloud. This is naturally an equally important issue regardless in what Cloud you choose to store your data.&lt;/p&gt;  &lt;p&gt;The thing about storing your data in the Cloud is that the cost for this storage is a lot more self evident compared to storing your data in your own servers on premise. This is because you receive a bill each month stating exactly what you have spent for storage in the passed period. What we tend to mitigate is that storage, backups, crash plans and managing all of this on premise, or even hosted, does not exactly come for free either. The big question then becomes what difference in cost there will be for your company for storing and transferring data to a Cloud compared to local hardware operated by your local IT department.&lt;/p&gt;  &lt;p&gt;Another aspect of the cost for storage in general is the question of how you build your apps. We believe that many have been a bit oblivious to this issue when they have run their apps and stored our data only on premise or with traditional hosting. Perhaps we have been cost inefficient in this area until now? In the age of Soft Deletes - deleting data does not remove it but rather marks it as deleted – no data is ever actually deleted - managing costs for storing data will become a more and more important question for everyone. Building Cloud applications and storing data in the Cloud has to be done with data storing and management costs in mind. As we hinted to above; the same should have been true for all of your applications up until now. Storing data in the Cloud just makes this point more obvious.&lt;/p&gt;  &lt;h3&gt;Storage Security when your data in the Cloud&lt;/h3&gt;  &lt;p&gt;Perhaps the fist fear when it comes to storing data in the Cloud is the fear of security breeches and the perceived loss of control of this security. Your data is no longer at home.&lt;/p&gt;  &lt;p&gt;What you need to ask yourself, and here we choose to quote a good colleague Sergio Molero &lt;a href="http://twitter.com/sergio_molero"&gt;@sergio_molero&lt;/a&gt;, is; “What sense of security do you have today? What security is it that you think you will loose when storing your data in the Cloud?”&lt;/p&gt;  &lt;p&gt;The same exact thing can be said for hosted data as compared to data stored in the Cloud because the two concepts are very much the same thing in this regard. Data in the Cloud is also hosted data!&lt;/p&gt;  &lt;p&gt;One important thing to also keep in mind is that moving to a data storage solution in the Cloud does not mean all of your data must move “out there” somewhere. It is still quite possible to store the most sensitive data on premise and use Cloud storage for more public data only. We believe that the higher focus on security for data in the Cloud will put higher focus on your security efforts compared to your present security on premise. The move to the Cloud could in conclusion prove to heighten the overall security level for your data.&lt;/p&gt;  &lt;p&gt;Also look into &lt;a href="http://msdn.microsoft.com/en-us/security/aa570351.aspx"&gt;Windows Identity Foundation (WIF)&lt;/a&gt; for insights on how to secure your data in the Cloud.&lt;/p&gt;  &lt;h3&gt;Trusting someone else with your data in the Cloud&lt;/h3&gt;  &lt;p&gt;If security from external attacks to your data in the Cloud is a concern you feel somewhat confident you will be able to handle then perhaps trusting someone else with storing your company sensitive data is a concern that causes you to think twice? Trust is a complex issue not to be taken lightly. We will only raise the concern here and not delve too deep into it.&lt;/p&gt;  &lt;p&gt;Can you trust a hosted data storage provider today? If you can, then this is no difference compared to trusting data stored in the Cloud.&lt;/p&gt;  &lt;p&gt;If you can’t trust anyone else with your data, then the Cloud is not an option for you. Clean and simple.&lt;/p&gt;  &lt;h3&gt;Legal ramifications for storing data in the Cloud&lt;/h3&gt;  &lt;p&gt;It feels important to also comment briefly on the potential legal ramifications for storing your data in a Cloud in another country somewhere. That being said we’d just like to point out that we are no lawyers. Let’s be clear on that.&lt;/p&gt;  &lt;p&gt;Some data cannot legally be moved outside the borders of your own country. Hospital patient information is a good example of this. Other data must for legal reasons stay within your geo-region, say for instance the EU.&lt;/p&gt;  &lt;p&gt;As we hinted to above; not all of your data has this limiting concern. You do have more public data as well as more private data in your business. Only the latter is a potential legal problem. There will be more hybrid solutions for data Storage in the future. Some data for a company will be stored in the Clouds and other data will be stored on premise.&lt;/p&gt;  &lt;p&gt;But how can we store data both on premise and in the Cloud without having two separate data storage solutions? One thing that will assist in this endeavor is the rumored upcoming availability of local on-premise, or hosted, Clouds that will be possible to install locally and behave in the same way the big Clouds out there on the Internet do. Right now there does not exist many offerings of this nature. There will in time when companies start to demand them. In fact, given that the storage Clouds today has a REST interface, it is not very difficult to implement that same interface on your own on premise machines! Doing this however will require a good effort on your part and you will end up with a proprietary solution that you will have to evolve over time as the real Clouds shift. Another option is to wait a bit and let the Cloud vendors come up with packaged solutions that you may install simply install in your own data centers. The hybrid solutions that will result from this will play an important role in shaping the solutions for data storage for the coming decade.&lt;/p&gt;  &lt;h3&gt;SQL Azure&lt;/h3&gt;  &lt;p&gt;In our SQL Azure part of our talk we pointed out three pros and three cons for attendees to take away from our session.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Because Windows Azure takes care of all of the maintenance normally associated with a SQL Server server developers are now allowed to &lt;strong&gt;focus on the database&lt;/strong&gt; rather than performing administrative tasks. &lt;/li&gt;    &lt;li&gt;The database is always available in the sense that upgrades are seamless as well as failover, backup and crash plan which means that the SQL Azure database is &lt;strong&gt;always available&lt;/strong&gt;. &lt;/li&gt;    &lt;li&gt;The &lt;strong&gt;scaling &lt;/strong&gt;story is very strong in SQL Azure. If you can shard your database into many pieces you can scale out from one database to many (say 100) databases to meet a peek load and then back down again to just one at will. For now there is no built in support for this in SQL Azure but this will likely be a story further down the road and you can build it your self today. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;There are some things to consider when looking into deploying to SQL Azure.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Security is locked down &lt;/strong&gt;compared to SQL Server. The sys-tables are not yet available for instance. This is because your databases are co-located on the same servers as data belonging to some one else. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Your data is no longer at home&lt;/strong&gt; in the sense that it is moved outside your company and even outside your country. &lt;/li&gt;    &lt;li&gt;The size right now for one database in SQL Azure is 10 GB but this will increase to 50 GB in June. &lt;/li&gt; &lt;/ol&gt;  &lt;h3&gt;Windows Azure Storage&lt;/h3&gt;  &lt;p&gt;With Windows Azure Storage Microsoft addresses a set of data storage stories that do not really fit well into a standard relational database structure.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;There is a powerful and “infinite” message queuing system that enables messaging between the different machines in your applications. This queuing system does not guarantee 100% ordering of the messages in the queue. This is in case of more than one machine working on the same queue. However the queues require dequeue (GET) and delete (DELETE) in order to complete an actual removal of a message. This method will ensure each message will actually be handled in full in case of some error when decueing and handling the message the first time. &lt;/li&gt;    &lt;li&gt;There is also a concept in Windows Azure of that which is known as a “Big Table”. Say you want to store 200 million user data sets for Facebook or why not all the messages ever sent to Twitter. For this you need an “infinite” tabular structure that has no end that you will ever hit. &lt;/li&gt;    &lt;li&gt;The third thing you can store in Windwos Azure are two kinds of blobs. 1) A so called Block Blob. A blob that is handled in blocks when uploaded. A block Blob is not really intended to be changed once uploaded. You can exchange blocks in the blob but that is not the intent. This is typically a video file or a picture or something that is rarely if ever changed. You would use block blobs to build the next youtube where you can stream files down into a player. 2) A Page Blob. This kind of blob is divided into pages rather than blocks. You can real or write pages at will into this kind of blob. This means that you are able to create a Virtual HardDrive (VHD) and upload it as a page blob to Windows Azure. Then you can actually mount this blob to one or more virtual machines in Windows Azure as an extra hard drive. This comes in handy when moving an application to Windows Azure that uses a lot of disk. All of your old File.Open will continue to work. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;When considering Windows Azure for your storage think about:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;You should use only the parts of Windows Azure that you really need. If you need to store backup blob type of data – use only that. There is a good advantage to consider when you have applications to run that will use Windows Azure Storage. If you deploy your application to Windows Azure as well as the data to &lt;em&gt;the same&lt;/em&gt; datacenter then the internal data transfer costs between this application and storage is without cost. If you instead run your apps on premise and store data in the cloud the transfer of data to and from the cloud carries a cost. &lt;/li&gt;    &lt;li&gt;Regarding the cost you should really make a data storage estimate and analysis before you start up deploying. Your application should have a plan for the amount of data stored, transferred and the duration of storage. This is true on premise too, as discussed above, and data storage plans and budgets will become a more and more important part of any application for the future. &lt;/li&gt;    &lt;li&gt;Windows Azure Storage means learning to use a new API. Perhaps not a dramatic thing but the three storage options also affects the way you build the rest of your application. If you disagree chances are you need to learn more about storing data in the cloud. &lt;/li&gt; &lt;/ol&gt;  &lt;h3&gt;Links&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=AA40F3E2-AFC5-484D-B4E9-6A5227E73590&amp;amp;displaylang=en"&gt;Windows Azure SDK&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=5664019e-6860-4c33-9843-4eb40b297ab6&amp;amp;displaylang=en"&gt;Windows Azure Tools for Visual Studio&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.cerebrata.com/Products/CloudStorageStudio/Default.aspx"&gt;Cloud Storage Studio&lt;/a&gt; – a SSMS look alike for Windows Azure Storage&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.fiddler2.com/fiddler2/"&gt;Fiddler2&lt;/a&gt; – Web debugging proxy&lt;/p&gt;  &lt;p&gt;&lt;a href="http://cloudstorageapi.codeplex.com/"&gt;CloudStorage.API&lt;/a&gt; @ CodePlex&lt;/p&gt;  &lt;p&gt;&lt;a href="http://azurecontrib.codeplex.com/"&gt;Azure Contrib&lt;/a&gt; @ CodePlex&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlazuremw.codeplex.com/"&gt;SQL Azure Migration Wizard&lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;Downloads&lt;/h3&gt;  &lt;p&gt;Ziped version of the demo code in the Windows Azure Queue Storage sample:&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:9c19fcff-f46a-441d-a712-6ce95a7a344b" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/EffectiveandInfinitestorageinthecloudses_CCAA/AzureStorage_1.zip" target="_self"&gt;AzureStorage.zip&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;Slides from the presentation&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:087e5289-4682-409e-a3c3-0216a108b135" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/EffectiveandInfinitestorageinthecloudses_CCAA/EffektivOchOandligLagringIMolnet.ppsx" target="_self"&gt;EffektivOchOandligLagringIMolnet.ppsx&lt;/a&gt;&lt;/div&gt;&lt;/div&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:15e5a745-9a56-4cb1-ba71-56d8bed5ada8" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/EffectiveandInfinitestorageinthecloudses_CCAA/EffektivOchOandligLagringIMolnet.pdf" target="_self"&gt;EffektivOchOandligLagringIMolnet.pdf&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p /&gt;  &lt;p&gt;Slides for the demo attached as a zip above&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:e8fb6368-2809-4064-933b-aa8c8b5ce9c9" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/EffectiveandInfinitestorageinthecloudses_CCAA/DemoWindowsAzureCloudQueue_1.ppsx" target="_self"&gt;DemoWindowsAzureCloudQueue.ppsx&lt;/a&gt;&lt;/div&gt;&lt;/div&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:c5a9b81b-a8ec-49c7-8350-bb0d7b221fd7" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;a href="http://blog.noop.se/images/blog_noop_se/WindowsLiveWriter/EffectiveandInfinitestorageinthecloudses_CCAA/DemoWindowsAzureCloudQueue.pdf" target="_self"&gt;DemoWindowsAzureCloudQueue.pdf&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p /&gt;  &lt;h3&gt;Final words&lt;/h3&gt;  &lt;p&gt;We would like to thank Microsoft for arranging TechDays, we had a blast! Thanks to the audience for listening.&lt;/p&gt;  &lt;p&gt;Cheers,&lt;/p&gt;  &lt;p&gt;Peter von Lochow &lt;a href="http://twitter.com/vonlochow"&gt;@vonlochow&lt;/a&gt; and Magnus Mårtensson &lt;a href="http://twitter.com/noopman"&gt;@noopman&lt;/a&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:676fb488-651a-440a-a552-c2ac975454b5" 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/SQL+Azure" rel="tag"&gt;SQL Azure&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Cloud" rel="tag"&gt;Cloud&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Microsoft" rel="tag"&gt;Microsoft&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/305.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/vLLbuY5O7tlCgG_PQRVwSCuQbdE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vLLbuY5O7tlCgG_PQRVwSCuQbdE/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/vLLbuY5O7tlCgG_PQRVwSCuQbdE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vLLbuY5O7tlCgG_PQRVwSCuQbdE/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/RFjJ4xwh2Kk" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/04/06/effective-and-infinite-storage-in-the-cloud-session-at-techdays-2010-sweden.aspx</guid>
            <pubDate>Tue, 06 Apr 2010 20:31:07 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/305.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/04/06/effective-and-infinite-storage-in-the-cloud-session-at-techdays-2010-sweden.aspx#feedback</comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/305.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/305.aspx</trackback:ping>
        <coop:keyword>Architecture</coop:keyword><coop:keyword>Windows Azure</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/04/06/effective-and-infinite-storage-in-the-cloud-session-at-techdays-2010-sweden.aspx</feedburner:origLink></item>
        <item>
            <title>Microsoft Enterprise Library 5.0 and Microsoft Unity 2.0 Beta 1 released</title>
            <category>Architecture</category>
            <category>Patterns &amp; Practices</category>
            <category>Microsoft</category>
            <link>http://feeds.noop.se/~r/Techienotepad/~3/68_3p-VRkic/microsoft-enterprise-library-5.0-and-microsoft-unity-2.0-beta-1.aspx</link>
            <description>&lt;p&gt;There are a whole bunch of changes in the just released &lt;a href="http://entlib.codeplex.com/wikipage?title=EntLib5Beta1ChangeLog"&gt;Microsoft Enterprise Library 5.0 Beta 1&lt;/a&gt; and in &lt;a href="http://unity.codeplex.com/wikipage?title=Unity2Beta1ChangeLog&amp;amp;referringTitle=Unity2%20Beta1"&gt;Microsoft Unity 2.0 Beta 1&lt;/a&gt;. I have been using these libraries for years and have greatly been looking forward to this new release.&lt;/p&gt;  &lt;p&gt;The list of changes for Enterprise Library is very long indeed and going into specific changes here on this page seems redundant when you can go check it out for your self.&lt;/p&gt;  &lt;p&gt;Regarding Unity it seems it is not required any more to reference the ObjectBuilder assembly when using Unity; any functionality previously required from this assembly are now included in the Unity assembly. Nice.&lt;/p&gt;  &lt;p&gt;Cheers,&lt;/p&gt;  &lt;p&gt;Magnus&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:c1e0181d-e7d4-41af-a7d3-617d1ba8d3ef" 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/Patterns+%26+Practices" rel="tag"&gt;Patterns &amp;amp; Practices&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Arcitecture" rel="tag"&gt;Arcitecture&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.noop.se/aggbug/303.aspx" width="1" height="1" /&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/jZRUxlyUENErOKpHQQiC4hI3gbM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jZRUxlyUENErOKpHQQiC4hI3gbM/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/jZRUxlyUENErOKpHQQiC4hI3gbM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jZRUxlyUENErOKpHQQiC4hI3gbM/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/68_3p-VRkic" height="1" width="1"/&gt;</description>
            <dc:creator>Magnus Mårtensson</dc:creator>
            <guid isPermaLink="false">http://blog.noop.se/archive/2010/02/10/microsoft-enterprise-library-5.0-and-microsoft-unity-2.0-beta-1.aspx</guid>
            <pubDate>Wed, 10 Feb 2010 18:14:28 GMT</pubDate>
            <wfw:comment>http://blog.noop.se/comments/303.aspx</wfw:comment>
            <comments>http://blog.noop.se/archive/2010/02/10/microsoft-enterprise-library-5.0-and-microsoft-unity-2.0-beta-1.aspx#feedback</comments>
            <wfw:commentRss>http://blog.noop.se/comments/commentRss/303.aspx</wfw:commentRss>
            <trackback:ping>http://blog.noop.se/services/trackbacks/303.aspx</trackback:ping>
        <coop:keyword>Architecture</coop:keyword><coop:keyword>Patterns &amp; Practices</coop:keyword><coop:keyword>Microsoft</coop:keyword><feedburner:origLink>http://blog.noop.se/archive/2010/02/10/microsoft-enterprise-library-5.0-and-microsoft-unity-2.0-beta-1.aspx</feedburner:origLink></item>
    </channel>
</rss>
