<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>MICHEL'S WEBLOG</title>
  <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/" />
  <link rel="self" href="http://www.michelvandervlugt.net/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2010-08-20T14:37:33.5438841+02:00</updated>
  <author>
    <name>Michel van der Vlugt</name>
  </author>
  <subtitle>MICHELVANDERVLUGT.NET</subtitle>
  <id>http://www.michelvandervlugt.net/</id>
  <generator uri="http://www.dasblog.net" version="1.9.6264.0">DasBlog</generator>
  <entry>
    <title>Windows Azure Table Storage in the development environment, part II</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,9ecd462b-ee39-4f6a-bc61-72e66fb5ef6b.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,9ecd462b-ee39-4f6a-bc61-72e66fb5ef6b.aspx</id>
    <published>2010-08-20T14:33:32.4580446+02:00</published>
    <updated>2010-08-20T14:33:32.4580446+02:00</updated>
    <category term=".NET" label=".NET" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET.aspx" />
    <category term="Windows Azure" label="Windows Azure" scheme="http://www.michelvandervlugt.net/CategoryView,category,Windows%2BAzure.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/WindowsAzureTableStorageinthedevelopment_A7CA/image_2.png">
            <img style="border-right-width: 0px; margin: 0px 0px 0px 5px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" align="right" src="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/WindowsAzureTableStorageinthedevelopment_A7CA/image_thumb.png" width="304" height="304" />
          </a>
        </p>
        <p>
In addition to my previous post <a href="http://www.michelvandervlugt.net/PermaLink,guid,d7f7cf98-fd3d-4830-9620-aa5f9644639f.aspx" target="_blank">Windows
Azure Table Storage in the development environment</a> I’ve created a demo application
in which responsibilities are distributed over classes in a way that hides the implementation
details of Azure’s table storage.
</p>
        <p>
The application is pretty simple, a one page ASP.NET app in which you can add/edit
movies. The movies are displayed on the same page in a GridView.
</p>
        <p>
          <strong>Movie model</strong>
          <br />
The model has changed a bit. The RowKey is now filled with a GUID (instead of the
title) to uniquely identify a movie in the table. The title now has a dedicated property.
The PartitionKey remains the movie category as this helps Azure to partition the data
over multiple servers if the load gets too heavy.
</p>
        <p>
          <strong>StorageContext</strong>
          <br />
        </p>
        <p>
The StorageContext is the actual entry point to the data. It inherits from TableServiceContext
and provides a property Movies that returns an IQueryable of type Movie, which can
be used to query the data from storage. It also provides an AddMovie method to avoid
exposure of the table name that is required for using TableServiceContext.AddObject.
</p>
        <p>
To create a new StorageContext the StorageContextFactory can be used, which hides
the creation/initialization of the StorageContext.
</p>
        <p>
          <strong>MovieRepository</strong>
          <br />
The repository is used to access the data from the application. Creation of a MovieRepository
requires a context that implements IStorageContext (e.g. StorageContext), which can
be obtained by the StorageContextFactory. The reason for using an interface to the
context is to be able to replace the context by some other implementation, for example
in case of a non-Azure application or in case of unit-testing.
</p>
        <p>
          <strong>Example</strong>
        </p>
        <p>
        </p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>MovieRepository repository
= </pre>
          <pre>
            <span class="lnum"> 2: </span>
            <span class="kwrd">new</span> MovieRepository(StorageContextFactory.GetContext());</pre>
          <pre>
            <span class="lnum"> 3: </span> </pre>
          <pre>
            <span class="lnum"> 4: </span>
            <span class="rem">// Get all movies</span>
          </pre>
          <pre>
            <span class="lnum"> 5: </span>IEnumerable&lt;Movie&gt; allMovies = repository.GetAll();</pre>
          <pre>
            <span class="lnum"> 6: </span> </pre>
          <pre>
            <span class="lnum"> 7: </span>
            <span class="rem">// Get a single movie</span>
          </pre>
          <pre>
            <span class="lnum"> 8: </span>Movie movie = repository.GetById(key);</pre>
          <pre>
            <span class="lnum"> 9: </span> </pre>
          <pre>
            <span class="lnum"> 10: </span>
            <span class="rem">// Add a new movie</span>
          </pre>
          <pre>
            <span class="lnum"> 11: </span>Movie newMovie = MovieFactory.Create();</pre>
          <pre>
            <span class="lnum"> 12: </span>newMovie.PartitionKey = <span class="str">"Action"</span>;</pre>
          <pre>
            <span class="lnum"> 13: </span>newMovie.Title = <span class="str">"An action
movie!"</span>;</pre>
          <pre>
            <span class="lnum"> 14: </span>repository.Add(newMovie);</pre>
        </div>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <br />
A few notes: 
<p></p><ul><li>
A MovieFactory is used to create a new Movie. In this factory an instance of a Movie
is created and the RowKey property is populated with a new GUID. There are several
alternatives but in this way you avoid unnecessary creation of GUIDs when objects
are created by the context (in case you initialize in the Movie’s constructor), and
you avoid the need for an initialization method that can potentially be forgotten
to invoke.</li><li>
The Add and Update methods on repository directly save to the storage. This might
not be what you want in other apps, but for this example (with only one update in
each postback) it is good enough.</li></ul><p>
The source of the example can be downloaded <a href="http://michelvandervlugt.net/content/binary/CloudServiceDemo.zip" target="_blank">here</a>.
</p><img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=9ecd462b-ee39-4f6a-bc61-72e66fb5ef6b" /></div>
    </content>
  </entry>
  <entry>
    <title>Windows Azure Table Storage in the development environment</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,d7f7cf98-fd3d-4830-9620-aa5f9644639f.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,d7f7cf98-fd3d-4830-9620-aa5f9644639f.aspx</id>
    <published>2010-08-20T01:36:47.6434761+02:00</published>
    <updated>2010-08-20T14:37:33.5438841+02:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Lately I’ve been playing around with Microsoft Azure (for now in the offline development
environment only) and it is really easy to get started. However, as easy it is to
get your ASP.NET app running, it is a bit more difficult to get access to the storage
part (blobs, table storage and queues). There is no straight forward how-to available,
at least I couldn’t find one. So I’ve combined some of the resources I’ve found (e.g.
the example from <a href="http://msdn.microsoft.com/en-us/magazine/ff796231.aspx" target="_blank">Windows
Azure Table Storage – Not Your Father’s Database, by Julie Lerman</a> and the PDC09
presentation <a href="http://www.microsoftpdc.com/2009/svc09" target="_blank">Windows
Azure Tables and Queues Deep Dive, by Jai Haridas</a>) and here it is…
</p>
        <p>
          <strong>Step 1</strong>
          <br />
