<?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>2011-12-03T10:14:56.0476171+01: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>User authentication with WP7 and SSL WCF Service</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,90eed5dc-18ae-46bb-adc9-2a1bf2a5483d.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,90eed5dc-18ae-46bb-adc9-2a1bf2a5483d.aspx</id>
    <published>2011-12-03T10:12:25.8757421+01:00</published>
    <updated>2011-12-03T10:14:56.0476171+01:00</updated>
    <category term="C#" label="C#" scheme="http://www.michelvandervlugt.net/CategoryView,category,C%23.aspx" />
    <category term="WP7" label="WP7" scheme="http://www.michelvandervlugt.net/CategoryView,category,WP7.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my <a href="http://www.michelvandervlugt.net/PermaLink,guid,05664e33-0fde-4aa7-8c04-99e4c5647f8b.aspx" target="_blank">last
post</a> I described all steps that were needed to get a WP7 app connecting to a WCF
Service using SSL and a self-signed certificate. What needs to be added to this solution
is username/password authentication.
</p>
        <p>
It turns out that this is pretty easy, after reading the post ‘<a href="http://www.silverlightshow.net/items/Windows-Phone-7-Data-Access-Strategies-Security.aspx" target="_blank">Windows
Phone 7 Data Access Strategies: Security</a>’ by Andrea Boschin.
</p>
        <strong>Step 1</strong>
        <p>
Replace the custom binding of the service for a basic http binding:
</p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>&lt;binding name=<span class="str">"customBinding"</span>&gt;</pre>
          <pre>
            <span class="lnum"> 2: </span> &lt;security mode=<span class="str">"TransportWithMessageCredential"</span> &gt;</pre>
          <pre>
            <span class="lnum"> 3: </span> &lt;message clientCredentialType=<span class="str">"UserName"</span> /&gt;</pre>
          <pre>
            <span class="lnum"> 4: </span> &lt;/security&gt;</pre>
          <pre>
            <span class="lnum"> 5: </span>&lt;/binding&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>
        <strong>Step 2</strong>
        <p>
Add a custom username/password validator to the service:
</p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">class</span> MyPasswordValidator</pre>
          <pre>
            <span class="lnum"> 2: </span> : UserNamePasswordValidator</pre>
          <pre>
            <span class="lnum"> 3: </span>{</pre>
          <pre>
            <span class="lnum"> 4: </span>
            <span class="kwrd">public</span>
            <span class="kwrd">override</span>
            <span class="kwrd">void</span> Validate(<span class="kwrd">string</span> userName, <span class="kwrd">string</span> password)</pre>
          <pre>
            <span class="lnum"> 5: </span> {</pre>
          <pre>
            <span class="lnum"> 6: </span>
            <span class="kwrd">if</span> (!AuthenticateUser(userName,
password))</pre>
          <pre>
            <span class="lnum"> 7: </span>
            <span class="kwrd">throw</span>
            <span class="kwrd">new</span> SecurityTokenValidationException(<span class="str">"..."</span>);</pre>
          <pre>
            <span class="lnum"> 8: </span> }</pre>
          <pre>
            <span class="lnum"> 9: </span> </pre>
          <pre>
            <span class="lnum"> 10: </span>
            <span class="kwrd">private</span>
            <span class="kwrd">bool</span> AuthenticateUser(<span class="kwrd">string</span> userName, <span class="kwrd">string</span> password)</pre>
          <pre>
            <span class="lnum"> 11: </span> {</pre>
          <pre>
            <span class="lnum"> 12: </span>
            <span class="kwrd">return</span> userName == <span class="str">"foo"</span></pre>
          <pre>
            <span class="lnum"> 13: </span> &amp;&amp; password == <span class="str">"bar"</span>;</pre>
          <pre>
            <span class="lnum"> 14: </span> }</pre>
          <pre>
            <span class="lnum"> 15: </span>}</pre>
        </div>
        <p>
          <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>
