<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Fredrik Normén</title><subtitle type="html">ASP.NET, AJAX, Silverlight, RIA, Architecture, Clean Code</subtitle><id>http://weblogs.asp.net/fredriknormen/atom.aspx</id><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/default.aspx" /><link rel="self" type="application/atom+xml" href="http://weblogs.asp.net/fredriknormen/atom.aspx" /><generator uri="http://communityserver.org" version="3.0.20510.895">Community Server</generator><updated>2010-05-09T11:09:00Z</updated><entry><title>Build, first day</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2011/09/14/build-first-day.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2011/09/14/build-first-day.aspx</id><published>2011-09-14T13:21:00Z</published><updated>2011-09-14T13:21:00Z</updated><content type="html"> 

It was an interesting day at the Microsoft Build Conference. I will in this blog post share some of the things that were presented during the first Keynote and big screen day.
&lt;br&gt;&lt;br&gt;The keynote started to present Windows 8, and the Metro (The new GUI). Focus was on the new user experience and UI. As many of you have probably already seen, the UI is similar to Windows Phone 7 and is created for touch. The idea of the Metro UI is to bring everything in front of the user, easy to find what they want and need.

Microsoft wanted to make Windows 7 even better. Everything that runs on Windows 7 runs on Windows 8.&lt;br&gt;&lt;br&gt;They reimage Windows to experience a new range of capabilities, scenarios, and factors. Windows 8 uses about 281 Mb memories and in the size an antivirus program is also running.


One feature I like the most was the Spell Checking through the whole system
Windows 8 will be a great new platform for developers.&lt;br&gt;&lt;br&gt;Developers can use the new WinRT API to build great apps, they can use different kind of languages, like HTML/CSS and Java-script, XAML and C# or VB, C or C++. The interesting part is HTML and Java-script. It will make it much easier to move web developers into writing Mobile apps and Windows 8 apps in an easy way.


App developers want rich connectivity and sharing capabilities, connection to customers. Reach every customer using Windows. Touch will be the future, and the WinRT deliver it all. Developer can use the WinRT APIs for Communication &amp;amp; Data, Graphics &amp;amp; Media, Devices and Printing. 


Contracts can be used to make app communicate with each other, an example of an Contract is the "Search Contract", that can be used to hook up an app into the Windows 8 "search engine". There are different ways apps can communicate with each other, loacaly, Scheduled or with Push (using Windows Push Notification WSN). With Tiles developer can extend the apps with information instead of using an icon.&lt;br&gt;&lt;br&gt;Developers will get a set of great of tools for writing Windows 8 apps. With Visual Studio 11 and Expression Blend 5, developers can be up and running in a short time by using predefined templates. Expression Blend 5 will now handle HTML and Java-script, and Blend is now integrated into Visual Studio 11. Visual Studio 11 also has a simulation feature when it comes to debugging, so developers could select for example a Slate and hit debug, and a simulation of a Slate will appear.

&lt;br&gt;&lt;br&gt;The new platform will have a great set of Could Services to make it easier to share images etc with friends over the cloud, apps working together. Apps are connected to each other and the cloud. Pull information from other apps. A tiny API can be used for this for developers.


Microsoft will also give developers and customers access to Windows Store, it will make it much easier to buy and sell apps. This will probably also boost and increase development of apps for Windows 8. Within Visual Studio 11, developers can easily package apps and publish them to the Windows Store; they just use the Store menu in Visual Studio 11.&lt;br&gt;&lt;br&gt;Everyone at Build got a Developer Preview of Samsung’s iCore5 Slate with a Developer Preview of Windows 8, Expression Blend 5 and Visual Studio 11 Express. So now the fun can begin.


You can download the bits by yourself at the Windows developer center: &lt;a href="http://dev.windows.com" mce_href="http://dev.windows.com"&gt;http://dev.windows.com&lt;/a&gt;. If you follow me on twitter (&lt;a href="http://www.twitter.com/fredrikn" mce_href="http://www.twitter.com/fredrikn"&gt;@fredrikn&lt;/a&gt;), you can read more about what will be presented during the conference. Tomorrow there will be a keynote, and then a lot of sessions, I will probably attend the HTML5 sessions and some other sessions like ASP.NET 4.5 and C# 5 etc.&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7952346" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="HTML" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/HTML/default.aspx" /><category term="Javascript" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Javascript/default.aspx" /><category term="Build" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Build/default.aspx" /><category term="Windows 8" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Windows+8/default.aspx" /></entry><entry><title>Update on ASP.NET Vulnerability</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/09/25/update-on-asp-net-vulnerability.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/09/25/update-on-asp-net-vulnerability.aspx</id><published>2010-09-25T07:15:36Z</published><updated>2010-09-25T07:15:36Z</updated><content type="html">&lt;p&gt;Scott Guthrie updated his blog with more info about the ASP.NTE Vulnerability, check it out here: &lt;a title="http://weblogs.asp.net/scottgu/archive/2010/09/24/update-on-asp-net-vulnerability.aspx" href="http://weblogs.asp.net/scottgu/archive/2010/09/24/update-on-asp-net-vulnerability.aspx"&gt;http://weblogs.asp.net/scottgu/archive/2010/09/24/update-on-asp-net-vulnerability.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7620144" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="ASP.Net" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx" /></entry><entry><title>Vulnerability in ASP.Net</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/09/18/vulnerability-in-asp-net.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/09/18/vulnerability-in-asp-net.aspx</id><published>2010-09-18T08:18:58Z</published><updated>2010-09-18T08:18:58Z</updated><content type="html">&lt;p&gt;I would recommend all of you who are managed ASP.Net application to read this:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://blogs.technet.com/b/msrc/archive/2010/09/17/security-advisory-2416728-released.aspx" href="http://blogs.technet.com/b/msrc/archive/2010/09/17/security-advisory-2416728-released.aspx"&gt;http://blogs.technet.com/b/msrc/archive/2010/09/17/security-advisory-2416728-released.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7614584" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author></entry><entry><title>Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/08/04/make-sure-the-web-config-pages-namespace-can-be-used-together-with-asp-net-mvc-3-p1-and-razor.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/08/04/make-sure-the-web-config-pages-namespace-can-be-used-together-with-asp-net-mvc-3-p1-and-razor.aspx</id><published>2010-08-04T09:24:00Z</published><updated>2010-08-04T09:24:00Z</updated><content type="html">&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Note: This blog post is based on the ASP.NET MVC 3.0 Preview 1.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The following section is from &lt;A href="http://www.forkcan.com/" mce_href="http://www.forkcan.com"&gt;Forkcan.com&lt;/A&gt; (If you didn’t know, you can get code shared to ForkCan.com on your blog, &lt;A href="http://weblogs.asp.net/fredriknormen/archive/2010/07/25/integrate-your-blog-or-sites-with-forkcan-com.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2010/07/25/integrate-your-blog-or-sites-with-forkcan-com.aspx"&gt;read more here about it&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;
&lt;SCRIPT type=text/javascript src="http://www.forkcan.com/share/view/226?highlight=true" mce_src="http://www.forkcan.com/share/view/226?highlight=true"&gt;&lt;/SCRIPT&gt;
&lt;/P&gt;
&lt;P&gt;If you want to know when I publish a new blog post you can follow me on twitter: &lt;A href="http://www.twitter.com/fredrikn" mce_href="http://www.twitter.com/fredrikn"&gt;@fredrikn&lt;/A&gt;&lt;/P&gt;&lt;/SCRIPT&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7578425" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="ASP.Net" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/ASP.Net/default.aspx" /><category term=".Net" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/.Net/default.aspx" /><category term="VS2010" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/VS2010/default.aspx" /><category term="MVC3.0" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/MVC3.0/default.aspx" /></entry><entry><title>ASP.Net MVC 3.0 Preview 1, Razor and nested master pages</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/08/01/asp-net-mvc-3-0-preview-1-razor-and-nested-master-pages.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/08/01/asp-net-mvc-3-0-preview-1-razor-and-nested-master-pages.aspx</id><published>2010-08-01T08:16:40Z</published><updated>2010-08-01T08:16:40Z</updated><content type="html">&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: This blog posts is based on the ASP.NET MVC 3.0 Preview 1.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In this blog post I will explain how I have created a nested master page in &lt;a href="http://www.forkcan.com"&gt;ForkCan.com&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Here is the master of the master pages ;) (_Loyout.cshtml) (I have removed some HTML from this example only to reduce the code):&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;br /&gt;@inherits System.Web.Mvc.WebViewPage
@using ForkCan.Web.Infrastructure.WebPageHelpers

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Head1&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;@View.Title&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    
    @RenderSection( &amp;quot;TitleContent&amp;quot;, optional : true)

&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;centerBlock&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        
       ... &lt;/pre&gt;

&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;clear&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;header&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;topBar&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;topMenu&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;nav&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;/&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Code&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                            @if (WebSecurity.IsAuthenticated) {
                                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;/user/login&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Log in&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                            } else {
                                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;/user/logout&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Log out&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                            }
                           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ch&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;

            ...

        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;clear&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;mainmasterContent&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

            @RenderBody()

            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

        ...
 
        @Google.GetAnalyticHtml()

   
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Something to point out in the _Layout.cshtml is the @RenderSection and @RenderBody mehtods. When using Master pages with Razor we can add the @RenderBody method where we want our “main content” of our content page to be located. If we want to specify some kind of “content place holders” we can use the @RenderSection methods. ForkCan.com has two “Content place holders” in the _Layout.cshtml, the RenderBody and the &amp;quot;RenderSection “TitleContent”. The TitleContent can be used from a “content page” to add styles, client-side scripts etc to the header of the _Layout.cshtml. To create a nested master page we just need to create a new “cshtml” file and then use the LayoutPage property to point to the master page we want our nested to be used (_TwoColumnsLayout.cshtml):&lt;/p&gt;

&lt;pre class="csharpcode"&gt;@inherits System.Web.Mvc.WebViewPage

@{
    LayoutPage = &amp;quot;~/Views/Shared/_Layout.cshtml&amp;quot;;
}

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;right-content&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

    @RenderSection(&amp;quot;RightContent&amp;quot;, optional : true)