Create a new ‘Windows Azure Cloud Service’ project and name it something like CloudServiceDemo,
hit ok. Then select an ‘ASP.NET Web Role’ to be included in your cloud service solution
and again hit Ok. Now two projects are created, one with roles and configuration and
one with the actual ASP.NET web site.
</p>
        <p>
When you run this the ‘Windows Azure Simulation Environment’ starts and your web application
appears in your browser (note: at this moment my Queue storage fails with an error
that it is being used by another process, something which I haven’t figured out yet).
</p>
        <p>
          <strong>Step 2</strong>
          <br />
Change two configuration files:
</p>
        <ol>
          <li>
In the ServiceDefinition.csdef, add line 3 in the configuration settings: <blockquote><div class="csharpcode"><pre><span class="lnum"> 1: </span>&lt;ConfigurationSettings&gt;</pre><pre><span class="lnum"> 2: </span> &lt;Setting name=<span class="str">"DiagnosticsConnectionString"</span> /&gt;</pre><pre><span class="lnum"> 3: </span> &lt;Setting name=<span class="str">"DataConnectionString"</span> /&gt;</pre><pre><span class="lnum"> 4: </span>&lt;/ConfigurationSettings&gt;</pre></div><style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style></blockquote></li>
          <li>
In the ServiceConfiguration.cscfg, add line 3 in the configuration settings: <blockquote><div class="csharpcode"><pre><span class="lnum"> 1: </span>&lt;ConfigurationSettings&gt;</pre><pre><span class="lnum"> 2: </span> &lt;Setting name=<span class="str">"DiagnosticsConnectionString"</span><span class="kwrd">value</span>=<span class="str">"UseDevelopmentStorage=true"</span> /&gt;</pre><pre><span class="lnum"> 3: </span> &lt;Setting name=<span class="str">"DataConnectionString"</span><span class="kwrd">value</span>=<span class="str">"UseDevelopmentStorage=true"</span> /&gt;</pre><pre><span class="lnum"> 4: </span>&lt;/ConfigurationSettings&gt;</pre></div><style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style></blockquote></li>
        </ol>
        <p>
          <strong>Step 3</strong>
          <br />
Add the following code to the OnStart method of the WebRole.cs
</p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>
            <span class="rem">// Setup
a handler to update CloudStorageAccount instances when the</span>
          </pre>
          <pre>
            <span class="lnum"> 2: </span>
            <span class="rem">// configuration settings change
in the service configuration file.</span>
          </pre>
          <pre>
            <span class="lnum"> 3: </span>CloudStorageAccount.SetConfigurationSettingPublisher(</pre>
          <pre>
            <span class="lnum"> 4: </span> (configName, configSetter) =&gt;</pre>
          <pre>
            <span class="lnum"> 5: </span> {</pre>
          <pre>
            <span class="lnum"> 6: </span>
            <span class="rem">// Provide the configSetter
with the initial value</span>
          </pre>
          <pre>
            <span class="lnum"> 7: </span> configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));</pre>
          <pre>
            <span class="lnum"> 8: </span> </pre>
          <pre>
            <span class="lnum"> 9: </span> RoleEnvironment.Changed +=</pre>
          <pre>
            <span class="lnum"> 10: </span> (sender, arg) =&gt;</pre>
          <pre>
            <span class="lnum"> 11: </span> {</pre>
          <pre>
            <span class="lnum"> 12: </span>
            <span class="kwrd">if</span> (arg.Changes.OfType&lt;RoleEnvironmentConfigurationSettingChange&gt;()</pre>
          <pre>
            <span class="lnum"> 13: </span> .Any((change) =&gt; (change.ConfigurationSettingName
== configName)))</pre>
          <pre>
            <span class="lnum"> 14: </span> {</pre>
          <pre>
            <span class="lnum"> 15: </span>
            <span class="rem">// The corresponding configuration
setting has changed, propagate the value</span>
          </pre>
          <pre>
            <span class="lnum"> 16: </span>
            <span class="kwrd">if</span> (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))</pre>
          <pre>
            <span class="lnum"> 17: </span> {</pre>
          <pre>
            <span class="lnum"> 18: </span>
            <span class="rem">// In this case, the change
to the storage account credentials in the</span>
          </pre>
          <pre>
            <span class="lnum"> 19: </span>
            <span class="rem">// service configuration is
significant enough that the role needs to be</span>
          </pre>
          <pre>
            <span class="lnum"> 20: </span>
            <span class="rem">// recycled in order to use
the latest settings. (for example, the </span>
          </pre>
          <pre>
            <span class="lnum"> 21: </span>
            <span class="rem">// endpoint has changed)</span>
          </pre>
          <pre>
            <span class="lnum"> 22: </span> RoleEnvironment.RequestRecycle();</pre>
          <pre>
            <span class="lnum"> 23: </span> }</pre>
          <pre>
            <span class="lnum"> 24: </span> }</pre>
          <pre>
            <span class="lnum"> 25: </span> };</pre>
          <pre>
            <span class="lnum"> 26: </span> }</pre>
          <pre>
            <span class="lnum"> 27: </span>);</pre>
        </div>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <p>
        </p>
        <p>
          <strong>Step 4</strong>
          <br />
Create a class that represents the data in the table storage (you need to add a reference
to System.Data.Services.Client)
</p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>
            <span class="rem">/// &lt;summary&gt;</span>
          </pre>
          <pre>
            <span class="lnum"> 2: </span>
            <span class="rem">/// Representation of a movie</span>
          </pre>
          <pre>
            <span class="lnum"> 3: </span>
            <span class="rem">/// &lt;/summary&gt;</span>
          </pre>
          <pre>
            <span class="lnum"> 4: </span>
            <span class="rem">/// &lt;remarks&gt;PartitionKey,