And add it to the service behaviors:
</p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>&lt;serviceCredentials&gt;</pre>
          <pre>
            <span class="lnum"> 2: </span> &lt;userNameAuthentication </pre>
          <pre>
            <span class="lnum"> 3: </span> customUserNamePasswordValidatorType=<span class="str">"WP7toWCFtestService.MyPasswordValidator,
WP7toWCFtestService"</span></pre>
          <pre>
            <span class="lnum"> 4: </span> userNamePasswordValidationMode=<span class="str">"Custom"</span> /&gt;</pre>
          <pre>
            <span class="lnum"> 5: </span>&lt;/serviceCredentials&gt;</pre>
        </div>
        <p>
          <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>
        <strong>Step 3</strong>
        <p>
Refresh the service reference in the WP7 app and change the code to access the service:
</p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>var ws = <span class="kwrd">new</span> Service1Client();</pre>
          <pre>
            <span class="lnum"> 2: </span>ws.ClientCredentials.UserName.UserName = <span class="str">"foo"</span>;</pre>
          <pre>
            <span class="lnum"> 3: </span>ws.ClientCredentials.UserName.Password = <span class="str">"bar"</span>;</pre>
          <pre>
            <span class="lnum"> 4: </span> </pre>
          <pre>
            <span class="lnum"> 5: </span>ws.GetDataCompleted += ...</pre>
        </div>
        <p>
          <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>
And voila, it is working!
</p>
        <p>
Of course this is only a starting point, password should not be hard coded and you
probably want to support multiple accounts. In the near future I will at least remove
the username/password from the WP7 app. The user will have to supply them and they
will be stored with encryption on the phone.
</p>
        <p>
For my current app this single user solution is enough. When you want to support multiple
users the ASP.NET Membership Framework (described in <a href="http://blog.lifeplugins.com/2011/01/building-secure-authenticated-wcf.html#!/2011/01/building-secure-authenticated-wcf.html" target="_blank">this
post</a> by Jon Simpson) can be a good option.
</p>
        <p>
With the infrastructure secured it is finally time to write the actual app...
</p>
        <p>
          <a href="http://www.michelvandervlugt.net/content/binary/WP7toWCFtest2.zip" target="_blank">Download
example</a>
        </p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=90eed5dc-18ae-46bb-adc9-2a1bf2a5483d" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Using SSL WCF Service in Windows Phone 7 app</title>
    <link rel="alternate" type="text/html" href="http://www.michelvandervlugt.net/PermaLink,guid,05664e33-0fde-4aa7-8c04-99e4c5647f8b.aspx" />
    <id>http://www.michelvandervlugt.net/PermaLink,guid,05664e33-0fde-4aa7-8c04-99e4c5647f8b.aspx</id>
    <published>2011-11-30T21:45:41.6557573+01:00</published>
    <updated>2011-11-30T21:49:46.7651323+01:00</updated>
    <category term="C#" label="C#" scheme="http://www.michelvandervlugt.net/CategoryView,category,C%23.aspx" />
    <category term="WP7" label="WP7" scheme="http://www.michelvandervlugt.net/CategoryView,category,WP7.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Last few evenings I’ve been working on a WP7 app that I want to connect to a new WCF
service using SSL. Since it is only in a dev environment I want to use a self-signed
certificate instead of an official one. Doesn’t sound too hard, does it? It took some
time before it finally worked...
</p>
        <p>
          <strong>Dev environment</strong>
          <br />
Laptop with Windows 7, IIS 7, VS2010 + Windows Phone SDK 7.1 and a Samsung Omnia 7
device (Mango).
</p>
        <p>
          <strong>Project</strong>
        </p>
        <ul>
          <li>
Windows Phone app (the one in the <a href="http://www.michelvandervlugt.net/content/binary/WP7toWCFtest.zip">download</a> is
very simple and only shows access to the WCF service); 
</li>
          <li>
WCF Service Application; I used the default Service1 that VS2010 generates.</li>
        </ul>
        <p>
          <strong>Step 1</strong>
          <br />
First step is to get the app running, connecting with the service that runs in IIS.
</p>
        <ul>
          <li>
Configure the service to use IIS (hit the ‘Create Virtual Directory’ button); 
</li>
          <li>