&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;content&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

@RenderBody()

&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The content of the code above will be rendered where the @RenderBody of the _Layout.cshtml is placed. The nested masterpage in ForkCan.com have one “content place holder” added “RightContent” and the @RenderBody where the content of the content pages which will use the nested masterpage will be rendered. As you can see it’s quite easy to create nested master pages with Razor.&lt;/p&gt;

&lt;p&gt;The following is one content page using the _TwoColumnsLayout.cshtml (It’s the view that renders the list of shared codes on Forkcan.com):
  &lt;br /&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;@inherits System.Web.Mvc.WebViewPage&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ForkCan.Models.PresentationModel.ListOfShareCodes&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
@using ForkCan.Web.Infrastructure.Extensions
@using ForkCan.Web.Infrastructure.WebPageHelpers

@{
    View.Title = &amp;quot;ForkCan - Top Used Code&amp;quot;;
    LayoutPage = &amp;quot;~/Views/Shared/_twoColumnsLayout.cshtml&amp;quot;;
}


@section RightContent
{
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;/Scripts/jquery.tools.min.js&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

    &amp;lt;div &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;rssFeed&amp;quot;&lt;/span&gt;&amp;gt;
        &amp;lt;a href=&lt;span class="str"&gt;&amp;quot;@Model.FeedUrl&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;img &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;feedback&amp;quot;&lt;/span&gt; src=&lt;span class="str"&gt;&amp;quot;/content/rss.png&amp;quot;&lt;/span&gt; /&amp;gt;@Model.FeedTitle&amp;lt;/a&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;div&amp;gt;
        &amp;lt;span style=&lt;span class="str"&gt;&amp;quot;font-weight:bold;&amp;quot;&lt;/span&gt;&amp;gt;Latest activity&amp;lt;/span&amp;gt;
        &lt;span class="preproc"&gt;@foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;var&lt;/span&gt; activity &lt;span class="kwrd"&gt;in&lt;/span&gt; Model.LastActivities) {
        &amp;lt;div &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;activityItem&amp;quot;&lt;/span&gt;&amp;gt;
            @(&lt;span class="kwrd"&gt;new&lt;/span&gt; HtmlString(activity.ActivityText))
        &amp;lt;/div&amp;gt;
        }
    &amp;lt;/div&amp;gt;
}


    &amp;lt;div &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;viewHeader&amp;quot;&lt;/span&gt;&amp;gt;
        
        &amp;lt;h1&amp;gt;@Model.Header&amp;lt;/h1&amp;gt;

        &amp;lt;div id=&lt;span class="str"&gt;&amp;quot;filters&amp;quot;&lt;/span&gt;&amp;gt;
            &amp;lt;a &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;@(Model.OrderBy == &amp;quot;&lt;/span&gt;views&lt;span class="str"&gt;&amp;quot; ? &amp;quot;&lt;/span&gt;selectedFilter&lt;span class="str"&gt;&amp;quot; : &amp;quot;&lt;/span&gt;&lt;span class="str"&gt;&amp;quot;)&amp;quot;&lt;/span&gt; href=&lt;span class="str"&gt;&amp;quot;@(Model.ListUrl + &amp;quot;&lt;/span&gt;?orderby=views&amp;amp;search=&lt;span class="str"&gt;&amp;quot; + Server.UrlEncode(Helper.Request.Search))&amp;quot;&lt;/span&gt; title=&lt;span class="str"&gt;&amp;quot;Order by views&amp;quot;&lt;/span&gt;&amp;gt;Credits&amp;lt;/a&amp;gt;
            &amp;lt;a &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;@(Model.OrderBy == &amp;quot;&lt;/span&gt;used&lt;span class="str"&gt;&amp;quot; ? &amp;quot;&lt;/span&gt;selectedFilter&lt;span class="str"&gt;&amp;quot; : &amp;quot;&lt;/span&gt;&lt;span class="str"&gt;&amp;quot; )&amp;quot;&lt;/span&gt; href=&lt;span class="str"&gt;&amp;quot;@(Model.ListUrl + &amp;quot;&lt;/span&gt;?orderby=used&amp;amp;search=&lt;span class="str"&gt;&amp;quot; + Server.UrlEncode(Helper.Request.Search))&amp;quot;&lt;/span&gt; title=&lt;span class="str"&gt;&amp;quot;Order by most used code&amp;quot;&lt;/span&gt;&amp;gt;Used&amp;lt;/a&amp;gt;
            &amp;lt;a &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;@(Model.OrderBy == &amp;quot;&lt;/span&gt;newest&lt;span class="str"&gt;&amp;quot; ? &amp;quot;&lt;/span&gt;selectedFilter&lt;span class="str"&gt;&amp;quot; : &amp;quot;&lt;/span&gt;&lt;span class="str"&gt;&amp;quot; )&amp;quot;&lt;/span&gt; href=&lt;span class="str"&gt;&amp;quot;@(Model.ListUrl + &amp;quot;&lt;/span&gt;?orderby=newest&amp;amp;search=&lt;span class="str"&gt;&amp;quot; + Server.UrlEncode(Helper.Request.Search))&amp;quot;&lt;/span&gt; title=&lt;span class="str"&gt;&amp;quot;Order by newest code&amp;quot;&lt;/span&gt;&amp;gt;Newest&amp;lt;/a&amp;gt;
            &amp;lt;a &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;@(Model.OrderBy == &amp;quot;&lt;/span&gt;oldest&lt;span class="str"&gt;&amp;quot; ? &amp;quot;&lt;/span&gt;selectedFilter&lt;span class="str"&gt;&amp;quot; : &amp;quot;&lt;/span&gt;&lt;span class="str"&gt;&amp;quot; )&amp;quot;&lt;/span&gt; href=&lt;span class="str"&gt;&amp;quot;@(Model.ListUrl + &amp;quot;&lt;/span&gt;?orderby=oldest&amp;amp;search=&lt;span class="str"&gt;&amp;quot;+ Server.UrlEncode(Helper.Request.Search))&amp;quot;&lt;/span&gt; title=&lt;span class="str"&gt;&amp;quot;Order by oldest code&amp;quot;&lt;/span&gt;&amp;gt;Oldest&amp;lt;/a&amp;gt;
        &amp;lt;/div&amp;gt;

   &amp;lt;/div&amp;gt;
   
    &amp;lt;div id=&lt;span class="str"&gt;&amp;quot;container&amp;quot;&lt;/span&gt;&amp;gt;
   
    &lt;span class="preproc"&gt;@foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;var&lt;/span&gt; code &lt;span class="kwrd"&gt;in&lt;/span&gt; Model.Items)
    {
        &amp;lt;div &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;'mySharedCodeItem'&lt;/span&gt;&amp;gt;

            &amp;lt;div id=&lt;span class="str"&gt;&amp;quot;shareCodeRating&amp;quot;&lt;/span&gt;&amp;gt;
                &amp;lt;br /&amp;gt;
                &amp;lt;span title=&lt;span class="str"&gt;&amp;quot;Number of views&amp;quot;&lt;/span&gt;&amp;gt;@code.Views&amp;lt;/span&amp;gt;&amp;lt;br /&amp;gt;
                Views
            &amp;lt;/div&amp;gt;

            &amp;lt;div id=&lt;span class="str"&gt;&amp;quot;shareCodeItemLeft&amp;quot;&lt;/span&gt;&amp;gt;

                &amp;lt;a href=&lt;span class="str"&gt;'/viewcode/@code.CodeId/@code.TitleInUrl'&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;sharedCodeListItemTitle&amp;quot;&lt;/span&gt;&amp;gt;@code.Title&amp;lt;/a&amp;gt;&amp;lt;br /&amp;gt;

                &amp;lt;span &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;posted&amp;quot;&lt;/span&gt;&amp;gt;
                By &amp;lt;a href=&lt;span class="str"&gt;&amp;quot;/user/view/@code.CreatedById/@code.CreatedBy.ToUrlTitle()&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;sharedCodeListItemPostedBy&amp;quot;&lt;/span&gt; title=&lt;span class="str"&gt;'@(code.CreatedBy + &amp;quot; created this code&amp;quot;)'&lt;/span&gt;&amp;gt;@code.CreatedBy&amp;lt;/a&amp;gt;
                &amp;amp;nbsp;@code.CreatedByTitle&amp;amp;nbsp;
                @code.Created&amp;lt;/span&amp;gt;

                &amp;lt;br /&amp;gt;

               &amp;lt;br /&amp;gt;
               &amp;lt;span &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;codeDescription&amp;quot;&lt;/span&gt;&amp;gt;&amp;amp;quot;@code.ShortDescription&amp;amp;quot;&amp;lt;/span&amp;gt;

               @Html.Partial(&lt;span class="str"&gt;&amp;quot;_tags&amp;quot;&lt;/span&gt;, code.Tags)

            &amp;lt;/div&amp;gt;
            
            &amp;lt;div id=&lt;span class="str"&gt;&amp;quot;shareCodeItemRight&amp;quot;&lt;/span&gt;&amp;gt;

                &amp;lt;div &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;feedbacks&amp;quot;&lt;/span&gt;&amp;gt;
                    &amp;lt;img &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;feedback&amp;quot;&lt;/span&gt; title=&lt;span class="str"&gt;&amp;quot;Number of users used this code&amp;quot;&lt;/span&gt; src=&lt;span class="str"&gt;&amp;quot;/Content/used.png&amp;quot;&lt;/span&gt; /&amp;gt; @code.Used
                    &amp;lt;img &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;feedback&amp;quot;&lt;/span&gt; title=&lt;span class="str"&gt;&amp;quot;Number of positive feedback&amp;quot;&lt;/span&gt; src=&lt;span class="str"&gt;&amp;quot;/Content/positive.png&amp;quot;&lt;/span&gt; /&amp;gt; @code.PosetiveFeedback
                    &amp;lt;img &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;feedback&amp;quot;&lt;/span&gt; title=&lt;span class="str"&gt;&amp;quot;Number of negative feedback&amp;quot;&lt;/span&gt; src=&lt;span class="str"&gt;&amp;quot;/Content/negative.png&amp;quot;&lt;/span&gt; /&amp;gt; @code.NegativeFeedback
                    &amp;lt;img &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;feedback&amp;quot;&lt;/span&gt; title=&lt;span class="str"&gt;&amp;quot;Number of discussions about the code&amp;quot;&lt;/span&gt; src=&lt;span class="str"&gt;&amp;quot;/Content/discussions.png&amp;quot;&lt;/span&gt; /&amp;gt; @code.Discussions
                &amp;lt;/div&amp;gt;

            &amp;lt;/div&amp;gt;

            &amp;lt;br style=&lt;span class="str"&gt;&amp;quot;clear:both;&amp;quot;&lt;/span&gt; /&amp;gt;
         &amp;lt;/div&amp;gt;

        &amp;lt;div &lt;span class="kwrd"&gt;class&lt;/span&gt;=&lt;span class="str"&gt;&amp;quot;sharedCodeItemEnd&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/div&amp;gt;
    }

    @Pager.Render(Model.SelectedPage, 15, Model.TotalNumberOfCodeBlocks, Model.ListUrl + &lt;span class="str"&gt;&amp;quot;?page={0}&amp;amp;orderby=&amp;quot;&lt;/span&gt; + Model.OrderBy +&lt;span class="str"&gt;&amp;quot;&amp;amp;search=&amp;quot;&lt;/span&gt; + Server.UrlEncode(Helper.Request.Search));

    &amp;lt;/div&amp;gt;

    &amp;lt;script type=&lt;span class="str"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&amp;gt;
        $(&lt;span class="str"&gt;&amp;quot;a.tagLink&amp;quot;&lt;/span&gt;).tooltip(
            {
                effect: &lt;span class="str"&gt;'toggle'&lt;/span&gt;,
                position: &lt;span class="str"&gt;'bottom center'&lt;/span&gt;
            });
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The main content of the content page will be rendered where the @RenderBody method is located in the &amp;quot;_TwoColumnLayout.cshtml nested master page. To place something into the “content place holder” RightConent, we can in razor add a “content control” by using the @section syntax, the following code inside of the @section will be placed where the @RenderSection(“RightContent”) is located in the nested master page:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;@section RightContent
{
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;/Scripts/jquery.tools.min.js&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;rssFeed&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;@Model.FeedUrl&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;img&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;feedback&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;/content/rss.png&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;@Model.FeedTitle&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt; &lt;span class="attr"&gt;style&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;font-weight:bold;&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Latest activity&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        @foreach (var activity in Model.LastActivities) {
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;activityItem&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            @(new HtmlString(activity.ActivityText))
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        }
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
}&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Summery&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;By using the LayoutPage property we can specify which master page we want to use. The @RenderBody should be used and places where the main content of a content page’s content should be rendered. By using the @RenderSection we can create “content place holders”, and fill them with content from a content page by using @section.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;If you want to know when I publish a blog post to my blog, then you can follow me on twitter: &lt;a href="http://www.twitter.com/fredrikn"&gt;@fredrikn&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7576879" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term=".Net" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/.Net/default.aspx" /><category term="VS2010" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/VS2010/default.aspx" /><category term="ForkCan" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/ForkCan/default.aspx" /><category term="Razor" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Razor/default.aspx" /><category term="MVC3.0" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/MVC3.0/default.aspx" /></entry><entry><title>UnitOfWork Action filter for ASP.NET MVC and nHibernate</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/07/27/unitofwork-action-filter-for-asp-net-mvc-and-nhibernate.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/07/27/unitofwork-action-filter-for-asp-net-mvc-and-nhibernate.aspx</id><published>2010-07-27T14:26:00Z</published><updated>2010-07-27T14:26:00Z</updated><content type="html">
&lt;SCRIPT type=text/javascript src="http://www.forkcan.com/share/view/219?highlight=true" mce_src="http://www.forkcan.com/share/view/219?highlight=true"&gt;&lt;/SCRIPT&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7574330" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="C# nHibernate Repository aspmvc" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/C_2300_+nHibernate+Repository+aspmvc/default.aspx" /></entry><entry><title>Loading Strategy for Entity Framework 4.0</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/07/25/loading-strategy-for-entity-framework-4-0.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/07/25/loading-strategy-for-entity-framework-4-0.aspx</id><published>2010-07-25T10:23:00Z</published><updated>2010-07-25T10:23:00Z</updated><content type="html">
&lt;SCRIPT type=text/javascript src="http://www.forkcan.com/share/view/218?highlight=true" mce_src="http://www.forkcan.com/share/view/218?highlight=true"&gt;&lt;/SCRIPT&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7573532" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="C# EF4.0 Repository" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/C_2300_+EF4.0+Repository/default.aspx" /></entry><entry><title>Integrate your blog or sites with ForkCan.com</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/07/25/integrate-your-blog-or-sites-with-forkcan-com.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/07/25/integrate-your-blog-or-sites-with-forkcan-com.aspx</id><published>2010-07-25T09:20:00Z</published><updated>2010-07-25T09:20:00Z</updated><content type="html">&lt;P mce_keep="true"&gt;If you didn't know, you can get the code you have posted on ForkCan.com and include it to your blog posts or sites. The following is an instruction (The Instruction itself is included from &lt;A href="http://www.forkcan.com/" mce_href="http://www.forkcan.com"&gt;ForkCan.com&lt;/A&gt;):&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;SCRIPT type=text/javascript src="http://www.forkcan.com/share/view/188?highlight=true"&gt;&lt;/SCRIPT&gt;