RowKey and TimeStamp are required for Azure table storage.&lt;/remarks&gt;</span>
          </pre>
          <pre>
            <span class="lnum"> 5: </span>[DataServiceKey(<span class="str">"PartitionKey"</span>, <span class="str">"RowKey"</span>)]</pre>
          <pre>
            <span class="lnum"> 6: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">class</span> Movie</pre>
          <pre>
            <span class="lnum"> 7: </span>{</pre>
          <pre>
            <span class="lnum"> 8: </span>
            <span class="rem">/// &lt;summary&gt;</span>
          </pre>
          <pre>
            <span class="lnum"> 9: </span>
            <span class="rem">/// Category of the movie, e.g.
Action, SciFi, etc.</span>
          </pre>
          <pre>
            <span class="lnum"> 10: </span>
            <span class="rem">/// &lt;/summary&gt;</span>
          </pre>
          <pre>
            <span class="lnum"> 11: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">string</span> PartitionKey
{ get; set; }</pre>
          <pre>
            <span class="lnum"> 12: </span> </pre>
          <pre>
            <span class="lnum"> 13: </span>
            <span class="rem">/// &lt;summary&gt;</span>
          </pre>
          <pre>
            <span class="lnum"> 14: </span>
            <span class="rem">/// Title of the movie</span>
          </pre>
          <pre>
            <span class="lnum"> 15: </span>
            <span class="rem">/// &lt;/summary&gt;</span>
          </pre>
          <pre>
            <span class="lnum"> 16: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">string</span> RowKey
{ get; set; }</pre>
          <pre>
            <span class="lnum"> 17: </span> </pre>
          <pre>
            <span class="lnum"> 18: </span>
            <span class="kwrd">public</span> DateTime TimeStamp
{ get; set; }</pre>
          <pre>
            <span class="lnum"> 19: </span> </pre>
          <pre>
            <span class="lnum"> 20: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">int</span> ReleaseYear
{ get; set; }</pre>
          <pre>
            <span class="lnum"> 21: </span>}</pre>
        </div>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <p>
        </p>
        <p>
          <strong>Step 5</strong>
          <br />
Now you can create the ‘Movie’ table using a CloudStorageAccount 
</p>
        <p>
        </p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>CloudStorageAccount storageAccount
=</pre>
          <pre>
            <span class="lnum"> 2: </span> CloudStorageAccount.FromConfigurationSetting(<span class="str">"DataConnectionString"</span>);</pre>
          <pre>
            <span class="lnum"> 3: </span> </pre>
          <pre>
            <span class="lnum"> 4: </span>storageAccount.CreateCloudTableClient().CreateTableIfNotExist(<span class="str">"Movies"</span>);</pre>
        </div>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <p>
        </p>
        <p>
          <strong>Step 6</strong>
          <br />
And access the storage using a TableServiceContext
</p>
        <p>
        </p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>
            <span class="rem">// Create
a context</span>
          </pre>
          <pre>
            <span class="lnum"> 2: </span>TableServiceContext context = </pre>
          <pre>
            <span class="lnum"> 3: </span>
            <span class="kwrd">new</span> TableServiceContext(storageAccount.TableEndpoint.ToString(),
storageAccount.Credentials);</pre>
          <pre>
            <span class="lnum"> 4: </span> </pre>
          <pre>
            <span class="lnum"> 5: </span>
            <span class="rem">// Add movie</span>
          </pre>
          <pre>
            <span class="lnum"> 6: </span>Movie newMovie = </pre>
          <pre>
            <span class="lnum"> 7: </span>
            <span class="kwrd">new</span> Movie { PartitionKey
= <span class="str">"Action"</span>, RowKey = <span class="str">"An action movie"</span>,
ReleaseYear = 2005 };</pre>
          <pre>
            <span class="lnum"> 8: </span>context.AddObject(<span class="str">"Movies"</span>,
newMovie);</pre>
          <pre>
            <span class="lnum"> 9: </span>context.SaveChanges();</pre>
          <pre>
            <span class="lnum"> 10: </span>
          </pre>
          <pre>
            <span class="lnum"> 11: </span>
            <span class="rem">// Get movies</span>
          </pre>
          <pre>
            <span class="lnum"> 12: </span>IEnumerable&lt;Movie&gt; movies = from m <span class="kwrd">in</span> context.CreateQuery&lt;Movie&gt;(<span class="str">"Movies"</span>)</pre>
          <pre>
            <span class="lnum"> 13: </span>
            <span class="kwrd">where</span> m.PartitionKey
== <span class="str">"Action"</span></pre>
          <pre>
            <span class="lnum"> 14: </span> select m;</pre>
        </div>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <br />
        <p>
Of course this is just a start, now the code has to be placed into specific classes
dividing the responsibilities. Something like a StorageContext (which inherits from
TableServiceContext) to encapsulate the tablename and the context.CreateQuery, and
a MovieRepository to encapsulate the context and the get-, add- and update-methods…
</p>
        <p>
Update: see <a href="http://www.michelvandervlugt.net/PermaLink,guid,9ecd462b-ee39-4f6a-bc61-72e66fb5ef6b.aspx" target="_blank">Windows
Azure Table Storage in the development environment, part II</a> for an example.
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=d7f7cf98-fd3d-4830-9620-aa5f9644639f" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Fluent test data builder for unit tests</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,43497857-6369-48f6-9e36-64db53800f08.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,43497857-6369-48f6-9e36-64db53800f08.aspx</id>
    <published>2010-07-26T15:29:00+02:00</published>
    <updated>2010-07-27T11:42:07.4936714+02:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
