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

<channel>
	<title>Technical Jargon &#187; C#</title>
	<atom:link href="http://www.jeremyskinner.co.uk/category/csharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeremyskinner.co.uk</link>
	<description>On Error Resume Coding</description>
	<lastBuildDate>Sun, 08 Aug 2010 13:36:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Lambda Abuse: The MvcContrib Hash</title>
		<link>http://www.jeremyskinner.co.uk/2009/12/02/lambda-abuse-the-mvccontrib-hash/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/12/02/lambda-abuse-the-mvccontrib-hash/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 20:44:59 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=255</guid>
		<description><![CDATA[There&#8217;s recently been a lot of discussion on this post on StackOverflow about the use of lambda expressions to build dictionaries as part of the MvcContrib grid. The Grid API allows you to define HTML attributes for a table by chaining a call to the &#8216;Attributes&#8217; method as part of a grid definition: &#60;%= Html.Grid&#40;Model.People&#41;.Columns&#40;column [...]]]></description>
			<content:encoded><![CDATA[<p>
There&#8217;s recently been a lot of discussion on <a href="http://stackoverflow.com/questions/1718037/abuse-of-c-lambda-expressions-or-syntax-brilliance">this post on StackOverflow</a> about the use of lambda expressions to build dictionaries as part of the <a href="http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/">MvcContrib grid</a>.
</p>
<p>
The Grid API allows you to define HTML attributes for a table by chaining a call to the &#8216;Attributes&#8217; method as part of a grid definition:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">People</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Columns</span><span style="color: #006600; font-weight:bold;">&#40;</span>column <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Id</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">DateOfBirth</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
.<span style="color: #9900cc;">Attributes</span><span style="color: #006600; font-weight:bold;">&#40;</span>style <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #cc0000;">&quot;width:100%&quot;</span>, cellpadding <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #800000;">0</span><span style="color: #006600; font-weight:bold;">&#41;</span>
 <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>
This makes use of the <a href="http://mvccontrib.codeplex.com/sourcecontrol/network/Show?projectName=MVCContrib&#038;changeSetId=877a66c13756#src%2fMVCContrib%2fHash.cs">MvcContrib.Hash class</a> to construct an IDictionary from lambda expressions. So the following Hash definiton&#8230;:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var hash <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Hash<span style="color: #000000;">&#40;</span>foo <span style="color: #008000;">=&gt;</span> <span style="color: #666666;">&quot;bar&quot;</span>, baz <span style="color: #008000;">=&gt;</span> <span style="color: #666666;">&quot;blah&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>&#8230;is the equivalent of:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var hash <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> 
<span style="color: #000000;">&#123;</span>
   <span style="color: #000000;">&#123;</span> <span style="color: #666666;">&quot;foo&quot;</span>, <span style="color: #666666;">&quot;bar&quot;</span> <span style="color: #000000;">&#125;</span>,
   <span style="color: #000000;">&#123;</span> <span style="color: #666666;">&quot;baz&quot;</span>, <span style="color: #666666;">&quot;blah&quot;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
Internally, this is achieved by using a technique I first saw on <a href="http://blog.bittercoder.com/PermaLink,guid,206e64d1-29ae-4362-874b-83f5b103727f.aspx">Alex Henderson&#8217;s blog</a>. By taking an array of delegates, it is possible to capture the compile-time variable names used by the lambda expressions and use them as keys for the dictionary.
</p>
<p>
The responses to this approach have been quite polarized. There has been lots of positive feedback on twitter&#8230;
</p>
<blockquote><p>
&#8220;@JeremySkinner I like it, no problem for me, infact I&#8217;ll apply this technique where ever I can from now one forward <img src='http://www.jeremyskinner.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &#8221; (from <a href="http://twitter.com/marknijhof">Mark Nijhof</a>)
</p></blockquote>
<blockquote><p>
&#8220;@JeremySkinner the lambda abuse is awesome, very ruby symbols like, and pushing the language envelope.&#8221; (from <a href="http://twitter.com/AdamTybor">Adam Tybor</a>)
</p></blockquote>
<p>
&#8230;and also in the blogosphere (eg <a href="http://odetocode.com/Blogs/scott/archive/2009/11/30/your-abomination-is-my-clever-hack.aspx">here</a> and <a href="http://www.corebvba.be/blog/post/Rant-Your-abomination-is-my-clever-hack.aspx">here</a>) as well as on the StackOverflow post.
</p>
<p>
There has also been a lot of negative feedback:
</p>
<blockquote><p>
&#8220;I hardly ever came across this kind of usage. I think it&#8217;s inappropriate&#8221;
</p></blockquote>
<blockquote><p>
&#8220;I find that odd not so much because of the name, but because the lambda is unnecessary; it could use an anonymous-type and be more flexible&#8221;
</p></blockquote>
<blockquote><p>
&#8220;This is horrible on more than one level. And no, this is nothing like Ruby its an abuse of C# and .Net. &#8221;
</p></blockquote>
<blockquote><p>
&#8220;It&#8217;s counter-intuitive, there is no way of just looking at the code to figure out what it does.&#8221;
</p></blockquote>
<p>
Interestingly, some of the most negative comments were from <a href="http://blogs.msdn.com/ericlippert/">Eric Lippert</a> on the C# compiler team:
</p>
<blockquote><p>
&#8220;This is horrid in so many ways.&#8221;
</p></blockquote>
<blockquote><p>
&#8220;I just asked Anders (and the rest of the design team) what they thought. Let&#8217;s just say the results would not be printable in a family-friendly newspaper&#8221;
</p></blockquote>
<p>
I thought Eric&#8217;s response was possibly a little harsh, but wanted to try and address and explain some of the issues here:
</p>
<h2>Issue #1: This is not interop friendly!</h2>
<p>
One of the issues raised was that this is <a href="http://stackoverflow.com/questions/1718037/abuse-of-c-lambda-expressions-or-syntax-brilliance/1718088#1718088">not interop friendly</a>. If, for example, you wanted to build a grid using F# (or possibly another CLR language) then this approach would not work as it is dependent on the C# compiler. This is a good point and is very true.
</p>
<p>
The thing I want to point out here is that the syntax is <strong>completely optional</strong>. This particular overload for the &#8216;Attributes&#8217; method on the grid is an extension method for convenience when using C#. The main implementation for this method takes an IDictionary. If you don&#8217;t like the abusive-lambda approach (or you can&#8217;t use it from another language) then you can use the &#8216;regular&#8217; overload instead:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">.<span style="color: #0000FF;">Attributes</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#123;</span> <span style="color: #666666;">&quot;style&quot;</span>, <span style="color: #666666;">&quot;width:100%&quot;</span> <span style="color: #000000;">&#125;</span>, <span style="color: #000000;">&#123;</span> <span style="color: #666666;">&quot;cellpadding&quot;</span>, <span style="color: #FF0000;">0</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>
The abusive-lambdas are merely a preference &#8211; I happen to like it as it saves me some keystrokes. If you don&#8217;t like it, don&#8217;t use it.
</p>
<h2>Issue #2: This is unintuitive and not intellisense-friendly</h2>
<p>
From a readability perspective, I do not agree that this is unintuitive. If you saw &#8220;Attributes(style =&gt; &#8220;width:100%&#8221;, cellpadding =&gt; 0)&#8221; in a grid definition then I think it&#8217;s pretty obvious what&#8217;s going on. It even looks like HTML attributes (kinda).
</p>
<p>
From an Intellisense perspective, I completely agree. If you see the following method signature, it is not immediately obvious what&#8217;s going on:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IGridWithOptions<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Attributes<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IGridWithOptions<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> grid, <span style="color: #0600FF;">params</span> Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">object</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> hash<span style="color: #000000;">&#41;</span></pre></div></div>

<p>
But hold on a second&#8230;let&#8217;s compare this to the approach that the ASP.NET MVC Framework uses for all its built-in HTML helpers:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> MvcHtmlString TextBox<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> HtmlHelper htmlHelper, <span style="color: #FF0000;">string</span> name, <span style="color: #FF0000;">object</span> value, <span style="color: #FF0000;">object</span> htmlAttributes<span style="color: #000000;">&#41;</span></pre></div></div>

<p>
Notice that &#8220;object htmlAttributes&#8221; at the end? That allows you to specify an anonymous type containing HTML attributes:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; 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;foo&quot;</span>, <span style="color: #cc0000;">&quot;bar&quot;</span>, <span style="color: #0000ff; font-weight: bold;">new</span><span style="color: #006600; font-weight:bold;">&#123;</span> style <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;width:100%&quot;</span>, <span style="color: #006600; font-weight: bold;">@</span><span style="color: #0000ff; font-weight: bold;">class</span><span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;foo&quot;</span> <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>
This is <strong>just as opaque as using abusive lambdas.</strong> In fact, I&#8217;d aruge that it&#8217;s *more* opaque because the htmlAttributes parameter is defined as type object. This means you could accidentally pass *anything* in this parameter (eg a string) and the compiler would not complain. I have done this on many occasions and I find it very frustrating. At least with a parameter of type Func&lt;object, object&gt;[] you can&#8217;t accidentally pass in a string. (Much like the MvcContrib approach, ASP.NET MVC&#8217;s helpers also have overloads that take an IDictionary if you prefer).
</p>
<h2>Issue #3: You can achieve the same result with an anonymous type:</h2>
<p>
I don&#8217;t believe that the Hash is really any worse than the anonymous type. In fact, in some ways I think the use of an anonymous type is worse (see my point above regarding opacity)
</p>
<h2>Issue #4: Performance implications!</h2>
<p>
There seems to be some misunderstanding around the use of the lambda syntax. On the one hand you can use a lambda to define an expression tree which can then be parsed (this is how LINQ to SQL works &#8211; the expression tree is then converted to SQL). These expressions can then be compiled to a delegate and invoked which is an expensive operation. However, lambdas can also be used for anonymous methods. This is essentially syntactic sugar over a standard .NET delegate and is an improvement over C# 2&#8242;s <a href="http://msdn.microsoft.com/en-us/library/0yw3tz5k%28VS.80%29.aspx">anonymous delegate syntax</a>. </p>
<p>
The MvcContrib uses the latter approach to do its work. This way, there is no overhead for compiling the expressions. In fact, there is very little overhead versus using the standard Dictionary.Add method. Alex Henderson talks more about the different approaches for doing this <a href="http://blog.bittercoder.com/PermaLink,guid,206e64d1-29ae-4362-874b-83f5b103727f.aspx">on his blog</a>, but these were the results of his benchmark for 10000 calls:
</p>
<ul>
<li>Using Dictionary.Add: 10.0144ms</li>
<li>Using lambdas with expression compilation: 9713.968ms (240.3456ms with constants) </li>
<li><strong>Using lambdas as delegates: 30.0432ms</strong></strong></li>
</ul>
<h2>Issue #5: Why not use method chaining?</h2>
<p>
One suggestion was to chain multiple calls to &#8216;Attribute&#8217;:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;style&quot;</span>, <span style="color: #666666;">&quot;width:100%&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Attribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;cellpadding&quot;</span>, <span style="color: #666666;">&quot;0&quot;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>
This definitely works, but the whole reason why I decided to use the Hash was because I want to mimimize the amount of typing (my fingers tire easily) and making multiple calls to &#8216;Attribute&#8217; is rather verbose.
</p>
<h2>And finally&#8230;</h2>
<p>
In the end, this is really just down to personal preference:</p>
<ul>
<li> I do not like C#&#8217;s dictionary initializers &#8211; I think they&#8217;re too verbose</li>
<li>I do not like the use of anonymous types as dictionaries as they require you to take parameters of type &#8220;object&#8221;</li>
</ul>
<p>The Abusive Lambda Pattern (I think that&#8217;s what I&#8217;ll call this) is just something I happen to like, but if you don&#8217;t like it then you certainly don&#8217;t have to use it.</p>
<p>Perhaps Eric and the C# team would like to give us a more succinct collection initializer for Dictionaries in C#5? <img src='http://www.jeremyskinner.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/12/02/lambda-abuse-the-mvccontrib-hash/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Validating with Lambda Expressions</title>
		<link>http://www.jeremyskinner.co.uk/2008/06/28/validating-with-lambda-expressions/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/06/28/validating-with-lambda-expressions/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 14:29:57 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[FluentValidation]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=36</guid>
		<description><![CDATA[I&#8217;ve recently been working on a small library for doing validation with lambda expressions. It allows you to run a set of validators against an object, then collects the validation errors in a list. Here are some examples: var person = new Person&#40;&#41;; &#160; var validator = Validator.For&#40;person&#41;.Rules&#40; rule =&#62; rule.For&#40;p =&#62; p.Forename&#41;.NotNull&#40;&#41;, //Forename cannot [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been working on a small library for doing validation with lambda expressions. It allows you to run a set of validators against an object, then collects the validation errors in a list. Here are some examples:</p>

<div class="wp_syntax"><div class="code"><pre class="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><span style="color: #008000;">;</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: #008080; font-style: italic;">//Forename cannot be null</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><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//The Id must not be equal to 0</span>
<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//run the validators</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><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//Collect the errors</span>
IList<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> errors <span style="color: #008000;">=</span> validator.<span style="color: #0000FF;">Errors</span><span style="color: #008000;">;</span></pre></div></div>

<p>It is also possible to chain validators together:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"> <span style="color: #008080; font-style: italic;">//Forename cannot be null, cannot be equal to 'Foo' and must be between 1-10 characters.</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;">NotEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Foo&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">10</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>If a validator fails, then it will put an error message into the errors collection. For example, the above NotEqual validator would produce an error like <em>&#8216;Forename&#8217; should not be equal to &#8216;Foo&#8217;</em>. You can change the name of the field by calling the <strong>WithName</strong> method:</p>

<div class="wp_syntax"><div class="code"><pre class="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><span style="color: #666666;">&quot;Foo&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithName</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;First Name&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
The error would now be generated as <em>&#8216;First Name&#8217; should not be equal to &#8216;Foo&#8217;</em>
</p>
<p>You can also replace the entire error message by using <strong>WithMessage</strong></p>

<div class="wp_syntax"><div class="code"><pre class="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: #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><span style="color: #666666;">&quot;Foo&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithMessage</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Please ensure that you have entered a sensible first name.&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
The source code is available <a href="http://www.jeremyskinner.co.uk/svn/trunk">in my svn repository</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/06/28/validating-with-lambda-expressions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails-like features in C# 3</title>
		<link>http://www.jeremyskinner.co.uk/2007/12/04/rails-like-features-in-c-3/</link>
		<comments>http://www.jeremyskinner.co.uk/2007/12/04/rails-like-features-in-c-3/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 14:05:44 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://jeremyskinner.wordpress.com/2007/12/04/rails-like-features-in-c-3/</guid>
		<description><![CDATA[I recently purchased a copy of Agile Web Development with Rails in an attempt to learn more about Ruby on Rails. I&#8217;ve been slowly working through the sample application and so far working with Rails has been a very pleasant experience. One of the thing I really like about RoR is the very readable method [...]]]></description>
			<content:encoded><![CDATA[<p>I recently purchased a copy of <a href="http://www.amazon.co.uk/Agile-Development-Rails-Pragmatic-Programmers/dp/0977616630/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1196774162&amp;sr=8-1">Agile Web Development with Rails</a> in an attempt to learn more about Ruby on Rails. I&#8217;ve been slowly working through the sample application and so far working with Rails has been a very pleasant experience.</p>
<p>One of the thing I really like about RoR is the very readable method names. For example, compare Ruby on Rails &#8220;Times&#8221; method to a C# &#8220;For&#8221; loop:</p>
<pre>
// C#
for(int i = 0; i &lt; 10; i++) {
  Console.WriteLine(i);
}

# Rails
10.times do |i|
  puts i
end
</pre>
<p>I know which one I prefer. </p>
<p>Just for fun, I decided to write a number of Rails-like methods using <a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx">C# Extension Methods.</a> Here are some examples:</p>
<pre>//Rails style "For" loop
3.Times(i =&gt; Console.WriteLine(i));

//Rails style "Foreach" loop
List&lt;int&gt; ints = new List { 1, 2, 3 };
ints.Each(i =&gt; Console.WriteLine(i));

//Working with dates and times...
DateTime tenDaysAgo = 10.Days().Ago();
DateTime threeHoursInTheFuture = 3.Hours().FromNow();
//Or...
threeHoursInTheFuture = 3.Hours().Since(DateTime.Now);

//String manipulation
string test = "My Test String";
string first = test.First(); //"M"
string firstThree = test.First(3); //"My "
string last = test.Last(); //"g"
string LastThree = test.Last(3); //"ing"</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2007/12/04/rails-like-features-in-c-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
