<?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>the tar pit &#187; aspnetmvc</title>
	<atom:link href="http://blog.goneopen.com/tag/aspnetmvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.goneopen.com</link>
	<description>Thrashing around in the stickiness of software</description>
	<lastBuildDate>Mon, 09 Jan 2012 23:35:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>UrlBinder for model properties using Url as a type</title>
		<link>http://blog.goneopen.com/2011/02/aspnet-mvc-url-model-binder/</link>
		<comments>http://blog.goneopen.com/2011/02/aspnet-mvc-url-model-binder/#comments</comments>
		<pubDate>Sun, 27 Feb 2011 09:39:34 +0000</pubDate>
		<dc:creator>todd</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[aspnetmvc]]></category>

		<guid isPermaLink="false">http://blog.goneopen.com/2011/02/aspnet-mvc-url-model-binder/</guid>
		<description><![CDATA[I had the simple problem that my Url properties wouldn&#8217;t bind when return back into an action on a controller (in asp.net mvc 2). Take the model below, I want to have a destination as an Url. Seems fair? public class Banner { public string Name { get; set; } public Url Destination { get; [...]]]></description>
			<content:encoded><![CDATA[<p>I had the simple problem that my <code>Url</code> properties wouldn&#8217;t bind when return back into an action on a controller (in asp.net mvc 2). Take the model below, I want to have a destination as an Url. Seems fair?</p>



<pre class="brush:csharp">
  public class Banner
  {
      public string Name { get; set; }
      public Url Destination { get; set; }
  }
</pre>

<p> </p>

<p>Looking at the controller code below, when I enter the action where I had done a form post to I just couldn&#8217;t see a value in the <code>banner.Destination</code> but I could see one in <code>banner.Name</code>. </p>



<pre class="brush:csharp">
  public ActionResult Create(Banner banner)
   {
       // banner had no Destination value but did have Name  
       // ... rest of code
   }
</pre>



<p>Wow, I thought. No <code>Url</code>. HHhhhhmmmm. But I really want to be strongly typed &#8211; all my unit and integration tests worked and I felt that I wanted my domain model to remain, well, <em>true</em>. Given how long it took me to research the solution one suspects I should have just made the <code>Url</code> and <code>string</code>. But I didn&#8217;t and I&#8217;m glad. The solutions <em>is</em> simple and I understand the pipeline much better.</p>

<p>I need to implement <code>IModelBinder</code> for the model <code>Url</code> (which is used by the property). While <code>Url</code> isn&#8217;t a custom class for our domain it is treated as one by mvc. Here is my <code>UrlModelBinder</code> implementation and hooking it up. </p>



<pre class="brush:csharp">
  public class UrlBinder : IModelBinder
  {
      public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
      {
          if (!String.IsNullOrEmpty(bindingContext.ModelName))
          {
              var name = bindingContext.ModelName;
              var rawvalue = bindingContext.ValueProvider.GetValue(name).AttemptedValue;
              var value = new ValueProviderResult(rawvalue, rawvalue, CultureInfo.CurrentCulture);
              bindingContext.ModelState.SetModelValue(name, value);
              return new Url(rawvalue);  // I'm unclear that this needs to be returned
          }
          return null;
      }
  }
</pre>



<p>And then ensuring that it resolves against the <code>Url</code>.</p>



<pre class="brush:csharp">
  protected override void  Application_Start()
  { 
      ModelBinders.Binders.Add(typeof(Url), new UrlBinder());
  }
</pre>



<p>So, the key to this solution is understanding &#8211; and this is quite obvious now that I understand the implementation in <code>DefaultModelBinder</code> &#8211; is that <code>Url</code> is a <strong>model</strong> and not a <strong>property</strong>. I kept thinking that I needed bind the form value to the property. Well I do but for a &#8220;custom&#8221; type. <code>Url</code> is custom because (as the documentation clearly states) it is does not inherit from string. Only string properties are bind by default &#8211; that includes enumerations of string (eg <code>IList</code>). So once you know that, it is straightforward that the solution is to implement your own <code>IModelBinder</code> for <code>Url</code>. (If you are interested use Reflector or the source on CodePlex and look through the <code>DefaultModelBinder</code> &#8211; you&#8217;ll see the way they implement <code>BindSimpleModel</code> and <code>BindComplexModel</code>)</p>

<p>This binder is pretty naive so I expect as I learn more it will be improved. Something I didn&#8217;t expect was that I don&#8217;t really need to construct an <code>Url</code> because in the model binding at this stage I just need be able to have a binder for the type rather than worry about constructing the type. I do this by putting <code>rawvalue</code> into the <code>ValueProviderResult</code> &#8211; it only accepts strings. This code then gets picked up again in the <code>DefaultModelBinder</code> when it constructs the model (in this case <code>Banner</code>) which it then easily creates a <code>new Url(value)</code>.</p>

<p>A simple question, why isn&#8217;t there a binder for <code>Url</code> already in the code? Is not <code>Url</code> effectively primitive for the web? <em>Clearly</em> not.</p>

<p>If you&#8217;ve got this far and are interested there are other ways that you really don&#8217;t want to use to solve this problem:</p>


<ul>
<li>use an action filter (<code>[Bind(exclude=&quot;Destination&quot;)]</code>) and then read it from the <code>Request.Form</code></li>
<li>implement my own <code>IValueProvider</code> on the property <code>banner</code></li>
<li>rewrite the <code>ModelState</code> explicitly clearing errors first</li>
<li>implement an <code>IModelBinder</code> for the <code>Banner</code> class</li>
<li>and the biggest and most heavy-handed of all, subclass <code>DefaultModelBinder</code></li>
</ul>



<p>Luckily, none of these solutions are needed. None will feel right because they are either too specific to the class or property, too kludgy because they use <code>Request.Form</code> and just way too big for a small problem.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.goneopen.com/2011/02/aspnet-mvc-url-model-binder/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Asp.Net MVC &#8211; basic resources</title>
		<link>http://blog.goneopen.com/2009/07/using-aspnet-mvc-basic-resources-2/</link>
		<comments>http://blog.goneopen.com/2009/07/using-aspnet-mvc-basic-resources-2/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 21:18:04 +0000</pubDate>
		<dc:creator>todd</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[aspnetmvc]]></category>

		<guid isPermaLink="false">http://blog.goneopen.com/2009/07/using-aspnet-mvc-basic-resources-2/</guid>
		<description><![CDATA[I&#8217;ve just started working with a team on asp.net mvc. I&#8217;m now starting to create a list of resources that I am finding useful. You&#8217;ll notice that I have been reading around Steve Sanderson&#8217;s work because our team is tending to focus around his solutions. General books Steve Sanderson. Pro Asp.Net MVC Nerd Dinner &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just started working with a team on asp.net mvc. I&#8217;m now starting to create a list of resources that I am finding useful. You&#8217;ll notice that I have been reading around Steve Sanderson&#8217;s work because our team is tending to focus around his solutions.</p>

<h2>General books</h2>


<ul>
<li><a href="http://www.amazon.com/ASP-NET-Framework-Experts-Voice-NET/dp/1430228865" title="2nd Ed">Steve Sanderson. Pro Asp.Net <span class="caps">MVC</span></a></li>
<li><a href="http://www.wrox.com/WileyCDA/Section/id-321793.html">Nerd Dinner</a> &#8211; including free chapter</li>
</ul>



<h2>Sample Code Downloads</h2>


<ul>
<li><a href="http://blog.codeville.net/blogfiles/2009/June/IntegrationTestingDemo.zip">MvcIntegrationTestFramework</a></li>
<li><a href="http://blog.codeville.net/blogfiles/2009/February/xVal-0.8-BookingsDemo.zip">xVal</a></li>
<li><a href="http://code.google.com/p/codecampserver/">CodeCamp</a>  from <span class="caps">SVN </span>repos only <code>svn export http://codecampserver.googlecode.com/svn/trunk/</code></li>
<li><a href="http://nerddinner.codeplex.com/">Nerd Dinner</a></li>
<li><a href="http://mvcsamples.codeplex.com/"><span class="caps">MVC</span> Storefront</a></li>
<li><a href="http://blog.codeville.net/blogfiles/2009/May/ProASPNETMVC-SourceCode.zip">Pro Asp.net source code samples</a></li>
<li><a href="http://blog.codeville.net/blogfiles/2009/March/LightweightTestAutomationFrameworkMVCDemo.zip">Lightweight Test Automation Sample</a></li>
<li><a href="http://blog.codeville.net/blogfiles/2009/January/BookingsDemo.zip">Bookings Demo</a></li>
</ul>



<h2>Blogs/Tutorials</h2>


<ul>
<li><a href="http://blog.codeville.net/">Steve Sanderson&#8217;s site</a></li>
<li><a href="http://blog.codeville.net/2009/03/27/first-steps-with-lightweight-test-automation-framework/">Lightweight Test Automation</a></li>
<li><a href="http://blog.codeville.net/2009/06/11/integration-testing-your-aspnet-mvc-application/">MvcIntegrationTestFramework</a></li>
<li><a href="http://www.asp.net/Learn/mvc/"><span class="caps">MVC</span> Tutorials</a> &#8211; the official site</li>
<li><a href="http://nhforge.org/blogs/nhibernate/archive/2008/11/23/populating-entities-from-stored-procedures-with-nhibernate.aspx">nHibernate with stored procs</a></li>
<li><a href="http://nhforge.org">nHibernate forge</a></li>
</ul>



<h2>Libraries</h2>


<ul>
<li><a href="http://mvccontrib.codeplex.com"><span class="caps">MVC</span> Contrib</a></li>
<li><a href="http://mvccontrib.codeplex.com/Wiki/View.aspx?title=SimplyRestfulRouting&amp;referringTitle=Home">Simply Restful Routing</a></li>
<li><a href="http://docs.jquery.com/Plugins/Validation">jQuery Validator</a></li>
<li><a href="http://xval.codeplex.com/">xVal</a></li>
<li>&#8220;MvcIntegrationTestFramework&#8221;: unknown home &#8211; from the demo above</li>
<li><a href="http://www.castleproject.org/components/validator/index.html">Castle Validations</a></li>
<li><a href="http://nhforge.org/Default.aspx">nhibernate</a></li>
<li><a href="http://fluentnhibernate.org/">fluent nhibernate</a></li>
</ul>



<p>Other libraries that I will fill out as needed:</p>


<ul>
<li>moq</li>
<li>nbehave (for the assert helper syntax)</li>
<li>storyq  &#8211; acceptance testing</li>
<li>selenium/watin &#8211; browser testing</li>
<li>jquery/jsspec &#8211; for gui specific testing (see this blog for samples and jqueryplugingen for scaffolding)</li>
<li>sdc tasks &#8211; for deployment</li>
<li>teamcity/tfs for CI</li>
<li><a href="http://code.google.com/p/migratordotnet">migratordotnet</a> &#8211; for migrations (I&#8217;m also thinking SqlLite for base dev testing)</li>
<li>Gallio -</li>
</ul>



<p>Strategies:<br />
* fakes, object mothers and using extensions</p>]]></content:encoded>
			<wfw:commentRss>http://blog.goneopen.com/2009/07/using-aspnet-mvc-basic-resources-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