A while ago I came across the presentation <a href="http://www.infoq.com/presentations/Sustainable-Test-Driven-Development" target="_blank">Sustainable
Test-Driven Development by Steve Freeman</a>. In this great presentation Steve talks
about writing unit-tests and how to avoid the danger of them growing to huge, complex,
hard to read and hard to maintain pieces of code. One of the solutions he talked about
was using ‘test data builders’ to compose an object graph of test data in a fluent
way, which greatly improves the readability of the unit-test. For example:
</p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>Order o = anOrder()</pre>
          <pre>
            <span class="lnum"> 2: </span> .WithOrderDate(DateTime.Now)</pre>
          <pre>
            <span class="lnum"> 3: </span> .WithCustomer(aCustomer()</pre>
          <pre>
            <span class="lnum"> 4: </span> .WithName(<span class="str">"John Doe"</span>)</pre>
          <pre>
            <span class="lnum"> 5: </span> .WithCity(<span class="str">"Somewhere"</span>)</pre>
          <pre>
            <span class="lnum"> 6: </span> .Build())</pre>
          <pre>
            <span class="lnum"> 7: </span> .WithOrderLine(anOrderLine()</pre>
          <pre>
            <span class="lnum"> 8: </span> .WithProduct(aProduct()</pre>
          <pre>
            <span class="lnum"> 9: </span> .WithDescription(<span class="str">"Windows Phone
7"</span>)</pre>
          <pre>
            <span class="lnum"> 10: </span> .WithPrice(499)</pre>
          <pre>
            <span class="lnum"> 11: </span> .Build())</pre>
          <pre>
            <span class="lnum"> 12: </span> .WithQuantity(2)</pre>
          <pre>
            <span class="lnum"> 13: </span> .Build())</pre>
          <pre>
            <span class="lnum"> 14: </span> .Build();</pre>
        </div>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <br />
        <p>
My first thought was: great, but do we really need this in C# (the presentation was
Java) since we have the new type initialization introduced in C# 3.5? Creating the
same object graph using C# is very similar, e.g.:
</p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>Order o = <span class="kwrd">new</span> Order()</pre>
          <pre>
            <span class="lnum"> 2: </span> {</pre>
          <pre>
            <span class="lnum"> 3: </span> OrderDate = DateTime.Now,</pre>
          <pre>
            <span class="lnum"> 4: </span> Customer = <span class="kwrd">new</span> Customer()</pre>
          <pre>
            <span class="lnum"> 5: </span> {</pre>
          <pre>
            <span class="lnum"> 6: </span> Name = <span class="str">"John Doe"</span>,</pre>
          <pre>
            <span class="lnum"> 7: </span> City = <span class="str">"Somewhere"</span></pre>
          <pre>
            <span class="lnum"> 8: </span> },</pre>
          <pre>
            <span class="lnum"> 9: </span> OrderLines = <span class="kwrd">new</span> List&lt;OrderLine&gt;()</pre>
          <pre>
            <span class="lnum"> 10: </span> { </pre>
          <pre>
            <span class="lnum"> 11: </span>
            <span class="kwrd">new</span> OrderLine()</pre>
          <pre>
            <span class="lnum"> 12: </span> {</pre>
          <pre>
            <span class="lnum"> 13: </span> Product = <span class="kwrd">new</span> Product()</pre>
          <pre>
            <span class="lnum"> 14: </span> {</pre>
          <pre>
            <span class="lnum"> 15: </span> Description = <span class="str">"Windows Phone
7"</span>,</pre>
          <pre>
            <span class="lnum"> 16: </span> Price = 499</pre>
          <pre>
            <span class="lnum"> 17: </span> },</pre>
          <pre>
            <span class="lnum"> 18: </span> Quantity = 2</pre>
          <pre>
            <span class="lnum"> 19: </span> }</pre>
          <pre>
            <span class="lnum"> 20: </span> }</pre>
          <pre>
            <span class="lnum"> 21: </span> };</pre>
        </div>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <br />
        <p>
However, this means that you have to make the set-method of the OrderLine collection
public. If you don’t want that (or can’t because you use something like EF, in which
collections are created internally) you end up with creating a new order and adding
each new order line afterwards.
</p>
        <p>
Which syntax is better? That is really a matter of personal opinion, but the builder
solution comes with additional benefits like:
</p>
        <ul>
          <li>
default values of properties (e.g. required values) can be set in the builder. This
means that a unit-test only has to set those properties of an object that are relevant
for the specific test, not bothering with other properties. This makes the test smaller
and more readable; 
</li>
          <li>
builders can be extended with all sorts of With… methods. For example a method like
WithInvalidOrderDate(), which encapsulates an order date of 1990/1/1. This is more
readable than .WithOrderDate(new DateTime(1990, 1, 1)) because the method explains
to the reader of the test that the order has an invalid order date.</li>
        </ul>
        <p>
          <em>T4</em>
        </p>
        <p>
The downside of these builders is of course that they have to be coded, which is not
the most fun part and decreases your productivity. Therefore I’ve started a T4 (Text
Template Transformation Toolkit) template that generates a (partial) builder for each
domain class. To extent a builder you can create a new partial class with the same
name. Do not change the generated files because they will be overwritten by the generator.
</p>
        <p>
Note that this is only the first version of the template. The next version will include
better implementation for compiling the source files, support for other collections
than ICollection&lt;T&gt; only and a real implementation of singularizing the collection
name.
</p>
        <p>
          <em>Conclusion</em>
        </p>
        <p>
I’ve used these builders in a several projects now and I must say that it really adds
value to the readability and quality of the unit tests. The additional time required
to create the builders is a small price for what you get in return, especially when
you use a code generator.
</p>
        <p>
          <em>Download</em>
        </p>
        <p>
The sample project, including the T4 template, can be downloaded from this link: <a href="http://michelvandervlugt.net/content/binary/FluentTestDataBuilderExampleWithT4.zip" target="_blank">FluentTestDataBuilderExampleWithT4.zip</a>.
</p>
        <p>
UPDATE 27/7/2010: new version of the T4 template that includes: bugfixing, refactoring
and use of EF’s PluralizationService: <a href="http://michelvandervlugt.net/content/binary/GenerateTestDataBuilders.0.2.zip" target="_blank">GenerateTestDataBuilders.tt
v0.2</a>.
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=43497857-6369-48f6-9e36-64db53800f08" />
      </div>
    </content>
  </entry>
  <entry>
    <title>WPF: Document binding with WebBrowser control</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,a31d5ba1-cf6a-46c7-a4ff-7cecd7308fcc.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,a31d5ba1-cf6a-46c7-a4ff-7cecd7308fcc.aspx</id>
    <published>2010-07-01T12:47:40.0875442+02:00</published>
    <updated>2010-07-01T23:35:20.5546992+02:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
The WebBrowser control in WPF is an excellent control for embedding a web browser
in your WPF application. If you want to show a HTML document in your WPF app this
control is also the way to go. However, in this current version of the control (up
until WPF 4) it is not possible to use databinding because there is no dependency
property available that you can use. The only possibility is invoking the method NavigateToString(string)
somewhere in code. 
</p>
        <p>
Although this is working it doesn’t fit nicely in the currently popular MVVM pattern,
in which everything (at least as much as possible) should be done using databinding.
Moreover, when you want to display a browser control in every item of a listbox, a
databinding solution is much simpler.
</p>
        <p>