&lt;P&gt;If you want to know when I have published a blog post, then you can follow me in twitter: &lt;A href="http://www.twitter.com/fredrikn" mce_href="http://www.twitter.com/fredrikn"&gt;@fredrikn&lt;/A&gt; &lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7573528" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="ForkCan" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/ForkCan/default.aspx" /></entry><entry><title>ForkCan is now available public – Share, Discuss, Improve code etc</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/07/11/forkcan-is-not-available-public-share-discuss-improve-code-etc.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/07/11/forkcan-is-not-available-public-share-discuss-improve-code-etc.aspx</id><published>2010-07-11T13:19:00Z</published><updated>2010-07-11T13:19:00Z</updated><content type="html">&lt;P&gt;ForkCan is now available, you can read more about ForkCan &lt;A href="http://weblogs.asp.net/fredriknormen/archive/2010/07/10/what-is-forkcan-com-do-you-have-what-it-takes-to-share-your-code.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2010/07/10/what-is-forkcan-com-do-you-have-what-it-takes-to-share-your-code.aspx"&gt;here&lt;/A&gt;. If you have an idea of a code, or want to see if it can be improved, want to find code for your project etc or talk about code. Then ForkCan is a place for you..&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.forkcan.com/" mce_href="http://www.forkcan.com"&gt;ForkCan&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://feedback.forkcan.com/" mce_href="http://feedback.forkcan.com/"&gt;Feedback&lt;/A&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7561794" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="Write code" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Write+code/default.aspx" /><category term="Code" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Code/default.aspx" /><category term="ForkCan" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/ForkCan/default.aspx" /></entry><entry><title>What is ForkCan.com – Do you have what it takes to Share your code?</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/07/10/what-is-forkcan-com-do-you-have-what-it-takes-to-share-your-code.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/07/10/what-is-forkcan-com-do-you-have-what-it-takes-to-share-your-code.aspx</id><published>2010-07-10T09:20:18Z</published><updated>2010-07-10T09:20:18Z</updated><content type="html">&lt;p&gt;I have worked on a project for a while now, I call it ForkCan and can be found on the following URL &lt;a href="http://www.forkcan.com"&gt;www.forkcan.com&lt;/a&gt;. ForkCan is all about sharing code in a social way. I have notice that several developers on twitter paste pointers to sites like &lt;a href="http://paste-it.net/"&gt;paste-it.net&lt;/a&gt; to share some code with others, and to get feedback on the code. I moved this a little bit further and created a similar site BUT! I also want to make sure developers can learn from each other, discuss the code with each other and much more. There are so many great code examples shared and distributed on the web, but to find them we need to search for it and it’s spread around different blog posts, forums etc. I wanted to create one place where developers can add the code, make a repository of codes where everyone can find great code to be used in a project. I also created ForkCan to save my own “good to have” code snippets, so I can find it easy and and copy and paste it into my projects. You may wonder why I call it ForkCan? It’s because it can be used as a can of &lt;a href="http://en.wikipedia.org/wiki/Fork"&gt;forks&lt;/a&gt; ;) &lt;a href="http://en.wikipedia.org/wiki/Fork_(software_development)"&gt;Fork in Software development&lt;/a&gt; is about taking an existing code and make development on it. So with ForkCan you can share your code, someone else can make it even better by making a Fork of it. The idea of ForkCan is to help each other to be better developers. The site also is a great tool for the Copy &amp;amp; Pasters.. ;)&lt;/p&gt;  &lt;p&gt;The feature ForkCan supports today are:   &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;1) Share Code (Anonymous or as a registered user)&lt;/p&gt;  &lt;p&gt;2) Add tags&lt;/p&gt;  &lt;p&gt;3) Give feedback (Positive, neutral and Negative) – feedback adds rating to the code.&lt;/p&gt;  &lt;p&gt;4) Rate the code (Is not yet implemented, but the rating is displayed)&lt;/p&gt;  &lt;p&gt;5) Make a Fork of the code and see the Fork in a tree form&lt;/p&gt;  &lt;p&gt;6) Discuss the code (will give points to the user that wrote the comment), in the future comments can be rated (the model is in place but not the code).&lt;/p&gt;  &lt;p&gt;7) User Titles (User can get different titles based on contributions)&lt;/p&gt;  &lt;p&gt;8) User Scoring&lt;/p&gt;  &lt;p&gt;9) Search for code&lt;/p&gt;  &lt;p&gt;10) Use OpenId&lt;/p&gt;  &lt;p&gt;11) Markdown for the content of the shared code&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I have much more features on my list to do, like Achievements, some sort of badges and advanced search etc. I will also work more with the User Interface, add more tips and helps to the site, a way to easily post Feedback about how to improve ForkCan and also report bugs etc.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The rest part of this post is how ForkCan looks like today and about some of the features that are added at the moment.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;First Page&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;First page will show a list of all the shared codes, it will by default be listed in a order where the most used code will be at the top.    &lt;br /&gt;&lt;strong&gt;&lt;em&gt;Note: At the moment the sorting is not yet implemented.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/fredriknormen/image_63E41696.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/fredriknormen/image_thumb_10209D7B.png" width="745" height="408" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The list shows the title of the code a developer have shared, a short description of the code, tags (there is not a predefined list of tags, just create your own tags), information about the code, such as when it was created, created by and the current points the user who created the code has. The smalls icons above the creator of the code shows the number of views, number of devs who have used the code, number of positive, neutral and negative feedback and also number of discussions (Should I add number of forks?)&lt;/p&gt;  &lt;p&gt;To Share a code, you simply press the big Share Code button at the right top corner under the search textbox.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Share Code&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Anyone can share codes, ForkCan uses OpenID for users that want to register them self on the site (gives of course a lot of benefits, such as making a fork, give feedback to other users etc.)&amp;#160; You can share code by just mark the code with a name. Here is how the Share Code form looks like today:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/fredriknormen/image_35AA1ADC.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/fredriknormen/image_thumb_78A5E949.png" width="735" height="735" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;ForkCan use the WMD RichTextArea control. The WMD control uses Markdown. I will later add a better help for how to you use Markdown, if you are familiar to StackOverflow you will know hot to use it. When you add a code block remember that you need to indent the code with 4 spaces. You can use some HTML tags and also add links.&lt;/p&gt;  &lt;p&gt;You can share code and login at the same time by specifying an OpenId, if you aren’t a registered user you will come to a Edit profile form to enter some information about your self:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/fredriknormen/image_05339666.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/fredriknormen/image_thumb_240A0A44.png" width="725" height="791" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;ForCan uses Gavatar for handling avatars. If you have earned one or more titles, you can select a title that will be displayed together with your user information when user are watching your code. The 20 first registered users will get three titles “No. 1” , “I was first here” and “1337”.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;View code&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;When you select a code to view from the list of code from the start page, you can as a registered and authenticated user post feedback to the user who has created the code (You will earn points if you post feedback, and the user who shared the code will also earn points as long as it’s positive feedback), you can also create a Fork or write comments about the code.   &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/fredriknormen/image_335D6C53.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/fredriknormen/image_thumb_672562A4.png" width="716" height="1319" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The following is a view where feedback, forks and comments are added:   &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/fredriknormen/image_5E555A58.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/fredriknormen/image_thumb_4F3E7B7E.png" width="723" height="1292" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I hope you will like ForkCan.com and I also hope this site will bring value to you as a developer and also to other developers. A Beta will be out later this day (the time from when this post was created) or tomorrow. I hope some of you are interested to do some contribution and also be a beta user.&lt;/p&gt;  &lt;p&gt;Hope to see you all at ForkCan.com&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;If you want to know when I have published a new blog post, then follow me on twitter: &lt;a href="http://www.twitter.com/fredriknormen"&gt;@fredrikn&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7560692" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="Write code" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Write+code/default.aspx" /><category term="Code" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Code/default.aspx" /><category term="ForkCan" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/ForkCan/default.aspx" /></entry><entry><title>Microsoft WebMatrix – Easy way to build ASP.NET Web Sites</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/07/07/microsoft-webmatrix-easy-way-to-build-asp-net-web-sites.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/07/07/microsoft-webmatrix-easy-way-to-build-asp-net-web-sites.aspx</id><published>2010-07-07T04:26:00Z</published><updated>2010-07-07T04:26:00Z</updated><content type="html">&lt;P&gt;Yesterday Microsoft &lt;A href="http://weblogs.asp.net/scottgu/archive/2010/07/06/introducing-webmatrix.aspx" mce_href="http://weblogs.asp.net/scottgu/archive/2010/07/06/introducing-webmatrix.aspx"&gt;announced&lt;/A&gt; &lt;A href="http://www.microsoft.com/web/webmatrix/" mce_href="http://www.microsoft.com/web/webmatrix/"&gt;WebMatrix&lt;/A&gt;, a tool to build standalone ASP.NET Pages using the new &lt;A href="http://weblogs.asp.net/fredriknormen/archive/2010/07/06/razor-the-new-asp-net-view-engine.aspx" mce_href="http://weblogs.asp.net/fredriknormen/archive/2010/07/06/razor-the-new-asp-net-view-engine.aspx"&gt;Razor&lt;/A&gt; view eninge. With WebMatrix developers will get the new &lt;A href="http://weblogs.asp.net/scottgu/archive/2010/06/28/introducing-iis-express.aspx" mce_href="http://weblogs.asp.net/scottgu/archive/2010/06/28/introducing-iis-express.aspx"&gt;IIS Developer Express&lt;/A&gt; and &lt;A href="http://weblogs.asp.net/scottgu/archive/2010/06/30/new-embedded-database-support-with-asp-net.aspx" mce_href="http://weblogs.asp.net/scottgu/archive/2010/06/30/new-embedded-database-support-with-asp-net.aspx"&gt;SQL Compact Edtition&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The idea of WebMatrix is to make it easy for developers to build web sites, not yet suitable for Enterprise Apps in my opinion, more for devs that want to get started really fast. With WebMatrix we will get a tool to easily create a Site, Browse our files, manages our database etc: &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/blogs/scottgu/image_18418D7A.png" mce_href="http://weblogs.asp.net/blogs/scottgu/image_18418D7A.png"&gt;&lt;IMG title=image border=0 alt=image src="http://weblogs.asp.net/blogs/scottgu/image_thumb_6117FF80.png" width=754 height=691 mce_src="http://weblogs.asp.net/blogs/scottgu/image_thumb_6117FF80.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;The tool is easy to use, easy to learn and manage. You don’t need to have Visual Studio to create a simple ASP.NET website. The following is an image editing a file within the tool: &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/blogs/scottgu/image_595C1EAC.png" mce_href="http://weblogs.asp.net/blogs/scottgu/image_595C1EAC.png"&gt;&lt;IMG title=image border=0 alt=image src="http://weblogs.asp.net/blogs/scottgu/image_thumb_676AE7DC.png" width=808 height=685 mce_src="http://weblogs.asp.net/blogs/scottgu/image_thumb_676AE7DC.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;Razor is a new View engine which can be used to write ASP.NET Web pages, the "Razor" view&amp;nbsp;engine together with WebMatrix reminds me a lot about classic ASP pages but using .Net, something that I like. It will make it much easier for those devs that find it hard to move from classic ASP to ASP.Net. Here is an example why I think WebMatrix and "Razor" reminds me of classic ASP pages, the code will read from a database and list members that belongs to a specific group, the value of the group is taken from the QueryString “group” by using the Request helper method:&lt;/P&gt;&lt;PRE class=csharpcode&gt;@{
   string group = Request[“group”];&lt;/PRE&gt;&lt;PRE class=csharpcode&gt;   var db = Database.Open("Members");
   var members = db.Query("SELECT * FROM Members WHERE Group = @0", group);
}

