<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Fredrik Normén</title><link>http://weblogs.asp.net/fredriknormen/default.aspx</link><description>Any fool can write code that a computer can understand. Good programmers write code that humans can understand - Fowler</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Avoid returning "null" and use the Null Object pattern?</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/05/22/avoid-returning-quot-null-quot-and-use-the-null-object-pattern.aspx</link><pubDate>Thu, 22 May 2008 08:57:24 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6209982</guid><dc:creator>Fredrik N</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6209982</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/05/22/avoid-returning-quot-null-quot-and-use-the-null-object-pattern.aspx#comments</comments><description>&lt;p&gt;When I build applications and add methods to return a list of objects, I make it robust. So I always return an instance of the list instead or returning null. The reason is because I like the use of Count and also use foreach. I don't want to add extra code to see if the method returns null. For example:   &lt;br /&gt;&lt;/p&gt;  &lt;div&gt;   &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IList&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt; GetCultures()
{
    IList&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt; cultures = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;();

    &lt;span style="color: #008000"&gt;//...&lt;/span&gt;

    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; cultures;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Then when I make a call to the GetCultures method I can see if it contains any cultures by using Count, and if I need to list Cultures I can also use foreach without getting a null exception.
  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;IList&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt; cultures = GetCultures();

&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (cultures.Count &amp;gt; 0 )
   &lt;span style="color: #008000"&gt;//..&lt;/span&gt;


&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var culture &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; GetCultures())
   //..&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;If I prefer Robustness before Correctness I return null if I only need to return a single object, for example:

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Culture GetCulture(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; cultureCode)
{
    Culture culture = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
            
    &lt;span style="color: #008000"&gt;//..&lt;/span&gt;

    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; culture;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;In this case I need to see if the returned object is null or not before using it.
  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;Culture culture = GetCulture();

&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (cultures !=&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;I could also throw an exception if the cultureCode could not be located, in that way I will never return null. But I try to avoid throwing exception and use try catch block when I try to get an object. The reason is performance and also skip adding several try catch block in my code.

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Culture GetCulture(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; cultureCode)
{
    Culture culture = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
            
    &lt;span style="color: #008000"&gt;//Can't locate the culture throw an exception&lt;/span&gt;

    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; culture;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;Culture culture = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;

&lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
{
    culture = GetCulture();
}
&lt;span style="color: #0000ff"&gt;catch&lt;/span&gt;
{
    &lt;span style="color: #008000"&gt;//...&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;I only throw an exception when it's really an exception, for example the cultureCode was in a correct format or wasn't specified etc. Everything is based on the type of application, and if Robustness is more important than Correctness. Another solution is to use the &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object Pattern&lt;/a&gt; and always return an instance of an object.

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;Culture culture = GetCulture();

&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (cultures.IsNull)&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;Or use the Cultures other members and not get a null exception. The Null Object Pattern is also useful if I always want to return a default value. For example if I want to get a Customer and the Customer can't be located, I could return a John Doe ;) I don't use the Null Object Pattern so often in my applications.

  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;What solution do you use or prefer when returning a list or single object and why?
  &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6209982" width="1" height="1"&gt;</description></item><item><title>Avoid "else" as much as possible, use "?:" instead</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/05/16/avoid-quot-else-quot-as-much-as-possible-use-quot-quot-instead.aspx</link><pubDate>Fri, 16 May 2008 06:19:36 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6194697</guid><dc:creator>Fredrik N</dc:creator><slash:comments>19</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6194697</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/05/16/avoid-quot-else-quot-as-much-as-possible-use-quot-quot-instead.aspx#comments</comments><description>&lt;p&gt;To have a 10 week old baby Chihuahua takes a lot of time&amp;#8230; the housekeeping is not so easy. I need to go up twice at night to let the little baby boy do his stuff. But it&amp;#8217;s really wonderful to have such a small dog in my home, I bought a lot of books about how to be a pack leader.. I love reading books about leadership etc so it should be interesting to read those books about packs. Here is a video on YouTube where I play with him, if anyone is interested to see a dog that has the same size as a cat puppy. &lt;a href="http://www.youtube.com/watch?v=KOWZjHTBiKo"&gt;http://www.youtube.com/watch?v=KOWZjHTBiKo&lt;/a&gt;     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;Well enough about dog talk. I read a book for some days ago about avoiding using &amp;#8220;else&amp;#8221; to use OOP. Well I try to avoid switch case and if statements as much as possible, thanks to OO it can be easy to handle that. But the interesting part in the book was just about avoiding the use of else even for a simple check. &lt;/p&gt;  &lt;p&gt;The example in the book was like this:   &lt;br /&gt;&lt;/p&gt;  &lt;div&gt;   &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (experssion)
    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;font color="#0000ff"&gt;something&lt;/font&gt;;
&lt;span style="color: #0000ff"&gt;else&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;somethingelse&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Instead of using &amp;#8220;else&amp;#8221; the author wanted us to write the following code instead: 
  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (expression) ? &lt;span style="color: #0000ff"&gt;something&lt;/span&gt; : &lt;span style="color: #0000ff"&gt;sometingelse&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;Now we get rid of the &amp;#8220;else&amp;#8221;, isn&amp;#8217;t that cool? But is it really worth it, I mean what is most readable? I think the if else in the first example is easier for people to read and understand, and also for developers that haven&amp;#8217;t seen ?: before. &lt;/p&gt;

&lt;p&gt;When it comes to returning true and false we could use the following code:
  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (expression);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#8220;The beauty is in the eye of the beholder&amp;#8221; or? 
  &lt;br /&gt;What do you think, is the ?: more beauty full than if else, and is there a reason I should always use ?: instead of if else (Well it will reduce the line of code, but is that so important in this case)? What do you use and can I still use if else instead of ?: and fell like I&amp;#8217;m writing beautiful and good code?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6194697" width="1" height="1"&gt;</description></item><item><title>Use "constraints" instead of argument validation for int and double etc</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/05/10/use-constraints-instead-of-argument-validation.aspx</link><pubDate>Sat, 10 May 2008 11:21:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6175899</guid><dc:creator>Fredrik N</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6175899</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/05/10/use-constraints-instead-of-argument-validation.aspx#comments</comments><description>&lt;P&gt;I will still continue with the argument validation track in this post also, I think it's an interesting topic. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;If you haven't read my earlier posts about this topic, take a look at the following posts on my blog:&lt;/P&gt;
&lt;P&gt;&lt;A title=http://weblogs.asp.net/fredriknormen/archive/2008/05/07/lazy-developers.aspx href="http://weblogs.asp.net/fredriknormen/archive/2008/05/07/lazy-developers.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2008/05/07/lazy-developers.aspx"&gt;http://weblogs.asp.net/fredriknormen/archive/2008/05/07/lazy-developers.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A title=http://weblogs.asp.net/fredriknormen/archive/2008/05/08/how-to-validate-a-method-s-arguments.aspx href="http://weblogs.asp.net/fredriknormen/archive/2008/05/08/how-to-validate-a-method-s-arguments.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2008/05/08/how-to-validate-a-method-s-arguments.aspx"&gt;http://weblogs.asp.net/fredriknormen/archive/2008/05/08/how-to-validate-a-method-s-arguments.aspx&lt;/A&gt; &lt;BR&gt;&lt;BR&gt;This time I will focus on argument of type int and double etc. Lets assume we have a method which should be used to set a price on a product: &lt;BR&gt;&lt;/P&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Product
{
    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; SetPrice(&lt;SPAN style="COLOR: #0000ff"&gt;double&lt;/SPAN&gt; price)
    {
        price.RequireArgument(&lt;SPAN style="COLOR: #006080"&gt;"price"&lt;/SPAN&gt;)
             .GreaterOrEqualTo(0);

        &lt;SPAN style="COLOR: #008000"&gt;//..&lt;/SPAN&gt;
    }
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Note: I will use my friend &lt;A href="http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/" mce_href="http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/"&gt;Roger Alsing cool argument validation solution&lt;/A&gt; in this example you can read more about it on his &lt;A href="http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/" mce_href="http://rogeralsing.com/2008/05/10/followup-how-to-validate-a-methods-arguments/"&gt;blog&lt;/A&gt;. &lt;BR&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;I use a Set method instead of a property in this example.&lt;/P&gt;
&lt;P&gt;I use a validation here to make sure price is not lesser then 0. Here is another method that takes three argument, year, month and day as integers: &lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; SetDate(&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; year, &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; month, &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; day)
{
    year.RequireArgument(&lt;SPAN style="COLOR: #006080"&gt;"year"&lt;/SPAN&gt;)
        .InRange(1900, 3000);

    month.RequireArgument(&lt;SPAN style="COLOR: #006080"&gt;"month"&lt;/SPAN&gt;)
        .InRange(1, 12);

    day.RequireArgument(&lt;SPAN style="COLOR: #006080"&gt;"day"&lt;/SPAN&gt;)
       .InRange(1, 31);

   &lt;SPAN style="COLOR: #008000"&gt;//...&lt;/SPAN&gt;
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Note: The Day argument here is special, as we know there is not always 31 days in each month, so another kind of validation is needed, but I make this code only simple for this post.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Now lets change this code and make sure the int and double is not an argument that can take a number larger and lesser than the validation. What we do is to create a Price, Year, Month and a Day&amp;nbsp;class and use them as an argument instead of double and int.&lt;/P&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Price
{
       &lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;double&lt;/SPAN&gt; _price;

       &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Price(&lt;SPAN style="COLOR: #0000ff"&gt;double&lt;/SPAN&gt; price)
       {
           price.RequireArgument(&lt;SPAN style="COLOR: #006080"&gt;"price"&lt;/SPAN&gt;)
                .GreaterOrEqualTo(0);

           &lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt;._price = price;
       }

       &lt;SPAN style="COLOR: #008000"&gt;//...&lt;/SPAN&gt;
   }


   &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Year
   {
       &lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; _year;

       &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Year(&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; year)
       {
           year.RequireArgument(&lt;SPAN style="COLOR: #006080"&gt;"year"&lt;/SPAN&gt;)
               .InRange(1900, 3000);
       }

       &lt;SPAN style="COLOR: #008000"&gt;//...&lt;/SPAN&gt;
   }


   &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Month
   {
       &lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; _month;

       &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Month(&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; month)
       {
           month.RequireArgument(&lt;SPAN style="COLOR: #006080"&gt;"month"&lt;/SPAN&gt;)
              .InRange(1, 12);
       }
   }


   &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Product
   {
       &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; SetPrice(Price price)
       {
           &lt;SPAN style="COLOR: #008000"&gt;//..&lt;/SPAN&gt;
       }
}


&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; SetDate(Year year, Month month, Day day)
{
    &lt;SPAN style="COLOR: #008000"&gt;//...&lt;/SPAN&gt;
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Note: I skip the Day struct because of not adding to much code, you still get the basic ideas from the other structs.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;As you see now the method takes a Price, Year, Month and a Day object, not a double or int and it will directly add a constraint. The user of the method can't pass in a invalid value. I also moved the validation to the&amp;nbsp;classes instead. The Price&amp;nbsp;class could&amp;nbsp;be replaced with the &lt;A href="http://martinfowler.com/eaaCatalog/money.html" mce_href="http://martinfowler.com/eaaCatalog/money.html"&gt;Money pattern&lt;/A&gt; Fowler mention in his book if we want it to handle Money.&lt;/P&gt;
&lt;P&gt;What do you think, is this something you use today?&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6175899" width="1" height="1"&gt;</description></item><item><title>How to validate a method's arguments?</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/05/08/how-to-validate-a-method-s-arguments.aspx</link><pubDate>Thu, 08 May 2008 08:19:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6168786</guid><dc:creator>Fredrik N</dc:creator><slash:comments>32</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6168786</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/05/08/how-to-validate-a-method-s-arguments.aspx#comments</comments><description>&lt;P&gt;Yesterday I wrote a post about &lt;A href="http://weblogs.asp.net/fredriknormen/archive/2008/05/07/lazy-developers.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2008/05/07/lazy-developers.aspx"&gt;developers that skip validation of arguments&lt;/A&gt;. In this post I will give some example of how validation can be done. There are probably better way to do it, and that is why I need your feedback and comments. &lt;BR&gt;&lt;BR&gt;The first example is a simple method that uses a simple validation by using “If”. &lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; DoSomething(&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; value1, &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; value2)
{
    &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (value1 &amp;gt; 10 || value1 &amp;lt; 0)
       &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; ArgumentException(&lt;SPAN style="COLOR: #006080"&gt;"The value must be between 0 or 10"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #006080"&gt;"value1"&lt;/SPAN&gt;);

    &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;.IsNullOrEmpty(value2))
       &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; ArgumentException(&lt;SPAN style="COLOR: #006080"&gt;"The value can't be null or empty"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #006080"&gt;"value2"&lt;/SPAN&gt;);

    &lt;SPAN style="COLOR: #008000"&gt;//...&lt;/SPAN&gt;
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;BR&gt;I started to use this solution to validate arguments, but what I notice really fast was that I repeat my self over and over again when I created more methods. I solved this by creating an ArgumentHelper class where I add my validation logic. &lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; ArgumentHelper
{
    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; RequireRange(&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; minValue, &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; maxValue, &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; argumentName)
    {
        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt; &amp;gt; maxValue || &lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt; &amp;lt; minValue)
           &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; ArgumentException(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: #006080"&gt;"The value must be between {0} and {1}"&lt;/SPAN&gt;, minValue, maxValue), argumentName);
    }

    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; RequireNotNullOrEmpty(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; argumentName)
    {
        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;.IsNullOrEmpty(&lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt;))
           &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; ArgumentException(&lt;SPAN style="COLOR: #006080"&gt;"The value can't be null or empty"&lt;/SPAN&gt;, argumentName);
    }
}&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;BR&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; DoSomething(&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; value1, &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; value2)
{
    ArgumentHelper.RequireRange(value1, 0, 10, &lt;SPAN style="COLOR: #006080"&gt;"value1"&lt;/SPAN&gt;);

    ArgumentHelper.RequireNotNullOrEmpty(value2, &lt;SPAN style="COLOR: #006080"&gt;"value2"&lt;/SPAN&gt;);

    &lt;SPAN style="COLOR: #008000"&gt;//...&lt;/SPAN&gt;
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;BR&gt;With C# 3.0 we now have Extension Methods, so why&amp;nbsp; not use Extension Methods to do the validation for a given type? Here is an example where I use Extension methods instead of an ArgumentHelper class: &lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; ArgumentHelperExtension
{
    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; RequireRange(&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; minValue, &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; maxValue, &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; argumentName)
    {
        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt; &amp;gt; maxValue || &lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt; &amp;lt; minValue)
            &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; ArgumentException(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: #006080"&gt;"The value must be between {0} and {1}"&lt;/SPAN&gt;, minValue, maxValue), argumentName);
    }


    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; RequireNotNullOrEmpty(&lt;SPAN style="COLOR: #0000ff"&gt;this&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; argumentName)
    {
        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;.IsNullOrEmpty(&lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt;))
            &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; ArgumentException(&lt;SPAN style="COLOR: #006080"&gt;"The value can't be null or empty"&lt;/SPAN&gt;, argumentName);
    }
}&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; DoSomething(&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; value1, &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; value2)
{
    value1.RequireRange(0, 10, &lt;SPAN style="COLOR: #006080"&gt;"value1"&lt;/SPAN&gt;);
    
    value2.RequireNotNullOrEmpty(&lt;SPAN style="COLOR: #006080"&gt;"value2"&lt;/SPAN&gt;);

    &lt;SPAN style="COLOR: #008000"&gt;//...&lt;/SPAN&gt;
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;As you can see the ArgumentHelper class have no changes, well the "this" key word is added to the first argument in the methods. I can then simply use my argument and call an Extension method that will do the validation. &lt;BR&gt;&lt;BR&gt;I recently had a discussion with my friend &lt;A href="http://rogeralsing.com/" mce_href="http://rogeralsing.com/"&gt;Roger Alsing&lt;/A&gt; and he had some interesting ideas. If we take a look at the Extension Method example, we can see that we add validation extension method to a given type. Let assume we don't want to add several Extension method to the type Int, instead only one method and that method returns a given "interface" which we can extend instead. For example to Int we can have a Extension method called Require and let it return a Numeric&amp;lt;T&amp;gt;, and then we can create Extension methods for the Numeric&amp;lt;T&amp;gt; class instead: &lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; DoSomething(&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; value)
{
    value1.Require().InRange(0, 10, &lt;SPAN style="COLOR: #006080"&gt;"value1"&lt;/SPAN&gt;);

    &lt;SPAN style="COLOR: #008000"&gt;//...&lt;/SPAN&gt;
}&lt;/PRE&gt;&lt;BR&gt;Another interesting solution Roger had is the following: &lt;BR&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; MyMethod(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; arne)
{
    RequireNotNull( () =&amp;gt; arne);
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; RequireNotNull&amp;lt;T&amp;gt;(ArgDelegate&amp;lt;T&amp;gt; del)
{
    T &lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt; = del();

    FieldInfo[] fields = del.Target.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
    &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (fields.Length != 1)
       &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; NotSupportedException(&lt;SPAN style="COLOR: #006080"&gt;"Invalid argument"&lt;/SPAN&gt;);

    &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; argName = fields[0].Name;

    &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #0000ff"&gt;value&lt;/SPAN&gt; == &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;)
       &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; Exception(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: #006080"&gt;"Parameter '{0}' may not be null"&lt;/SPAN&gt;, argName));
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;BR&gt;By using this solution we don't need to pass the name of the argument as a string like in the following solutions: &lt;BR&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;ArgumentHelper.RequireRange(value1, 0, 10, &lt;SPAN style="COLOR: #006080"&gt;"value1"&lt;/SPAN&gt;);

value1.RequireRange(0, 10, &lt;SPAN style="COLOR: #006080"&gt;"value1"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Instead reflection is used to get the name of the argument. This can of course affect performance. &lt;BR&gt;&lt;BR&gt;How do you implement your validation code, do you have some cool solution? &lt;BR&gt;&lt;BR&gt;Why can't we just have Design By Contract support for C#... ;)&lt;/DIV&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6168786" width="1" height="1"&gt;</description></item><item><title>Lazy Developers?</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/05/07/lazy-developers.aspx</link><pubDate>Wed, 07 May 2008 18:29:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6166250</guid><dc:creator>Fredrik N</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6166250</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/05/07/lazy-developers.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;When I was out with my new dog (I Chihuahua, wonderful dog), I was thinking about Defensive Programming and Design by Contract. I like to do some refactoring and help other people to write cleaner code etc,&amp;nbsp; not that I’m an expert in the area but I think most of developers can see stuff that blinds other developers during development. I remember a scenario that made me kind of mad, I often notice that some developers doesn’t even care to validate arguments on public API or other methods, they assume the caller pass in the right values. When I started to develop apps, I didn’t validate arguments (In know I was a noob ;)). But since I started to do validation, it reduces the number of bugs in my code, and the difference was big. But that isn’t the only reason I like to do validation. Another&amp;nbsp; reason is because the caller of the method should know if they have passed a wrong value, there are of course some other reasons also, but the main reason is make sure the caller or the method pass in the correct value, and if they do, they will get what they want.&lt;BR&gt;&lt;BR&gt;I notice that several developers today don’t care about validation, I ask them why and they all gave me the same argument “Why should I, it will only make me add more code and it’s boring to write code that checks values”. Can this be summarized that some developers are lazy?&lt;BR&gt;&lt;BR&gt;Maybe I’m stupid because I get mad when I hear that kind of argument, but I’m special ;)&lt;BR&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6166250" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/Code/default.aspx">Code</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/Defensive+Programming/default.aspx">Defensive Programming</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/Design+By+Contract/default.aspx">Design By Contract</category></item><item><title>I also want Spec#</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/29/i-also-want-spec.aspx</link><pubDate>Tue, 29 Apr 2008 04:45:32 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6140163</guid><dc:creator>Fredrik N</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6140163</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/29/i-also-want-spec.aspx#comments</comments><description>&lt;p&gt;I will copy &lt;a href="http://codebetter.com/blogs/gregyoung/archive/2008/04/28/i-want-spec.aspx"&gt;Greg Young&lt;/a&gt;! I also want Spec#. If you also wants it, please let Microsoft know about it.&lt;/p&gt;  &lt;p&gt;Something I like with Spec# is the support for Design by Contract.&lt;/p&gt;  &lt;h3&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/h3&gt; &lt;strong&gt;&lt;/strong&gt;  &lt;h1&gt;&lt;strong&gt;I want verifiable software...&lt;/strong&gt;&lt;/h1&gt; &lt;strong&gt;&lt;/strong&gt;  &lt;h1&gt;&lt;strong&gt;I WANT SPEC#!     &lt;br /&gt;&lt;/strong&gt;&lt;/h1&gt;  &lt;p&gt;&lt;img src="http://codebetter.com/blogs/gregyoung/WindowsLiveWriter/IWantSpec_D85E/spec_thumb.gif" /&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Here is an old post about &lt;a href="http://weblogs.asp.net/fredriknormen/archive/2007/11/28/defensive-programming-and-design-by-contract-on-a-routine-level.aspx"&gt;Defensive programming and Design by Contract&lt;/a&gt; on a routine level.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6140163" width="1" height="1"&gt;</description></item><item><title>Why can't I be satisfied when I'm looking back on old applications?</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/28/why-can-t-i-be-satisfied-when-i-m-looking-back-on-old-applications.aspx</link><pubDate>Mon, 28 Apr 2008 14:07:17 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6139110</guid><dc:creator>Fredrik N</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6139110</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/28/why-can-t-i-be-satisfied-when-i-m-looking-back-on-old-applications.aspx#comments</comments><description>&lt;p&gt;Most of the time I build an application I&amp;#160; have the feeling &amp;quot;this is going to be a great app. Good design, nice written code and a perfect application&amp;quot;. I think most of you had the same feeling sometimes. But when I look back on previous applications, I say &amp;quot;What!? NO!!! This is not good, well this could have been done much better ... omg! What have I done! .. what a bad method name....&amp;quot;. I don't know why I often feel like that when looking back on my previous apps. When I'm looking back on my code, I always find something I could have done better. Why can't I just write the perfect app, so I can go back later and say to my self &amp;quot;Well Fredrik, this one is great, good work!&amp;quot;.&lt;/p&gt;  &lt;p&gt;(Note: I often refractoring my code to make the code better if needed..)   &lt;br /&gt;    &lt;br /&gt;Is the reason because I'm mostly involved in Agile project and I think of YAGNI and KISS, or is it only that I sucks or my code blinds me...&amp;#160; &amp;quot;hey where is my coding mate, I want to do eXtreme Programming!&amp;quot; ... hmm.. or will that help me? NO it want. The same thing here, when I look back there are always something I could have done better, and I don't think it has anything to do with Agile, even before I was using Agile I felt the same.    &lt;br /&gt;    &lt;br /&gt;When will the day come when I can go back to an old app and print my code and put it on the wall and say &amp;quot;Hi, I wrote this!&amp;quot; and be satisfied. I don't think that day will come... why?.. I think it's in the nature of the developer, or!? hmmm....    &lt;br /&gt;    &lt;br /&gt;How about artists, are they satisfied with the paintings they have done?... An old school teacher once told me &amp;quot;There is no artist that is satisfied with the painting they have done, they always say that they can do a better one!&amp;quot;. Is is true? Hmm, I don't know I'm not that kind of artist I refer too.. well I can of course ask one:&lt;/p&gt;  &lt;p&gt;&amp;quot;Is this true?&amp;quot;. &lt;/p&gt;  &lt;p&gt;I think we developers are artists, no WE are artist, wring code is to be an artist... isn't it!?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6139110" width="1" height="1"&gt;</description></item><item><title>The configurable application, no need for recompilation and new deployment!</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/28/the-configurable-application-no-need-for-recompilation-and-new-deployment.aspx</link><pubDate>Mon, 28 Apr 2008 08:03:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6138390</guid><dc:creator>Fredrik N</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6138390</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/28/the-configurable-application-no-need-for-recompilation-and-new-deployment.aspx#comments</comments><description>&lt;P&gt;The following I will write about is something I have used in some projects to see if it gives me any value, I haven't seen any evolutionary results yet, but maybe it's because I often move along to other projects when the previous one is completed ;)&lt;/P&gt;
&lt;P&gt;Let assume I have a User and the User has a property called Rebate. This method can have some business logic to calculate the rebate the use has when ordering products. Here is an dummy example of the method: &lt;BR&gt;&lt;/P&gt;
&lt;DIV&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;P&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; User
{
    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;double&lt;/SPAN&gt; Rebate
    {
        get { &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; superUser ? 10: 5; }
    }
}&lt;/P&gt;&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;User user = new User(....);&lt;BR&gt;double rebate = user.Rebate;
&lt;/P&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;The business rule for calculating the Rebate is something that can be changed in the future based in new business needs. Can we do the changes without&amp;nbsp; a new recompilation and deployment of the application? &lt;BR&gt;&lt;BR&gt;By using the Interfaces and also the Factory Pattern we could solve this. Here is an example where an Interface is added to the User and a Factory is used to create the User:&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;P&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;interface&lt;/SPAN&gt; IUser
{
    &lt;SPAN style="COLOR: #0000ff"&gt;double&lt;/SPAN&gt; Rebate { get; }
}

&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; User : IUser
{
    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;double&lt;/SPAN&gt; Rebate
    {
        get { &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; superUser ? 10: 5; }
    }
}&lt;/P&gt;&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;IUser user = UserFactory.Create(...);&lt;BR&gt;double rebate = user.Rebate;
&lt;/P&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;BR&gt;The UserFactory will now create an User an return an IUser. Because the factory creates the user I don't use "new User". I can now use late binding and load the assembly which has the implementation of the User and return it. I can for example use a configuration file where I specify the type of the user I want use.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&amp;lt;object name="IUser" type="MyNamespace.User, MyAssembly, Version 1.0 ..."&amp;gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;The UserFactory can now load the type specified in the configuration file and create an instance of it. If I need an new version of the User because &lt;BR&gt;some changes need to made based on business needs, I can create a new assembly and a new IUser. I can then modify my configuration file to make sure&amp;nbsp; the UserFactory loads my new implementation. By using this solution I don't need to compile my application when changes to classes is needed. Instead I create a new version of the class, put it into a new version of the assembly and deploy it. Then I only change the configuration file so the Factory gets my new implementation instead of the old version. &lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; User : IUser
{
    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;double&lt;/SPAN&gt; Rebate
    {
        get { &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; IsMegaUser() ? 11: 6; }
    }
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;BR&gt;&amp;lt;object name="IUser" type="MyNamespace.User, MyAssembly, Version 2.0 ..."&amp;gt; &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;Another solution to solve this could be to use Dependency Injection, where I inject a object with the implementation of the business logic. I can then for example use Spring.Net to inject the business object into my User. By using Spring.Net I can in a configuration file specify which business object that should be injected to the User class when it's instantiated. This will make it possible to only implement a new version of the business object rather then a new version of the User.&amp;nbsp;I will not use this solution because&amp;nbsp;it can be kind of ugly. But by using Spring.Net I can hide the injection of the dependency and never see it in my code. Let's think like an Object, would you like someone to easy change your behavior and you can't do any thing about it? &lt;BR&gt;&lt;BR&gt;I sometimes uses Interfaces and in some cases Factories, but not so often (It depends on the project and if the Customer already have specified a design to use). I use interfaces and factories when I want my application to be plugable, for example make it easy to inject Repositories into my Services etc, and also to make it possible to use Mock object during test etc.&lt;/P&gt;
&lt;P&gt;About using this solution for most of the classes in a application, wouldn't it in an Agile project sort of violate YAGNI!? &lt;BR&gt;&lt;BR&gt;It can sort of help the people that need to maintain the application after it's deployed, as long as the interface of the classes can be intact. What do you think about it? Is this something you use today in your application and have it result into something good?&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6138390" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/Patterns/default.aspx">Patterns</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/Desigm/default.aspx">Desigm</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/Write+code/default.aspx">Write code</category></item><item><title>Can the use of Extension methods break the Law of Demeter?</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/25/can-the-use-of-extension-methods-break-the-law-of-demeter.aspx</link><pubDate>Fri, 25 Apr 2008 19:05:26 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6130377</guid><dc:creator>Fredrik N</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6130377</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/25/can-the-use-of-extension-methods-break-the-law-of-demeter.aspx#comments</comments><description>&lt;p&gt;To make an easy description of &lt;a href="http://en.wikipedia.org/wiki/Law_of_Demeter"&gt;Law of Demeter&lt;/a&gt; we can summarize it to the following sentence:    &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;&amp;#8220;In particular, an object should avoid invoking methods of a member object returned by another method.&amp;#8221;     &lt;br /&gt;&lt;/strong&gt;    &lt;br /&gt;What does this has to do with Extension methods? It depends on how it&amp;#8217;s used. For example we have several Extension Methods for the IEnumerable interface, like Where and OrderBy. By using those methods we can easy select data out from lists, for example (I&amp;#8217;m so worthless when it comes to give methods good name, and the following method is so stupid, but it&amp;#8217;s only an example.):    &lt;br /&gt;&lt;/p&gt;  &lt;div&gt;   &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IList&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; GetNumbersGreaterThanTwo(IList&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; numbers)
{
         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; numbers
                       .Where(n=&amp;gt; n &amp;gt; 2)
                       .OrderBy(n =&amp;gt; n);
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;If this code should not break the law of Demeter, it will look like this:

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IList&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; GetNumbersGreaterThanTwo(IList&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; numbers)
{
    var result = numbers.Where(n=&amp;gt; n&amp;gt;2);
    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; result.OrderBy(n =&amp;gt; n);
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;But that is not true, regarding to the book &lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/020161622X/qid=1107281514/sr=8-1/ref=pd_csp_1/002-5857481-5032056?v=glance&amp;amp;s=books&amp;amp;n=507846"&gt;The Pragmatic Programmer&lt;/a&gt;, we don&amp;#8217;t &amp;#8220;own&amp;#8221; result and it wasn&amp;#8217;t passed to us, so we can&amp;#8217;t use OrderBy. So we break the law. How about using LINQ instead?

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IList&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; GetNumbersGreaterThanTwo(IList&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; numbers)
{
     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (from n &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; numbers
                    &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; n &amp;gt; 2
                   select n).ToList();
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;Will it break it?

  &lt;br /&gt;

  &lt;br /&gt;If we have the following Extension method:

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; StringExtension
{
        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; ToInt(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;)
        {
            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.IsNullOrEmpty(&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;))
                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; 0;
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Int32.Parse(&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;);
        }
} &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;And use it in like this: &lt;/p&gt;

&lt;div&gt;
  &lt;br /&gt;

  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; number = 45.ToString().ToInt(); &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;Yes, I know, why make an Int a string and then back to an Int, well I&amp;#8217;m so bad when it comes to write good examples too ;) &lt;/p&gt;

&lt;p&gt;Will the code break the Law of Demeter? &lt;/p&gt;

&lt;p&gt;If I have understood how Extension method works, it will not. Why? Well it&amp;#8217;s because an Extension method is only a compiler trick, the compiler will change the above code into something like this (Please correct me if I&amp;#8217;m totally wrong):
  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; temp = 45.ToString();
&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; number = StringExtension.ToInt(temp); 

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; StringExtension
{
     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; ToInt(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;)
     {
          &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.IsNullOrEmpty(&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;))
              &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; 0;
          &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Int32.Parse(&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;);
     }
} &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;Should we even care about this Law at all? Well is some cases, and in some not. I don&amp;#8217;t think we should slavery follow it. I try to avoid larger response sets, but I will still write my code as the first example in my post where I use Where and OrderBy, I will not split it down and create extra wrapper methods to avoid breaking the Law. The reason I will break the law in that case is because I think it&amp;#8217;s more readable and easy to understand. By using wrapper methods etc for that example, only make the code smell &lt;/p&gt;

&lt;p&gt;Here are some examples I try to avoid, only to give you at least some examples: 
  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;ds.Tables[0].Rows[10].Cols[5].ToString(); 

customerRepository.GetCustomerById(10).FirstName 

myPlaceHolder.FindControl(&lt;span style="color: #006080"&gt;&amp;quot;TextBox&amp;quot;&lt;/span&gt;).Text = &lt;span style="color: #006080"&gt;&amp;quot;text&amp;quot;&lt;/span&gt;; 

myPlaceHolder.FindControl(&lt;span style="color: #006080"&gt;&amp;quot;myPlacehodler2&amp;quot;&lt;/span&gt;).FindControl(&lt;span style="color: #006080"&gt;&amp;quot;Button&amp;quot;&lt;/span&gt;).Text = &lt;span style="color: #006080"&gt;&amp;quot;My Button&amp;quot;&lt;/span&gt;; &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;Regarding to the book &lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/020161622X/qid=1107281514/sr=8-1/ref=pd_csp_1/002-5857481-5032056?v=glance&amp;amp;s=books&amp;amp;n=507846"&gt;The Pragmatic Programmer&lt;/a&gt;, there was studies which shown that classes in C++ with larger response sets are more prone to error than classes with smaller response sets. &lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;&lt;strong&gt;&amp;#8220;System with many unnecessary dependencies are very hard (and expensive) to maintain, and tend to be highly unstable. In order to keep the dependencies to a minimum, we&amp;#8217;ll use the Law of Demeter to design our methods and functions.&amp;#8221; &amp;#8211; &lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/020161622X/qid=1107281514/sr=8-1/ref=pd_csp_1/002-5857481-5032056?v=glance&amp;amp;s=books&amp;amp;n=507846"&gt;The Pragmatic Programmer&lt;/a&gt; page. 140&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6130377" width="1" height="1"&gt;</description></item><item><title>What purpose does the Repository Pattern have?</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/24/what-purpose-does-the-repository-pattern-have.aspx</link><pubDate>Thu, 24 Apr 2008 15:20:53 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6127177</guid><dc:creator>Fredrik N</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6127177</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/24/what-purpose-does-the-repository-pattern-have.aspx#comments</comments><description>&lt;p&gt;I have watch &lt;a href="http://blog.wekeroad.com/"&gt;Rob Conery&amp;#8217;s&lt;/a&gt; great &lt;a href="http://blog.wekeroad.com/mvc-storefront/mvc-storefront-part-1/"&gt;screencast&lt;/a&gt; about &lt;a href="http://blog.wekeroad.com/mvc-storefront/mvc-storefront-part-1/"&gt;MVC Storefront&lt;/a&gt;. If you haven&amp;#8217;t seen them, you should take a look. Really interesting, he build and app by using Agile, &amp;quot;TDD&amp;quot; etc. I have some comments about his implementation I want to share, and if you don't agree with me, it's fine, because I'm not an expert, this post is based on my own experience and knowledge ;)&lt;/p&gt;  &lt;p&gt;Feel absolutely free to criticize me, but please give suggestions about what things can be done better, and also a reason why. There is no use if you add comments like &amp;quot;I don't agree with you!&amp;quot; if you don't say why.   &lt;br /&gt;    &lt;br /&gt;Something that I don&amp;#8217;t like with his implementation so far is his use of the &lt;a href="http://www.martinfowler.com/eaaCatalog/repository.html"&gt;Repository Pattern&lt;/a&gt;. I don't know what pattern Rob refer to, I assume it's &lt;a href="http://www.martinfowler.com/"&gt;Fowler's&lt;/a&gt; Repository Pattern. This post is based on my experience and interpretation of the Repository pattern, only so you know.    &lt;br /&gt;    &lt;br /&gt;Rob creates a Repository which has a simple interface, he have for example a method GetCategories which returns an IQueryable&amp;lt;Category&amp;gt; object.     &lt;br /&gt;&lt;/p&gt;  &lt;div&gt;   &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; ICategoryRepository
{
        IQueryable&amp;lt;Category&amp;gt; GetCategories();
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;He also use the Service layer to implement common used queries such as GetCategories.&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; CategoryService
{
    &lt;span style="color: #008000"&gt;//...&lt;/span&gt;

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IList&amp;lt;Category&amp;gt; GetCategories()
    {
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _repository.GetCategories().ToList();
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Another method that Rob put in to the Service Layer is GetProductsByCategory(int categoryId).
  &lt;br /&gt;

  &lt;br /&gt;This is the part I don&amp;#8217;t like; I will try to explain why and based on my knowledge and experience of the Repository pattern.

  &lt;br /&gt;

  &lt;br /&gt;The Repository has a responsibility to return entities. What Rod does is returning an IQueryable object, a query nothing else. So his Repository basically don&amp;#8217;t return any entities, it&amp;#8217;s when he first make a call to the ToList() he execute the query and then get the entities, but it&amp;#8217;s the object he returns from the Repository that gives him the entities, not the Repository itself. For me the Repository he use is sort of meaningless; it only works as a query provider not as a Repository regarding to the definition of the Repository Pattern. &lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;&lt;strong&gt;&lt;em&gt;&amp;#8220;The Repository will delegate to the appropriate infrastructure services to get the job done. Encapsulating in the mechanisms of storage, retrieval and query is the most basic feature of a Repository implementation&amp;#8221; 
      &lt;br /&gt;

      &lt;br /&gt;&amp;#8220;With a Repository, client code constructs the criteria and then passes them to the Repository, asking it to select those of its objects that match. From the client code&amp;#8217;s perspective, there&amp;#8217;s no notion of query &amp;#8220;execution&amp;#8221;; rather there&amp;#8217;s the selection of appropriate object through the &amp;#8220;satisfaction&amp;#8221; of the query&amp;#8217;s specification.&amp;#8221;

      &lt;br /&gt;

      &lt;br /&gt; &amp;#8220;Most common queries should also be hard coded to the Repositories as methods.&amp;#8221;

      &lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;

  &lt;br /&gt;Source: &lt;a href="http://www.martinfowler.com/books.html#eaa"&gt;PoEAA&lt;/a&gt; [Fowler] and &lt;a href="http://domaindrivendesign.org/books/aboutthecover.html"&gt;DDD&lt;/a&gt; [Evans] &lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;The interface of a Repository I should have used if I should slavery follow the definition of the Repository pattern is:

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IProductRepository
{
    &lt;span style="color: #008000"&gt;//...&lt;/span&gt;

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IList&amp;lt;Product&amp;gt; GetProductsByCategory(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; categoryID);

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IList&amp;lt;Products&amp;gt; GetProducts();
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;What infrastructure service the Repository should use&amp;#160; is something I will decide later in my project (This is also something Rob mention in his screencasts), first I will make sure my Domain Model is correct, then I decide based on my model what infrastructure service I should use to persist my model. The way to persist my model is something I probably never going to change, and to follow &lt;a href="http://en.wikipedia.org/wiki/You_Ain't_Gonna_Need_It"&gt;YAGNI&lt;/a&gt; which is a important part when working Agile, I shouldn't care or write code which make is possible to easy replace the infrastructure service a Repository use because I think it may change or I may need it later. But if I decide to use LINQ to SQL, the implementation of my Repository will probably look like this:

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ProductRepository : IProductRepository
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IList&amp;lt;Product&amp;gt; GetProductsByCategory(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; categoryID)
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (MyDataContext dataContext = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MyDataContext())
        {
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (from p &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; _dataContext.Products
                    &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; p.CategoryId == categoryID
                    select p).ToList();
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;Something to observe here is that I dispose the DataContext after I execute my Query. By doing that I will lose the track changing of my entities, my Unit of Work and also my Identity map which is handle by the Context. But when I write this code using Agile, I don't need it at the moment.

  &lt;br /&gt;

  &lt;br /&gt;My Mock object for the ProductRepository would probably look like this:

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MockProductRepository : IProductRepository
{
       &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IList&amp;lt;Product&amp;gt; GetProducts()
       {
           var products = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;Product&amp;gt;();

           &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 10; i++)
               products.Add(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Product(.....));

           &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; products;
       }


       &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IList&amp;lt;Product&amp;gt; GetProductsByCategory(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; categoryID)
       {
           var products = &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.GetProducts();

           &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (from p &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; products
                   &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; p.CategoryID == categoryID
                   select p).Single();
       }

       &lt;span style="color: #008000"&gt;//...&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;If we return an IQueryable&amp;lt;&amp;gt; instead of a IList from our Repository and decide to use LINQ To SQL, we will need to have something in mind. Correct me if I'm wrong, but the GetTable method used by LINQ to SQL will return a Table&amp;lt;&amp;gt; object, which implements the IQueryable&amp;lt;&amp;gt; interface. The Table&amp;lt;&amp;gt; object will hold a reference to the DataContext. So a call to the ToList() method of the IQueryable&amp;lt;&amp;gt; require the context. So the following code will not work:
  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ProductRepository : IProductRepository
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IQueryable&amp;lt;Product&amp;gt; GetProductsByCategory(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; categoryID)
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (MyDataContext dataContext = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MyDataContext())
        {
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; dataContext.Products;
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;The implementation of the Repository can for example look like this to make it work:

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ProductRepository : IProductRepository
{
     &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; MyDataContext dataContext = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MyDataContext();
     
     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IQueryable&amp;lt;Product&amp;gt; GetProductsByCategory(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; categoryID)
     {
         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; dataContext.Products;
     }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;What does this code really do? Well it only expose Table&amp;lt;&amp;gt; objects at the moment and serve more like a query provider than as a Repository.

  &lt;br /&gt;

  &lt;br /&gt;If we still keep this implementation we need to make sure the DataContext get disposed, so we don't add to much unnecessary entities to the Identity map etc, right!? This is something the Service layer need to do if we use the solution Rob uses in his project. If we don't want to reuse the same context for all method in our Repository we can use the following implementation:

  &lt;br /&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ProductRepository : IProductRepository
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IQueryable&amp;lt;Product&amp;gt; GetProductsByCategory(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; categoryID)
    {
        MyDataContext dataContext = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MyDataContext();

        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; dataContext.Products;
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The problem here is that each call to the Repository's methods, will create an instance of the DataContext which will be added to the memory, each will have it's on Identity Map etc, those features can probably be turned of so it will not be unnecessary copies of entities in the memory. But still I assume we need to have more things in concern when returning a IQueryable&amp;lt;&amp;gt;, maybe not at an early stage but later. Most of those can be avoided by not returning the IQuerable&amp;lt;&amp;gt;.
  &lt;br /&gt;

  &lt;br /&gt;One last thing that I'm not a fan about is the following code:&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _repository.GetProducts.WithId(10).ToList();&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It will break the &lt;a href="http://en.wikipedia.org/wiki/Law_of_Demeter"&gt;Law of Demeter&lt;/a&gt;. Instead if we do a call like this:&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _repository.GetProductsWithId(10);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We will not break the law, and this is the kind of method a Repository should have, if the query of a products is a common query we need to use.&lt;/p&gt;

&lt;p&gt;No one says that the Repository pattern Fowler and Evans talk about is a Silver bullet, and Rob has a good point when he told me:
  &lt;br /&gt;

  &lt;br /&gt;&lt;strong&gt;&lt;em&gt; &amp;#8220;One thing I&amp;#8217;ll suggest is that with a new feature set (.NET 3.5) comes some new ways of doing things.&amp;#8221;&lt;/em&gt;&lt;/strong&gt; 

  &lt;br /&gt;

  &lt;br /&gt;Even within the computer world, there are evolutions and we shouldn&amp;#8217;t be afraid of changes and test new way to solve things.&lt;/p&gt;

&lt;p&gt;It will be interesting to see how the final version of Rob's application will look like, maybe I will change my mind or his implementation will change ;)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6127177" width="1" height="1"&gt;</description></item><item><title>ASP.Net MVC Framework - How do I design my apps with the MVC pattern and +P</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/22/how-do-i-design-my-apps-with-mvc-p-what-do-i-refer-to-when-i-say-mvc-p.aspx</link><pubDate>Tue, 22 Apr 2008 20:36:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6121915</guid><dc:creator>Fredrik N</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6121915</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/22/how-do-i-design-my-apps-with-mvc-p-what-do-i-refer-to-when-i-say-mvc-p.aspx#comments</comments><description>&lt;P&gt;When I'm playing around with the ASP.NET MVC Framework I have created several prototype applications with different solutions to solve some "problems." In this post I will write down how I combine a Presentation Model (When I talk about Presentation Model in this post, I refer to a model which purpose is only to define a model for presentation purpose) with the MVC, it's what the P in MVC+P stands for.&amp;nbsp; &lt;BR&gt;&lt;BR&gt;When I build apps today I use Domain Driven Design and I use the MVC pattern in some of my web based applications. As many of you know the M in MVC stands for the Model and contains our entities, business logic can data access components. Often when I build my apps with the ASP.Net MVC Framework (still only use in as a prototype because the framework is under development) I create my own custom Controller Factory to support Dependency Injection (DI). For example I use constructor injection or setter injection to inject my Repositories or manual injection of Mock objects during test. I use Spring.Net as my DI framework because it’s the framework I like the most.&lt;/P&gt;
&lt;P&gt;Here is a simple example how my Controllers can look like and are prepared for constructor injection:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;HomeController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: #2b91af"&gt;ICompanyRepository &lt;/SPAN&gt;_companyRepository;

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;HomeController() : &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;CompanyRepository()) { }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;HomeController(&lt;SPAN style="COLOR: #2b91af"&gt;ICompanyRepository &lt;/SPAN&gt;companyRepository)
    {
        &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyRepository = companyRepository;
    }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;Index()
    {
        CompanyInfo companyInfo = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyRepository.GetCompanyInfo();
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Index"&lt;/SPAN&gt;, companyInfo);
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;As you can see I create an instance of a CompanyRepository in the default constructor, the reason to this is because if I create a normal instance of the HomeController, I want it to use a default Repository. I also have a constructor which takes a ICompanyRepository as an argument, this is because to enable constructor injection. I can use the constructor to pass in my Mock object of the CompanyRepository, or I can use Spring.Net to inject a ICompanyRepository, which I use in my own custom Controller Factory. Here is how my test could look like where I have a Mock object for the CompanyRepository: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: #2b91af"&gt;MockCompanyRepository &lt;/SPAN&gt;mockCompanyRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MockCompanyRepository&lt;/SPAN&gt;();

    mockCompanyRepository.Create(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;CompanyInfo(&lt;SPAN style="COLOR: #a31515"&gt;"........"&lt;/SPAN&gt;));

    &lt;SPAN style="COLOR: #2b91af"&gt;CompanyController &lt;/SPAN&gt;companyController = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyController&lt;/SPAN&gt;(mockCompanyRepository);

    var result = &lt;SPAN style="COLOR: #2b91af"&gt;CompanyController&lt;/SPAN&gt;.Index() &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RenderViewResult&lt;/SPAN&gt;;

    &lt;SPAN style="COLOR: green"&gt;//Assert...
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;&lt;EM&gt;Note: If you have read my Step by Step Guide, I use the LINQ to SQL's DataContext object directly in my Controllers, it's because they can sometimes serve as a "Repository" because the power of the LINQ.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;When I use MasterPage which should always render a part of my Model, like content for a Menu etc, or when I need to render more than one entities of my Model, I need to make sure my Controllers will fill the ViewData property with more information than just one single entity from my Model. In this case I may have several Repositories which I need to call from my Controllers. If I use constructor injection, I need to add an argument to a Controller's constructor for each Repository. This can make the constructor cumbersome. To solve the issue and reduce the number of argument, I instead use setter injection, so I inject the Repositories needed through a set method (using properties). By doing this I can easy manually inject my Mock objects in my test:&lt;/P&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: #2b91af"&gt;MockCompanyRepository &lt;/SPAN&gt;mockCompanyRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MockCompanyRepository&lt;/SPAN&gt;();
    &lt;SPAN style="COLOR: #2b91af"&gt;MockCustomerRepository &lt;/SPAN&gt;mockCustomerRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MockCustomerRepository&lt;/SPAN&gt;();

    &lt;SPAN style="COLOR: green"&gt;//...

    &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyController &lt;/SPAN&gt;companyController = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyController&lt;/SPAN&gt;();

    companyController.CompanyRepository = mockCompanyRepository;
    companyController.CustomerRepository = mockCustomerRepository;

    var result = &lt;SPAN style="COLOR: #2b91af"&gt;CompanyController&lt;/SPAN&gt;.Index() &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RenderViewResult&lt;/SPAN&gt;;

    &lt;SPAN style="COLOR: green"&gt;//Assert...
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;I can still reuse my own custom Controller Factory without changes to support setter injection, because I simple only let Spring.Net know that I want to inject my Repositories by using setter injection. When I use setter injection, I don't create an instance of the default Repository or Repositories in the default constructor of the Controller.&lt;/P&gt;
&lt;P&gt;The following is an example of a Controller which uses to Repositories and pass two entities to the "View":&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;HomeController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: #2b91af"&gt;ICompanyRepository &lt;/SPAN&gt;_companyRepository;
    &lt;SPAN style="COLOR: #2b91af"&gt;ICustomerRepository &lt;/SPAN&gt;_customerRepository;

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;HomeController() { }


    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;Index()
    {
        ViewData[&lt;SPAN style="COLOR: #a31515"&gt;"CompanyInfo"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CompanyRepository.GetCompanyInfo();&lt;BR&gt;        ViewData[&lt;SPAN style="COLOR: #a31515"&gt;"Customer"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CustomerRepository.GetCustomer();
        
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RenderView();
    }

    
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ICustomerRepository &lt;/SPAN&gt;CustomerRepository
    {
        &lt;SPAN style="COLOR: blue"&gt;get
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._customerRepository == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._customerRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerRepository&lt;/SPAN&gt;();

            &lt;SPAN style="COLOR: blue"&gt;return this&lt;/SPAN&gt;._customerRepository;
        }
        &lt;SPAN style="COLOR: blue"&gt;set
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._customerRepository = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;;
        }
    }


    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ICompanyRepository &lt;/SPAN&gt;CompanyRepository
    {
        &lt;SPAN style="COLOR: blue"&gt;get
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyRepository == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyRepository&lt;/SPAN&gt;();

            &lt;SPAN style="COLOR: blue"&gt;return this&lt;/SPAN&gt;._companyRepository;
        }
        &lt;SPAN style="COLOR: blue"&gt;set
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyRepository = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;;
        }
    }

}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The Index method will now return two entities. Assume the CompanyInfo is for the MasterPage and the Customer for the Content Page. The ViewData property in this case will sort of be a Presentation Model (&lt;EM&gt;"The essence of a Presentation Model is of a fully self-contained class that represents all the data and behavior of the UI window"&lt;/EM&gt;) but don't contain any behavior.&lt;/P&gt;
&lt;P&gt;Something that can make the above Controller or other Controllers ugly and start to smell, is when most action method must set the ViewData for the Model which the MasterPage will render. There is a tiny little problem regarding to how I will work and think when it comes to Test Driven Developement, when I use a MasterPage or User Controls on a View which will render different models. The problem is, when I create a test for a Controller before the Controller is implemented, I must know about what Models the UI will render, all entity objects even for the MasterPage and User Controls.&amp;nbsp; I can't only assume that the CustomerController's List action method will return a list of Customers, I also need to have in mind the model the MasterPage and User Controls will use and when writing the test. Well basically I can ignore it, but it can affect how my ViewData will look like, and what key I need to use to get access to my list of Customers etc. This is something that I don't want to care about for every action method I want to create a test for. I also like the way I can type the ViewData property for the View. To solve the issue I decided to create a base class called PresentationModel. This class have one typed property for the model the MasterPage should render, MasterPageModel and one for the Repository which will get the Model for the MastePage, here is an example:&lt;/P&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;PRE&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;PresentationModel
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;private &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyInfo &lt;/SPAN&gt;_companyInfo;

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CompanyInfo &lt;/SPAN&gt;MasterPageModel
    {
        &lt;SPAN style="COLOR: blue"&gt;get
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CompanyRepository == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
                &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ApplicationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"A ICompanyRepository must be set"&lt;/SPAN&gt;);

            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(this._companInfo == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)&lt;BR&gt;                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyInfo = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CompanyRepository.GetCompanyInfo();

            &lt;SPAN style="COLOR: blue"&gt;return this&lt;/SPAN&gt;._companyInfo;
        }
        &lt;SPAN style="COLOR: blue"&gt;set
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;._companyInfo = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;;
        }
    }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ICompanyRepository &lt;/SPAN&gt;CompanyRepository { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;If the Model or the MasterPage is changed and that affect what the MastePage should render, I only need to change what model the PresentationModel class returns through the MastePageModel property, I don't need to go through all action methods and update them. That is one benefit with this design solution.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Note: I use a property for the CompanyRepository to make it easy to add a Mock object in my tests.&lt;/EM&gt; &lt;BR&gt;&lt;BR&gt;The PresentationModel is the type I specify for the MastePage to make it typed. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public partial class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Site &lt;/SPAN&gt;: System.Web.Mvc.&lt;SPAN style="COLOR: #2b91af"&gt;ViewMasterPage&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;PresentationModel&lt;/SPAN&gt;&amp;gt;
{
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;I then create a sub class of the PresentationModel called GenericPresentationModel. The GenericPresentaionModel class is a generic class which inherits from the PresentationModel and have a generic property called Model. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;GenericPresentationModel&lt;/SPAN&gt;&amp;lt;T&amp;gt; : &lt;SPAN style="COLOR: #2b91af"&gt;PresentationModel
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;GenericPresentationModel() {}

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;GenericPresentationModel(T model)
    {
        &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Model = model;
    }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;T Model { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;The Controllers can now use the GenericPresentationModel and pass it to the View, the following Controller has an Action method which will render a Customer:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;Index()
{
    &lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CustomerRepository.GetCustomer();

    &lt;SPAN style="COLOR: #2b91af"&gt;var &lt;/SPAN&gt;presentationModel = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;GenericPresentationModel&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;(customer);

    &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Index"&lt;/SPAN&gt;, presentationModel);
}&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;Because I pass the GenericPresentationModel to my View, I can also type the View:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public partial class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerView &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ViewPage&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;GenericresentationModel&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;&amp;gt;
{
}&lt;/PRE&gt;
&lt;P&gt;Then my ViewData property of my View will be of Type GenericPresentationModel&amp;lt;Customer&amp;gt;. So I can easy get access to my Customer by using ViewData.Model. &lt;BR&gt;&lt;BR&gt;The following example shows how the test of an Action which will render a Customer could look like:&lt;/P&gt;&lt;PRE class=code&gt;&lt;BR&gt;        &lt;SPAN style="COLOR: #2b91af"&gt;MockCustomerRepository &lt;/SPAN&gt;mockCustomerRepository = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MockCustomerRepository&lt;/SPAN&gt;();

        &lt;SPAN style="COLOR: green"&gt;//...

        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;HomeController &lt;/SPAN&gt;controller = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;HomeController&lt;/SPAN&gt;();
        controller.CustomerRepository = mockCustomerRepository();

        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;result = controller.Index() &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RenderViewResult&lt;/SPAN&gt;;

        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;presentationModel = result.ViewData &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;GenericPresentationModel&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;;

        &lt;SPAN style="COLOR: green"&gt;//Assert.....
&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;Because I don't use the MastePageModel property in my test of the Index method, I don't need to inject the Mock Repository for the PresentationModel base class. When I created the PresentationModel I have already created a test to make sure the PresentationModel returns the correct data, so I don't need to care about testing that again in my other tests. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;I still use this approach in a prototype solution to see how well it will work. When I work with Domain Driven Design I often need to render different information from different entities from my domain model. In my case I don't always want to pass several entities to my Views, so instead I create new object that will combine information from different entities, with another word, I create a Presentation Model. Most larger apps which I have created with the ASP.Net MVC Framework, often result in the use of a Presentation Model, and that is the P in the MVC+P. I know that several people don't like this because it requires us to create a lot of extra classes, but the benefits is that the Views will use the Presentation Model and don't even need to know about the domain model. If we don't pass a single entity from our domain model to the View, instead we use a Presentation Model and map the domain model to the Presentation Model, we can change the domain model, and it doesn't need to change the Views, only the mapping between the Presentation Model and the domain model. To be honest I don't create a complete Presentation Model, I still pass entities from the domain model to the View, mostly to keep it simple and not spend extra time to write new classes for each View. &lt;BR&gt;&lt;BR&gt;I'm interested about what you think, how you solve similar problems, do you use similar solutions that I do and have notice problems that I haven't notice yet, please let me know.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6121915" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/MVC+Framework/default.aspx">MVC Framework</category><category domain="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx">ASP.Net</category></item><item><title>Do you want to become a great developer?</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/18/do-you-want-to-become-a-great-developer.aspx</link><pubDate>Fri, 18 Apr 2008 15:12:25 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6110160</guid><dc:creator>Fredrik N</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6110160</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/18/do-you-want-to-become-a-great-developer.aspx#comments</comments><description>&lt;p&gt;I found this old post on my old blog and still think it may have some good book tips which I will share with you on my new blog, where I also have new visitors.&lt;/p&gt;  &lt;p&gt;In this post I will give my suggestions of books that in my opinion could make you become a great developer. Remember that books will only give you theoretical knowledge and not the practical knowledge, so you also need to use your knowledge and do some practical programming to success. You will also learn from your mistakes and the best way to succeed is to follow those who have succeeded.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Note: I assume you already have some great skills in some programming language, such as C#, VB.Net, Java and C/C++ etc. The books I will list in this post will be easier to understand if you have basic knowledge in C#, C/C++ or Java. Most examples in the book are written in Java, C# or C/C++.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Before you start your path to become a great developer, I should suggest you to read the following book:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/0806635703/qid=1096611950/sr=ka-1/ref=pd_ka_1/102-9178777-1113747"&gt;The Balanced Life: Achieving Success in Work &amp;amp; Love&lt;/a&gt; - Alan McGinnis&lt;/p&gt;  &lt;p&gt;To become a great developer you will need time, and you should be careful so you don&amp;#8217;t scarify the one you love.&lt;/p&gt;  &lt;p&gt;Read the books in the order they are listed:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Learn how to become a Pragmatic programmer:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;1) &lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/020161622X/qid=1107281514/sr=8-1/ref=pd_csp_1/002-5857481-5032056?v=glance&amp;amp;s=books&amp;amp;n=507846"&gt;The Pragmatic Programmer&lt;/a&gt; - Andrew Hunt, David Thomas&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Learn how to write well structured code&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;2) &lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/0735619670/qid=1096611465/sr=1-1/ref=sr_1_1/102-9178777-1113747?v=glance&amp;amp;s=books"&gt;Code Complete, Second Edition&lt;/a&gt; - Steve McConnell&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Learn about how to write secure code&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;3) &lt;a href="http://www.microsoft.com/MSPress/books/5957.asp"&gt;Writing Secure Code 2:Edition&lt;/a&gt; - Howard and LeBlanc&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Learn about Object Oriented thought process&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;4) &lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/0672326116/qid=1109092000/sr=8-1/ref=sr_8_xs_ap_i1_xgl14/103-5998940-8591861?v=glance&amp;amp;s=books&amp;amp;n=507846"&gt;The Object-Oriented thought Process&lt;/a&gt; &amp;#8211; Matt Weisfeld&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Learn GOF&amp;#8217;s Design Patterns&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;5) &lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/0201633612/002-4435631-3884048?v=glance"&gt;Design Patterns&lt;/a&gt; &amp;#8211; GOF&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Learn about different principles and patterns etc&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;6) &lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/0135974445/qid=1096614822/sr=8-4/ref=pd_csp_4/102-9178777-1113747?v=glance&amp;amp;s=books&amp;amp;n=507846"&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/a&gt; - Robert C. Martin&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Learn how to write high quality code by suing TDD&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;7) &lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/0321146530/qid=1109088589/sr=8-2/ref=pd_csp_2/103-5998940-8591861?v=glance&amp;amp;s=books&amp;amp;n=507846"&gt;Test Driven Development: By Example&lt;/a&gt; - Kent Beck&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Learn about how you can improve the design of existing code&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;8) &lt;a href="http://www.martinfowler.com/books.html#refactoring"&gt;Refactoring: Improving the Design of Existing Code&lt;/a&gt; - Martin Fowler&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Learn about how to refactoring to patterns&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;9) &lt;a href="http://www.amazon.com/exec/obidos/ASIN/0321213351/qid%3D1096611232/sr%3D11-1/ref%3Dsr%5F11%5F1/102-9178777-1113747"&gt;Refactoring to Patterns&lt;/a&gt; - Joshua Kerievsky&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Learn about Data Structures and when to use them&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;10) &lt;a href="http://www.brpreiss.com/books/opus4/"&gt;Data Structures And Algorithms With Object-Oriented Design Patterns In C++&lt;/a&gt; - Bruno R. Preiss&lt;/p&gt;  &lt;p&gt;Another great book I can recommend is a book about how to help others to excel:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/0806621516/qid=1096611655/sr=1-6/ref=sr_1_6/102-9178777-1113747?v=glance&amp;amp;s=books"&gt;Bringing Out the Best in People: How to Enjoy Helping Others Excel&lt;/a&gt; - Alan McGinnis&lt;/p&gt;  &lt;p&gt;If anyone have some more book tips or stuff that make you to be a good developer, please feel free to add a comment!&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6110160" width="1" height="1"&gt;</description></item><item><title>ASP.Net MVC Framework pre- Preview 3 - A Step by Step guide to create a simple web app.</title><link>http://weblogs.asp.net/fredriknormen/archive/2008/04/17/asp-net-mvc-framework-pre-preview-3-a-step-by-step-guide-to-create-a-simple-web-app.aspx</link><pubDate>Thu, 17 Apr 2008 10:00:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6105638</guid><dc:creator>Fredrik N</dc:creator><slash:comments>20</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/fredriknormen/rsscomments.aspx?PostID=6105638</wfw:commentRss><comments>http://weblogs.asp.net/fredriknormen/archive/2008/04/17/asp-net-mvc-framework-pre-preview-3-a-step-by-step-guide-to-create-a-simple-web-app.aspx#comments</comments><description>&lt;P&gt;Yesterday I posted a &lt;A class="" href="http://weblogs.asp.net/fredriknormen/archive/2008/04/16/asp-net-mvc-framework-2-a-step-by-step-guide-to-create-a-simple-web-application.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2008/04/16/asp-net-mvc-framework-2-a-step-by-step-guide-to-create-a-simple-web-application.aspx"&gt;step by step guide by using the Preview 2&lt;/A&gt; of the ASP.Net MVC Framework, the following is an updated version that targets the pre-release of the Preview 3 version of the ASP.Net MVC Framework.&lt;/P&gt;
&lt;P&gt;In this post you will learn how to use most of the new features in the pre-release of the Preview 3 version of the ASP.NET Framework, you will also get a basic understanding about how to use LINQ to SQL. This step by step guide shouldn't be used as a "best way to create and design" a web applications by using the ASP.NET MVC Framework, this guide should only be used to get started with the MVC Framework in a fast and easy way and learn different ways to use some of the features shipped with the ASP.Net MVC Framework pre-release Preview 3. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;To get the pre-release version, you can download it from &lt;A class="" href="http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=12640" target=_blank mce_href="http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=12640"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;In this step by step guide you are going to create a web based application where you will be able to list and modify customers. This step by step guide will use C# 3.0 and the Northwind database.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;1. Open Visual Studio 2008&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;2. Create a new Project and select the ”ASP.NET MVC Web Project” project from the "My Template" section and name it to “CustomerApp”. &lt;/P&gt;
&lt;P&gt;&lt;BR&gt;3. Open global.asax and walk through the code. In Global.asax you can specify your routes. In this step by step guide you will use the default settings.&lt;/P&gt;&lt;PRE class=code&gt;routes.MapRoute(
               &lt;SPAN style="COLOR: #a31515"&gt;"Default"&lt;/SPAN&gt;,                                              &lt;SPAN style="COLOR: green"&gt;// Route name
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;"{controller}/{action}/{id}"&lt;/SPAN&gt;,                           &lt;SPAN style="COLOR: green"&gt;// URL with parameters
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;{ controller = &lt;SPAN style="COLOR: #a31515"&gt;"Home"&lt;/SPAN&gt;, action = &lt;SPAN style="COLOR: #a31515"&gt;"Index"&lt;/SPAN&gt;, id = &lt;SPAN style="COLOR: #a31515"&gt;"" &lt;/SPAN&gt;}, &lt;SPAN style="COLOR: green"&gt;// Parameter defaults
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;{ controller = &lt;SPAN style="COLOR: #a31515"&gt;@"[^\.]*" &lt;/SPAN&gt;}                          &lt;SPAN style="COLOR: green"&gt;// Parameter constraints
           &lt;/SPAN&gt;);&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;As you can see, there is a new method added to the RouteCollection, MapRoute. This method can be used to make the configuration of routing more cleaner. You can still use the old way with the Route object.&lt;/P&gt;
&lt;P&gt;The first thing to do is to create your business objects (your Model) which your Controllers will interact with. In this step by step guide you will use LINQ to SQL, even if it’s not advisable to generate domain models out from a data source you will learn how to do it, only to get up and running fast.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The business objects belongs to the Model in the MVC pattern.&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;4. Right click on the Models folder in the Solution Explorer and select Add &amp;gt; New Item.&lt;/P&gt;
&lt;P&gt;Select the “LINQ to SQL Classes” template and name the file to “Northwind.dbml” and press the Add button.&lt;/P&gt;
&lt;P&gt;To create your Model out from a data source you need to add a database connection to Visual Studio. This can be done by using the Server Explorer. In the Solution Explorer, right click on the Data Connections node and select Add Connection. Enter the following information to connect to the Northwind database:&lt;/P&gt;
&lt;P&gt;Server Name: (local)&lt;/P&gt;
&lt;P&gt;Select the Northwind database, test the connection and press OK if you were able to connect to the database.&lt;/P&gt;
&lt;P&gt;A connection to the Northwind database will now be added under the “Data Connections” node in the Server Explorer. Expand the database and the Tables nodes; drag out the Customer table into the design view of the Northwind.dbml file. Save the file. When you save the file, LINQ to SQL will generate a Customer object for you where the columns from the Customers table will be added as properties to the Customer object. LINQ to SQL will also generate a DataContext object (NorthwindDataContext) which you will use later to access your information from the data source. Expand the Northwind.dbml file in the Solution Explorer, double click on the Northwind.designer.cs file and walk through the code. LINQ to SQL uses attributes to map classes to a table.&lt;/P&gt;
&lt;P&gt;Now it’s time to create the Controller which you are going to use in this application.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;5. Right click on the Controllers folder in the Solution Explorer and add a new item and select the “MVC Controller Class” template from the "My Templates" section, name it to “CustomerController”.&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;6. Add a private field of the type NorhwindDataContext to the Controller and name the field to “northwindDataContext”. Set the field to an instance of the NorthwindDataContext object.&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Note: The NorthwindDataContext class will exist in the CustomerApp.Models namespace.&lt;/I&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;using &lt;/SPAN&gt;CustomerApp.Models;

&lt;SPAN style="COLOR: blue"&gt;namespace &lt;/SPAN&gt;CustomerApp.Controllers
{
    &lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IDisposable
    &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: blue"&gt;private &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext &lt;/SPAN&gt;northwindDataContext = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt;();
       
        &lt;SPAN style="COLOR: green"&gt;//...
    &lt;/SPAN&gt;}
}&lt;BR&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;7. Make sure the CustomerController implements the IDisposable interface, and implement the Dispose method and make sure it will make a call to the northwindDataContext’s Dispose method. This will make sure the NorthwindDataContext’s Dispose method will be called when the Controller have been used. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;Controller&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IDisposable
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;private &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext &lt;/SPAN&gt;northwindDataContext = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt;();
   
    &lt;SPAN style="COLOR: green"&gt;//...
    
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Dispose()
    {
        &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.northwindDataContext.Dispose();
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;8. Add an Action method and name it to “List”. The List action will retrieve all Customers from the Customers table of the Northwind database. LINQ to SQL will be used to get all the Customers, order the Customers by CompanyName. When all customers are retrieved make sure to render a View with the name “Customers” (The View will be added later to the project), the View should render a generic list of Customer objects. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ActionResult &lt;/SPAN&gt;List()
{
    &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;Customer&amp;gt; customers = (&lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;northwindDataContext.Customers
                                &lt;SPAN style="COLOR: blue"&gt;orderby &lt;/SPAN&gt;customer.CompanyName
                                &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;customer).ToList();

    &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;RenderView(&lt;SPAN style="COLOR: #a31515"&gt;"Customers"&lt;/SPAN&gt;, customers);
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;There are some changes to the Action methods between the Preview 2 and pre-release of Preview 3 of the ASP.Net MVC Framework. An Action method now returns a ActionResult. The RenderView method will now return a RenderViewResult, which inherits the ActionResult class. The changes are made to make it easier to create unit test without needing to create a lot of Mock objects, and also to make it easier in the future to support async. programming etc. You can read more about it on &lt;A href="http://weblogs.asp.net/scottgu/archive/2008/04/16/asp-net-mvc-source-refresh-preview.aspx"&gt;Scott Guthire's blog&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Note: By default if we make a call to RenderView, without passing the name of the View, it will use the name of the Action method as the View to render.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The following example shows an example of how a unit test can look like to test the List action method:&lt;/P&gt;&lt;PRE class=code&gt;&lt;BR&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestClass&lt;/SPAN&gt;]
&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CusomerControllerTest
&lt;/SPAN&gt;{
    [&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]
    &lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;ListCustomerTest()
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;CustomerController &lt;/SPAN&gt;custController = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;CustomerController&lt;/SPAN&gt;();

        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;result = custController.List() &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;RenderViewResult&lt;/SPAN&gt;;

        &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.IsNotNull(result, &lt;SPAN style="COLOR: #a31515"&gt;"Expected a RenderViewResult"&lt;/SPAN&gt;);

        &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;customers = result.ViewData &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;;
        
        &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.IsNotNull(result.ViewName, &lt;SPAN style="COLOR: #a31515"&gt;"Customers"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"..."&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.IsNotNull(customers, &lt;SPAN style="COLOR: #a31515"&gt;"..."&lt;/SPAN&gt;);
        &lt;SPAN style="COLOR: green"&gt;//...
    &lt;/SPAN&gt;}
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;&lt;EM&gt;Note: There is a helper method of the Controller base class called Redirect, which can be used to do a redirect within a Action method. The Redirect method returns a HttpRedirectResult, which inherits from the ActionResult class. The RedirectToAction method now returns an ActionRedirectResult class.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;9. Create a View with the name “Customers” which will render the generic list of Customer objects. To add a View, right click on the Views folder and add a new folder with the name “Customer”. Right click on the Customer folder and add a new item, select the “MVC View Page” template from the "My Template" section and name the View to “Customers”. Make sure the View data inherits from ViewPage&amp;lt;List&amp;lt;CustomerApp.Models.Customer&amp;gt;&amp;gt;. This will make sure the Model which is passed as an argument to the RenderView method is a known and typed for the View. Open the file Customers.aspx.cs file, replace so the page inherits from ViewPage&amp;lt;List&amp;lt;CustomerApp.Models.Customer&amp;gt;&amp;gt; instead of ViewPage.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public partial class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customers &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;ViewPage&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;CustomerApp.Models.&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;&amp;gt;&amp;gt;
{
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;BR&gt;10. Add code to render a list of Customers to the View, make sure the CustomerID and ContactName are listed. Open the Customer.aspx file, add an inline-code block and use foreach to iterate through the Customers, use the ViewData property to get access to the generic list of Customer objects.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&lt;BR&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;@ &lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Page &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="C#" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;AutoEventWireup&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="true" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;CodeBehind&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="Customers.aspx.cs" &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Inherits&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="CustomerApp.Views.Customer.Customers" &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;DOCTYPE &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;html PUBLIC &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;

&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="http://www.w3.org/1999/xhtml" &amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
    &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;table&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
        &lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;( &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;ViewData) { &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;tr&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.Encode(customer.CustomerID) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
                &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;Html.Encode(customer.ContactName) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;td&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a3