<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jb's Blog &#187; MVC</title>
	<atom:link href="http://www.mindscapehq.com/staff/jeremy/index.php/category/mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mindscapehq.com/staff/jeremy</link>
	<description>"behold the turtle, he only makes progress when he sticks his neck out"</description>
	<lastBuildDate>Wed, 28 Apr 2010 23:15:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ASP.NET MVC now open source!</title>
		<link>http://www.mindscapehq.com/staff/jeremy/index.php/2009/04/aspnet-mvc-now-open-source/</link>
		<comments>http://www.mindscapehq.com/staff/jeremy/index.php/2009/04/aspnet-mvc-now-open-source/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 02:07:52 +0000</pubDate>
		<dc:creator>Jeremy Boyd</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.mindscape.co.nz/staff/jeremy/index.php/2009/04/aspnet-mvc-now-open-source/</guid>
		<description><![CDATA[In a quick bit of news, Scott Guthrie has posted an announcement declaring that the code for ASP.NET MVC has now been officially open sourced under the MS-Pl license. 
Read more about it on the Gu’s blog and if you are looking to grab said source, then you can get it from here  
Nice [...]]]></description>
			<content:encoded><![CDATA[<p>In a quick bit of news, Scott Guthrie has posted an announcement declaring that the code for ASP.NET MVC has now been officially open sourced under the <a href="http://www.opensource.org/licenses/ms-pl.html">MS-Pl license</a>. </p>
<p>Read more about it <a href="http://weblogs.asp.net/scottgu/archive/2009/04/01/asp-net-mvc-1-0.aspx">on the Gu’s blog</a> and if you are looking to grab said source, then <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b&amp;displaylang=en">you can get it from here</a> <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Nice work guys – we appreciate it! <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/staff/jeremy/index.php/2009/04/aspnet-mvc-now-open-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An interesting sample with IronRuby and LightSpeed</title>
		<link>http://www.mindscapehq.com/staff/jeremy/index.php/2009/04/an-interesting-sample-with-ironruby-and-lightspeed/</link>
		<comments>http://www.mindscapehq.com/staff/jeremy/index.php/2009/04/an-interesting-sample-with-ironruby-and-lightspeed/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 20:11:12 +0000</pubDate>
		<dc:creator>Jeremy Boyd</dc:creator>
				<category><![CDATA[LightSpeed]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.mindscape.co.nz/staff/jeremy/index.php/2009/04/an-interesting-sample-with-ironruby-and-lightspeed/</guid>
		<description><![CDATA[Ivan Porto Carrero (you may know him as IvanPC  ) tweeted about some interesting work he is doing in building out a sample with IronRuby no doubt as part of his upcoming book on IronRuby, ASP.NET MVC and LightSpeed. Its been a while since I checked in on IronRuby, but having a skim over [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://flanders.co.nz/">Ivan Porto Carrero</a> (you may know him as IvanPC <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) tweeted about some interesting work he is doing in building out a sample with IronRuby no doubt as part of his <a href="http://manning.com/carrero/">upcoming book on IronRuby</a>, ASP.NET MVC and LightSpeed. Its been a while since I checked in on IronRuby, but having a skim over the source it looks like its coming along pretty well.</p>
<p><img style="display: block; float: none; margin-left: auto; margin-right: auto" src="http://manning.com/carrero/carrero_cover150.jpg" /> </p>
<p>You can <a href="http://github.com/casualjim/ironrubymvc-sample/tree/master">check out what he is doing over at Github</a> – and in particular if you are interesting in using LightSpeed with IronRuby – great work Ivan! <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/staff/jeremy/index.php/2009/04/an-interesting-sample-with-ironruby-and-lightspeed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVC and LightSpeed &#8211; Part 4 &#8211; Model Binding</title>
		<link>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part4/</link>
		<comments>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part4/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 21:31:43 +0000</pubDate>
		<dc:creator>Jeremy Boyd</dc:creator>
				<category><![CDATA[LightSpeed]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.mindscape.co.nz/staff/jeremy/?p=98</guid>
		<description><![CDATA[In our earlier instalment we started getting under way with our ASP.NET MVC site for the Film Festival and got our home page working by binding to our LightSpeed entities. Now we should look at what is involved with handling submission of a form where the target is a LightSpeed entity.
Setting up our Action
For this, [...]]]></description>
			<content:encoded><![CDATA[<p>In our <a href="http://www.mindscape.co.nz/staff/jeremy/index.php/2009/03/aspnet-mvc-part3/">earlier instalment</a> we started getting under way with our ASP.NET MVC site for the Film Festival and got our home page working by binding to our LightSpeed entities. Now we should look at what is involved with handling submission of a form where the target is a LightSpeed entity.</p>
<p><strong>Setting up our Action</strong></p>
<p>For this, I want to set up an admin form which we can use to create a new film. We wont concern ourselves too much with the UI here, but rather we are interested in how we can easily map our form data back to an entity and handle validation concerns.</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot017.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Shot017" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot017-thumb.png" border="0" alt="Shot017" width="479" height="249" /></a></p>
<p>Here is the associated View action code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> ActionResult <span style="color: #008000;">New</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      var film <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Film<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> Title <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;New Film&quot;</span>, Year <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">Year</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF;">return</span> <span style="color: #008000;">New</span><span style="color: #000000;">&#40;</span>film<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>NonAction<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> ActionResult <span style="color: #008000;">New</span><span style="color: #000000;">&#40;</span>Film film<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;New&quot;</span>, film<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Next I want to set up my Create() action which will take the POST from this form and will create me a new film.</p>
<p>There are several ways we could pass in input, either by named parameters, through a FormCollection object or through a Film instance directly – well that last option sounds ideal, particularly for when we come to write some tests, so how do I go about that? Surely the form knows nothing about my object structure, does MVC? How does this work?</p>
<p><strong>Cue the Model Binder</strong></p>
<p>If you have used another MVC framework such as Monorail, you will know that it provides some nice data binding support to allow you to map an inbound form collection to an object instance, or reconstruct a graph of objects. MVC provides us the same capabilities through Model Binders.</p>
<p>Model Binders are classes which provide the specifics of how to map a form collection (a bag of key/value pairs) back to a real entity. Out of the box there is a DefaultModelBinder available which does a blind property mapping.</p>
<p>I am going to create a specific model binder for LightSpeed entities which provide some specific understanding of how we want to perform this mapping.</p>
<p>The method we need to implement for IModelBinder is the BindModel method, in this call I am going to unbind all of the value fields on a LightSpeed entity based on the convention that the form parameter will map to a property name on my entity. If not, then we will ignore it. We could also possibly extend this by providing support for Included/Excluded properties which is something the DefaultModelBinder provides.</p>
<p>You can see the code for this class here: <a title="https://code.mindscape.co.nz/repos/LightSpeed/Mvc/Trunk/Src/Mindscape.LightSpeed.Mvc/Binding/EntityModelBinder.cs" href="https://code.mindscape.co.nz/repos/LightSpeed/Mvc/Trunk/Src/Mindscape.LightSpeed.Mvc/Binding/EntityModelBinder.cs">https://code.mindscape.co.nz/repos/LightSpeed/Mvc/Trunk/Src/Mindscape.LightSpeed.Mvc/Binding/EntityModelBinder.cs</a></p>
<p>To use this model binder, we will call EntityModelBinder.Register() from inside our Global.asax start up code – in turn this makes assignments to the ModelBinders.Binders collection.</p>
<p>Once these have been registered, the binding is handled automatically, as ASP.NET MVC sees a parameter and looks up if there is a specific model binder for that type, in the case of an entity there will be, and then it calls to BindModel() on that model binder to do the work.</p>
<p>So if we take this signature for our POST action -  <em>public ActionResult Create(Film film)</em> &#8211; then the result will be an instantiated Film entity.</p>
<p><strong>Handling Validation</strong></p>
<p>How do we determine if there are any errors with the inbound object? Well we can leverage LightSpeed’s baked in validation attributes to provide the answer.</p>
<p>When we first created the definition of our domain model, the LightSpeed Designer automatically assigned appropriate validations on the model based on the database definition – e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">  <span style="color: #000000;">&#91;</span>Serializable<span style="color: #000000;">&#93;</span>
  <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">CodeDom</span>.<span style="color: #0000FF;">Compiler</span></span>.<span style="color: #0000FF;">GeneratedCode</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;LightSpeedModelGenerator&quot;</span>, <span style="color: #666666;">&quot;1.0.0.0&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Film <span style="color: #008000;">:</span> Entity
  <span style="color: #000000;">&#123;</span>
    <span style="color: #008080;">#region Fields</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>ValidatePresence<span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ValidateLength<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">255</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _title<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#91;</span>ValidatePresence<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _description<span style="color: #008000;">;</span></pre></div></div>

<p>The ValidatePresence and ValidateLength attributes are specifying validation concerns.</p>
<p>In our model binder you will notice that after we have unbound the entity we call .IsValid – this triggers LightSpeed to validate the entity (it automatically does this when trying to save as well, so you would get an exception if you tried to save an invalid entity). We can then bind any errors it finds into the ModelState.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">      <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>entity.<span style="color: #0000FF;">IsValid</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var error <span style="color: #0600FF;">in</span> entity.<span style="color: #0000FF;">Errors</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
          bindingContext.<span style="color: #0000FF;">ModelState</span>.<span style="color: #0000FF;">AddModelError</span><span style="color: #000000;">&#40;</span>error.<span style="color: #0000FF;">PropertyName</span>, error.<span style="color: #0000FF;">ErrorMessage</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
      <span style="color: #000000;">&#125;</span></pre></div></div>

<p>So applying this, and then pushing Create on my form (silly me) we get this</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot018.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Shot018" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot018-thumb.png" border="0" alt="Shot018" width="532" height="334" /></a></p>
<p>Excellent! We can get the nice little validation summary by leveraging a method on the HtmlHelper class called ShowValidationSummary – I have set it up like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;!--</span>Html.<span style="color: #0000FF;">ValidationSummary</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;There were some errors with your submission..&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">--&gt;</span></pre></div></div>

<p>And my controller code now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> ActionResult Create<span style="color: #000000;">&#40;</span>Film film<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>ModelState.<span style="color: #0000FF;">IsValid</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">New</span><span style="color: #000000;">&#40;</span>film<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
&nbsp;
      UnitOfWork.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>film<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      UnitOfWork.<span style="color: #0000FF;">SaveChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF;">return</span> RedirectToAction<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Index&quot;</span>, <span style="color: #666666;">&quot;Home&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>You can see that we are just testing ModelState.IsValid to confirm if any binding errors occurred. We could also simply check film.IsValid to achieve the same thing.</p>
<p>This is not the only way to handle ASP.NET MVC validation, and I would point you to <a href="http://codebetter.com/blogs/david.hayden/archive/2009/02/03/an-aha-moment-on-mvc-validation-extensibility-in-defaultmodelbinder-bye-to-idataerrorinfo.aspx">some of the posts</a> <a href="http://codebetter.com/blogs/david.hayden/default.aspx">David Hayden</a> has put out on the facilities you have at your disposal along with the <a href="http://www.asp.net/learn/mvc/#MVC_Validation">tutorials on the ASP.NET MVC site</a>.</p>
<p>So server side validation plug’s in nicely with LightSpeed and its validation – what about if we also want some client side validation? Well thanks to a great suggestion by Justin Thirkell we can leverage jQuery and a validation plugin here along with <a href="http://xval.codeplex.com/">xVal</a> to make this nice and easy.</p>
<p><strong>xVal</strong></p>
<p>From <a href="http://blog.codeville.net/">Steve Sanderson’s</a> blog: “<a href="http://xval.codeplex.com/">xVal</a> lets you link up your choice of server-side validation mechanism with your choice of client-side validation library. It guides you to fit them both into ASP.NET MVC conventions, so everything plays nicely with model binding and errors registered in ModelState”. <a href="http://blog.codeville.net/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/">Read more.</a></p>
<p>It is nice and flexible allowing a many to many mapping of validations as follows:</p>
<p><img src="http://blog.codeville.net/wp-content/uploads/2009/01/image-thumb.png" alt="" /></p>
<p>Great – so all we need to do is teach it about LightSpeed’s validation attributes.</p>
<p>To do this, I have used some code Justin sent through which adds a custom IRulesProvider, you can see what this looks like here: <a title="https://code.mindscape.co.nz/repos/LightSpeed/Mvc/Trunk/Src/Mindscape.LightSpeed.Mvc/Validation/LightSpeedXValRulesProvider.cs" href="https://code.mindscape.co.nz/repos/LightSpeed/Mvc/Trunk/Src/Mindscape.LightSpeed.Mvc/Validation/LightSpeedXValRulesProvider.cs">https://code.mindscape.co.nz/repos/LightSpeed/Mvc/Trunk/Src/Mindscape.LightSpeed.Mvc/Validation/LightSpeedXValRulesProvider.cs</a></p>
<p>Adding this in to my project means I need to add the following to my startup code in Global:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">xVal.<span style="color: #0000FF;">ActiveRuleProviders</span>.<span style="color: #0000FF;">Providers</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> LightSpeedXValRulesProvider<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>and then a declaration on my page for xVal to apply its validation against a Film:&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;!--</span>Html.<span style="color: #0000FF;">ClientSideValidation</span><span style="color: #008000;">&lt;</span>FilmFestival.<span style="color: #0000FF;">Model</span>.<span style="color: #0000FF;">Film</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">--&gt;</span></pre></div></div>

<p>and the result is I have client side validation!</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot019.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="Shot019" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot019-thumb.png" border="0" alt="Shot019" width="539" height="316" /></a></p>
<p><strong>Playing along at home?</strong></p>
<p>Here is the code and associated database setup script (remember to create a database called FilmFestival and run the scripts under the context of that database first or use the Build.cmd script) to cover what we have done so far. Also remember to download and install LightSpeed.</p>
<p>We are now also leveraging some of the community code project – Mindscape.LightSpeed.Mvc – this has been included in the Lib folder, or you can <a href="http://www.mindscape.co.nz/code/">grab it independently from here</a>.</p>
<p>Download <a href="http://turtle.net.nz/blog/content/FilmFestival2009_Part4.zip">FilmFestival2009_Part4.zip</a> (94KB)</p>
<p><strong>Ok &#8211; What’s next?</strong></p>
<p>Next we should build out some of the remaining functionality which will probably bring up the topics of routing and some more jQuery <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part4/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>MVC and LightSpeed &#8211; Part 3 &#8211; Setting up the Site</title>
		<link>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part3/</link>
		<comments>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part3/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 00:25:01 +0000</pubDate>
		<dc:creator>Jeremy Boyd</dc:creator>
				<category><![CDATA[LightSpeed]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.mindscape.co.nz/staff/jeremy/?p=84</guid>
		<description><![CDATA[In our earlier instalments we set up our domain model for the Film Festival as well as a unit test project (currently for the model, but we will extend that for the web site soon enough..). Now its time to create our MVC site.
Create the Web
First step is to set up an ASP.NET MVC site [...]]]></description>
			<content:encoded><![CDATA[<p>In our <a href="http://www.mindscape.co.nz/staff/jeremy/index.php/2009/03/aspnet-mvc-part2/">earlier instalments</a> we set up our domain model for the Film Festival as well as a unit test project (currently for the model, but we will extend that for the web site soon enough..). Now its time to create our MVC site.</p>
<p><strong>Create the Web</strong></p>
<p>First step is to set up an ASP.NET MVC site instance called “Web”, this is easily achieved using the VS template if you have it installed.</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot013.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="Shot013" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot013-thumb.png" border="0" alt="Shot013" width="554" height="179" /></a></p>
<p>By default, the template includes quite a few things I don&#8217;t really care for such as an out of the box set of styles and some of the Microsoft AJAX scripts – I get away with just <a href="http://jquery.com/">jQuery</a>. You will notice that the current latest version of jQuery is installed with the RTM templates (1.3.2), however this will soon be updated so make sure you check for these on jQuery.com regularly enough. The focus of this current line (1.3.x) seems to be focused on really tuning the performance of the library so you will likely benefit from upgrades <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here is what my structure looks like at the end of some quick culling..</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot014.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="Shot014" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot014-thumb.png" border="0" alt="Shot014" width="293" height="331" /></a></p>
<p>I have obviously added in references to the Model project and LightSpeed, and I have added the Web project as a reference for our UnitTests.</p>
<p><strong>Cue the pre-canned stuff</strong></p>
<p>Given that I am updating my existing FilmFestival site – I do already have some of the creative taken care of <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   Cue the one I baked earlier, and our site now looks like this – I have included an existing style sheet and images, and then modified Site.Master and Views/Home/Index.aspx to make this happen.</p>
<p>Our site is now taking shape visually which is always motivating I find..</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot015.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="Shot015" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot015-thumb.png" border="0" alt="Shot015" width="558" height="367" /></a></p>
<p>What we now want to do is set up the home page so that it displays a list of our locations, a sample selection of films on the side bar and for it to display the first film as our “feature film” showing its photo and the name of the film.</p>
<p><strong>Controllers</strong></p>
<p>The process logic for our web application is executed in the Controller actions. The paradigm with MVC is we set up a routing table which will map incoming requests to controller actions based on the URL which has been requested.</p>
<p>The default routing table included with our MVC site is this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> RegisterRoutes<span style="color: #000000;">&#40;</span>RouteCollection routes<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      routes.<span style="color: #0000FF;">IgnoreRoute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{resource}.axd/{*pathInfo}&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      routes.<span style="color: #0000FF;">MapRoute</span><span style="color: #000000;">&#40;</span>
          <span style="color: #666666;">&quot;Default&quot;</span>,                                              <span style="color: #008080; font-style: italic;">// Route name</span>
          <span style="color: #666666;">&quot;{controller}/{action}/{id}&quot;</span>,                           <span style="color: #008080; font-style: italic;">// URL with parameters</span>
          <span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span> controller <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Home&quot;</span>, action <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Index&quot;</span>, id <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #000000;">&#125;</span>  <span style="color: #008080; font-style: italic;">// Parameter defaults</span>
      <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>You can see that currently our HomeController::Index() action is pretty thin – nothing wrong with that, Thin Controller / Thick Model is generally what we are aiming for, however what we do need it to do is to fetch us a list of locations and some films, and to do that we need to fetch them through our LightSpeed unit of work.</p>
<p><strong>Managing the LightSpeed Unit of Work on a Per Request basis</strong></p>
<p>When using LightSpeed the UnitOfWork manages interactions with your entities inside a scope, usually about the size of a business transaction. On the web, your transaction is likely to be the scope of a single request so ideally we want a unit of work to be set up at the beginning of a request and then to be disposed at the end. We can set up this approach using the PerRequestUnitOfWorkScope&lt;TUnitOfWork&gt; class, which is an implementation of UnitOfWorkScopeBase&lt;TUnitOfWork&gt; and will hold us an instance of TUnitOfWork which in our case will be our strongly typed UnitOfWork class which was generated for us when we set up the domain model earlier and has the IQueryable properties for our entity collections making querying a bit easier through LINQ.</p>
<p>To help set this up, we are going to create a base controller class for our project called FilmFestivalController which implements Controller. All of our specific controllers such as HomeController will implement this.</p>
<p>Here is the code we are going to add:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> UNIT_OF_WORK <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;__UnitOfWork__&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> UnitOfWorkScopeBase<span style="color: #008000;">&lt;</span>FilmFestival.<span style="color: #0000FF;">Model</span>.<span style="color: #0000FF;">UnitOfWork</span><span style="color: #008000;">&gt;</span> _unitOfWorkScope<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> UnitOfWorkScopeBase<span style="color: #008000;">&lt;</span>FilmFestival.<span style="color: #0000FF;">Model</span>.<span style="color: #0000FF;">UnitOfWork</span><span style="color: #008000;">&gt;</span> UnitOfWorkScope
    <span style="color: #000000;">&#123;</span>
      get
      <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_unitOfWorkScope <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
          _unitOfWorkScope <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Items</span><span style="color: #000000;">&#91;</span>UNIT_OF_WORK<span style="color: #000000;">&#93;</span> <span style="color: #0600FF;">as</span> PerRequestUnitOfWorkScope<span style="color: #008000;">&lt;</span>FilmFestival.<span style="color: #0000FF;">Model</span>.<span style="color: #0000FF;">UnitOfWork</span><span style="color: #008000;">&gt;;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_unitOfWorkScope <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
          _unitOfWorkScope <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PerRequestUnitOfWorkScope<span style="color: #008000;">&lt;</span>FilmFestival.<span style="color: #0000FF;">Model</span>.<span style="color: #0000FF;">UnitOfWork</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>MvcApplication.<span style="color: #0000FF;">LightSpeedDataContext</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
          <span style="color: #000000;">&#123;</span>
            <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Items</span><span style="color: #000000;">&#91;</span>UNIT_OF_WORK<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> _unitOfWorkScope<span style="color: #008000;">;</span>
          <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">return</span> _unitOfWorkScope<span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">protected</span> FilmFestival.<span style="color: #0000FF;">Model</span>.<span style="color: #0000FF;">UnitOfWork</span> UnitOfWork
    <span style="color: #000000;">&#123;</span>
      get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> UnitOfWorkScope.<span style="color: #0000FF;">Current</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnResultExecuted<span style="color: #000000;">&#40;</span>ResultExecutedContext filterContext<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_unitOfWorkScope <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        _unitOfWorkScope.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
&nbsp;
      <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnResultExecuted</span><span style="color: #000000;">&#40;</span>filterContext<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #008000;">new</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_unitOfWorkScope <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        _unitOfWorkScope.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
&nbsp;
      <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Basically we are facilitating a Festival.Model.UnitOfWork property on our controllers which will be bound to the current unit of work for this request – otherwise one will be instantiated as required by calling .CreateUnitOfWork on the LightSpeedContext (gives context on how we are connecting to the underlying database) which is a static object we have declared in our Global.asax</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">internal</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> LightSpeedContext<span style="color: #008000;">&lt;</span>FilmFestival.<span style="color: #0000FF;">Model</span>.<span style="color: #0000FF;">UnitOfWork</span><span style="color: #008000;">&gt;</span> LightSpeedDataContext 
  <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LightSpeedContext<span style="color: #008000;">&lt;</span>FilmFestival.<span style="color: #0000FF;">Model</span>.<span style="color: #0000FF;">UnitOfWork</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;default&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>You can see that we also implement an override for OnResultExecuted which will fire once the ActionResult has been processed which disposes the unit of work.</p>
<p>If we take an approach of not passing entities through to the views where the unit of work may possibly be needed to reference lazy child collections and the like then we could dispose those earlier, however you may find it more sensible to allow yourself this convenience in your view – but be aware you may be generating additional database request (== costly).</p>
<p><strong>Fetching what we need for the Home View</strong></p>
<p>Getting information to the view can be done in one of two ways, either we can use the generic ViewData property bag – e.g. ViewData[“Key”] = value; or we can create a strongly typed ViewModel class which we pass in place of the generic ViewDataDictionary instance. I currently prefer the latter approach because it is a little clearer about what needs to be sent to the view and I also find myself re-using these view models across a number of views which can also be helpful. The downsides I have noticed so far is that often your ViewModel may be specific to the view, so you either end up with a large ViewModel which is being shared around but some properties are never set which means you possibly should just go back to the property bag <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  YMMV so keep both options in mind.</p>
<p>So I want to quickly create a ViewModel for our Index action, nothing hard here, I create a class called HomePageViewModel.cs and put it under a ViewModels folder, it is going to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HomePageViewModel
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> IList<span style="color: #008000;">&lt;</span>Film<span style="color: #008000;">&gt;</span> Films <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
  <span style="color: #0600FF;">public</span> IList<span style="color: #008000;">&lt;</span>Location<span style="color: #008000;">&gt;</span> Locations <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
  <span style="color: #0600FF;">public</span> Film FeaturedFilm <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We can fetch our data by modifying the Index() action as follows</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">IList<span style="color: #008000;">&lt;</span>Film<span style="color: #008000;">&gt;</span> films <span style="color: #008000;">=</span> UnitOfWork.<span style="color: #0000FF;">Films</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">OrderBy</span><span style="color: #000000;">&#40;</span>o <span style="color: #008000;">=&gt;</span> Guid.<span style="color: #0000FF;">NewGuid</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">8</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
HomePageViewModel model <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HomePageViewModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  Locations <span style="color: #008000;">=</span> UnitOfWork.<span style="color: #0000FF;">Locations</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
  Films <span style="color: #008000;">=</span> films,
  FeaturedFilm <span style="color: #008000;">=</span> films<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>model<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>You can see how we can use the IQueryable properties on UnitOfWork as entry points for our queries, you can use LINQ syntax to structure up a query to fetch your entities as required – for example my “random” selection fetches all Films into memory and then sorts them randomly using GUIDs and then selects the top 8 for display.</p>
<p><strong>Applying this to the View</strong></p>
<p>I am currently using the default WebFormViewEngine (ASP.NET Web Forms) for the views, so I have .aspx files to work with. One of the nice things about ASP.NET MVC is that I am not limited to just using web forms. If I dont like it I could use something like <a href="http://andrewpeters.net/2007/12/19/introducing-nhaml-an-aspnet-mvc-view-engine/">NHaml</a>, which is currently my preferred view engine, however that is kind of a tangent so we will stick with web forms for now.</p>
<p>Firstly I want to use my typed view model, so I will declare a local variable on the form called Model to hold this</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span> var Model <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Web.<span style="color: #0000FF;">ViewModels</span>.<span style="color: #0000FF;">HomePageViewModel</span><span style="color: #000000;">&#41;</span>ViewData.<span style="color: #0000FF;">Model</span><span style="color: #008000;">;</span> <span style="color: #008000;">%&gt;</span></pre></div></div>

<p>This is a bit naff – There undoubtedly is a better way but my ignorance in the WebFormViewEngine space has left me a bit naive here. With NHaml we have our typed models available so I wouldn&#8217;t need to declare this.</p>
<p>The next step is to expand out our view code to make use of the Model we have available to wire up our required entities for display. e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #008000;">&lt;</span>h3<span style="color: #008000;">&gt;</span>
          <span style="color: #008000;">&lt;</span>a href<span style="color: #008000;">=</span><span style="color: #666666;">&quot;/films&quot;</span><span style="color: #008000;">&gt;</span>Explore Films<span style="color: #008000;">&lt;/</span>a<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;/</span>h3<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>ul<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;%</span> 
              <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var film <span style="color: #0600FF;">in</span> Model.<span style="color: #0000FF;">Films</span><span style="color: #000000;">&#41;</span>
              <span style="color: #000000;">&#123;</span>
            <span style="color: #008000;">%&gt;</span>
              <span style="color: #008000;">&lt;</span>li<span style="color: #008000;">&gt;</span>
                <span style="color: #008000;">&lt;</span>a href<span style="color: #008000;">=&lt;%=</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;/film/&quot;</span>, film.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;&gt;</span>
                  <span style="color: #008000;">&lt;%=</span> film.<span style="color: #0000FF;">Title</span> <span style="color: #008000;">%&gt;</span>
                <span style="color: #008000;">&lt;/</span>a<span style="color: #008000;">&gt;</span>
              <span style="color: #008000;">&lt;/</span>li<span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;%</span>
              <span style="color: #000000;">&#125;</span>
            <span style="color: #008000;">%&gt;</span>
        <span style="color: #008000;">&lt;/</span>ul<span style="color: #008000;">&gt;</span></pre></div></div>

<p>Easy enough. I am likely to want to refactor how those links are getting generated however because I know we can make use of some of the HtmlHelper utilities to generate these – we can refactor this once we have built out actions for these targets.</p>
<p><strong>Look at the Home Page now</strong></p>
<p>So with our model wired up, lets re-run our site – and here is what it now looks like</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot016.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="Shot016" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot016-thumb.png" border="0" alt="Shot016" width="552" height="362" /></a> </p>
<p>So that&#8217;s a great start, the only thing I probably want to add to this is a bit of Output caching to avoid generating unnecessary work on the server – we can do this easily in MVC by applying an [OutputCache(Duration=60, VaryByParam="")] attribute on the top of our Index action. How can we tell its working? Well previously the home page would likely display a new film after each refresh, now it does not. The [OutputCache] attribute is an example of an action filter, we will delve more into these at a later time.</p>
<p><strong>Playing along at home?</strong></p>
<p>Here is the code and associated database setup script (remember to create a database called FilmFestival and run the scripts under the context of that database first or use the Build.cmd script) to cover what we have done so far. Also remember to download and install LightSpeed.</p>
<p>Download <a href="http://turtle.net.nz/blog/content/FilmFestival2009_Part3.zip">FilmFestival2009_Part3.zip</a> (538KB)</p>
<p><strong>Ok &#8211; What’s next?</strong></p>
<p>Next we can look at setting up a form where we want to unbind the data into a LightSpeed entity and flesh out some functions of the site a bit more.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part3/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC now released</title>
		<link>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-now-released/</link>
		<comments>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-now-released/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 20:46:01 +0000</pubDate>
		<dc:creator>Jeremy Boyd</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.mindscape.co.nz/staff/jeremy/index.php/2009/03/aspnet-mvc-now-released/</guid>
		<description><![CDATA[In case you haven&#8217;t heard the news already, ASP.NET MVC was officially launched as part of MIX 09 today and you can now grab it in either source or binary forms (or both if you are like me)  
Check out the ASP.NET site for all the links and details you will need – I [...]]]></description>
			<content:encoded><![CDATA[<p>In case you haven&#8217;t heard the news already, ASP.NET MVC was officially launched as part of MIX 09 today and you can now grab it in either source or binary forms (or both if you are like me) <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Check out the <a href="http://www.asp.net/mvc/">ASP.NET site</a> for all the links and details you will need – I noticed they had a nice set of tutorials available as well so worth checking out <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>One of the other interesting tidbits to come out of the keynote this morning was Azure now supporting full trust deployments which will make life a lot easier for everyone. Great stuff!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-now-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET and MVC &#8211; Part 2 &#8211; UnitTests for the Model</title>
		<link>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part2/</link>
		<comments>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part2/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 02:05:41 +0000</pubDate>
		<dc:creator>Jeremy Boyd</dc:creator>
				<category><![CDATA[LightSpeed]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.mindscape.co.nz/staff/jeremy/?p=61</guid>
		<description><![CDATA[In our earlier instalment we embarked on setting up an ASP.NET MVC using LightSpeed, jQuery, Ninject and NUnit and started by building up our domain model in LightSpeed.
So it is about time we moved on to setting up our UnitTests project and got underway but getting some initial tests in for our model. So lets [...]]]></description>
			<content:encoded><![CDATA[<p>In our <a href="http://www.mindscape.co.nz/staff/jeremy/index.php/2009/03/aspnet-mvc-part1/">earlier instalment</a> we embarked on setting up an ASP.NET MVC using <a href="http://www.mindscape.co.nz/products/lightspeed/download.aspx">LightSpeed</a>, <a href="http://jquery.com/">jQuery</a>, <a href="http://ninject.org/">Ninject</a> and <a href="http://www.nunit.org/index.php">NUnit</a> and started by building up our domain model in LightSpeed.</p>
<p>So it is about time we moved on to setting up our UnitTests project and got underway but getting some initial tests in for our model. So lets get started..</p>
<p><strong>Some minor tweaks since last time..</strong></p>
<p>Before we get started, I should mention I have quickly tidied up some of the code from last time – namely I have renamed the namespaces for this solution to be FilmFestival.* rather than lacking a top level namespace as we did previously when we set up our Model project.</p>
<p>I have also updated the LightSpeed model to generate into the namespace FilmFestival.Model and not prefix the generated UnitOfWork so we can find it at FilmFestival.Model.UnitOfWork.</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot009.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Shot009" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot009-thumb.png" border="0" alt="Shot009" width="272" height="349" /></a></p>
<p>You can do this in the entity designer by altering the <em>Name</em> and <em>TargetNamespace</em> properties accordingly.</p>
<p>One other tweak I had to make was to update the property definition for Cinema.GeoLocation to be a string rather than an object (which is what the LightSpeedDesigner see’s it as in SQL 2005 mode since the underlying SQL type is a SQL 2008 geography). If I was using the SqlServer2008 provider we could natively use the SqlGeography type on our LightSpeedEntity, but that isn&#8217;t released yet <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><strong>Back to the tests!</strong></p>
<p>So first we need to set up a new project for our unit tests, I generally call this UnitTests (original is my middle name..), and we will start by adding references to our Model project, the LightSpeed assemblies and to NUnit.Framework which is what we will use for our testing. You could use XUnit, MBUnit, MSTest or whatever takes your fancy – I prefer NUnit for the time being <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot010.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Shot010" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot010-thumb.png" border="0" alt="Shot010" width="265" height="310" /></a></p>
<p>The last reference I generally add is to <a href="http://utilities.codeplex.com/">Newtonsoft Utilities.NET</a> which has some useful classes for testing entities. You should check this out in general as there is plenty of useful stuff in there, plus it was made by local ninja, crime fighting bat by night and occasional Supreme Commander victor <a href="http://james.newtonking.com/">James Newton-King</a> <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Managing Reference Assemblies</strong></p>
<p>At this point we have started making use of some 3rd party assemblies, namely NUnit and Utilities. They may not be standard on other developers machines or on my build server, so I want to reference them from a relative path within my project structure. If you read <a href="http://www.mindscape.co.nz/staff/jeremy/index.php/2009/03/setting-up-a-new-project-at-mindscape/">my earlier post on project structure</a>, you will know we should have a Lib folder for holding our reference assemblies together with the source for the project to make it more portable.</p>
<p><strong>Setting up a TestFixture</strong></p>
<p>The test fixture is your container for tests and allows the test framework (NUnit) to locate appropriate groups of tests to be executed. In a simple sense, what we would do is mark every class which contains tests up with a [TestFixture] attribute (found in NUnit.Framework) and that would be enough. Because we are generally likely to have common groups of tests separated across a number of files (e.g. 1 class for each model entities tests) I like to use base classes to help keep the common behaviour together.</p>
<p>So first off, lets provision a base TestFixture class.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">  
<span style="color: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> abstract <span style="color: #FF0000;">class</span> TestFixture    
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">static</span> LightSpeedContext<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>Model.<span style="color: #0000FF;">UnitOfWork</span><span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> _context<span style="color: #008000;">;</span>      
    <span style="color: #0600FF;">public</span> Model.<span style="color: #0000FF;">UnitOfWork</span> UnitOfWork <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">static</span> TestFixture<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>        
      InitializeLightSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span> 
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> InitializeLightSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// TODO</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> TransactionScope _transactionScope<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>SetUp<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> SetUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span> 
      _transactionScope <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TransactionScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>     
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>TearDown<span style="color: #000000;">&#93;</span>     
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> TearDown<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>     
    <span style="color: #000000;">&#123;</span>    
      _transactionScope.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>     
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span></pre></div></div>

<p>What this does is set up the framework for us to run transactional based tests against the database where the resulting transaction will always just be thrown away after each test execution – this is what we are doing with the TransactionScope in the [SetUp] and [TearDown] methods.</p>
<p>The last bit is we want to initialize LightSpeed. This can be done one of two ways, either from configuration or in code. For your standard applications we would recommend you use configuration, for tests, building up the context programatically is usually preferable.</p>
<p>So lets fill out the InitializeLightSpeed method as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> InitializeLightSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>       
<span style="color: #000000;">&#123;</span>         
  _context <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LightSpeedContext<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>Model.<span style="color: #0000FF;">UnitOfWork</span><span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>         
  <span style="color: #000000;">&#123;</span>           
    ConnectionString <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;server=localhost;database=FilmFestival;integrated security=sspi;&quot;</span>,           
    Logger <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ConsoleLogger<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
    PluralizeTableNames <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span>         
  <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Hopefully thats obvious enough as to what we are doing here. The LightSpeedContext is effectively a factory which will instantiate us units of work which we will use to scope our interactions with the repository. For the test scenarios it will be one unit of work per test, so we can add a UnitOfWork = _context.CreateUnitOfWork(); call into the [SetUp] method to round this off.</p>
<p>Now whenever we access the property UnitOfWork from in our text fixtures, it will be instantiated and ready for us to do some work, and that work will always be rolled back at the end of the test regardless of what happens.</p>
<p><strong>Setting up some Model Tests</strong></p>
<p>Lets create some tests for our Cinema entity. For Entities, I generally have 3 stock tests (as below) before we add in anything specific to the behaviour of this entity. These tests are just there to validate we can find things and that the entities properties are at least all being checked for assignment using the ClassTester from Utilities.NET.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CinemaTests <span style="color: #008000;">:</span> TestFixture
<span style="color: #000000;">&#123;</span>       
  <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> FindOne<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>       
  <span style="color: #000000;">&#123;</span>
      Cinema cinema <span style="color: #008000;">=</span> UnitOfWork.<span style="color: #0000FF;">FindOne</span><span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>Cinema<span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">300</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
      Assert.<span style="color: #0000FF;">IsNotNull</span><span style="color: #000000;">&#40;</span>cinema<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>       
  <span style="color: #000000;">&#125;</span> 
&nbsp;
  <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> FindAll<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>       
  <span style="color: #000000;">&#123;</span>
      Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">12</span>, UnitOfWork.<span style="color: #0000FF;">Cinemas</span>.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>       
  <span style="color: #000000;">&#125;</span> 
&nbsp;
  <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Properties<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>       
  <span style="color: #000000;">&#123;</span>
      ClassTester tester <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ClassTester<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Cinema<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      tester.<span style="color: #0000FF;">IgnoredProperties</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;EntityState&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      tester.<span style="color: #0000FF;">TestProperties</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>       
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You can see we are using a mixture of LightSpeed’s query syntax (FindOne) and LINQ (UnitOfWork.Cinemas.Count()) in these tests. Generally if you are using .NET 3.5 you will be using LINQ as your primary syntax.</p>
<p><strong>Oops, We need some Test Data</strong></p>
<p>Ok run the tests up. And it fails. That&#8217;s good <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  What are we missing? Oh yes, some test data. I have added in a Data.sql to our schema directory with some initial test data to cover our model so far, and one of the other things I like to add now is a little batch file to automate rebuilding things. You can have a look through both in the Schema folder in the attached code drop.</p>
<p><strong>What else can we test?</strong></p>
<p>In our earlier instalment we had added a helper property on to our Cinema which returned a parsed string with the co-ordinates of the Cinema’s location, so it would be good to validate our assumptions around its behaviour.</p>
<p>e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    
  <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Coordinates<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>       
  <span style="color: #000000;">&#123;</span>
      Cinema cinema <span style="color: #008000;">=</span> UnitOfWork.<span style="color: #0000FF;">FindOne</span><span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>Cinema<span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">300</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;-41.2947,174.7841&quot;</span>, cinema.<span style="color: #0000FF;">Coordinates</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
      cinema <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Cinema<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Foo&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
      Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;0,0&quot;</span>, cinema.<span style="color: #0000FF;">Coordinates</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>       
  <span style="color: #000000;">&#125;</span> 
&nbsp;
    <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>       
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> CoordinatesAreZeroWhenGeoLocationIsNullOrEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>         
      Cinema cinema <span style="color: #008000;">=</span> UnitOfWork.<span style="color: #0000FF;">FindOne</span><span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>Cinema<span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">300</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      cinema.<span style="color: #0000FF;">GeoLocation</span> <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span> 
&nbsp;
      Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;0,0&quot;</span>, cinema.<span style="color: #0000FF;">Coordinates</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
      cinema <span style="color: #008000;">=</span> UnitOfWork.<span style="color: #0000FF;">FindOne</span><span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>Cinema<span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">300</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      cinema.<span style="color: #0000FF;">GeoLocation</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span> 
&nbsp;
      Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;0,0&quot;</span>, cinema.<span style="color: #0000FF;">Coordinates</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>       
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>And so on..</strong></p>
<p>Because your domain model should be playing a fairly important role in the application it is important to make sure it is well tested. I prefer using the ClassTester to cover the basics of assignment to catch any silly errors and then write specific tests for known behaviours. Your personal approach may vary, but look to achieve a fairly high level of coverage for your model.</p>
<p>I would also highly recommend TestDriven.NET or Resharper to give you IDE integration to execute tests via shortcut keys in scope of whatever you are currently working with (single test, test class, whole project, whole solution) which speeds things up quite a lot.</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot011.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="TestDriven FTW" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot011-thumb.png" border="0" alt="TestDriven FTW" width="379" height="235" /></a></p>
<p><strong>Playing along at home?</strong></p>
<p>Here is the code and associated database setup script (remember to create a database called FilmFestival and run the scripts under the context of that database first or use the Build.cmd script) to cover what we have done so far. Also remember to download and install LightSpeed.</p>
<p>Download <a href="http://turtle.net.nz/blog/content/FilmFestival2009_Part2.zip">FilmFestival2009_Part2.zip</a> (94KB)</p>
<p><strong>Ok &#8211; What’s next?</strong></p>
<p>Next we are ready to get our MVC site itself underway, then we can build out some actual functionality.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Useful ASP.NET MVC Resource</title>
		<link>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/useful-aspnet-mvc-resource/</link>
		<comments>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/useful-aspnet-mvc-resource/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 22:33:59 +0000</pubDate>
		<dc:creator>Jeremy Boyd</dc:creator>
				<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.mindscape.co.nz/staff/jeremy/index.php/2009/03/useful-aspnet-mvc-resource/</guid>
		<description><![CDATA[ScottGu has posted about a new sample ASP.NET MVC application which has been put together by some of the vocal members of the ASP.NET MVC team (Phil Haack, Scott Hanselman..) which forms part of their new book (also on the topic of ASP.NET MVC).
 
They have posted up a free PDF of part of the [...]]]></description>
			<content:encoded><![CDATA[<p>ScottGu has posted about a new sample ASP.NET MVC application which has been put together by some of the vocal members of the ASP.NET MVC team (Phil Haack, Scott Hanselman..) which forms part of their new book (also on the topic of ASP.NET MVC).</p>
<p><img style="display: block; float: none; margin-left: auto; margin-right: auto" src="http://weblogs.asp.net/blogs/scottgu/bookcover1_6CAECF94.png" /> </p>
<p>They have posted up a <a href="http://tinyurl.com/aspnetmvc" target="_blank">free PDF of part of the book</a> which focused on getting started with ASP.NET MVC and building up the <a href="http://www.nerddinner.com/" target="_blank">NerdDinner</a> site – as well as having all the <a href="http://nerddinner.codeplex.com/" target="_blank">source code available on Codeplex</a> for you to make use of.</p>
<p>Thanks guys!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/useful-aspnet-mvc-resource/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC and LightSpeed – Building a Website</title>
		<link>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part1/</link>
		<comments>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part1/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 22:29:00 +0000</pubDate>
		<dc:creator>Jeremy Boyd</dc:creator>
				<category><![CDATA[LightSpeed]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.mindscape.co.nz/staff/jeremy/?p=47</guid>
		<description><![CDATA[There have been a number of recent posts discussing how to get started with ASP.NET MVC and various bits of kit, so I thought I would pop up my 2 cents on getting up and running with ASP.NET MVC and LightSpeed since I think they pair up quite nicely.
In addition we will also look at [...]]]></description>
			<content:encoded><![CDATA[<p>There have been a number of recent posts discussing how to get started with ASP.NET MVC and various bits of kit, so I thought I would pop up my 2 cents on getting up and running with ASP.NET MVC and LightSpeed since I think they pair up quite nicely.</p>
<p>In addition we will also look at leveraging:</p>
<ul>
<li><a href="http://jquery.com/">jQuery</a> – for our client side goodness</li>
<li><a href="http://www.nunit.org/index.php">NUnit</a> – for our unit testing</li>
<li><a href="http://ninject.org/">Ninject</a> – for managing our dependencies</li>
</ul>
<p><strong></strong></p>
<p><strong>What are we building?</strong></p>
<p>Last year for Tech Ed I used a sample built around a Film Festival website. I thought I would refresh the sample to use the latest bits following an earlier comment asking if I was going to publish the code (which I was originally intending to put on Codeplex but ended up getting lazy and letting it slip..)</p>
<p>So the domain centres around a schedule of films running in cinemas around the country and our system allows people to locate films and then book some tickets.</p>
<p><strong>What’s the plan?</strong></p>
<p>Generally I find it best to start with the an initial cut of the domain model, then we can set up some unit tests to cover the model and after we are happy with that we can then look at getting the web site itself set up. Finally we can start building out some functionality.</p>
<p><strong>So lets start with the Domain Model</strong></p>
<p>Like most developers I am fairly used to modelling the data first using a tool like SQL Server Management Studio diagramming. I also happen to have the database I already developed for this solution last year, so I think we can continue with that. It looks like this:</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot002.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Shot002" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot002-thumb.png" border="0" alt="Shot002" width="581" height="482" /></a></p>
<p>Note that the KeyTable table is some plumbing that we will use with LightSpeed to support our identity strategy. This is using the KeyTable identity pattern described by Fowler in <a href="http://www.amazon.com/Enterprise-Application-Architecture-Addison-Wesley-Signature/dp/0321127420/ref=sr_11_1?ie=UTF8&amp;qid=1236290078&amp;sr=11-1">PoEAA</a>.</p>
<p><strong>Creating our Domain Model using LightSpeed</strong></p>
<p>Now that we have our data definition of the model, lets bring that into a working domain model by using LightSpeed to describe our entities. This is going to be pretty straightforward, since we can use the Visual Studio designer to drag on our tables from our database definitions and then extend them from there.</p>
<p>I am going to start by setting up a fresh solution with a single class library project called Model. This is where we will set up our domain model entities.</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot003.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Shot003" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot003-thumb.png" border="0" alt="Shot003" width="383" height="200" /></a></p>
<p>Next we need to add a new LightSpeed model. If you have not already installed it (and why not?!?) – download and install the trial version of <a href="http://www.mindscape.co.nz/products/lightspeed/download.aspx">LightSpeed</a>, which will add in the associated templates into Visual Studio.</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot004.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Shot004" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot004-thumb.png" border="0" alt="Shot004" width="499" height="307" /></a></p>
<p>Once we add this, we get the LightSpeed design surface, which will allow us to drag tables from our server explorer over on to the surface and that code gen’s the associated entities into classes for us to use. Also because we are using .NET 3.5 it will also build us a LINQ context which we can use to start querying with LINQ straight away.</p>
<p>This should look somewhat familiar to our earlier ERD <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot005.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Shot005" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot005-thumb.png" border="0" alt="Shot005" width="601" height="342" /></a></p>
<p>If you have a look at Model.cs which is a resource associated with the LightSpeed .lsmodel file, you can see what has been code generated – a set of partial classes with the field, property and relationshop definitions which match the model which we have described.</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot007.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Shot007" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/03/shot007-thumb.png" border="0" alt="Shot007" width="600" height="367" /></a></p>
<p>So we now have a working domain model which we can start extending with some behaviour. To do this, we just need to create a partial class for the entity we want to extend. Let use the Cinema as an example, you will notice we have an object called GeoLocation which we are going to store as a SqlGeography data type in SQL Server. We may want to add a property to this to return us the co-ordinates of that location as a formatted string.</p>
<p>We would just create a new class file, Cinema.cs and fill it out as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Types</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> Model
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Cinema
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Coordinates
    <span style="color: #000000;">&#123;</span>
      get
      <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>GeoLocation <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;0,0&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">try</span>
        <span style="color: #000000;">&#123;</span>
          SqlGeography instance <span style="color: #008000;">=</span> SqlGeography.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>GeoLocation.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
          <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0},{1}&quot;</span>, instance.<span style="color: #0000FF;">Lat</span>, instance.<span style="color: #FF0000;">Long</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;0,0&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong></strong></p>
<p><strong>We now have our initial Domain Model</strong></p>
<p>In a very short space of time we have taken our existing data model, represented this as a domain model by using LightSpeed to reflect the data schema and automatically generate a class definition for each entity we have described. We can take the Model assembly and in conjunction with LightSpeed we can fetch and persist these entities against a database.</p>
<p><strong></strong></p>
<p><strong>Playing along at home?</strong></p>
<p>Here is the code and associated database setup script (remember to create a database called FilmFestival and run the script under the context of that database first!) to cover what we have done so far. Also remember to download and install LightSpeed</p>
<p>Download <a href="http://turtle.net.nz/blog/content/FilmFestival2009_Part1.zip">FilmFestival2009_Part1.zip </a>(8KB)</p>
<p><strong>Ok &#8211; What&#8217;s next?</strong></p>
<p>Next we should look at setting up some tests for the model, and then get our web infrastructure underway using ASP.NET MVC..</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Loving ASP.NET MVC</title>
		<link>http://www.mindscapehq.com/staff/jeremy/index.php/2009/02/loving-aspnet-mvc/</link>
		<comments>http://www.mindscapehq.com/staff/jeremy/index.php/2009/02/loving-aspnet-mvc/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 02:06:21 +0000</pubDate>
		<dc:creator>Jeremy Boyd</dc:creator>
				<category><![CDATA[LightSpeed]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.mindscape.co.nz/staff/jeremy/?p=25</guid>
		<description><![CDATA[ I thought I would share a few thoughts about ASP.NET MVC, as I have really enjoyed working with it over the last year. As you are likely aware this has been an “out of band” release, but is going to be bundled with .NET 4.0 so we can expect it to be a mainstay in [...]]]></description>
			<content:encoded><![CDATA[<p> I thought I would share a few thoughts about ASP.NET MVC, as I have really enjoyed working with it over the last year. As you are likely aware this has been an “out of band” release, but is going to be bundled with .NET 4.0 so we can expect it to be a mainstay in the .NET world going forward.</p>
<p>While not the most sophisticated pieces of technology out there, it stands out as a great example of Microsoft “getting it right” in recent times. The openness of the team, the responsiveness to feedback and the good design of the framework are what stand out for me.</p>
<p>For the latter part of last year, JD and I spent some time building out a reasonable size ASP.NET MVC implementation for <a href="http://www.givealittle.co.nz/" target="_blank">Givealittle.co.nz</a> which gave us some interesting insight into building a real world app on the framework.</p>
<p>The solution itself comprised of around 10000 lines of code, a suite of ~600 unit tests and is broken into 6 projects (Common, Model, Security, Web, Routes and UnitTests) – an integration build takes ~1 minute on our build server.</p>
<p><strong>So how did it work?</strong></p>
<p>Back when we first kicked off the project, the bits were at Preview 1, when we finished they were at Beta, now they are at RC. So quite a bit has happened in between, but the solution hasn&#8217;t changed too much architecturally and looks logically like this:</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/02/mvclogical.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MVCLogical" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/02/mvclogical-thumb.png" border="0" alt="MVCLogical" width="605" height="443" /></a></p>
<p>As you can see we deviated a bit from the “out of the box” offering with MVC on a few fronts:</p>
<p>We chose to use <a href="http://code.google.com/p/nhaml/" target="_blank">NHaml</a> for our View Engine for two reasons, first we really liked the symmetry with Rails, and secondly it made it feel more connected to the HTML and CSS being used in the solution. If you are in the same camp you should <a href="http://andrewpeters.net/2007/12/19/introducing-nhaml-an-aspnet-mvc-view-engine/" target="_blank">give NHaml a look</a>. There are some other interesting view engines available out there so remember to shop around and find one which gives you the best fit.</p>
<p>For the RIA aspects, we used <a href="http://jquery.com/" target="_blank">jQuery</a> – a totally awesome JavaScript framework which if you are not using – <a href="http://docs.jquery.com/Tutorials" target="_blank">you should be</a>! And of course we totally loved seeing the team pick up on this as well so this is actually now “out of the box” as such <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>We chose to use <a href="http://www.mindscape.co.nz/products/LightSpeed/default.aspx" target="_blank">LightSpeed</a> (of course) for our domain model, as this offered us a lot of advantages in getting up and running quickly (we generally modelled on paper, set up the entities in the database and then code-gen’ed our entities through the Visual Studio designer). All the behavioural aspects of the model were then added through partial classes. Another big advantage we got from using LightSpeed entities was the model validation which we used to manage a lot of the validation burden for form unbinding since most of it targeted new or existing entities in the model.</p>
<p>We used WCF to handle the distributed aspects of the solution (mainly in dealing with payment providers and the like). As most “real” solutions these days are likely to have some kind of distributed resources that they need to work with I think this would generally be considered “out of the box” <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>So what did we learn?</strong></p>
<p>1. By itself, the framework alone is not going to cut it for most real world apps. And by this I mean there are plenty of optimizations you are likely to make when building out an application on MVC which mean that you will ultimately develop your own “wrapper framework” which is based on MVC. Example of this for us were handling routing, adding extensions for link generation, adding in custom action results, adding in custom filters for concerns such as security and so on.</p>
<p>2. The model binding capabilities are great. Learn about how to extend these to more effectively manage your incoming data.</p>
<p>3. Routing can quickly become a PITA when your URL structure is not {controller}/{action}/* – most of the samples and guidance out there seem to prescribe an overly generic site structure – nice if you can handle it, but generally there are going to be plenty of edges to deal with. In Givealittle we had a custom structure with many of the concerns being duplicated across multiple domains in the site. e.g. Adding a blog post to Organisations and Projects – clearly we didn&#8217;t want to have the functionality duplicated in both of these controllers to save a few additional routes. The new routing API is great, and a welcome addition to the framework, however can be quite unwieldy for adding routes manually with the use of Constraint collections, Defaults collections and the like.. We simplified this for ourselves by using a helper which would build up a route on our behalf based on this style:</p>
<p><a href="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/02/routebuilder.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="RouteBuilder" src="http://www.mindscape.co.nz/staff/jeremy/wp-content/uploads/2009/02/routebuilder-thumb.png" border="0" alt="RouteBuilder" width="428" height="166" /></a></p>
<p>.Get() indicates it is a GET request for the resource such as /login which would then map to the Home controller, Login action.</p>
<p><strong>So how did it compare?</strong></p>
<p>Having now built solutions using WebForms, Monorail and even Rails, there are a number of clear benefits I think can be gained by using MVC.</p>
<p>These would be:</p>
<ul>
<li>Well suited for internet facing solutions where tighter control over the HTML/CSS matters</li>
<li>Improved quality from higher test coverage – the controllers are easily testable</li>
<li>Paired with the Routing framework you get great control over how the site structure is mapped</li>
<li>Simpler – there is a lot less going on with ASP.NET MVC compared to say WebForms so it was much easier to be productive and later on much easier to refactor. Compared to say Monorail, the lack of a lot of the “out of the box” infrastructure was also useful as there was less to contend with up front. I think this makes it more approachable for developers.</li>
</ul>
<p>Of course you are closer to the “bare metal” so you do have to have a better understanding of the web. Keep that in mind.</p>
<p>If none of those benefits apply or are high priorities, then you might find you are best off sticking with something else, but I reckon MVC is the way forward for building web applications on the .NET framework <img src='http://www.mindscapehq.com/staff/jeremy/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.mindscapehq.com/staff/jeremy/index.php/2009/02/loving-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