&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;html&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
      &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;head&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
               &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;title&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;List members&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;title&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
      &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;head&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
      &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;body&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
                &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;ul&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
                   @foreach (var member in members) {
                      &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;li&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;@member.FirstName - @member.LastName&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;li&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
                    }
                 &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;ul&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
      &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;body&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;html&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;
&lt;/P&gt;
&lt;P&gt;I like this way of writing a small app and the Razor’s much cleaner syntax. Have in mind that Razor view engine with ASP.NET MVC can be used for enterprise apps. Using Razor with WebMatrix will not target entperise apps developers in first place.&lt;/P&gt;
&lt;P&gt;Handling “submit “ HTTP Post and also caching is quite easy. To check if a Post is made, we can use the IsPost property, to cache information we can use the WebCache helper, the following is just a fast example I put together to demonstrate the use of IsPost and WebCache helper,&lt;/P&gt;&lt;PRE class=csharpcode&gt;@{

     var minutesToCache = 10;
     var useSliding = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;;
     &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; data = &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;.Empty;

     &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt;(IsPost)
     {
           data = Request[&lt;SPAN class=str&gt;"myData"&lt;/SPAN&gt;];

           &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (WebCache.Get(&lt;SPAN class=str&gt;"Key"&lt;/SPAN&gt;) == &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;)           
                WebChache.Set(&lt;SPAN class=str&gt;"Key"&lt;/SPAN&gt;, data, minutesToCache, useSliding);
     }

    &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt;(WebCache.Get(&lt;SPAN class=str&gt;"Key"&lt;/SPAN&gt;) != &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;)
          data = WebCache.Get(&lt;SPAN class=str&gt;"Key"&lt;/SPAN&gt;).ToString();
 }

&amp;lt;html&amp;gt;
      &amp;lt;form ..&amp;gt;
           &amp;lt;input type=&lt;SPAN class=str&gt;"text"&lt;/SPAN&gt; name=&lt;SPAN class=str&gt;"myData"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;=&lt;SPAN class=str&gt;"@data"&lt;/SPAN&gt; .../&amp;gt;
           &amp;lt;input type=&lt;SPAN class=str&gt;"Submit"&lt;/SPAN&gt; ..../&amp;gt;
      &amp;lt;/form&amp;gt;
&amp;lt;/html&amp;gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;With Razor we will get some social Web Helpers, for example Twitter, Gavatar etc: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;body&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
      @Twitter.Search("WebMatrix")
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;body&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;We can also an easy way create Code-based helper method, Microsoft is looking for supporting HTML Helpers, to globally share helpers among pages. Here is an example of a HTML Helper (will not work in the beta): &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;@helper RenderMembers(List&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;Member&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt; members) {
   &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;ul&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
      @foreach (var member in members) {
        &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;li&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;@member.FirstName @member.LastName&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;li&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
      }
   &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;ul&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
}