One of the solutions to solve this problem is to create a new custom control that
derives from the WebBrowser control and adds a new dependency property to which you
can databind your document. When the property changes you can simply invoke NavigateToString(string).
</p>
        <p>
Another solution is using WPF’s ‘Attached Properties’ which can be used to extend
existing controls with new properties and/or behaviors. In the code below I’ve created
a new dependency property called Document. When this property changes the event handler
DocumentPropertyChanged is executed that invokes the method NavigateToString(string)
of the WebBrowser. This causes to control to display the document.
</p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">static</span>
            <span class="kwrd">class</span> WebBrowserExtentions</pre>
          <pre>
            <span class="lnum"> 2: </span>{</pre>
          <pre>
            <span class="lnum"> 3: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">static</span>
            <span class="kwrd">readonly</span> DependencyProperty
DocumentProperty =</pre>
          <pre>
            <span class="lnum"> 4: </span> DependencyProperty.RegisterAttached(<span class="str">"Document"</span>, <span class="kwrd">typeof</span>(<span class="kwrd">string</span>), <span class="kwrd">typeof</span>(WebBrowserExtentions), </pre>
          <pre>
            <span class="kwrd">new</span> UIPropertyMetadata(<span class="kwrd">null</span>,
DocumentPropertyChanged));</pre>
          <pre>
            <span class="lnum"> 5: </span> </pre>
          <pre>
            <span class="lnum"> 6: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">static</span>
            <span class="kwrd">string</span> GetDocument(DependencyObject
element)</pre>
          <pre>
            <span class="lnum"> 7: </span> {</pre>
          <pre>
            <span class="lnum"> 8: </span>
            <span class="kwrd">return</span> (<span class="kwrd">string</span>)element.GetValue(DocumentProperty);</pre>
          <pre>
            <span class="lnum"> 9: </span> }</pre>
          <pre>
            <span class="lnum"> 10: </span> </pre>
          <pre>
            <span class="lnum"> 11: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">static</span>
            <span class="kwrd">void</span> SetDocument(DependencyObject
element, <span class="kwrd">string</span><span class="kwrd">value</span>)</pre>
          <pre>
            <span class="lnum"> 12: </span> {</pre>
          <pre>
            <span class="lnum"> 13: </span> element.SetValue(DocumentProperty, <span class="kwrd">value</span>);</pre>
          <pre>
            <span class="lnum"> 14: </span> }</pre>
          <pre>
            <span class="lnum"> 15: </span> </pre>
          <pre>
            <span class="lnum"> 16: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">static</span>
            <span class="kwrd">void</span> DocumentPropertyChanged(DependencyObject
target, DependencyPropertyChangedEventArgs e)</pre>
          <pre>
            <span class="lnum"> 17: </span> {</pre>
          <pre>
            <span class="lnum"> 18: </span> WebBrowser browser = target <span class="kwrd">as</span> WebBrowser;</pre>
          <pre>
            <span class="lnum"> 19: </span>
            <span class="kwrd">if</span> (browser != <span class="kwrd">null</span>)</pre>
          <pre>
            <span class="lnum"> 20: </span> {</pre>
          <pre>
            <span class="lnum"> 21: </span>
            <span class="kwrd">string</span> document = e.NewValue <span class="kwrd">as</span><span class="kwrd">string</span>;</pre>
          <pre>
            <span class="lnum"> 22: </span> browser.NavigateToString(document);</pre>
          <pre>
            <span class="lnum"> 23: </span> }</pre>
          <pre>
            <span class="lnum"> 24: </span> }</pre>
          <pre>
            <span class="lnum"> 25: </span>}</pre>
        </div>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <p>
 
</p>
        <p>
To use this new property in XAML you can simply do the following:
</p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>&lt;WebBrowser local:WebBrowserExtentions.Document=<span class="str">"{Binding
HtmlDoc}"</span> /&gt;</pre>
        </div>
        <style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
        <p>
 
</p>
        <p>
The WebBrowser is now bound to the property HtmlDoc of the provided DataContext (which
will probably be a ViewModel in case of MVVM).
</p>
        <p>
Question is though, is this better than creating a new control that is derived from
WebBrowser? Personally I don’t think there is a real winner, but I do like the power
of attached properties. One scenario that requires attached properties are controls
that are sealed and thus inheritance is not possible.
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=a31d5ba1-cf6a-46c7-a4ff-7cecd7308fcc" />
      </div>
    </content>
  </entry>
  <entry>
    <title>LINQ To SQL: Exception when removing child object from Master-Detail relationship</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,15329cb0-5a2b-4c4a-b536-6d36db6cf8db.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,15329cb0-5a2b-4c4a-b536-6d36db6cf8db.aspx</id>
    <published>2008-05-21T21:06:22.9123053+02:00</published>
    <updated>2008-05-21T21:06:22.9123053+02:00</updated>
    <category term=".NET 3.5" label=".NET 3.5" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET%2B3.5.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
This week I ran into a quite unexpected exception that was thrown when I tried to
remove an object from my LINQ ToSQL DataContext:
</p>
        <blockquote>
          <p>
System.InvalidOperationException: An attempt was made to remove a relationship between
a Master and a Detail. However, one of the relationship's foreign keys (Detail.MasterId)
cannot be set to null..
</p>
        </blockquote>
        <p>