Build the service and it should be accessible from a browser, in my case http://localhost/WP7toWCFtestService/Service1.svc 
</li>
          <li>
From the WP7 app add a ‘Service Reference’ to the WCF Service;</li>
        </ul>
        <p>
The following code connects to the service from the WP7 app:
</p>
        <p>
        </p>
        <div class="csharpcode">
          <pre>
            <span class="lnum"> 1: </span>var ws = <span class="kwrd">new</span> Service1Client();</pre>
          <pre>
            <span class="lnum"> 2: </span>ws.GetDataCompleted += </pre>
          <pre>
            <span class="lnum"> 3: </span> (s, e) =&gt; UpdateStatus(<span class="str">"Result:
"</span> + e.Result);</pre>
          <pre>
            <span class="lnum"> 4: </span>ws.GetDataAsync(42);</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>
At this point the app should work and data is retrieved from the service. 
</p>
        <p>
Note: This is where it suddenly stopped working. After a few successful updates of
the service reference Visual Studio stopped generating code in the Reference.cs. Various
blogs and forums state ‘fuzzy’ solutions like restarting VS, making changes in settings,
etc., but none of them worked. The only thing that did work was starting all over
again with a new solution.
</p>
        <b>Step 2</b>
        <p>
Create a Self-Signed Certificate in IIS, see <a href="http://learn.iis.net/page.aspx/144/how-to-set-up-ssl-on-iis/#IISManager" target="_blank">How
to Set Up SSL on IIS 7</a>. Note that you have to use a certificate name that is equal
to your web server name.
</p>
        <b>Step 3</b>
        <p>
Configure the app and service to use SSL
</p>
        <ul>
          <li>
Add a custom binding to web.config defining httpsTransport, see web.config in the <a href="http://www.michelvandervlugt.net/content/binary/WP7toWCFtest.zip">download</a>; 
</li>
          <li>
Change configuration of the Service Reference to point to the https URL and update
the reference. 
</li>
        </ul>
        <p>
In case you have a real certificate everything should work now using https. However,
in case of self-signed you will get the following exception:
</p>
        <p>
          <font size="2" face="Courier New">There was no endpoint listening at https://servername/WP7toWCFtestService/Service1.svc
that could accept the message. This is often caused by an incorrect address or SOAP
action. See InnerException, if present, for more details.</font>
        </p>
        <b>Step 4</b>
        <p>
The problem lies in the root-certificates that are accepted by Windows Phone. The
exact details about the problem and how to fix this is described in several great
blog posts by David Hardin: <a href="http://blogs.msdn.com/b/davidhardin/archive/2010/12/30/wp7-and-self-signed-ssl-certificates.aspx" target="_blank">WP7
and Self-Signed SSL Certificates</a>.
</p>
        <p>
Based on his guidelines <a href="http://blogs.msdn.com/b/davidhardin/archive/2011/01/12/wp7certinstaller-1-0-0-0-prerequisites.aspx" target="_blank">prerequisites</a> and <a href="http://blogs.msdn.com/b/davidhardin/archive/2011/01/19/wp7certinstaller-1-0-0-0-explained.aspx" target="_blank">explained</a> I
followed these steps:
</p>
        <ul>
          <li>
Download the WP7CertInstaller: <a href="http://wp7certinstaller.codeplex.com" target="_blank">http://wp7certinstaller.codeplex.com</a>; 
</li>
          <li>
Removed Azure Tools reference; 
</li>
          <li>
Installed the provided web site in IIS; 
</li>
          <li>
Changed the URL in MainPage.xaml.cs from localhost to my server name; 
</li>
          <li>
Executed the provided WP7 app to download and install the certificate. 
</li>
        </ul>
        <p>
And it works! My app now successfully connects to my service using SSL!
</p>
        <p>
Next is adding credentials and removing SOAP…
</p>
        <p>
          <a href="http://www.michelvandervlugt.net/content/binary/WP7toWCFtest.zip">Download
example</a>
        </p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=05664e33-0fde-4aa7-8c04-99e4c5647f8b" />
      </div>
    </content>
  </entry>
  <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>
</feed>