&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;body&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    @RenberMembers(Model.Menbers)
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;body&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;If you want to know when I have published a new blog post, follow me on twitter: &lt;A href="http://www.twitter.com/fredrikn" mce_href="http://www.twitter.com/fredrikn"&gt;fredrikn&lt;/A&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7556423" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="WebMatrix ASP.NET" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/WebMatrix+ASP.NET/default.aspx" /></entry><entry><title>“Razor” the new ASP.NET View Engine</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/07/06/razor-the-new-asp-net-view-engine.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/07/06/razor-the-new-asp-net-view-engine.aspx</id><published>2010-07-06T04:01:50Z</published><updated>2010-07-06T04:01:50Z</updated><content type="html">&lt;p&gt;Because of my work with the &lt;a href="http://www.forkcan.com"&gt;www.forkcan.com&lt;/a&gt; and the traveling around Sweden talking about ASPEN a Silverlight 4, MVVM, RIA Services, Unity 2.0, MEF application I haven’t spend so much time with blogging.&lt;/p&gt;  &lt;p&gt;“Razor” is a new view engine for ASP.NET, you can get more information about it on &lt;a href="http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx"&gt;Scott Guthrie’s&lt;/a&gt; blog. Here is an example of a old server-side script block in ASP.NET:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;: title &lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var item &lt;span class="kwrd"&gt;in&lt;/span&gt; items) {&lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;: item.FirstName &lt;span class="asp"&gt;%&amp;gt; &amp;lt;%: item.LastName %&amp;gt;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="asp"&gt;&amp;lt;%&lt;/span&gt;{&lt;span class="asp"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;With “Razor” the above code can look like this:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;@title&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

@foreach (var item in items {
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;@item.FirstName @item.LastName&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;span&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;br&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
}&lt;/pre&gt;

&lt;p&gt;
  &lt;br /&gt;“Razor” uses a new file suffix “cshtml” or “vbhtml”, where the prefix is the language used in the file.&lt;/p&gt;

&lt;p&gt;What I like the most with “Razor” is the new way to use expressions and the cleaner server-side code blocks.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;If you want to know when I will publish a new blog post, then you can follow me on twitter: &lt;a href="http://www.twitter.com/fredrikn"&gt;@fredrikn&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7555298" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author></entry><entry><title>Design by Contract with Microsoft .Net Code Contract</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/06/05/design-by-contract-with-microsoft-net-code-contract.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/06/05/design-by-contract-with-microsoft-net-code-contract.aspx</id><published>2010-06-05T09:54:00Z</published><updated>2010-06-05T09:54:00Z</updated><content type="html">&lt;P&gt;I have done some talks on different events and summits about Defensive Programming and Design by Contract, last time was at Cornerstone’s Developer Summit 2010. Next time will be at SweNug (Sweden .Net User Group). I decided to write a blog post about of some stuffs I was talking about.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Users are a terrible thing! Protect your self from them&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;”Human users have a gift for doing the worst possible thing at the worst possible time.”&lt;/STRONG&gt;&lt;/EM&gt; – Michael T. Nygard, Release It!&lt;/P&gt;
&lt;P&gt;The kind of users Michael T. Nygard are talking about is the users of a system. We also have users that uses our code, the users I’m going to focus on is the users of our code. Me and you and another developers.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”&lt;/EM&gt;&lt;/STRONG&gt; – Martin Fowler&lt;/P&gt;
&lt;P&gt;Good programmers also writes code that humans know how to use, good programmers also make sure software behave in a predictable manner despise inputs or user actions.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Design by Contract&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Design by Contract (DbC) is a way for us to make a contract between us (the code writer) and the users of our code. It’s about “If you give me this, I promise to give you this”. It’s not about business validations, that is something completely different that should be part of the domain model. DbC is to make sure the users of our code uses it in a correct way, and that we can rely on the contract and write code in a way where we know that the users will follow the contract. It will make it much easier for us to write code with a contract specified. Something like the following code is something we may see often: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; DoSomething(Object &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;)
{
   &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;.DoIKnowThatICanDoThis();
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Where “value” can be uses directly or passed to other methods and later be used. What some of us can easily forget here is that the “value” can be “null”. We will probably not passing a null value, but someone else that uses our code maybe will do it. I think most of you (including me) have passed “null” into a method because you don’t know if the argument need to be specified to a valid value etc. I bet most of you also have got the “Null reference exception”. Sometimes this “Null reference exception” can be hard and take time to fix, because we need to search among our code to see where the “null” value was passed in etc. Wouldn’t it be much better if we can as early as possible specify that the value can’t not be null, so the users of our code also know it when the users starts to use our code, and before run time execution of the code? This is where DbC comes into the picture. We can use DbC to specify what we need, and by doing so we can rely on the contract when we write our code. So the code above can actually use the DoIKnowThatICanDoThis() method on the value object without being worried that the “value” can be null. The contract between the users of the code and us writing the code, says that the “value” can’t be null.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Pre- and Postconditions&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;When working with DbC we are specifying pre- and postconditions.&amp;nbsp; &lt;A href="http://en.wikipedia.org/wiki/Precondition" mce_href="http://en.wikipedia.org/wiki/Precondition"&gt;Precondition&lt;/A&gt; is a condition that should be met before a query or command is executed. An example of a precondition is: “The Value argument of the method can’t be null”, and we make sure the “value” isn’t null before the method is called. &lt;A href="http://en.wikipedia.org/wiki/Postcondition" mce_href="http://en.wikipedia.org/wiki/Postcondition"&gt;Postcondition&lt;/A&gt; is a condition that should be met when a command or query is completed, a postcondition will make sure the result is correct. An example of a postconditon is “The method will return a list with at least 1 item”. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Commands an Quires&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;When using DbC, we need to know what a Command and a Query is, because some principles that can be good to follow are based on commands and queries. A Command is something that will not return anything, like the SQL’s CREATE, UPDATE and DELETE. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/blogs/fredriknormen/image_5777DB62.png" mce_href="http://weblogs.asp.net/blogs/fredriknormen/image_5777DB62.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://weblogs.asp.net/blogs/fredriknormen/image_thumb_2F652C43.png" width=240 height=148 mce_src="http://weblogs.asp.net/blogs/fredriknormen/image_thumb_2F652C43.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;There are two kinds of Commands when using DbC, the Creation commands (for example a Constructor), and Others. Others can for example be a Command to add a value to a list, remove or update a value etc. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=rem&gt;//Creation commands&lt;/SPAN&gt;

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; Stack(&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; size)

&lt;SPAN class=rem&gt;//Other commands&lt;/SPAN&gt;

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Push(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;);

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Remove();&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;A Query, is something that will return something, for example an Attribute, Property or a Function, like the SQL’s SELECT.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/blogs/fredriknormen/image_07527D24.png" mce_href="http://weblogs.asp.net/blogs/fredriknormen/image_07527D24.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://weblogs.asp.net/blogs/fredriknormen/image_thumb_6254BCAA.png" width=240 height=153 mce_src="http://weblogs.asp.net/blogs/fredriknormen/image_thumb_6254BCAA.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;There are two kinds of Queries, the Basic Queries&amp;nbsp; (Quires that aren’t based on another queries), and the Derived Queries, queries that is based on another queries. Here is an example of queries of a Stack: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=rem&gt;//Basic Queries&lt;/SPAN&gt;
        
&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; Count;

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; index] { get; }

&lt;SPAN class=rem&gt;//Derived Queries&lt;/SPAN&gt;

&lt;SPAN class=rem&gt;//Is related to Count Query&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;bool&lt;/SPAN&gt; IsEmpty()
{
      &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; Count == 0;
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;To understand about some principles that are good to follow when using DbC, we need to know about the Commands and different Queries.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The 6 Principles &lt;BR&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;When working with DbC, it’s advisable to follow some principles to make it easier to define and use contracts. The following DbC principles are: &lt;BR&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Separate commands and queries.&lt;/LI&gt;
&lt;LI&gt;Separate basic queries from derived queries.&lt;/LI&gt;
&lt;LI&gt;For each derived query, write a postcondition that specifies what result will be returned, in terms of one or more basic queries.&lt;/LI&gt;
&lt;LI&gt;For each command, write a postcondition that specifies the value of every basic query. &lt;/LI&gt;
&lt;LI&gt;For every query and command, decide on a suitable precondition.&lt;/LI&gt;
&lt;LI&gt;Write invariants to define unchanging properties of objects.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;BR&gt;Before I will write about each of them I want you to now that I’m going to use .Net 4.0 Code Contract. I will in the rest of the post uses a simple &lt;A href="http://en.wikipedia.org/wiki/Stack_(data_structure)" mce_href="http://en.wikipedia.org/wiki/Stack_(data_structure)"&gt;Stack&lt;/A&gt; (Yes I know, .Net already have a Stack class) to give you the basic understanding about using DbC. A Stack is a data structure where the last item in, will be the first item out. Here is a basic implementation of a Stack where not contract is specified yet: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; Stack
{
    &lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt;[] _array;

    &lt;SPAN class=rem&gt;//Basic Queries&lt;/SPAN&gt;
    
    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; u&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; Count;

    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[u&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; index]
    {
      get { &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; _array[index]; }
      set { _array[index] = &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;; }
    }

    &lt;SPAN class=rem&gt;//Derived Queries&lt;/SPAN&gt;

    &lt;SPAN class=rem&gt;//Is related to Count Query&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;bool&lt;/SPAN&gt; IsEmpty()
    {
      &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; Count == 0;
    }

    &lt;SPAN class=rem&gt;//Is related to Count and this[] Query&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; Top()
    {
      &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count];
    }

    &lt;SPAN class=rem&gt;//Creation commands&lt;/SPAN&gt;

    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; Stack(u&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; size)
    {
       Count = 0;
       _array = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt;[size];
    }

    &lt;SPAN class=rem&gt;//Other commands&lt;/SPAN&gt;

    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Push(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;)
    {
      &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[++Count] = &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;;
    }

    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Remove()
    {
      &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count] = &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;;
      Count--;
    }
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Note: The Stack is implemented in a way to demonstrate the use of Code Contract in a simple way, the implementation may not look like how you would implement it, so don’t think this is the perfect Stack implementation, only used for demonstration.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Before I will go deeper into the principles I will simply mention how we can use the .Net Code Contract.&lt;/P&gt;
&lt;P&gt;I mention before about pre- and postcondition, is about “Require” something and to “Ensure” something. When using Code Contract, we will use a static class called “Contract” and is located in he “System.Diagnostics.Contracts” namespace. The contract must be specified at the top or our member statement block. To specify a precondition with Code Contract we uses the Contract.Requires method, and to specify a postcondition, we uses the Contract.Ensure method. Here is an example where both a pre- and postcondition are used: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; Top()
{
     Contract.Requires(Count &amp;gt; 0, &lt;SPAN class=str&gt;"Stack is empty"&lt;/SPAN&gt;);
     Contract.Ensures(Contract.Result&amp;lt;&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt;&amp;gt;() == &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count]);

     &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count];
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;The contract above requires that the Count is greater than 0, if not we can’t get the item at the Top of a Stack. We also Ensures that the results (By using the Contract.Result method, we can specify a postcondition that will check if the value returned from a method is correct) of the Top query is equal to this[Count].&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;1. Separate Commands and Queries&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;When working with DbC, it’s important to &lt;A href="http://en.wikipedia.org/wiki/Command-query_separation" mce_href="http://en.wikipedia.org/wiki/Command-query_separation"&gt;separate Command and Quires&lt;/A&gt;. A method should either be a command that performs an Action, or returning information to the caller, not both. By asking a question the answer shouldn’t be changed. The following is an example of a Command and a Query of a Stack: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Push(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;)

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; Top()&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Push is a command and will not return anything, just add a value to the Stack, the Top is a query to get the item at the top of the stack.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;2. Separate basic queries from derived queries&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;There are two different kinds of queries,&amp;nbsp; the basic queries that doesn’t rely on another queries, and derived queries that uses a basic query. The “Separate basic queries from derived queries” principle is about about that derived queries can be specified in terms of basic queries. So this principles is more about recognizing that a query is a derived query or a basic query. It will then make is much easier to follow the other principles. The following code shows a basic query and a derived query: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=rem&gt;//Basic Queries&lt;/SPAN&gt;
        
&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; u&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; Count;

&lt;SPAN class=rem&gt;//Derived Queries&lt;/SPAN&gt;

&lt;SPAN class=rem&gt;//Is related to Count Query&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;bool&lt;/SPAN&gt; IsEmpty()
{
      &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; Count == 0;
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;We can see that IsEmpty will use the Count query, and that makes the IsEmpty a Derived query.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;3. For each derived query, write a postcondition that specifies what result will be returned, in terms of one or more basic queries.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;When the derived query is recognize we can follow the 3ed principle. For each derived query, we can create a postcondition that specifies what result our derived query will return in terms of one or more basic queries. Remember that DbC is about contracts between the users of the code and us writing the code. So we can’t use demand that the users will pass in a valid value, we must also ensure that we will give the users what the users wants, when the user is following our contract. The IsEmpty query of the Stack will use a Count query and that will make the IsEmpty a Derived query, so we should now write a postcondition that specified what results will be returned, in terms of using a basic query and in this case the Count query, &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=rem&gt;//Basic Queries&lt;/SPAN&gt;
        
&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; u&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; Count;

&lt;SPAN class=rem&gt;//Derived Queries&lt;/SPAN&gt;

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;bool&lt;/SPAN&gt; IsEmpty()
{
    Contract.Ensures(Contract.Result&amp;lt;&lt;SPAN class=kwrd&gt;bool&lt;/SPAN&gt;&amp;gt;() == (Count == 0));

    &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; Count == 0;
}&lt;/PRE&gt;
&lt;P&gt;The Contract.Ensures is used to create a postcondition. The above code will make sure that the results of the IsEmpty (by using the Contract.Result to get the result of the IsEmpty method) is correct, that will say that the IsEmpty will be either true or false based on Count is equal to 0 or not. The postcondition are using a basic query, so the IsEmpty is now following the 3ed principle. We also have another Derived Query, the Top query, it will also need a postcondition and it uses all basic queries. The Result of the Top method must be the same value as the this[] query returns. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=rem&gt;//Basic Queries&lt;/SPAN&gt;

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; u&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; Count;

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[u&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; index]
{
     get  { &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; _array[index]; }
     set  { _array[index] = &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;; }
}

&lt;SPAN class=rem&gt;//Derived Queries&lt;/SPAN&gt;

&lt;SPAN class=rem&gt;//Is related to Count and this[] Query&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; Top()
{
     Contract.Ensures(Contract.Result&amp;lt;&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt;&amp;gt;() == &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count]);

      &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count];
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;4. For each command, write a postcondition that specifies the value of every basic query. &lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;For each command we will create a postconditon that specifies the value of basic queries. If we look at the Stack implementation we will have three Commands, one Creation command, the Constructor, and two others commands, Push and Remove. Those commands need a postcondition and they should include basic query to follow the 4th principle.&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=rem&gt;//Creation commands&lt;/SPAN&gt;

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; Stack(u&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; size)
{&lt;BR&gt;  Contract.Ensures(Count == 0);
            
  Count = 0;
  _array = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt;[size];
}

&lt;SPAN class=rem&gt;//Other commands&lt;/SPAN&gt;

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Push(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;)
{
   Contract.Ensures(Count == Contract.OldValue&amp;lt;&lt;SPAN class=kwrd&gt;uint&lt;/SPAN&gt;&amp;gt;(Count) + 1);
   Contract.Ensures(&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count] == &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;);

   &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[++Count] = &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;;
}

&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Remove()
{
   Contract.Ensures(Count == Contract.OldValue&amp;lt;&lt;SPAN class=kwrd&gt;uint&lt;/SPAN&gt;&amp;gt;(Count) - 1);

   &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count] = &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;;

   Count--;
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;As you can see the Create command will Ensures that Count will be 0 when the Stack is created, when a Stack is created there shouldn’t be any items in the stack. The Push command will take a value and put it into the Stack, when an item is pushed into the Stack, the Count need to be increased to know the number of items added to the Stack, and we must also make sure the item is really added to the Stack. The postconditon of the Push method will make sure the that old value of the Count (by using the Contract.OldValue we can get the value a Query has before the method is called)&amp;nbsp; plus 1 will be equal to the Count query, this is the way we can ensure that the Push will increase the Count with one. We also make sure the this[] query will now contain the item we pushed into the Stack. The Remove method must make sure the Count is decreased by one when the top item is removed from the Stack. The Commands is now following the 4th principle, where each command now have a postcondition that used the value of basic queries.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Note: The principle says every basic Query, the Remove only used one Query the Count, it’s because this command can’t use the this[] query because an item is removed, so the only way to make sure an item is removed is to just use the Count query, so the Remove will still follow the principle.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;5. For every query and command, decide on a suitable precondition.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;We have now focused only on postcondition, now time for some preconditons. The 5th principle is about deciding a suitable preconditon for every query and command. If we starts to look at one of our basic queries (will not go through all Queries and commands here, just some of them) the this[] query, we can’t pass an index that is lower then 1 (.Net arrays and list are zero based, but not the stack in this blog post ;)) and the index can’t be lesser than the number of items in the stack. So here we will need a preconditon. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[&lt;SPAN class=kwrd&gt;uint&lt;/SPAN&gt; index]
{
     get
     {
         Contract.Requires(index &amp;gt;= 1);
         Contract.Requires(index &amp;lt;= Count);

          &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; _array[index];
      }
}&lt;BR&gt;&lt;BR&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;Think about the Contract as an documentation about how to use the code in a correct way, so if the contract could be specified elsewhere (not part of the method body), we could simply write “return _array[index]” and there is no need to check if index is greater or lesser than Count, because that is specified in a “contract”. The implementation of Code Contract, requires that the contract is specified in the code. As a developer I would rather have this contract elsewhere (Like &lt;A href="http://research.microsoft.com/en-us/projects/specsharp/" mce_href="http://research.microsoft.com/en-us/projects/specsharp/"&gt;Spec#&lt;/A&gt;) or implemented in a way Eiffel uses it as part of the language.&lt;/P&gt;
&lt;P&gt;Now when we have looked at one Query, we can also look at one command, the Remove command (You can see the whole implementation of the Stack at the end of this blog post, where precondition is added to more queries and commands then what I’m going to show in this section). We can only Remove an item if the Count is greater than 0. So we can write a precondition that will require that Count must be greater than 0. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Remove()
{
   Contract.Requires(Count &amp;gt; 0);
   Contract.Ensures(Count == Contract.OldValue&amp;lt;&lt;SPAN class=kwrd&gt;uint&lt;/SPAN&gt;&amp;gt;(Count) - 1);

   &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count] = &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;;
   Count--;
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;6. Write invariants to define unchanging properties of objects.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The last principle is about making sure the object are feeling great! This is done by using invariants. When using Code Contract we can specify invariants by adding a method with the attribute ContractInvariantMethod, the method must be private or public and can only contains calls to Contract.Invariant. To make sure the Stack feels great, the Stack must have 0 or more items, the Count can’t never be a negative value to make sure each command and queries can be used of the Stack. Here is our invariant for the Stack object:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;BR&gt;[ContractInvariantMethod]
&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; ObjectInvariant()
{
     Contract.Invariant(Count &amp;gt;= 0);
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Note: The ObjectInvariant method will be called every time after a Query or Commands is called.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Here is the full example using Code Contract:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; Stack
{
   &lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt;[] _array;

   &lt;SPAN class=rem&gt;//Basic Queries&lt;/SPAN&gt;

   &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;uint&lt;/SPAN&gt; Count;

   &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[&lt;SPAN class=kwrd&gt;uint&lt;/SPAN&gt; index]
   {
     get
     {
        Contract.Requires(index &amp;gt;= 1);
        Contract.Requires(index &amp;lt;= Count);&lt;BR&gt;
&lt;BR&gt;        &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; _array[index];
     }
     set
     {
        Contract.Requires(index &amp;gt;= 1);
        Contract.Requires(index &amp;lt;= Count);
                
        _array[index] = &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;;
     }
   }

   &lt;SPAN class=rem&gt;//Derived Queries&lt;/SPAN&gt;

   &lt;SPAN class=rem&gt;//Is related to Count Query&lt;/SPAN&gt;
   &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;bool&lt;/SPAN&gt; IsEmpty()
   {
      Contract.Ensures(Contract.Result&amp;lt;&lt;SPAN class=kwrd&gt;bool&lt;/SPAN&gt;&amp;gt;() == (Count == 0));

      &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; Count == 0;
   }

   &lt;SPAN class=rem&gt;//Is related to Count and this[] Query&lt;/SPAN&gt;
   &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; Top()
   {
      Contract.Requires(Count &amp;gt; 0, &lt;SPAN class=str&gt;"Stack is empty"&lt;/SPAN&gt;);
      Contract.Ensures(Contract.Result&amp;lt;&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt;&amp;gt;() == &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count]);

      &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count];
   }
        
   &lt;SPAN class=rem&gt;//Creation commands&lt;BR&gt;&lt;/SPAN&gt;
   &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; Stack(&lt;SPAN class=kwrd&gt;uint&lt;/SPAN&gt; size)
   {
      Contract.Requires(size &amp;gt; 0);
      Contract.Ensures(Count == 0);
            
      Count = 0;
      _array = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt;[size];
   }

   &lt;SPAN class=rem&gt;//Other commands&lt;/SPAN&gt;

   &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Push(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;)
   {
      Contract.Requires(&lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt; != &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;);
      Contract.Ensures(Count == Contract.OldValue&amp;lt;&lt;SPAN class=kwrd&gt;uint&lt;/SPAN&gt;&amp;gt;(Count) + 1);
      Contract.Ensures(&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count] == &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;);

      &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[++Count] = &lt;SPAN class=kwrd&gt;value&lt;/SPAN&gt;;
   }

   &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Remove()
   {
     Contract.Requires(Count &amp;gt; 0);
     Contract.Ensures(Count == Contract.OldValue&amp;lt;&lt;SPAN class=kwrd&gt;uint&lt;/SPAN&gt;&amp;gt;(Count) - 1);

     &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;[Count] = &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;;

     Count--;
  }

  [ContractInvariantMethod]
  &lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; ObjectInvariant()
  {
    Contract.Invariant(Count &amp;gt;= 0);
  }
        
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;&lt;BR&gt;Summary&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;By using Design By Contract we can make sure the users are using our code in a correct way, and we must also make sure the users will get the expected results when they uses our code. This can be done by specifying contracts. To make it easy to use Design By Contract, some principles may be good to follow like the separation of commands an queries. With .Net 4.0 we can use the Code Contract feature to specify contracts.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7518308" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="Design By Contract" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Design+By+Contract/default.aspx" /><category term="Defensive Programming" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Defensive+Programming/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/C_2300_/default.aspx" /><category term="Write code" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Write+code/default.aspx" /><category term="Code" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Code/default.aspx" /><category term="VS2010" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/VS2010/default.aspx" /></entry><entry><title>Why should I need to waste so many resources and 1-3 minutes to get started?</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/05/22/while-should-i-need-to-waste-so-many-resources-and-1-3-minutes-to-get-started.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/05/22/while-should-i-need-to-waste-so-many-resources-and-1-3-minutes-to-get-started.aspx</id><published>2010-05-22T04:42:00Z</published><updated>2010-05-22T04:42:00Z</updated><content type="html">&lt;P&gt;Every time I start my computer, I do the following:&lt;/P&gt;
&lt;P&gt;Start messenger,TweetDeck, Spotify, (most of the time a Web Browser) and then Visual Studio 2010 to do some work.&lt;/P&gt;
&lt;P&gt;4/5 are tools with a connection to the cloud (well 5/5 if the Start Page of VS2010 counts ;). So I’m actually only need access to the cloud and some tools to work with. I use Spotify to play music, music sent to me over the wire from the cloud, TweetDeck so see and send messages over the cloud, Messenger to keep contact with my friends and send messages over the cloud, a browser to get information located on the cloud.. Visual Studio 2010 to build apps for the cloud ;)&lt;/P&gt;
&lt;P&gt;I put my working project also in the cloud to get access to it from any places, I use Dropbox to store some documents, I use a NAS that is connected to the cloud to I can get access any of my files through the cloud. I’m in and on the cloud most of my time.&lt;/P&gt;
&lt;P&gt;What I need is a screen, a internet connection and a simple tool to get access to my hard drive for local storage, tools connected to the cloud. Why should I need to wait 1-3 minutes and waste about 1 gig of space just to see a boring loading screen of the OS too just use simple apps that will bring me into the cloud? Why use so many resources for such simple tasks? If I need to create a presentation, write a document etc, I can use tools located in the cloud, so what I only need is a OS that can connect me to the cloud and a “browser” to get access to the tools I need. The browser could be my OS!&lt;/P&gt;
&lt;P&gt;Look at iPhone, a very nice UI, just show me some buttons with my apps, easy to access the apps I want and easy to use. The best part, it’s in a little box and it’s quite (when no people calls me ;)). The following is a simple UI, I could live with a UI like this to just do my work:&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://cache.gawkerassets.com/assets/images/17/2010/03/sshot-2010-03-29-_02-03-09_-_1_.jpg" mce_src="http://cache.gawkerassets.com/assets/images/17/2010/03/sshot-2010-03-29-_02-03-09_-_1_.jpg"&gt;&lt;/P&gt;
&lt;P&gt;It’s simple, it’s in a little box, it gives me the basic information I need (most of the information is gathered from the cloud). I could easy press an image and open the program I want to use, and they could be located in the cloud. For example, Spotify. It’s like a browser, it only gather information from the cloud and show it on the screen. The app could easily be created so I could access it though a web browser, or as small as it’s on the iPhone (and how much resources is needed to just use Spotify on my iPhone?). &lt;BR&gt;&lt;BR&gt;When I turn my computer on every day, and look at my desktop after spending some loading times, I just feel that my computer is heavy. Something makes it heavy.. I want to see something new, I want to see a light weight OS, a simple OS that doesn’t takes to much time to load just to get started.. how long can it take for a computer so just show some images?&lt;/P&gt;
&lt;P&gt;Just a thought!&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7496454" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author></entry><entry><title>Use Extension method to write cleaner code</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/fredriknormen/archive/2010/05/09/use-extension-method-to-write-cleaner-code.aspx" /><id>http://weblogs.asp.net/fredriknormen/archive/2010/05/09/use-extension-method-to-write-cleaner-code.aspx</id><published>2010-05-09T09:09:00Z</published><updated>2010-05-09T09:09:00Z</updated><content type="html">&lt;P&gt;This blog post will show you step by step to refactoring some code to be more readable (at least what I think). &lt;A href="http://blog.lowendahl.net/" mce_href="http://blog.lowendahl.net/"&gt;Patrik Löwnedahl&lt;/A&gt; gave me some of the ideas when we where talking about making code much cleaner.&lt;/P&gt;
&lt;P&gt;The following is an simple application that will have a list of movies (Normal and Transfer). The task of the application is to calculate the total sum of each movie and also display the price of each movie. &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; Program
{    
    &lt;SPAN class=kwrd&gt;enum&lt;/SPAN&gt; MovieType
    {
        Normal,
        Transfer
    }

    &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Main(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;[] args)
    {
        var movies = GetMovies();

        &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfNormalMovie = 0;
        &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfTransferMovie = 0;

        &lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies)
        {
            &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (movie == MovieType.Normal)
           {
               totalPriceOfNormalMovie += 2;
               Console.WriteLine(&lt;SPAN class=str&gt;"$2"&lt;/SPAN&gt;);
            }
            &lt;SPAN class=kwrd&gt;else&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (movie == MovieType.Transfer)
            {
                totalPriceOfTransferMovie += 3;
                Console.WriteLine(&lt;SPAN class=str&gt;"$3"&lt;/SPAN&gt;);
             }
         }
     }

     &lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; IEnumerable&amp;lt;MovieType&amp;gt; GetMovies()
     {
         &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; List&amp;lt;MovieType&amp;gt;()
                             {
                                 MovieType.Normal,
                                 MovieType.Transfer,
                                 MovieType.Normal
                             };
     }
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;In the code above I’m using an enum, a good way to add types (isn’t it ;)). I also use one foreach loop to calculate the price, the loop has a condition statement to check what kind of movie is added to the list of movies. I want to reuse the foreach only to increase performance and let it do two things (isn’t that smart of me?! ;)).&lt;/P&gt;
&lt;P&gt;First of all I can admit, I’m not a big fan of enum. Enum often results in ugly condition statements and can be hard to maintain (if a new type is added we need to check all the code in our app to see if we use the enum somewhere else). I don’t often care about pre-optimizations when it comes to write code (of course I have performance in mind). I rather prefer to use two foreach to let them do one things instead of two. So based on what I don’t like and &lt;A href="http://martinfowler.com/" mce_href="http://martinfowler.com/"&gt;Martin Fowler’s&lt;/A&gt; &lt;A href="http://www.refactoring.com/catalog/index.html" mce_href="http://www.refactoring.com/catalog/index.html"&gt;Refactoring catalog&lt;/A&gt;, I’m going to refactoring this code to what I will call a more elegant and cleaner code.&lt;/P&gt;
&lt;P&gt;First of all I’m going to use &lt;A href="http://www.refactoring.com/catalog/splitLoop.html" mce_href="http://www.refactoring.com/catalog/splitLoop.html"&gt;Split Loop&lt;/A&gt; to make sure the foreach will do one thing not two, it will results in two foreach (Don’t care about performance here, if the results will results in bad performance, you can refactoring later, but computers are so fast to day, so iterating through a list is not often so time consuming.) &lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;&lt;EM&gt;Note: The foreach actually do four things, will come to is later.&lt;/EM&gt;&lt;/STRONG&gt; &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;var movies = GetMovies();

