<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Followup: How to validate a method&#8217;s arguments?</title>
	<atom:link href="http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/feed/" rel="self" type="application/rss+xml" />
	<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/</link>
	<description></description>
	<lastBuildDate>Sat, 28 Jan 2012 14:35:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Janga &#8211; A Validation Framework with a Fluent API &#171; ActiveEngine</title>
		<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/#comment-1714</link>
		<dc:creator><![CDATA[Janga &#8211; A Validation Framework with a Fluent API &#171; ActiveEngine]]></dc:creator>
		<pubDate>Sun, 26 Sep 2010 21:31:04 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=99#comment-1714</guid>
		<description><![CDATA[[...] Roger Alsing &#8211; Fluent Argument Validation Specification [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Roger Alsing &#8211; Fluent Argument Validation Specification [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard Spiller</title>
		<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/#comment-1041</link>
		<dc:creator><![CDATA[Richard Spiller]]></dc:creator>
		<pubDate>Wed, 11 Mar 2009 16:51:34 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=99#comment-1041</guid>
		<description><![CDATA[Hey Roger,

I loved this post. I came across it while doing some research on an idea I had to remove a bunch of tedious code that I found myself writing over and over again to validate arguments. I started playing around with your code and made some changes that I thought I would share in case others might find it useful.

namespace ArgVal
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Linq;
    using System.Linq.Expressions;

    public static class ValidateThat
    {
        public static Argument Argument(Expression&lt;Func&gt; arg)
        {
            return new Argument(arg);
        }
    }

    public sealed class Argument : IHideObjectMembers
    {
        [DebuggerNonUserCode]
        internal Argument(Expression&lt;Func&gt; arg)
        {
            switch (arg.Body.NodeType)
            {
                case ExpressionType.MemberAccess:
                    this.Name = ((MemberExpression)arg.Body).Member.Name;
                    break;
                case ExpressionType.Parameter:
                    this.Name = ((ParameterExpression)arg.Body).Name;
                    break;
                default:
                    throw new ArgumentException(&quot;Expression body must be of type MemberAccess or Parameter.&quot;, &quot;arg&quot;);
            }

            this.Value = arg.Compile().Invoke();
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public TArg Value { get; private set; }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public string Name { get; private set; }
    }

    public static class ArgumentValidationExtensions
    {
        [DebuggerNonUserCode]
        public static Argument IsNotNull(this Argument arg)
            where T : class
        {
            if (arg.Value == null)
                throw new ArgumentNullException(arg.Name, &quot;Cannot be null.&quot;);

            return arg;
        }

        [DebuggerNonUserCode]
        public static Argument IsNotEmpty(this Argument arg)
        {
            if ((arg.Value != null) &amp;&amp; (arg.Value.Length == 0))
                throw new ArgumentException(&quot;Cannot be an empty string.&quot;, arg.Name);

            return arg;
        }

        // Additional validation extensions omitted for brevity
    }
}

I removed comments and condensed the code for brevity. The IHideObjectMembers interface is also omitted but can be seen here . It along with the EditorBrowsable attributes on the Value and Name properties help to make the fluent API cleaner by removing from intellisense non-validation methods. Hopefully, in my copying and condensing I didn&#039;t screw anything up so that this code could be copied directly and played with. 

Note that the EditorBrowsable attribute only works if compiled into an assembly and referenced in some other consuming project. This was killing me yesterday until I found this .


For my purposes, I like how this came out. I like using the Linq expressions for strongly-type references to the arguments therefore removing the need for strings and starting with the static ValidateThat class as opposed to an extension method that sort of &quot;shows up everywhere&quot; once brought into scope with a using declaration.

Thoughts?

Also, I saw that one or two people talked about making this a published API on CodePlex or Google Code. I think it is a cool idea and would start it if anyone is interested. Anyone have a cool name in mind?
NArg anyone? :)

Again, thanks for the great idea. I plan on using it a lot.]]></description>
		<content:encoded><![CDATA[<p>Hey Roger,</p>
<p>I loved this post. I came across it while doing some research on an idea I had to remove a bunch of tedious code that I found myself writing over and over again to validate arguments. I started playing around with your code and made some changes that I thought I would share in case others might find it useful.</p>
<p>namespace ArgVal<br />
{<br />
    using System;<br />
    using System.Collections.Generic;<br />
    using System.ComponentModel;<br />
    using System.Diagnostics;<br />
    using System.Linq;<br />
    using System.Linq.Expressions;</p>
<p>    public static class ValidateThat<br />
    {<br />
        public static Argument Argument(Expression&lt;Func&gt; arg)<br />
        {<br />
            return new Argument(arg);<br />
        }<br />
    }</p>
<p>    public sealed class Argument : IHideObjectMembers<br />
    {<br />
        [DebuggerNonUserCode]<br />
        internal Argument(Expression&lt;Func&gt; arg)<br />
        {<br />
            switch (arg.Body.NodeType)<br />
            {<br />
                case ExpressionType.MemberAccess:<br />
                    this.Name = ((MemberExpression)arg.Body).Member.Name;<br />
                    break;<br />
                case ExpressionType.Parameter:<br />
                    this.Name = ((ParameterExpression)arg.Body).Name;<br />
                    break;<br />
                default:<br />
                    throw new ArgumentException(&#8220;Expression body must be of type MemberAccess or Parameter.&#8221;, &#8220;arg&#8221;);<br />
            }</p>
<p>            this.Value = arg.Compile().Invoke();<br />
        }</p>
<p>        [EditorBrowsable(EditorBrowsableState.Never)]<br />
        public TArg Value { get; private set; }</p>
<p>        [EditorBrowsable(EditorBrowsableState.Never)]<br />
        public string Name { get; private set; }<br />
    }</p>
<p>    public static class ArgumentValidationExtensions<br />
    {<br />
        [DebuggerNonUserCode]<br />
        public static Argument IsNotNull(this Argument arg)<br />
            where T : class<br />
        {<br />
            if (arg.Value == null)<br />
                throw new ArgumentNullException(arg.Name, &#8220;Cannot be null.&#8221;);</p>
<p>            return arg;<br />
        }</p>
<p>        [DebuggerNonUserCode]<br />
        public static Argument IsNotEmpty(this Argument arg)<br />
        {<br />
            if ((arg.Value != null) &amp;&amp; (arg.Value.Length == 0))<br />
                throw new ArgumentException(&#8220;Cannot be an empty string.&#8221;, arg.Name);</p>
<p>            return arg;<br />
        }</p>
<p>        // Additional validation extensions omitted for brevity<br />
    }<br />
}</p>
<p>I removed comments and condensed the code for brevity. The IHideObjectMembers interface is also omitted but can be seen here . It along with the EditorBrowsable attributes on the Value and Name properties help to make the fluent API cleaner by removing from intellisense non-validation methods. Hopefully, in my copying and condensing I didn&#8217;t screw anything up so that this code could be copied directly and played with. </p>
<p>Note that the EditorBrowsable attribute only works if compiled into an assembly and referenced in some other consuming project. This was killing me yesterday until I found this .</p>
<p>For my purposes, I like how this came out. I like using the Linq expressions for strongly-type references to the arguments therefore removing the need for strings and starting with the static ValidateThat class as opposed to an extension method that sort of &#8220;shows up everywhere&#8221; once brought into scope with a using declaration.</p>
<p>Thoughts?</p>
<p>Also, I saw that one or two people talked about making this a published API on CodePlex or Google Code. I think it is a cool idea and would start it if anyone is interested. Anyone have a cool name in mind?<br />
NArg anyone? :)</p>
<p>Again, thanks for the great idea. I plan on using it a lot.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Argument Validation using C# 3.0 extension methods &#171; JBloggin&#8217;</title>
		<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/#comment-945</link>
		<dc:creator><![CDATA[Argument Validation using C# 3.0 extension methods &#171; JBloggin&#8217;]]></dc:creator>
		<pubDate>Wed, 07 Jan 2009 18:11:56 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=99#comment-945</guid>
		<description><![CDATA[[...] http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/ [...]]]></description>
		<content:encoded><![CDATA[<p>[...] <a href="http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/" rel="nofollow">http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Algumas Idéias &#187; Blog Archive &#187; Fluent Argument Validation</title>
		<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/#comment-258</link>
		<dc:creator><![CDATA[Algumas Idéias &#187; Blog Archive &#187; Fluent Argument Validation]]></dc:creator>
		<pubDate>Sun, 31 Aug 2008 17:21:25 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=99#comment-258</guid>
		<description><![CDATA[[...] bom exemplo de como usar uma fluent interface para fazer validação de pré e pós condições escrito por [...]]]></description>
		<content:encoded><![CDATA[<p>[...] bom exemplo de como usar uma fluent interface para fazer validação de pré e pós condições escrito por [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fluent Argument Validation - Colin&#39;s Blog</title>
		<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/#comment-184</link>
		<dc:creator><![CDATA[Fluent Argument Validation - Colin&#39;s Blog]]></dc:creator>
		<pubDate>Thu, 29 May 2008 09:18:36 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=99#comment-184</guid>
		<description><![CDATA[[...] written some neat code for argument validation using fluent C# extension methods. Read the &quot;How to validate a method&#8217;s arguments?&quot; blog entry. Here&#039;s a sample taken from that blog [...]]]></description>
		<content:encoded><![CDATA[<p>[...] written some neat code for argument validation using fluent C# extension methods. Read the &quot;How to validate a method&#8217;s arguments?&quot; blog entry. Here&#39;s a sample taken from that blog [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Argument validation framework released &#171; Roger Alsing Weblog</title>
		<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/#comment-182</link>
		<dc:creator><![CDATA[Argument validation framework released &#171; Roger Alsing Weblog]]></dc:creator>
		<pubDate>Wed, 28 May 2008 21:33:46 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=99#comment-182</guid>
		<description><![CDATA[[...] I&#8217;ve finally got my thumb out and cleaned up and made a mini framework of the fluent argument validation concept I blogged about in my last post: http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/ [...]]]></description>
		<content:encoded><![CDATA[<p>[...] I&#8217;ve finally got my thumb out and cleaned up and made a mini framework of the fluent argument validation concept I blogged about in my last post: <a href="http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/" rel="nofollow">http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torkel</title>
		<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/#comment-171</link>
		<dc:creator><![CDATA[Torkel]]></dc:creator>
		<pubDate>Tue, 13 May 2008 17:44:42 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=99#comment-171</guid>
		<description><![CDATA[I agree that it hurts readability to some extent, and doesn&#039;t offer that much of more value to compared to arg.NotNull() (except that you can define the attributes on interfaces and have them checked on each implementation). 

But the biggest problem is that it can only be applied to interface and virtual functions, and you have to construct the object using a proxy generator (if the class is a component in an IoC container this is not so big of a problem). Another problem could be performance. 

But it&#039;s a nice idea, maybe not the most practical with the current limitations for method interception :)]]></description>
		<content:encoded><![CDATA[<p>I agree that it hurts readability to some extent, and doesn&#8217;t offer that much of more value to compared to arg.NotNull() (except that you can define the attributes on interfaces and have them checked on each implementation). </p>
<p>But the biggest problem is that it can only be applied to interface and virtual functions, and you have to construct the object using a proxy generator (if the class is a component in an IoC container this is not so big of a problem). Another problem could be performance. </p>
<p>But it&#8217;s a nice idea, maybe not the most practical with the current limitations for method interception :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Alsing</title>
		<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/#comment-170</link>
		<dc:creator><![CDATA[Roger Alsing]]></dc:creator>
		<pubDate>Tue, 13 May 2008 17:21:59 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=99#comment-170</guid>
		<description><![CDATA[Also, Argument validation is not a very good AOP candidate IMO.

While it is 100% true that validation is a crosscutting concern, you still have to specify somehow _how_ the arguments should be verified _per usecase_.

You will still have to add some sort of meta data to your methods.

So the benefit is small while you add alot of extra complexity to your app.

If you write &quot;[NotNull]arg&quot; outside of the method , or &quot;arg.NotNull()&quot; inside it doesnt really matter, you are still expressing the same thing.

The AOP version ofcourse come with some benefits, eg you don&#039;t have to specify the arg name and it can be applied to multiple return statements etc.

But all in all, very small benefits for a quite high cost, unless your app already relies on AOP for other tasks.

Also, multiple attributes per argument makes the code somewhat hard to read.]]></description>
		<content:encoded><![CDATA[<p>Also, Argument validation is not a very good AOP candidate IMO.</p>
<p>While it is 100% true that validation is a crosscutting concern, you still have to specify somehow _how_ the arguments should be verified _per usecase_.</p>
<p>You will still have to add some sort of meta data to your methods.</p>
<p>So the benefit is small while you add alot of extra complexity to your app.</p>
<p>If you write &#8220;[NotNull]arg&#8221; outside of the method , or &#8220;arg.NotNull()&#8221; inside it doesnt really matter, you are still expressing the same thing.</p>
<p>The AOP version ofcourse come with some benefits, eg you don&#8217;t have to specify the arg name and it can be applied to multiple return statements etc.</p>
<p>But all in all, very small benefits for a quite high cost, unless your app already relies on AOP for other tasks.</p>
<p>Also, multiple attributes per argument makes the code somewhat hard to read.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torkel</title>
		<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/#comment-169</link>
		<dc:creator><![CDATA[Torkel]]></dc:creator>
		<pubDate>Tue, 13 May 2008 16:38:23 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=99#comment-169</guid>
		<description><![CDATA[That is nice, I haven&#039;t tried NAspect, I figured someone else had done it :) 

I have only used Castle Dynamic Proxy / Windsor to handle AOP. I find myself using interceptors  more and more and should probably move to a framework that offers more out of the box AOP support :)]]></description>
		<content:encoded><![CDATA[<p>That is nice, I haven&#8217;t tried NAspect, I figured someone else had done it :) </p>
<p>I have only used Castle Dynamic Proxy / Windsor to handle AOP. I find myself using interceptors  more and more and should probably move to a framework that offers more out of the box AOP support :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Alsing</title>
		<link>http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/#comment-168</link>
		<dc:creator><![CDATA[Roger Alsing]]></dc:creator>
		<pubDate>Tue, 13 May 2008 16:09:54 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=99#comment-168</guid>
		<description><![CDATA[Hi Torkel, 
Have you ever looked at my AOP framework NAspect?

I&#039;ve got a DbC sample in there that does exactly the same as you :-)

Snip:
&lt;code&gt;public virtual void ReceiveAString(int abc,[NotNull]string def)&lt;/code&gt;
...

Snip:
&lt;code&gt;[NotNull] //your version with return: is nicer
public virtual string ReturnAString(int i)&lt;/code&gt;
...

Snip:
&lt;code&gt;public object HandleCall(MethodInvocation call)
{
ParameterInfo[] parameters =call.Method.GetParameters();&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p>Hi Torkel,<br />
Have you ever looked at my AOP framework NAspect?</p>
<p>I&#8217;ve got a DbC sample in there that does exactly the same as you :-)</p>
<p>Snip:<br />
<code>public virtual void ReceiveAString(int abc,[NotNull]string def)</code><br />
&#8230;</p>
<p>Snip:<br />
<code>[NotNull] //your version with return: is nicer<br />
public virtual string ReturnAString(int i)</code><br />
&#8230;</p>
<p>Snip:<br />
<code>public object HandleCall(MethodInvocation call)<br />
{<br />
ParameterInfo[] parameters =call.Method.GetParameters();</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>

