<?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"
	>

<channel>
	<title>Technical Jargon</title>
	<atom:link href="http://www.jeremyskinner.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeremyskinner.co.uk</link>
	<description>Did you notice the information bar?</description>
	<pubDate>Tue, 23 Dec 2008 12:47:11 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>FluentValidation 0.2 Released</title>
		<link>http://www.jeremyskinner.co.uk/2008/12/20/fluentvalidation-02-released/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/12/20/fluentvalidation-02-released/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 14:04:44 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
		
		<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=83</guid>
		<description><![CDATA[The second release of my validation library, FluentValidation, is now available for download.
Changes in this version:

 Added Silverlight compatibility
 Moved support for the CommonServiceLocator into a separate assembly 
 Adding additional interfaces for some of the validators (ILengthValidator, INotNullValidator)
 AbstractValidator now implements IEnumerable&#60;IRuleBuilder&#60;T&#62;&#62;
 Added ShouldHaveValidationError extension method for testing validators

]]></description>
			<content:encoded><![CDATA[<p>The second release of my validation library, FluentValidation, is now <a href="http://fluentvalidation.codeplex.com">available for download.</a></p>
<p>Changes in this version:</p>
<ul>
<li> Added Silverlight compatibility</li>
<li> Moved support for the CommonServiceLocator into a separate assembly </li>
<li> Adding additional interfaces for some of the validators (ILengthValidator, INotNullValidator)</li>
<li> AbstractValidator now implements IEnumerable&lt;IRuleBuilder&lt;T&gt;&gt;</li>
<li> Added ShouldHaveValidationError extension method for testing validators</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/12/20/fluentvalidation-02-released/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using ModelState with MvcContrib&#8217;s Fluent HTML Helpers</title>
		<link>http://www.jeremyskinner.co.uk/2008/12/18/using-modelstate-with-mvccontribs-fluent-html-helpers/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/12/18/using-modelstate-with-mvccontribs-fluent-html-helpers/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 12:11:04 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
		
		<category><![CDATA[ASP.NET MVC]]></category>

		<category><![CDATA[mvccontrib]]></category>

		<category><![CDATA[aspnetmvc]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=71</guid>
		<description><![CDATA[What is ModelState?

The ASP.NET MVC ModelState dictionary allows you to record validation errors which can then be displayed in your web page.

For example, imagine you add a textbox to a page using the MVC framework&#8217;s built-in HTML helpers, then this will render a standard text field:


Your name: &#60;%= Html.TextBox&#40;&#34;Name&#34;&#41; %&#62;

In your controller, if you add [...]]]></description>
			<content:encoded><![CDATA[<h3>What is ModelState?</h3>
<p>
The ASP.NET MVC ModelState dictionary allows you to record validation errors which can then be displayed in your web page.
</p>
<p>For example, imagine you add a textbox to a page using the MVC framework&#8217;s built-in HTML helpers, then this will render a standard text field:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp asp" style="font-family:monospace;">Your name: <span style="color: #0000ff; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold">=</span> Html.<span style="color: #9900cc;">TextBox</span><span style="color: #006600; font-weight:bold">&#40;</span><span style="color: #cc0000;">&quot;Name&quot;</span><span style="color: #006600; font-weight:bold">&#41;</span> <span style="color: #0000ff; font-weight: bold;">%&gt;</span></pre></div></div>

<p>In your controller, if you add an error to the ModelState dictionary with the same name as the textbox, then the HTML helper will change the styling of the input field by adding a css class of &#8220;input-validation-error&#8221;. </p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HomeController <span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">public</span> ActionResult Index<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
       ModelState.<span style="color: #0000FF;">AddModelError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span>, <span style="color: #666666;">&quot;Please enter a name&quot;</span><span style="color: #000000;">&#41;</span>;
       <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> MyViewModel <span style="color: #000000;">&#123;</span> Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Using the default ASP.NET MVC template, it will add a red border with a light pink background:</p>
<p><img src="/files/textbox-error.png" alt="Textbox error" style="border:1px solid #bebebe" /></p>
<p>
You can also display the actual validation errors by making use of the ValidationSummary helper:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp asp" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold">=</span> Html.<span style="color: #9900cc;">ValidationSummary</span><span style="color: #006600; font-weight:bold">&#40;</span><span style="color: #006600; font-weight:bold">&#41;</span> <span style="color: #0000ff; font-weight: bold;">%&gt;</span>
Enter your name: <span style="color: #0000ff; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold">=</span> Html.<span style="color: #9900cc;">TextBox</span><span style="color: #006600; font-weight:bold">&#40;</span><span style="color: #cc0000;">&quot;Name&quot;</span><span style="color: #006600; font-weight:bold">&#41;</span> <span style="color: #0000ff; font-weight: bold;">%&gt;</span></pre></div></div>

<p>Which will display something like this:</p>
<p><img src="/files/textbox-validationsummary.png" style="border:1px solid #bebebe" alt="Validation Summary" /></p>
<h3>ModelState with FluentHtml</h3>
<p>
By default, the Fluent HTML helpers in MvcContrib do not have support for ModelState. However, it is very easy to add support for this by writing a custom MemberBehaviour (I previously wrote about extending the HTML helpers with MemberBehaviors  <a href="http://www.jeremyskinner.co.uk/2008/12/13/integrating-fluentvalidation-with-mvccontribs-fluent-html-helpers/">here</a>)
</p>
<p>
Here is the implementation of the ModelStateMemberBehavior class:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ModelStateMemberBehavior <span style="color: #008000;">:</span> IMemberBehavior <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> ModelStateDictionary modelState;
&nbsp;
	<span style="color: #0600FF;">public</span> ModelStateMemberBehavior<span style="color: #000000;">&#40;</span>ModelStateDictionary modelState<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">modelState</span> <span style="color: #008000;">=</span> modelState;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Execute<span style="color: #000000;">&#40;</span>IMemberElement element<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		ModelState state;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>element.<span style="color: #0000FF;">Builder</span>.<span style="color: #0000FF;">Attributes</span>.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;name&quot;</span><span style="color: #000000;">&#41;</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>modelState.<span style="color: #0000FF;">TryGetValue</span><span style="color: #000000;">&#40;</span>element.<span style="color: #0000FF;">Builder</span>.<span style="color: #0000FF;">Attributes</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;name&quot;</span><span style="color: #000000;">&#93;</span>, <span style="color: #0600FF;">out</span> state<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				element.<span style="color: #0000FF;">Builder</span>.<span style="color: #0000FF;">AddCssClass</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;input-validation-error&quot;</span><span style="color: #000000;">&#41;</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>This class will check whether the name of the element being rendered is present in the specified ModelState dictionary. If so, it will add a css class of &#8220;input-validation-error&#8221; to the element being rendered.</p>
<p>
Now we need to hook this into the HTML helpers. This can be done by implementing the IViewModelContainer interface on your base View Page:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyBaseViewPage<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> ViewPage<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>, IViewModelContainer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">private</span> List<span style="color: #008000;">&lt;</span>IMemberBehavior<span style="color: #008000;">&gt;</span> memberBehaviors <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>IMemberBehavior<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
   <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> SetViewData<span style="color: #000000;">&#40;</span>ViewDataDictionary viewData<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">SetViewData</span><span style="color: #000000;">&#40;</span>viewData<span style="color: #000000;">&#41;</span>;
      behaviors.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ModelStateMemberBehavior<span style="color: #000000;">&#40;</span>ViewData.<span style="color: #0000FF;">ModelState</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF;">public</span> T ViewModel <span style="color: #000000;">&#123;</span>
      get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> ViewData.<span style="color: #0000FF;">Model</span>; <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF;">public</span> IEnumerable<span style="color: #008000;">&lt;</span>IMemberBehavior<span style="color: #008000;">&gt;</span> MemberBehaviors <span style="color: #000000;">&#123;</span>
	get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> memberBehaviors; <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now, so long as all your view pages inherit from MyBaseViewPage<T>, any calls to the fluent HTML helpers will have support for ModelState:</p>

<div class="wp_syntax"><div class="code"><pre class="asp asp" style="font-family:monospace;">Your name: <span style="color: #0000ff; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold">=</span> this.<span style="color: #9900cc;">TextBox</span><span style="color: #006600; font-weight:bold">&#40;</span>model <span style="color: #006600; font-weight: bold">=&gt;</span> model.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold">&#41;</span> <span style="color: #0000ff; font-weight: bold;">%&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/12/18/using-modelstate-with-mvccontribs-fluent-html-helpers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Integrating FluentValidation with MvcContrib&#8217;s Fluent Html Helpers</title>
		<link>http://www.jeremyskinner.co.uk/2008/12/13/integrating-fluentvalidation-with-mvccontribs-fluent-html-helpers/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/12/13/integrating-fluentvalidation-with-mvccontribs-fluent-html-helpers/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 13:00:21 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
		
		<category><![CDATA[Apple]]></category>

		<category><![CDATA[LLBLGen Pro]]></category>

		<category><![CDATA[MonoRail]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=66</guid>
		<description><![CDATA[A recent addition to the MvcContrib project are a set of HTML helpers that use a fluent interface written by Tim Scott.


One of the great things about these helpers is that you can change how the HTML is rendered by writing custom IMemberBehavior objects. For example, the helpers come with a set of attributes that [...]]]></description>
			<content:encoded><![CDATA[<p>A recent addition to the <a href="http://mvccontrib.org">MvcContrib</a> project are a set of HTML helpers that use a fluent interface written by <a href="http://lunaverse.wordpress.com/2008/11/24/mvcfluenthtml-fluent-html-interface-for-ms-mvc/">Tim Scott.</a>
</p>
<p>
One of the great things about these helpers is that you can change how the HTML is rendered by writing custom IMemberBehavior objects. For example, the helpers come with a set of attributes that you can use to decorate your model:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Customer <span style="color: #000000;">&#123;</span>
   <span style="color: #000000;">&#91;</span>MaxLength<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
   <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Then, when you render your view, the HTML helper will automatically detect the presence of this attribute and render a &#8220;maxlength&#8221; attribute accordingly. For example, this textbox helper&#8230;
</p>

<div class="wp_syntax"><div class="code"><pre class="asp asp" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold">=</span> this.<span style="color: #9900cc;">TextBox</span><span style="color: #006600; font-weight:bold">&#40;</span>m <span style="color: #006600; font-weight: bold">=&gt;</span> m.<span style="color: #9900cc;">Customer</span>.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold">&#41;</span> <span style="color: #0000ff; font-weight: bold;">%&gt;</span></pre></div></div>

<p>&#8230;would render this html:</p>

<div class="wp_syntax"><div class="code"><pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">maxlength</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;Customer_Name&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Customer.Name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>
I wanted to take this a step further and integrate it with <a href="http://codeplex.com/FluentValidation">my validation library</a>, so the HTML helpers will automatically pick up maxlength/required attributes from my validator classes.
</p>
<h3>Step 1: Custom Validator Class</h3>
<p>The first stage was to create a metadata class to hold information about a validator.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PropertyModel <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> MaxLength <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Required <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">public</span> PropertyInfo Property <span style="color: #000000;">&#123;</span> get; <span style="color: #0600FF;">private</span> set; <span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> PropertyModel<span style="color: #000000;">&#40;</span>PropertyInfo property<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		Property <span style="color: #008000;">=</span> property;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Next, I introduced a new interface, IPropertyDescriptor, that all my validator classes will implement. This interface defines the signature for a method that creates PropertyModel instances based on a PropertyInfo:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IPropertyDescriptor <span style="color: #000000;">&#123;</span>
	PropertyModel GetPropertyModel<span style="color: #000000;">&#40;</span>PropertyInfo property<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Next, I added a custom base-class for all my validators which implements the above interface:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> abstract <span style="color: #FF0000;">class</span> Validator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>, IPropertyDescriptor <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> PropertyModel GetPropertyModel<span style="color: #000000;">&#40;</span>PropertyInfo property<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var model <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyModel<span style="color: #000000;">&#40;</span>property<span style="color: #000000;">&#41;</span>;
&nbsp;
		var validators <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>IPropertyValidatorContainer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Property</span> <span style="color: #008000;">==</span> property<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Validator</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var validator <span style="color: #0600FF;">in</span> validators<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>HandleLengthValidator<span style="color: #000000;">&#40;</span>validator, model<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> continue;
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>HandleRequiredValidator<span style="color: #000000;">&#40;</span>validator, model<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> continue;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> model;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> HandleLengthValidator<span style="color: #000000;">&#40;</span>IPropertyValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> validator, PropertyModel model<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var length <span style="color: #008000;">=</span> validator <span style="color: #0600FF;">as</span> ILengthValidator;
		<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>length <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			model.<span style="color: #0000FF;">MaxLength</span> <span style="color: #008000;">=</span> length.<span style="color: #0000FF;">Max</span>;
			<span style="color: #0600FF;">return</span> true;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">return</span> false;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> HandleRequiredValidator<span style="color: #000000;">&#40;</span>IPropertyValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> validator, PropertyModel model<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var required <span style="color: #008000;">=</span> validator <span style="color: #0600FF;">as</span> INotNullValidator;
		<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>required <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			model.<span style="color: #0000FF;">Required</span> <span style="color: #008000;">=</span> true;
			<span style="color: #0600FF;">return</span> true;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">return</span> false;
	<span style="color: #000000;">&#125;</span>	
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Essentially, the GetPropertyModel method iterates through all of the property validators to see if any required or max-length validators have been defined. If so, it records them in the PropertyModel object.
</p>
<p>I can now define my validators as usual, but they now inherit from my new Validator&lt;T&gt; class, rather than AbstractValidator&lt;T&gt;.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomerValidator <span style="color: #008000;">:</span> Validator<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">public</span> CustomerValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     RuleFor<span style="color: #000000;">&#40;</span>customer <span style="color: #008000;">=&gt;</span> customer.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#40;</span>0, <span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span>;
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Step 2: Custom Validator Factory</h3>
<p>
The next stage is to impelement a custom validator factory that can be used to instantiate validators. For this example, I&#8217;ll be using the <a href="http://castleproject.org">Windsor</a> IoC container.
</p>
<p>
Firstly, in my application startup routine I need to register all of my validators with Windsor:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">var container <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WindsorContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
container.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>AllTypes.<span style="color: #0000FF;">Of</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>IValidator<span style="color: #008000;">&lt;&gt;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FromAssembly</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>CustomerValidator<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Assembly</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithService</span>.<span style="color: #0600FF;">Base</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Next, I create a validator factory that uses Windsor (or more accurently, Microkernel) to create the validator instances.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ValidatorFactory <span style="color: #008000;">:</span> IValidatorFactory <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IKernel container;
&nbsp;
	<span style="color: #0600FF;">public</span> ValidatorFactory<span style="color: #000000;">&#40;</span>IKernel container<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">container</span> <span style="color: #008000;">=</span> container;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> GetValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> container.<span style="color: #0000FF;">TryResolve</span><span style="color: #008000;">&lt;</span>IValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IValidator GetValidator<span style="color: #000000;">&#40;</span>Type type<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var genericType <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>IValidator<span style="color: #008000;">&lt;&gt;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">MakeGenericType</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span>;
		<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>IValidator<span style="color: #000000;">&#41;</span>container.<span style="color: #0000FF;">TryResolve</span><span style="color: #000000;">&#40;</span>genericType<span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Note that TryResolve is an extension method on IKernel that will attept to resolve a component and return null if it fails (by default, Windsor/Microkernel will throw an exception).
</p>
<p>
This validator factory can now be registered with the container:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">container.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>Component.<span style="color: #0600FF;">For</span><span style="color: #008000;">&lt;</span>IValidatorFactory<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ImplementedBy</span><span style="color: #008000;">&lt;</span>ValidatorFactory<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<h3>
Step 3: The PropertyModelFactory<br />
</h3>
<p>The next stage is to create a class that can be used to locate the correct validator from a PropertyInfo object (by using the validator factory) and return a corresponding PropertyModel.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PropertyModelFactory <span style="color: #008000;">:</span> IPropertyDescriptor <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">private</span> IValidatorFactory validatorFactory;
&nbsp;
   <span style="color: #0600FF;">public</span> PropertyModelFactory<span style="color: #000000;">&#40;</span>IValidatorFactory validatorFactory<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">validatorFactory</span> <span style="color: #008000;">=</span> validatorFactory;
   <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> PropertyModel GetPropertyModel<span style="color: #000000;">&#40;</span>PropertyInfo property<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     var descriptor <span style="color: #008000;">=</span> validatorFactory.<span style="color: #0000FF;">GetValidator</span><span style="color: #000000;">&#40;</span>property.<span style="color: #0000FF;">ReflectedType</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> IPropertyDescriptor;
     <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>descriptor <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: #0600FF;">return</span> descriptor.<span style="color: #0000FF;">GetPropertyModel</span><span style="color: #000000;">&#40;</span>property<span style="color: #000000;">&#41;</span>;
     <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">return</span> null;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Again, this needs to be registered with the container:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">container.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>Component.<span style="color: #0600FF;">For</span><span style="color: #008000;">&lt;</span>IPropertyDescriptor<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ImplementedBy</span><span style="color: #008000;">&lt;</span>PropertyModelFactory<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<h3>Step 4: Custom IMemberBehavior</h3>
<p>
Next, we need to create a custom IMemberBehavior that will make use of the PropertyModelFactory:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ValidatorMemberBehaviour <span style="color: #008000;">:</span> IMemberBehavior <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IPropertyDescriptor descriptor;
&nbsp;
   <span style="color: #0600FF;">public</span> ValidatorMemberBehaviour<span style="color: #000000;">&#40;</span>IPropertyDescriptor descriptor<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">descriptor</span> <span style="color: #008000;">=</span> descriptor;
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Execute<span style="color: #000000;">&#40;</span>IMemberElement element<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>element.<span style="color: #0000FF;">ForMember</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> return;
&nbsp;
      var property <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">ForMember</span>.<span style="color: #0000FF;">Member</span> <span style="color: #0600FF;">as</span> PropertyInfo;
      <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>property <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> return;
&nbsp;
      var model <span style="color: #008000;">=</span> descriptor.<span style="color: #0000FF;">GetPropertyModel</span><span style="color: #000000;">&#40;</span>property<span style="color: #000000;">&#41;</span>;
      <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>model <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> return;
&nbsp;
      var maxLengthMethod <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetMethod</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;MaxLength&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
      <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>maxLengthMethod <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
         element.<span style="color: #0000FF;">Builder</span>.<span style="color: #0000FF;">MergeAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;maxlength&quot;</span>, model.<span style="color: #0000FF;">MaxLength</span>.<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: #000000;">&#125;</span>
      <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>model.<span style="color: #0000FF;">Required</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
         element.<span style="color: #0000FF;">Builder</span>.<span style="color: #0000FF;">AddCssClass</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;required&quot;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>MemberBehaviors are invoked when certain HTML helper objects are created, so this behaviour now needs to be registered with our ViewPage:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyViewPage<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> ModelViewPage<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">public</span> MyViewPage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
     <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ValidatorMemberBehaviour<span style="color: #000000;">&#40;</span>ServiceLocator.<span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>IPropertyDescriptor<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note that the ServicLocator static class is just a wrapper around Windsor.</p>
<p>So now the HTML Helpers will use my validator definitions to generate required/maxlength attributes. I think the extensibility of these helpers is incredibly powerful and I am planning on using them going forward in my next project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/12/13/integrating-fluentvalidation-with-mvccontribs-fluent-html-helpers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Interviewing .NET developers</title>
		<link>http://www.jeremyskinner.co.uk/2008/11/20/interviewing-net-developers/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/11/20/interviewing-net-developers/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 17:38:55 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=62</guid>
		<description><![CDATA[I was recently involved in interviewing several candidates for a .NET developer job.  This is the first time I&#8217;ve really been involved in interviewing and here are some initial thoughts:

If you say you know C# on your CV, I expect you to know it.
If you don&#8217;t understand a question or get stuck on a [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently involved in interviewing several candidates for a .NET developer job.  This is the first time I&#8217;ve really been involved in interviewing and here are some initial thoughts:</p>
<ul>
<li>If you say you know C# on your CV, I expect you to know it.</li>
<li>If you don&#8217;t understand a question or get stuck on a test, ask for clarification.</li>
<li>Just because someone has a Microsoft Certification doesn&#8217;t automatically make them a great developer. </li>
<li>Not being able to diagnose the cause of a simple NullReferenceException (when you have access to a debugger and the internet) is worrying.</li>
<li>Don&#8217;t just tell me what I want to hear - be honest.</li>
<li>Being able to show and demonstrate a project you&#8217;ve worked on creates a great impression.</li>
<li>Not being able to explain the last project you worked on creates a very bad impression.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/11/20/interviewing-net-developers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dependency Injection with ASP.NET MVC Action Filters</title>
		<link>http://www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/#comments</comments>
		<pubDate>Sat, 08 Nov 2008 18:59:49 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
		
		<category><![CDATA[ASP.NET MVC]]></category>

		<category><![CDATA[aspnetmvc]]></category>

		<category><![CDATA[autofac]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=52</guid>
		<description><![CDATA[Action Filter attributes in ASP.NET MVC are a very nice way of encapsulating logic into small reusable components that can easily be reused across multiple controllers.
However, if you need your action filters to do anything complex, injecting services into action filters using an Inversion of Control container (such as Windsor or StructureMap) can be painful. [...]]]></description>
			<content:encoded><![CDATA[<p>Action Filter attributes in ASP.NET MVC are a very nice way of encapsulating logic into small reusable components that can easily be reused across multiple controllers.</p>
<p>However, if you need your action filters to do anything complex, injecting services into action filters using an <a href="http://martinfowler.com/articles/injection.html">Inversion of Control container</a> (such as <a href="http://castleproject.org/container/index.html">Windsor</a> or <a href="http://structuremap.sourceforge.net/Default.htm">StructureMap</a>) can be painful. </p>
<p>Typically, when using an IoC container it is necessary to register all your components with the container up-front and then use the container to create instances for you. When the container creates the object, it will also create any required dependencies and pass them to the constructor (or property setters).</p>
<p>However, as ActionFilters are defined as attributes, they cannot be instantiated by the container. One solution to this problem is to wrap the container with a static class and then make use of constructor chaining to create the illusion of automatic dependency resolution:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LoggingFilter <span style="color: #008000;">:</span> ActionFilterAttribute <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">private</span> ILogger logger;
&nbsp;
  <span style="color: #0600FF;">public</span> LoggingFilter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#40;</span>IoC.<span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>ILogger<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> LoggingFilter<span style="color: #000000;">&#40;</span>ILogger logger<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">logger</span> <span style="color: #008000;">=</span> logger;
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnActionExecuting<span style="color: #000000;">&#40;</span>ActionExecutingContext context<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    logger.<span style="color: #0000FF;">log</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Entering action: &quot;</span> <span style="color: #008000;">+</span> context.<span style="color: #0000FF;">RouteData</span>.<span style="color: #0000FF;">GetRequiredString</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;action&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In this example, the default constructor will call into a static &#8216;IoC&#8217; class (which wraps the underlying container) to resolve an ILogger instance and then pass this to the second constructor. In a unit-testing scenario, you can make use of the second constructor directly in order to supply a mocked ILogger instance.</p>
<p>There are several things I don&#8217;t like about this code:</p>
<ol>
<li>The filter now has a coupling to the container</li>
<li>If you trigger the instantiation of the attribute without first initialising the container then this can lead to problems (eg reflecting over a method&#8217;s attributes in a unit-testing scenario)</li>
<li>It&#8217;s just plain ugly!</li>
</ol>
<p>Thankfully, there is a better way. Several IoC containers (including <a href="http://code.google.com/p/autofac">Autofac</a> and <a href="http://codeplex.com/unity">Unity</a>) can inject services into objects using property setters <strong>without the target object needing to be registered with the container.</strong> I&#8217;ll be using Autofac for the sample code.</p>
<p>
We can write a custom ControllerActionInvoker to make use of this feature by intercepting the action filters before they are invoked. The custom action invoker looks like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> MyApp <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span>;
  <span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span>;
  <span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span>;
  <span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.Mvc</span>;
  <span style="color: #0600FF;">using</span> <span style="color: #008080;">Autofac</span>;
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyActionInvoker <span style="color: #008000;">:</span> ControllerActionInvoker <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IContainer container;
&nbsp;
    <span style="color: #0600FF;">public</span> MyActionInvoker<span style="color: #000000;">&#40;</span>IContainer container<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">container</span> <span style="color: #008000;">=</span> container;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> ActionExecutedContext InvokeActionMethodWithFilters<span style="color: #000000;">&#40;</span>MethodInfo methodInfo, IDictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> parameters, IList<span style="color: #008000;">&lt;</span>IActionFilter<span style="color: #008000;">&gt;</span> filters<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 filter <span style="color: #0600FF;">in</span> filters<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        container.<span style="color: #0000FF;">InjectProperties</span><span style="color: #000000;">&#40;</span>filter<span style="color: #000000;">&#41;</span>;
      <span style="color: #000000;">&#125;</span>
      <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">InvokeActionMethodWithFilters</span><span style="color: #000000;">&#40;</span>methodInfo, parameters, filters<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
The custom action invoker takes an instance of IContainer (the autofac container) in its constructor, which it can then use to inject services into the action filter attributes before they are invoked. This is done by overriding the <strong>InvokeActionMethodWithFilters</strong> method and calling <strong>container.InjectProperties</strong> for each object in the filters collection.</p>
<p>
In order to get Autofac working with ASP.NET MVC, you need to register an HTTP module as well as set up the AutofacControllerFactory (<a href="http://code.google.com/p/autofac/wiki/MvcIntegration">instructions here</a>.)
</p>
<p>
It is also necessary to register the custom action invoker with Autofac in your global.asax:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">var builder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContainerBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
builder.<span style="color: #0000FF;">Register</span><span style="color: #008000;">&lt;</span>MyActionInvoker<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">As</span><span style="color: #008000;">&lt;</span>IActionInvoker<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FactoryScoped</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>
As well as the ActionInvoker, you also need to register your controllers. Autofac contains a &#8216;module&#8217; for doing this (the <a href="http://autofac.googlecode.com/svn/trunk/src/Source/Autofac.Integration.Web.Mvc/AutofacControllerModule.cs">AutofacControllerModule</a>), but we need to make a slight modification in order to use our custom action invoker. The modified module looks like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ControllerModule <span style="color: #008000;">:</span> Module <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> Assembly assembly;
  <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IControllerIdentificationStrategy controllerNamingStrategy <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DefaultControllerIdentificationStrategy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
  <span style="color: #0600FF;">public</span> ControllerModule<span style="color: #000000;">&#40;</span>Assembly assembly<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">assembly</span> <span style="color: #008000;">=</span> assembly;
  <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> Load<span style="color: #000000;">&#40;</span>ContainerBuilder builder<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    var controllers <span style="color: #008000;">=</span> from type <span style="color: #0600FF;">in</span> assembly.<span style="color: #0000FF;">GetExportedTypes</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                           where <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>IController<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">IsAssignableFrom</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span> 
			   where <span style="color: #008000;">!</span>type.<span style="color: #0000FF;">IsAbstract</span>
		           select type;
&nbsp;
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var type <span style="color: #0600FF;">in</span> controllers<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      builder.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span>
        .<span style="color: #0000FF;">FactoryScoped</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        .<span style="color: #0600FF;">As</span><span style="color: #000000;">&#40;</span>controllerNamingStrategy.<span style="color: #0000FF;">ServiceForControllerType</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        .<span style="color: #0600FF;">As</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span>
        .<span style="color: #0000FF;">OnActivating</span><span style="color: #000000;">&#40;</span>InjectInvoker<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> InjectInvoker<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, ActivatingEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>Controller<span style="color: #000000;">&#41;</span>e.<span style="color: #0000FF;">Instance</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ActionInvoker</span> <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">Context</span>.<span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>IActionInvoker<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Now when a controller is instantiated, the ActionInvoker property will be set to an instance of our custom action invoker. The module should be registered with the container builder:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">builder.<span style="color: #0000FF;">RegisterModule</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ControllerModule<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>HomeController<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Assembly</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>So, assuming an ILogger instance is registered with the container, the sample LoggingFilter could be written like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LoggingFilter <span style="color: #008000;">:</span> ActionFilterAttribute <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">public</span> ILogger Logger <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnActionExecuting<span style="color: #000000;">&#40;</span>ActionExecutingContext context<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    Logger.<span style="color: #0000FF;">log</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Entering action: &quot;</span> <span style="color: #008000;">+</span> context.<span style="color: #0000FF;">RouteData</span>.<span style="color: #0000FF;">GetRequiredString</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;action&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Much better!</p>
<p>
The same approach can also be used for other filter types, (such as AuthorizationFilters) by overriding the appropriate method.
</p>
<p>Note that container.InjectProperties makes use of reflection, so there is a potetial performance issue here.</p>
<p>
<strong>Edit (5 December 2008):</strong> Here is an extension method that adds the InjectProperties method to the Microkernel from Castle Windsor.
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Castle.MicroKernel</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Castle.MicroKernel.ComponentActivator</span>;
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> WindsorExtension <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> InjectProperties<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IKernel kernel, <span style="color: #FF0000;">object</span> target<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var type <span style="color: #008000;">=</span> target.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var property <span style="color: #0600FF;">in</span> type.<span style="color: #0000FF;">GetProperties</span><span style="color: #000000;">&#40;</span>BindingFlags.<span style="color: #0600FF;">Public</span> | BindingFlags.<span style="color: #0000FF;">Instance</span><span style="color: #000000;">&#41;</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>property.<span style="color: #0000FF;">CanWrite</span> <span style="color: #008000;">&amp;&amp;</span>  kernel.<span style="color: #0000FF;">HasComponent</span><span style="color: #000000;">&#40;</span>property.<span style="color: #0000FF;">PropertyType</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				var value <span style="color: #008000;">=</span> kernel.<span style="color: #0000FF;">Resolve</span><span style="color: #000000;">&#40;</span>property.<span style="color: #0000FF;">PropertyType</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span>
					property.<span style="color: #0000FF;">SetValue</span><span style="color: #000000;">&#40;</span>target, value, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					var message <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Error setting property {0} on type {1}, See inner exception for more information.&quot;</span>, property.<span style="color: #0000FF;">Name</span>, type.<span style="color: #0000FF;">FullName</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ComponentActivatorException<span style="color: #000000;">&#40;</span>message, ex<span style="color: #000000;">&#41;</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>

]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Lambda Validators - Moved to CodePlex</title>
		<link>http://www.jeremyskinner.co.uk/2008/11/06/lambda-validators-moved-to-codeplex/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/11/06/lambda-validators-moved-to-codeplex/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 21:22:59 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
		
		<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=51</guid>
		<description><![CDATA[I recently moved my validation library to CodePlex. It is now known as &#8216;Fluent Validation&#8217; and can be found here.
]]></description>
			<content:encoded><![CDATA[<p>I recently moved my validation library to CodePlex. It is now known as &#8216;Fluent Validation&#8217; and <a href="http://www.codeplex.com/FluentValidation">can be found here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/11/06/lambda-validators-moved-to-codeplex/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Storing ModelState in TempData with ASP.NET MVC</title>
		<link>http://www.jeremyskinner.co.uk/2008/10/18/storing-modelstate-in-tempdata-with-aspnet-mvc/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/10/18/storing-modelstate-in-tempdata-with-aspnet-mvc/#comments</comments>
		<pubDate>Sat, 18 Oct 2008 15:19:15 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
		
		<category><![CDATA[ASP.NET MVC]]></category>

		<category><![CDATA[mvccontrib]]></category>

		<category><![CDATA[aspnetmvc]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=44</guid>
		<description><![CDATA[
MVC Preview 5 introduced the contept of ModelState for storing validation failures.


As the ModelState is part of the ViewData, it is only available for the current request. This means that you cannot redirect to another action in order to show the error summary, instead you have to render a view directly from your Save action. [...]]]></description>
			<content:encoded><![CDATA[<p>
MVC Preview 5 <a href="http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx">introduced the contept of ModelState</a> for storing validation failures.
</p>
<p>
As the ModelState is part of the ViewData, it is only available for the current request. This means that you cannot redirect to another action in order to show the error summary, instead you have to render a view directly from your <em>Save</em> action. Personally I don&#8217;t think this is a good idea - the Save action should just try and save changes, and then redirect to another action to display validation errors.
</p>
<p>
One solution is to store the ModelState inside the TempData (as anything added to the TempData will be made available to the next HTTP request). It is possible to use an ActionFilterAttribute to make this happen automatically.
</p>
<p>
Under the covers, TempData makes use of ASP.NET Session State which means that every object that is stored in TempData needs to be marked as Serializable if you are using an out of process session store. Unfortunately, the ModelStateDictionary is not marked as serializable, so in order for this approach to work it is necessary to copy the contents of the ModelState into a temporary serializable dictionary.
</p>
<p>
I&#8217;ve made the code <a href="http://mvccontrib.googlecode.com/svn/trunk/src/MVCContrib/Filters/ModelStateToTempDataAttribute.cs">available in MvcContrib</a>. You can use it by decorating your controllers with the ModelStateToTempData attribute:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">MvcContrib.Filters</span>;
&nbsp;
<span style="color: #000000;">&#91;</span>ModelStateToTempData<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyController <span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">//...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
The filter has the following features:</p>
<ul>
<li>If your controller action returns a RedirectToRouteResult, anything in the ModelState dictionary will be wrapped in a ModelStateSerializable class and stored in TempData.</li>
<li>If your action returns a ViewResult, then any ModelState objects that were previously copied to TempData will be re-added to the ModelState dictionary.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/10/18/storing-modelstate-in-tempdata-with-aspnet-mvc/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Lambda Validators Part 2 - Custom Validators</title>
		<link>http://www.jeremyskinner.co.uk/2008/07/26/lambda-validators-part-2-custom-validators/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/07/26/lambda-validators-part-2-custom-validators/#comments</comments>
		<pubDate>Sat, 26 Jul 2008 12:28:01 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
		
		<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=40</guid>
		<description><![CDATA[I previously posted about a small validation library that I wrote that uses lambda expressions for defining validation rules.

Over the last couple of days I&#8217;ve spent some time refactoring it in order to support custom validator classes which can be useful in keeping your validation logic nicely hidden and out of the way.
So imagine you [...]]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://www.jeremyskinner.co.uk/2008/06/28/validating-with-lambda-expressions/">previously posted</a> about a small validation library that I wrote that uses lambda expressions for defining validation rules.
</p>
<p>Over the last couple of days I&#8217;ve spent some time refactoring it in order to support custom validator classes which can be useful in keeping your validation logic nicely hidden and out of the way.</p>
<p>So imagine you have a set of validation rules like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">var person <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
var validator <span style="color: #008000;">=</span> Validator.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>person<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Rules</span><span style="color: #000000;">&#40;</span>
	rule <span style="color: #008000;">=&gt;</span> rule.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Forename</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Lengh</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span>,
	rule <span style="color: #008000;">=&gt;</span> rule.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEqual</span><span style="color: #000000;">&#40;</span>0<span style="color: #000000;">&#41;</span>,
	rule <span style="color: #008000;">=&gt;</span> rule.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Lengh</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #FF0000;">bool</span> success <span style="color: #008000;">=</span> validator.<span style="color: #0000FF;">Validate</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Using a custom validator, the validation rules can now be moved into a separate class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyPersonValidator <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> MyPersonValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		RuleFor<span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Forename</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Lengh</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span>;
		RuleFor<span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEqual</span><span style="color: #000000;">&#40;</span>0<span style="color: #000000;">&#41;</span>;
		RuleFor<span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Lengh</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>&#8230;and now the code for doing the validation is greatly simplified:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">var person <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
var validator <span style="color: #008000;">=</span> Validator.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>person<span style="color: #000000;">&#41;</span>;
<span style="color: #FF0000;">bool</span> success <span style="color: #008000;">=</span> validator.<span style="color: #0000FF;">Validate</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>
In order for this to work you will need to register the validator class with your favourite IoC container and then tell the <em>ValidatorFactory</em> class to use that IoC container to instantiate the validators. Typically you would do this in your application&#8217;s startup routine - Application_Start for an ASP.NET app. </p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Application_Start<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">//Set up windsor</span>
	IoC.<span style="color: #0000FF;">Initialise</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> WindsorContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #008080; font-style: italic;">//Register the validator with Windsor</span>
	IoC.<span style="color: #0000FF;">Container</span>.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>
		Component.<span style="color: #0600FF;">For</span><span style="color: #008000;">&lt;</span>IValidator<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ImplementedBy</span><span style="color: #008000;">&lt;</span>MyPersonValidator<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">LifeStyle</span>.<span style="color: #0000FF;">Transient</span>;
	<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #008080; font-style: italic;">//Tell the ValidatorFactory to use Windsor to look for validators:</span>
	JS.<span style="color: #0000FF;">Validation</span>.<span style="color: #0000FF;">ValidatorFactory</span>.<span style="color: #0000FF;">Initialise</span><span style="color: #000000;">&#40;</span>type <span style="color: #008000;">=&gt;</span> IoC.<span style="color: #0000FF;">TryResolve</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In the above example, the <em>IoC</em> class is a static wrapper around the <a href="http://www.castleproject.org/container/index.html">Windsor container</a></p>
<h2>How does it work?</h2>
<p>
When you call <em>Validator.For(object)</em>, the ValidatorBuilder will ask the ValidatorFactory for an IValidator&lt;T&gt; (where T is the object being validated), and the delegate that you passed to <em>ValidatorFactory.Initialise()</em> is invoked with that type as its parameter.
</p>
<p>I decided to use this approach as I didn&#8217;t really want to create a dependency on a particular IoC container.</p>
<p>The validation rules defined in the validation class are combined with any rules defined by calling <em>Validator.For(object).Rules()</em>, so you can mix and match validation rules as necessary.</p>
<h2>Custom Property Validators</h2>
<p>It is also possible to create custom property validators. For example, to create a custom validator that checks whether a property&#8217;s value is not equal to &#8220;Foo&#8221;, you might do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">Validator.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>person<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Rules</span><span style="color: #000000;">&#40;</span>
	rule <span style="color: #008000;">=&gt;</span> rule.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> NotEqualToFooValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>NotEqualToFooValidator would be defined like so:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NotEqualToFooValidator <span style="color: #008000;">:</span> PropertyValidatorBase <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> Validate<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> instance, <span style="color: #FF0000;">object</span> value<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: #666666;">&quot;Foo&quot;</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			Error <span style="color: #008000;">=</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}' must not be equal to 'Foo'.&quot;</span>, FieldName<span style="color: #000000;">&#41;</span>;
			<span style="color: #0600FF;">return</span> false;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">return</span> true;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This can also be wrapped in an extension method&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> ValidatorExtensions <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> ILambdaPropertyValidatorBuilderWithOptions<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span> CannotBeFoo<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> ILambdaPropertyValidatorBuilder<span style="color: #008000;">&lt;</span>T, TProperty<span style="color: #008000;">&gt;</span> builder<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> builder.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> NotEqualToFooValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>&#8230;which makes thing a little more fluent:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">Validator.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>person<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Rules</span><span style="color: #000000;">&#40;</span>
	rule <span style="color: #008000;">=&gt;</span> rule.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CannotBeFoo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>For something this simple, you&#8217;d usually use the NotEqual validator (<em>rule.For(x => x.Surname).NotEqual(&#8221;Foo&#8221;)</em>), but you get the idea.</p>
<p>Any custom property validators can also be used in conjunction with the custom validator classes I mentioned above.</p>
<h2>NotEqualPropertyValidator</h2>
<p>I&#8217;ve also added  NotEqualProperty validator that ensures the value of one property is not equal to the value of another:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">Validator.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>person<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Rules</span><span style="color: #000000;">&#40;</span>
	rule <span style="color: #008000;">=&gt;</span> rule.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Forename</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">NotEqual</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Surname</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>As usual, the code is in my <a href="http://www.jeremyskinner.co.uk/svn/trunk">svn repository</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/07/26/lambda-validators-part-2-custom-validators/feed/</wfw:commentRss>
		</item>
		<item>
		<title>HandleErrorAttribute and Remote Connections</title>
		<link>http://www.jeremyskinner.co.uk/2008/07/23/handleerrorattribute-and-remote-connections/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/07/23/handleerrorattribute-and-remote-connections/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 08:15:20 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
		
		<category><![CDATA[ASP.NET MVC]]></category>

		<category><![CDATA[aspnetmvc]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=39</guid>
		<description><![CDATA[Last week I upgraded my main application to MVC Preview 4. This morning I noticed that a number of users were receiving HTTP 500 error messages instead of being presented with a friendly error page. 
It turns out the HandleErrorAttribute in Preview 4 is the culprit. Previously I&#8217;d been using MvcContrib&#8217;s RescueAttribute. 

It appears that [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I upgraded my main application to MVC Preview 4. This morning I noticed that a number of users were receiving HTTP 500 error messages instead of being presented with a friendly error page. </p>
<p>It turns out the HandleErrorAttribute in Preview 4 is the culprit. Previously I&#8217;d been using MvcContrib&#8217;s <a href="http://www.codeplex.com/MVCContrib/Wiki/View.aspx?title=Rescue&#038;referringTitle=Documentation">RescueAttribute</a>. </p>
<p>
It appears that the HandleError attribute only works locally - when using a remote connection the server just displays the standard HTTP 500 message (this is under IIS7 using Integrated Pipeline).
</p>
<p>
I fired up the MVC source code and removed this line from HandleErrorAttribute.cs (line 85):
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;">filterContext.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Response</span>.<span style="color: #0000FF;">StatusCode</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">500</span>;</pre></div></div>

<p>After doing this, the error pages are displayed correctly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/07/23/handleerrorattribute-and-remote-connections/feed/</wfw:commentRss>
		</item>
		<item>
		<title>ASP.NET MVC Preview 4</title>
		<link>http://www.jeremyskinner.co.uk/2008/07/18/aspnet-mvc-preview-4/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/07/18/aspnet-mvc-preview-4/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 13:47:53 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
		
		<category><![CDATA[ASP.NET MVC]]></category>

		<category><![CDATA[mvccontrib]]></category>

		<category><![CDATA[aspnetmvc]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=38</guid>
		<description><![CDATA[I spent yesterday updating our largest application to MVC Preview 4. As part of the upgrade, I also made some tweaks to MvcContrib&#8217;s ConventionController (documentation not yet updated).
Here&#8217;s a summary of some of the things that have changed:

TempData finally works with out of process session state
Filters that implement IExceptionFilter can easily catch exceptions thrown by [...]]]></description>
			<content:encoded><![CDATA[<p>I spent yesterday updating our largest application to MVC Preview 4. As part of the upgrade, I also made some tweaks to MvcContrib&#8217;s <a href="http://www.codeplex.com/MVCContrib/Wiki/View.aspx?title=ConventionController&#038;referringTitle=Documentation">ConventionController</a> (documentation not yet updated).</p>
<p>Here&#8217;s a summary of some of the things that have changed:</p>
<ul>
<li>TempData finally works with out of process session state</li>
<li>Filters that implement IExceptionFilter can easily catch exceptions thrown by other filters</li>
<li>The RescueAttribute in MvcContrib is now an IExceptionFilter, so it can be used with classes that don&#8217;t inherit from ConventionController</li>
<li>Multiple filters can now have the same sort order. This makes it easy to define a &#8216;default&#8217; order, and then have other filters execute before/after</li>
<li>TempData is now testable without needing to mock anything</li>
<li>All the ComponentController hackery is gone - RenderAction is a much nicer alternative</li>
<li>The <a href="http://www.jeremyskinner.co.uk/2008/06/03/aspnet-mvc-intercepting-the-routevaluedictionary/">bug that broke URL generation</a> is fixed</li>
</ul>
<p>Here&#8217;s what sucks with Preview 4:</p>
<ul>
<li>SubDataItems <a href="http://blog.eworldui.net/post/2008/06/Using-SubDataItems-and-View-User-Controls-in-ASPNET-MVC.aspx">still doesn&#8217;t seem to work properly</a></li>
<li>AuthorizeAttribute, OutputCacheAttribute and HandleError attribute are sealed and have no virtual methods</li>
<li>The new built in Ajax support doesn&#8217;t seem to be easily replaceable with alternative libraries</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/07/18/aspnet-mvc-preview-4/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