&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfNormalMovie = 0;
&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfTransferMovie = 0;

&lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies)
{
    &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (movie == MovieType.Normal)
    {
       totalPriceOfNormalMovie += 2;
       Console.WriteLine(&lt;SPAN class=str&gt;"$2"&lt;/SPAN&gt;);
    }
}

&lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies)
{
    &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (movie == MovieType.Transfer)
    {
        totalPriceOfTransferMovie += 3;
        Console.WriteLine(&lt;SPAN class=str&gt;"$3"&lt;/SPAN&gt;);
     }
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;To remove the condition statement we can use the Where extension method added to the IEnumerable&amp;lt;T&amp;gt; and is located in the System.Linq namespace: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies.Where( m =&amp;gt; m == MovieType.Normal))
{
    totalPriceOfNormalMovie += 2;
    Console.WriteLine(&lt;SPAN class=str&gt;"$2"&lt;/SPAN&gt;);
}

&lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies.Where( m =&amp;gt; m == MovieType.Transfer))
{
    totalPriceOfTransferMovie += 3;
    Console.WriteLine(&lt;SPAN class=str&gt;"$3"&lt;/SPAN&gt;);
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;The above code will still do two things, calculate the total price, and display the price of the movie. I will not take care of it at the moment, instead I will focus on the enum and try to remove them. One way to remove enum is by using the &lt;A href="http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html" mce_href="http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html"&gt;Replace Conditional with Polymorphism&lt;/A&gt;. So I will create two classes, one base class called Movie, and one called MovieTransfer. The Movie class will have a property called Price, the Movie will now hold the price:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; Movie
{
    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;virtual&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; Price
    {
        get { &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; 2; }
    }
}


&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; MovieTransfer : Movie
{
    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;override&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; Price
    {
       get { &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; 3; }
    }
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;The following code has no enum and will use the new Movie classes instead: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; Program
{    
   &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Main(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;[] args)
   {
       var movies = GetMovies();

       &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfNormalMovie = 0;
       &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfTransferMovie = 0;

       &lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies.Where( m =&amp;gt; !(m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; MovieTransfer)))
       {
          totalPriceOfNormalMovie += movie.Price;
          Console.WriteLine(movie.Price);
       }

       &lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies.Where( m =&amp;gt; m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; MovieTransfer))
       {
         totalPriceOfTransferMovie += movie.Price;
         Console.WriteLine(movie.Price);
       }
     }

     &lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; IEnumerable&amp;lt;Movie&amp;gt; GetMovies()
     {
         &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; List&amp;lt;Movie&amp;gt;()
                            {
                                 &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Movie(),
                                 &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; MovieTransfer(),
                                 &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Movie()
                             };
     }
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;If you take a look at the foreach now, you can see it still actually do two things, calculate the price and display the price. We can do some more refactoring here by using the Sum extension method to calculate the total price of the movies:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Main(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;[] args)
{
    var movies = GetMovies();

     &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfNormalMovie = movies.Where(m =&amp;gt; !(m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; MovieTransfer))
                                         .Sum(m =&amp;gt; m.Price);

     &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfTransferMovie = movies.Where(m =&amp;gt; m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; MovieTransfer)
                                           .Sum(m =&amp;gt; m.Price);
            
     &lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies.Where( m =&amp;gt; !(m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; MovieTransfer)))
           Console.WriteLine(movie.Price);

     &lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies.Where( m =&amp;gt; m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; MovieTransfer))
           Console.WriteLine(movie.Price);
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;Now when the Movie object will hold the price, there is no need to use two separate foreach to display the price of the movies in the list, so we can use only one instead: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies)
     Console.WriteLine(movie.Price);&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;If we want to increase the Maintainability index we can use the &lt;A href="http://www.refactoring.com/catalog/extractMethod.html" mce_href="http://www.refactoring.com/catalog/extractMethod.html"&gt;Extract Method&lt;/A&gt; to move the Sum of the prices into two separate methods. The name of the method will explain what we are doing: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Main(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;[] args)
{
    var movies = GetMovies();

    &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfMovie = TotalPriceOfMovie(movies);

    &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfTransferMovie = TotalPriceOfMovieTransfer(movies);
            
    &lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies)
        Console.WriteLine(movie.Price);

}