The example in which this occurred isn't very exotic, just a Master table and a Detail
table and a relationship with a foreign key MasterId in the Detail table (which doesn't
allow null's). So it should be perfectly valid to remove a row from the Detail table
and submit the changes. 
</p>
        <p>
  
</p>
        <p>
Unfortunately there is a problem with this in LINQ To SQL, which is best explained
by <a href="http://blogs.msdn.com/bethmassi/archive/2007/10/02/linq-to-sql-and-one-to-many-relationships.aspx" target="_blank">this</a> post
of Beth Massi. She states that to work around this problem you have to manually alter
the dbml using an XML editor and add <font face="Courier New">DeleteOnNull="true"</font> to
the association. And yes, this solves the problem.  
</p>
        <p>
  
</p>
        <p>
Beth discovered this problem in VS2008 Beta 2 and it still exists in the final release.
Let's hope the problem is solved with the upcoming SP1...
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=15329cb0-5a2b-4c4a-b536-6d36db6cf8db" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Text Template Transformation Toolkit (T4)</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,6f9d9555-e20b-48d6-a7c6-bc092ab67ea1.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,6f9d9555-e20b-48d6-a7c6-bc092ab67ea1.aspx</id>
    <published>2008-04-19T17:01:48.2172201+02:00</published>
    <updated>2008-04-19T17:01:48.2172201+02:00</updated>
    <category term="VS2008" label="VS2008" scheme="http://www.michelvandervlugt.net/CategoryView,category,VS2008.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.infoq.com" target="_blank">InfoQ</a> recently posted an <a href="http://www.infoq.com/news/2008/04/T4" target="_blank">article</a> about
a very interesting feature in Visual Studio 2008, Text Template Transformation Toolkit
(also called T4). This toolkit is a standard feature of VS2008 and is all about generating
text files based on templates. This toolkit is part of the Domain Specific Language
Tools but it is certainly not limited to DSL's. One example of using T4 which I think
is quite interesting is the generation of config files for different deployment scenario's,
which can be found <a href="http://www.olegsych.com/2007/12/how-to-use-t4-to-generate-config-files/" target="_blank">here</a>.
</p>
        <p>
Useful links:
</p>
        <ul>
          <li>
            <a href="http://msdn2.microsoft.com/en-us/library/bb126445.aspx" target="_blank">MSDN:
Generating Artifacts By Using Text Templates</a>, the MSDN entry point with a list
of many links about this topic 
</li>
        </ul>
        <ul>
          <li>
            <a href="http://www.olegsych.com/2007/12/text-template-transformation-toolkit/" target="_blank">Text
Template Transformation Toolkit by Oleg Sych</a>, tutorial with a lot of examples
and links 
</li>
        </ul>
        <ul>
          <li>
            <a href="http://www.t4editor.net/" target="_blank">T4 Editor by Clarius</a>, a free
template editor which is very useful because template support in VS2008 is very limited</li>
        </ul>
        <p>
The T4 toolkit is also available for VS2005 in the VS2005 SDK v4 and the Guidance
Automation Toolkit (GAT).
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=6f9d9555-e20b-48d6-a7c6-bc092ab67ea1" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Deferred Execution behavior in LINQ providers</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,881d538d-c9c3-49c2-8f7a-2633f0d48839.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,881d538d-c9c3-49c2-8f7a-2633f0d48839.aspx</id>
    <published>2008-04-07T19:21:43.6582158+02:00</published>
    <updated>2008-04-07T19:21:43.6582158+02:00</updated>
    <category term=".NET 3.5" label=".NET 3.5" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET%2B3.5.aspx" />
    <category term="C#" label="C#" scheme="http://www.michelvandervlugt.net/CategoryView,category,C%23.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <style type="text/css">
    .csharpcode, .csharpcode pre
    {
        font-size: small;
        color: black;
        font-family: consolas, "Courier New" , courier, monospace;
        background-color: #ffffff; /*white-space: pre;*/
    }
    .csharpcode pre
    {
        margin: 0em;
    }
    .csharpcode .rem
    {
        color: #008000;
    }
    .csharpcode .kwrd
    {
        color: #0000ff;
    }
    .csharpcode .str
    {
        color: #006080;
    }
    .csharpcode .op
    {
        color: #0000c0;
    }
    .csharpcode .preproc
    {
        color: #cc6633;
    }
    .csharpcode .asp
    {
        background-color: #ffff00;
    }
    .csharpcode .html
    {
        color: #800000;
    }
    .csharpcode .attr
    {
        color: #ff0000;
    }
    .csharpcode .alt
    {
        background-color: #f4f4f4;
        width: 100%;
        margin: 0em;
    }
    .csharpcode .lnum
    {
        color: #606060;
    }
</style>
        <p>
LINQ uses a Deferred Execution model which means that nothing really happens until
the results of the query are accessed, e.g. in a for(each)-loop. One of the advantages
of this model is that you can compose complex queries in multiple steps to make them
more readable. So from a execution point of view I expected that it should not matter
whether you create the query in one complex statement, or in multiple smaller statements.
But unfortunately that's not always the case...
</p>
        <p>
To see the difference I've created an XML document and two simple queries that create
Album objects when the where-clause matches. 
</p>
        <p>
The 'complex' query:
</p>
        <p>
        </p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>var query1 = <span class="kwrd">from</span> a <span class="kwrd">in</span> albums.Descendants(<span class="str">"Album"</span>)</pre>
          <pre>
            <span class="lnum"> 2: </span>
            <span class="kwrd">where</span> a.Element(<span class="str">"Artist"</span>).Value
== <span class="str">"Radiohead"</span></pre>
          <pre>
            <span class="lnum"> 3: </span>
            <span class="kwrd">select</span>
            <span class="kwrd">new</span> Album</pre>
          <pre>
            <span class="lnum"> 4: </span> {</pre>
          <pre>
            <span class="lnum"> 5: </span> Artist = a.Element(<span class="str">"Artist"</span>).Value,</pre>
          <pre>
            <span class="lnum"> 6: </span> Title = a.Element(<span class="str">"Title"</span>).Value</pre>
          <pre>
            <span class="lnum"> 7: </span> };</pre>
        </div>
        <p>
        </p>
        <p>
The decomposed, more readable query:
</p>
        <p>
        </p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>var query2 = <span class="kwrd">from</span> a <span class="kwrd">in</span> albums.Descendants(<span class="str">"Album"</span>)</pre>
          <pre>
            <span class="lnum"> 2: </span>
            <span class="kwrd">select</span>
            <span class="kwrd">new</span> Album</pre>
          <pre>
            <span class="lnum"> 3: </span> {</pre>
          <pre>
            <span class="lnum"> 4: </span> Artist = a.Element(<span class="str">"Artist"</span>).Value,</pre>
          <pre>
            <span class="lnum"> 5: </span> Title = a.Element(<span class="str">"Title"</span>).Value</pre>
          <pre>
            <span class="lnum"> 6: </span> };</pre>
          <pre>
            <span class="lnum"> 7: </span> </pre>
          <pre>
            <span class="lnum"> 8: </span>query2 = <span class="kwrd">from</span> a <span class="kwrd">in</span> query2</pre>
          <pre>
            <span class="lnum"> 9: </span>
            <span class="kwrd">where</span> a.Artist == <span class="str">"Radiohead"</span></pre>
          <pre>
            <span class="lnum"> 10: </span>
            <span class="kwrd">select</span> a;</pre>
        </div>
        <p>
        </p>
        <p>
The result from both queries is exactly the same, but they execute differently. What
happens in Query2 is that first a list of <u>all</u> Album objects is created and
then the ‘where’ part is evaluated over each object. This is different compared to
Query1 in which an Album object is only created when it matches the ‘where’ part.
So in this example Query1 is more efficient.
</p>
        <p>
Is this what we should expect of deferred execution with LINQ? Yes, at least on implementations
based on IEnumerable&lt;T&gt;. What basically happens is that a chain of methods is
created and executed in the same order as added to the query. This makes Query1: albums.Where(...).Select(...)
and Query2: albums.Select(...).Where(...).
</p>
        <p>
With implementations based on IQueryable&lt;T&gt;, like LINQ To SQL, this is different
in a way that it uses an Expression Tree to analyze/optimize the query. Using the
same two queries with LINQ To SQL both queries execute exactly the same! Only one
SQL statement is send to database and they both include the where-clause, which make
them equally efficient.
</p>
        <p>
In this example it isn't that much a problem but when more data is involved this is
definitely something to be aware of when composing LINQ queries. 
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=881d538d-c9c3-49c2-8f7a-2633f0d48839" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Vista Sidebar Notes disappeared</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,50f04d32-829b-4db0-8156-5c3c08f4bffd.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,50f04d32-829b-4db0-8156-5c3c08f4bffd.aspx</id>
    <published>2007-11-07T22:42:13.5173852+01:00</published>
    <updated>2007-11-07T22:49:29.4219587+01:00</updated>
    <category term="miscellaneous" label="miscellaneous" scheme="http://www.michelvandervlugt.net/CategoryView,category,miscellaneous.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today I lost all my notes from the Notes gadget of the Vista Sidebar, which of course
contained some very important stuff.  After some research on the net it turns
out that I'm not the only one who experienced this problem...
</p>
        <p>
There is a way to get them back, but that depends on having the 'Previous Versions'
turned on in Vista. If you have you can restore the settings.ini file that is in the
following directory:
</p>
        <blockquote>
          <p>
C:\Users\<em>username</em>\AppData\Local\Microsoft\Windows Sidebar
</p>
        </blockquote>
        <p>
In this file all the settings of your active Sidebar gadgets are stored, including
your notes.
</p>
        <p>
In my case I could restore the file and after a restart of Sidebar.exe I got my notes back.
So a good file to include in your back-up if you don't have the 'Previous Versions'
option turned on.
</p>
        <a href="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/VistaSidebarNotesdisappeared_1384F/settings.restore_1.jpg" atomicselection="true">
          <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="240" alt="settings.restore" src="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/VistaSidebarNotesdisappeared_1384F/settings.restore_thumb_1.jpg" width="176" border="0" />
        </a>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=50f04d32-829b-4db0-8156-5c3c08f4bffd" />
      </div>
    </content>
  </entry>
  <entry>
    <title>.NET Framework source code</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,85f2509d-684f-430a-a0ab-e8eb1ce132af.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,85f2509d-684f-430a-a0ab-e8eb1ce132af.aspx</id>
    <published>2007-10-06T12:16:45.8704456+02:00</published>
    <updated>2007-10-06T12:16:45.8704456+02:00</updated>
    <category term=".NET 3.5" label=".NET 3.5" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET%2B3.5.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
With the release of Visual Studio 2008 a great new feature will be added: access to
the source code of the .NET Framework libraries! In <a href="http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx" target="_blank">this
post</a> of Scott Guthrie he announces that VS2008 will support downloading the source
code from the Internet so you are able to debug the .NET Framework libraries.
</p>
        <p>
This is really a great thing because now we get more insight in how the .NET Framework
is implemented and why some things might not work as expected. Up until now there
is the possibility to use <a href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a> to
explore the framework libraries, but this will be much easier as it supports
the debug ability...
</p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=85f2509d-684f-430a-a0ab-e8eb1ce132af" />
      </div>
    </content>
  </entry>
  <entry>
    <title>.NET 3.5: Types, Variables and Initializers</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,b631cfe8-178b-40ac-9ce1-4b6777095d37.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,b631cfe8-178b-40ac-9ce1-4b6777095d37.aspx</id>
    <published>2007-08-15T21:58:16.6004225+02:00</published>
    <updated>2007-08-15T21:58:16.6004225+02:00</updated>
    <category term=".NET 3.5" label=".NET 3.5" scheme="http://www.michelvandervlugt.net/CategoryView,category,.NET%2B3.5.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
In this post I will wrap up some of the smaller but very useful new features of .NET
3.5/C# 3.0. Other features of .NET 3.5 I've already covered are <a href="http://www.michelvandervlugt.net/PermaLink,guid,d0d57645-1310-4c05-9931-8be40e6e0f27.aspx" target="_blank">Extension
Methods</a> and <a href="http://www.michelvandervlugt.net/PermaLink,guid,1090afda-a98a-484b-b963-a40d24eb3ab6.aspx" target="_blank">Lambda
Expressions</a>.
</p>
        <p>
          <em>Object and Collection Initializers</em>
          <br />
This feature makes it easier to create objects and assign values to the properties
of the objects. Until .NET 3.5 you either had to write code that created an object
and initialized each property of the object, or use a constructor that takes a list
of parameters (which had to change of be overloaded when a property was added). The
same is true for creating lists of object, calling the Add method for every new object.
The following example shows that object and collection initializers makes this easier,
without the use of parameter constructors:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e3c78f6c-ff5e-451d-9ebe-0ba282eaf305" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">persons </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> List</span>
              <span style="COLOR: #000000">&lt;</span>
              <span style="COLOR: #000000">Person</span>
              <span style="COLOR: #000000">&gt;</span>
              <span style="COLOR: #000000"> { </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> Person
{ Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
I</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> }, </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> Person
{ Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
II</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> }, </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> Person
{ Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
III</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> }, </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> Person
{ Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
IV</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> } };</span>
            </div>
          </pre>
        </div>
        <p>
In the example above the default constructor (without parameters) of Person will be
used, witch will be executed before the property initialization. In case an alternative
constructor is required, e.g. including a read only Id, the following syntax
can be used:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4e373a4f-9de1-4bc0-8482-52c460a37948" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">Person
person </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> Person(</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">NewGuid</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">)
{ Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> };</span>
            </div>
          </pre>
        </div>
        <p>
          <em>Automatic Property get/set</em>
          <br />
The feature of automatic property takes away the burden of explicitly coding
a private field and the get/set of a property, these are generated
automatically by the compiler. The syntax is similar to the way of defining an abstract
property or a property in an interface:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c7fce94c-fe2f-437c-9ca5-4075cb349655" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">class</span>
              <span style="COLOR: #000000"> Person
{ </span>
              <span style="COLOR: #0000ff">public</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">string</span>
              <span style="COLOR: #000000"> Name
{ </span>
              <span style="COLOR: #0000ff">get</span>
              <span style="COLOR: #000000">; </span>
              <span style="COLOR: #0000ff">set</span>
              <span style="COLOR: #000000">;
} }</span>
            </div>
          </pre>
        </div>
        <p>
If necessary the get/set can be changed to limit accessibility, e.g. Name
{ get; private set; }. Of course the object initializers as explained above cannot
be used outside the class anymore.
</p>
        <p>
          <em>Implicitly Typed Local Variables and Arrays</em>
          <br />
With implicitly typed variables the type of the variable is not defined up front but
inferred by the compiler using the initialization of the variable. Basically this
looks the same as defining the variable as an object, but there is a huge difference:
after initialization the type is fixed and cannot be changed, e.g. by assigning a
value of another type.
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:cfb5dfd6-fb2d-4acc-84ad-f58fc23b918b" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">var
a </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">5</span>
              <span style="COLOR: #000000">; </span>
              <span style="COLOR: #008000">//</span>
              <span style="COLOR: #008000"> int</span>
              <span style="COLOR: #008000">
              </span>
              <span style="COLOR: #000000">var
b </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">5.5</span>
              <span style="COLOR: #000000">; </span>
              <span style="COLOR: #008000">//</span>
              <span style="COLOR: #008000"> double</span>
              <span style="COLOR: #008000">
              </span>
              <span style="COLOR: #000000">var
c </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000"> a </span>
              <span style="COLOR: #000000">+</span>
              <span style="COLOR: #000000"> b; </span>
              <span style="COLOR: #008000">//</span>
              <span style="COLOR: #008000"> double</span>
              <span style="COLOR: #008000">
              </span>
              <span style="COLOR: #000000">var
d </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">int</span>
              <span style="COLOR: #000000">[]
{ </span>
              <span style="COLOR: #000000">1</span>
              <span style="COLOR: #000000">, </span>
              <span style="COLOR: #000000">2</span>
              <span style="COLOR: #000000"> }; </span>
              <span style="COLOR: #008000">//</span>
              <span style="COLOR: #008000"> int
array</span>
              <span style="COLOR: #008000">
              </span>
              <span style="COLOR: #000000">var
e </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000">[]
{ </span>
              <span style="COLOR: #000000">1</span>
              <span style="COLOR: #000000">, </span>
              <span style="COLOR: #000000">2</span>
              <span style="COLOR: #000000"> }; </span>
              <span style="COLOR: #008000">//</span>
              <span style="COLOR: #008000"> implicitly
typed int array</span>
              <span style="COLOR: #008000">
              </span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">foreach</span>
              <span style="COLOR: #000000"> (var
f </span>
              <span style="COLOR: #0000ff">in</span>
              <span style="COLOR: #000000"> e) Console.WriteLine(f); </span>
            </div>
          </pre>
        </div>
        <p>
Using the var keyword is only allowed for local variables, they cannot be used as
class variables for example. Also, they have to be initialized when they are
declared.
</p>
        <p>
The usage of implicitly typed variables as aboveis a bit doubtful I think, in fact it
does make the code less readable (although IntelliSense can help you out). In relation
to LINQ and Anonymous Types it does make more sense though, but more on that later.
</p>
        <p>
          <em>Anonymous Types</em>
          <br />
Anonymous Types are objects that are defined and created on the fly
using object initializers. The compiler actually creates properties of the
initializers which can be used later on. The name of the type remains unknown
and cannot be used within the program.
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:75a26faa-89c9-46bd-a1fd-b349b17abca3" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">var
p1 </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> {
Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">Person
I</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">, City </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">New
York</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> }; Console.WriteLine( </span>
              <span style="COLOR: #0000ff">string</span>
              <span style="COLOR: #000000">.Format(</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">{0}
from {1}</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">,
p1.Name, p1.City)); </span>
            </div>
          </pre>
        </div>
        <p>
Like implicitly Typed Variables the usage of Anonymous Types in this context doesn't
seem to be very useful because you really cannot do much with such
an object. With LINQ the usage will be more frequent, e.g. to display a selection
from an executed query:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:2e09d0b5-2afa-452b-af2e-ca8b89c97688" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">
          <pre style="BACKGROUND-COLOR: white">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="COLOR: #000000">var
nyPersons </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000"> from
q </span>
              <span style="COLOR: #0000ff">in</span>
              <span style="COLOR: #000000"> persons </span>
              <span style="COLOR: #0000ff">where</span>
              <span style="COLOR: #000000"> q.Address.City </span>
              <span style="COLOR: #000000">==</span>
              <span style="COLOR: #000000">
              </span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">New
York</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000"> select </span>
              <span style="COLOR: #0000ff">new</span>
              <span style="COLOR: #000000"> {
Name </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000"> q.Name,
City </span>
              <span style="COLOR: #000000">=</span>
              <span style="COLOR: #000000"> q.Address.City
}; </span>
              <span style="COLOR: #0000ff">foreach</span>
              <span style="COLOR: #000000"> (var
nyp </span>
              <span style="COLOR: #0000ff">in</span>
              <span style="COLOR: #000000"> nyPersons)
Console.WriteLine(</span>
              <span style="COLOR: #0000ff">string</span>
              <span style="COLOR: #000000">.Format(</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">{0}
from {1}</span>
              <span style="COLOR: #000000">"</span>
              <span style="COLOR: #000000">,
nyp.Name, nyp.City)); </span>
            </div>
          </pre>
        </div>
        <p>
The source code of these examples can be downloaded from here: <a href="http://www.michelvandervlugt.net/content/binary/TypesVariablesAndInitDemo.zip">TypesVariablesAndInitDemo.zip
(,95 KB)</a></p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=b631cfe8-178b-40ac-9ce1-4b6777095d37" />
      </div>
    </content>
  </entry>
</feed>