&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; TotalPriceOfMovieTransfer(IEnumerable&amp;lt;Movie&amp;gt; movies)
{
    &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; movies.Where(m =&amp;gt; m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; MovieTransfer)
                 .Sum(m =&amp;gt; m.Price);
}

&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; TotalPriceOfMovie(IEnumerable&amp;lt;Movie&amp;gt; movies)
{
    &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt;  movies.Where(m =&amp;gt; !(m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; MovieTransfer))
                  .Sum(m =&amp;gt; m.Price);
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;Now to the last thing, I love the ForEach method of the List&amp;lt;T&amp;gt;, but the IEnumerable&amp;lt;T&amp;gt; doesn’t have it, so I created my own ForEach extension, here is the code of the ForEach extension method: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; LoopExtensions
{
   &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; ForEach&amp;lt;T&amp;gt;(&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt; IEnumerable&amp;lt;T&amp;gt; values, Action&amp;lt;T&amp;gt; action)
   {
       Contract.Requires(values != &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;);
       Contract.Requires(action != &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;);

       &lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var v &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; values)
           action(v);
   }
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;I will now replace the foreach by using this ForEach method: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Main(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;[] args)
{
    var movies = GetMovies();

     &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfMovie = TotalPriceOfMovie(movies);

     &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfTransferMovie = TotalPriceOfMovieTransfer(movies);

     movies.ForEach(m =&amp;gt; Console.WriteLine(m.Price));
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;The ForEach on the movies will now display the price of the movie, but maybe we want to display the name of the movie etc, so we can use &lt;A href="http://www.refactoring.com/catalog/extractMethod.html" mce_href="http://www.refactoring.com/catalog/extractMethod.html"&gt;Extract Method&lt;/A&gt; by moving the lamdba expression into a method instead, and let the method explains what we are displaying:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;BR&gt;movies.ForEach(DisplayMovieInfo);


&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; DisplayMovieInfo(Movie movie)
{
     Console.WriteLine(movie.Price);
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;Now the refactoring is done, or?&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;EDIT: It's not done. &lt;A href="http://niclasnilsson.se/" mce_href="http://niclasnilsson.se/"&gt;Niclas Nilsson&lt;/A&gt; wrote a good comment that&amp;nbsp;I&amp;nbsp;actually do two things in the TotalPrice... methods, a filtering and a Sum. So both a command and a query. so it will not follow the Command and Query Principle. To make the code more seperated by concerns:&lt;BR&gt;&lt;BR&gt;&lt;/STRONG&gt;The TotalPriceXXX method are removed and replaced with on called TotalPriceOfMovie. Two properties are added, one for returning the normal movies and one for returning the transfer movies. Here is the result of the new refactoring:&lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Main(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;[] args)
{
    &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfMovie = TotalPriceOfMovie(NormalMovies);

    &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPriceOfTransferMovie = TotalPriceOfMovie(MovieTransfers);
            
    &lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (var movie &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; movies)
        Console.WriteLine(movie.Price);

}

&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; TotalPriceOfMovie(IEnumerable&amp;lt;Movie&amp;gt; movies)
{
    &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; movies.Sum(m =&amp;gt; m.Price);
}

&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;&lt;FONT color=#000000&gt;IEnumerable&amp;lt;Movie&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt; NormalMovies
{
    get { &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; AllMovies.Where(m =&amp;gt; !(m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; MovieTransfer)); }&lt;BR&gt;}&lt;/PRE&gt;&lt;PRE class=csharpcode&gt;&lt;FONT color=#000000&gt;&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;&lt;SPAN class=kwrd&gt;IEnumerable&amp;lt;Movie&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;MovieTransfers
&lt;BR&gt;{
    get { &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; AllMovies.Where(m =&amp;gt; m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; MovieTransfer); }&lt;BR&gt;}&lt;BR&gt;
&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;&lt;SPAN class=kwrd&gt;IEnumerable&amp;lt;Movie&amp;gt; &lt;/SPAN&gt;&lt;/SPAN&gt;AllMovies
&lt;BR&gt;{
    get { &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; GetAllMovies(); }&lt;BR&gt;}&lt;/FONT&gt;&lt;/PRE&gt;&lt;PRE class=csharpcode&gt;&lt;FONT color=#000000&gt;&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;static&lt;/SPAN&gt; IEnumerable&amp;lt;Movie&amp;gt; GetMovies()
{
     &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; List&amp;lt;Movie&amp;gt;()
                {
                  &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Movie(),
                  &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; MovieTransfer(),
                  &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Movie()
                };
}
&lt;/FONT&gt;&lt;/PRE&gt;&lt;PRE class=csharpcode&gt;&lt;FONT color=#000000&gt;&lt;STRONG&gt;&lt;EM&gt;Note: The GetMovies() method in this code is just a fake method to return fake data, so it could instead be a call to a Repository etc.&lt;/EM&gt;&lt;/STRONG&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;I think the new code is much cleaner than the first one, and I love the ForEach extension on the IEnumerable&amp;lt;T&amp;gt;, I can use it for different kind of things, for example: &lt;BR&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;movies.Where(m =&amp;gt; m &lt;SPAN class=kwrd&gt;is&lt;/SPAN&gt; Movie)
      .ForEach(DoSomething);&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;BR&gt;By using the Where and ForEach extension method, some if statements can be removed and will make the code much cleaner. But the beauty is in the eye of the beholder. What would you have done different, what do you think will make the first example in the blog post look much cleaner than my results, comments are welcome! &lt;BR&gt;&lt;BR&gt;If you want to know when I will publish a new blog post, you can follow me on twitter: &lt;A href="http://www.twitter.com/fredrikn" mce_href="http://www.twitter.com/fredrikn"&gt;http://www.twitter.com/fredrikn&lt;/A&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7476195" width="1" height="1"&gt;</content><author><name>Fredrik N</name><uri>http://weblogs.asp.net/members/Fredrik-N.aspx</uri></author><category term="Design By Contract" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Design+By+Contract/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/C_2300_/default.aspx" /><category term="Write code" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Write+code/default.aspx" /><category term="Design" scheme="http://weblogs.asp.net/fredriknormen/archive/tags/Design/default.aspx" /></entry></feed>
