<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Kenny Tordeur</title>
    <description></description>
    <link>http://blog.kennytordeur.be/</link>
    <atom:link href="http://blog.kennytordeur.be/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Mon, 14 Dec 2015 08:23:51 +0000</pubDate>
    <lastBuildDate>Mon, 14 Dec 2015 08:23:51 +0000</lastBuildDate>
    <generator>Jekyll v2.4.0</generator>
    
      <item>
        <title>CSRF prevention in asp.net mvc 6</title>
        <description>&lt;h2&gt;What is Cross-Site Request Forgery (CSRF)&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29&quot; title=&quot;CSRF&quot;&gt;Cross-Site Request Forgery (CSRF)&lt;/a&gt; is an attack that forces an end user to execute unwanted actions on a web application in which they&amp;#39;re currently authenticated. CSRF attacks specifically target state-changing requests, not theft of data, since the attacker has no way to see the response to the forged request. With a little help of social engineering (such as sending a link via email or chat), an attacker may trick the users of a web application into executing actions of the attacker&amp;#39;s choosing. If the victim is a normal user, a successful CSRF attack can force the user to perform state changing requests like transferring funds, changing their email address, and so forth. If the victim is an administrative account, CSRF can compromise the entire web application. (from &lt;a href=&quot;https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29&quot; title=&quot;The Open Web Application Security Project&quot;&gt;www.owasp.org&lt;/a&gt;)&lt;/p&gt;

&lt;h2&gt;How can Asp.Net MVC prevent CSRF&lt;/h2&gt;

&lt;p&gt;Luckily Asp.Net MVC has a build in mechanisme to fight &lt;a href=&quot;https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29&quot; title=&quot;CSRF&quot;&gt;CSRF&lt;/a&gt; attacks. In the previous versions of Asp.Net MVC you used an AntiForgeryToken. You needed to put a ValidateAntiForgeryToken attribute on your MVC action or controller. In the view, you needed to instruct RAZOR to create an AntiForgeryToken. This will give the visitor a cookie called __RequestVerificationToken. This cookie will have the same value as a hidden input field, called __RequestVerificationToken, generated by the Html.AntiForgeryToken() method. This method needed to be placed inside a form tag or the using of a Html.BeginForm(). &lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;   &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyController&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Controller&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;    [HttpPost]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;        [ValidateAntiForgeryToken]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IActionResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SomeModel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;   @using(Html.BeginForm())
   {
    @Html.AntiForgeryToken()
   }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;How does Asp.Net MVC 6 prevent CSRF&lt;/h2&gt;

&lt;p&gt;In the Asp.Net MVC 6 the mechanisme has changed a bit. You still need to put the ValidateAntiForgeryToken attribute on your MVC action, but if you use the build in form &lt;a href=&quot;http://docs.asp.net/projects/mvc/en/latest/views/tag-helpers/intro.html&quot; title=&quot;What are taghelpers?&quot;&gt;taghelper&lt;/a&gt;, the forgerytoken will by default be created and placed within the form tag. &lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;   &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyController&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Controller&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;    [HttpPost]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;        [ValidateAntiForgeryToken]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IActionResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SomeModel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;   &lt;span class=&quot;nt&quot;&gt;&amp;lt;form&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;asp-action=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Create&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;

   &lt;span class=&quot;nt&quot;&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will result in the following HTML.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;form&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;action=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/My/Create/&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;method=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;post&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;__RequestVerificationToken&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;hidden&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;CfDJ8DCDWZ4iOzZDmNKl5HFAUd2qQe4qwOhVP4znwDlTDINNK_h-1a2v0A1aDPCdb4lEgc9X_cTsjKkCDUCYh9EKr7HqXI3hvIRfnRItwfJwbImCZIx38uDfwVu5jzdVZOcaXUUmoPBDtG6-0__0FVezb-U&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you don&amp;#39;t want the forgerytoken to be created you can pass a false value to the form taghelper for the antiforgery parameter.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;   &lt;span class=&quot;nt&quot;&gt;&amp;lt;form&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;asp-action=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Create&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;asp-antiforgery=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;false&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;

   &lt;span class=&quot;nt&quot;&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
        <pubDate>Wed, 25 Nov 2015 13:45:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2015/11/25/csrf-prevention-in-aspnet-mvc</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2015/11/25/csrf-prevention-in-aspnet-mvc</guid>
        
        <category>asp.net</category>
        
        <category>mvc</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Taghelpers and Dependency Injection</title>
        <description>&lt;p&gt;I was wondering if you could use &lt;a href=&quot;https://en.wikipedia.org/wiki/Dependency_injection&quot; title=&quot;Dependency Injection&quot;&gt;Dependency Injection&lt;/a&gt; for &lt;a href=&quot;http://docs.asp.net/projects/mvc/en/latest/views/tag-helpers/intro.html&quot; title=&quot;What are taghelpers?&quot;&gt;taghelper&lt;/a&gt;. I&amp;#39;ll modify the example used in this my previous post &lt;a href=&quot;/post/2015/10/14/creating-a-custom-aspnet-mvc-taghelper&quot; title=&quot;Creating a custom taghelper&quot;&gt;Creating a custom taghelper&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Creating a service to inject&lt;/h2&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IPersonService&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PersonService&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IPersonService&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;PersonService.GetName()&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Register this class in the Dependency Injection system of Asp.net. Add it to services collection in the ConfigureServices method from the Startup class.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ConfigureServices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IServiceCollection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AddTransient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IPersonService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PersonService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Modifying the Asp.Net mvc taghelper&lt;/h2&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [TargetElement(&amp;quot;Person&amp;quot;)]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyTagHelper&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelper&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;IPersonService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_personService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MyTagHelper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IPersonService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;personService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;_personService&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;personService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TagHelperContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelperOutput&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SetContent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_personService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Testing&lt;/h2&gt;

&lt;p&gt;When I run the application, the DI-system will also inject dependencies for my custom taghelper.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2015-10-15-Taghelpers%20and%20DI/result.png&quot; alt=&quot;The result&quot; title=&quot;The result&quot;&gt;&lt;/p&gt;
</description>
        <pubDate>Thu, 15 Oct 2015 08:14:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2015/10/15/aspnet-mvc-taghelpers-and-dependency-injection</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2015/10/15/aspnet-mvc-taghelpers-and-dependency-injection</guid>
        
        <category>asp.net</category>
        
        <category>taghelper</category>
        
        <category>mvc</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Creating a custom taghelper</title>
        <description>&lt;h2&gt;What are taghelpers?&lt;/h2&gt;

&lt;p&gt;Basically &lt;a href=&quot;http://docs.asp.net/projects/mvc/en/latest/views/tag-helpers/intro.html&quot; title=&quot;What are taghelpers?&quot;&gt;taghelpers&lt;/a&gt; do the same thing as the &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper%28v=vs.118%29.aspx&quot; title=&quot;The HTMLhelper&quot;&gt;htmlhelpers&lt;/a&gt; we all know in Asp.net MVC. If you use a htmlhelper in your view, you&amp;#39;ll see that it is actually C# code. If you would use a taghelper, it would lean more to html than to C# code.&lt;/p&gt;

&lt;h2&gt;Defining your taghelper&lt;/h2&gt;

&lt;p&gt;Your custom taghelper will need to inherit from the TagHelper base class. This base class defines two virtual methods called Process and ProcessAsync. In these methods you&amp;#39;ll write your taghelper logic.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyTagHelper&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelper&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TagHelperContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelperOutput&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In order to use your taghelper, i&amp;#39;ll need to register it. This done in the &amp;quot;_ViewImports.cshtml&amp;quot; file that can be found in the View folder. &lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;      &lt;span class=&quot;n&quot;&gt;@addTagHelper&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;MyWebApplication.MyTagHelper, MyWebApplication&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The last part is your dll name. The first part is the name (namespace + classname) of the taghelper you want to register. If you want to register all the taghelpers from a specific dll you can use a wildcard.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;      &lt;span class=&quot;n&quot;&gt;@addTagHelper&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;*, MyWebApplication&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When you succesfully have registered your taghelper, i&amp;#39;ll get intellisense for your taghelper in your views.&lt;/p&gt;

&lt;p&gt;Next, you will need to specify a target html tag that will invoke your custom taghelper. This can be done in 2 ways:
    &lt;ul&gt;
        &lt;li&gt;by naming convention
            &lt;ul&gt;
                &lt;li&gt; If your your taghelper is called TestTagHelper, it will target all Test-tags&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul&gt;
        &lt;li&gt;using the TargetElement attribute
            &lt;ul&gt;
                &lt;li&gt;you explicitly set the target tag&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
    &lt;/ul&gt;&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [TargetElement(&amp;quot;Person&amp;quot;)]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyTagHelper&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelper&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;        
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TagHelperContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelperOutput&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Adding attributes/properties on a taghelper&lt;/h2&gt;

&lt;p&gt;I now want to pass a name and lastname to the custom taghelper. These parameters will be passed like this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;Person&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;kenny&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;lastname=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;tordeur&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/Person&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the taghelper you will need to define these attributes. This can be done using the TargetElement tag.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [TargetElement(&amp;quot;Person&amp;quot;,Attributes = &amp;quot;name,lastname&amp;quot;)]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyTagHelper&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelper&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;        
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TagHelperContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelperOutput&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In order to get the values of the attributes, you can use the TagHelperContext. This object will contain all the defined attributes. You can also create properties that will map to the attribute names.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [TargetElement(&amp;quot;Person&amp;quot;,Attributes = &amp;quot;name,lastname&amp;quot;)]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyTagHelper&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelper&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Lastname&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TagHelperContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelperOutput&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To map the attribute values to the correct properties, a naming convention is used. The attribute &amp;quot;name&amp;quot; will map to the property &amp;quot;Name&amp;quot;. So the first letter of the attribute will be transformed to an uppercased letter. If you would have an attribute &amp;quot;last-name&amp;quot;, it would be mapped to &amp;quot;LastName&amp;quot; property. So the &amp;quot;-&amp;quot; will be remove from the attribute name and the next letter will be transformed to an uppercased letter.&lt;/p&gt;

&lt;p&gt;You aren&amp;#39;t just limited by passing in simple types (string, int, ...) to the taghelper. You can also pass complex types to the taghelper.&lt;/p&gt;

&lt;h2&gt;Rendering some html&lt;/h2&gt;

&lt;p&gt;The only thing left to do is to implement an output for the taghelper. This can be done using the TagHelperOutput object. This object contains everything you need to create your custom html output. In this example i&amp;#39;ll just concatent the Name with the Lastname.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [TargetElement(&amp;quot;Person&amp;quot;,Attributes = &amp;quot;name,lastname&amp;quot;)]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyTagHelper&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelper&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Lastname&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TagHelperContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagHelperOutput&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SetContent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;{Name}, {Lastname}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;The result&lt;/h2&gt;

&lt;p&gt;I&amp;#39;ve but this in my view.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;Person&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;lastname=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Tordeur&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Kenny&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/Person&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The intellisense helped me.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2015-10-14-creating-a-custom-taghelper/intellisense.png&quot; alt=&quot;intellisense&quot; title=&quot;The result&quot;&gt;&lt;/p&gt;

&lt;p&gt;This is the output when the view is rendered in the browser.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;Person&amp;gt;&lt;/span&gt;Kenny, Tordeur&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Person&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
        <pubDate>Wed, 14 Oct 2015 08:14:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2015/10/14/creating-a-custom-aspnet-mvc-taghelper</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2015/10/14/creating-a-custom-aspnet-mvc-taghelper</guid>
        
        <category>asp.net</category>
        
        <category>mvc</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Injecting a service in a view</title>
        <description>&lt;p&gt;In the ASP.NET MVC 6, you can now use the &lt;em&gt;@inject&lt;/em&gt; keyword. This allows you to inject a service (class) into your view. This class must be public, non-nested and non-abstract.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;@inject&lt;/em&gt; needs 2 &amp;quot;parameters&amp;quot; in order to work. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@inject &lt;em&gt;[name of your class] [variable name that can be used in your view]&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;My Qoutes service&lt;/h2&gt;

&lt;p&gt;This is the class that i want to inject into my view.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Collections.Generic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;VSCode1.Models&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Quotes&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_quotes&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
            &lt;span class=&quot;s&quot;&gt;&amp;quot;Don&amp;#39;t cry because it&amp;#39;s over, smile because it happened&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&amp;quot;Be yourself; everyone else is already taken.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&amp;quot;Two things are infinite: the universe and human stupidity; and I&amp;#39;m not sure about the universe.&amp;quot;&lt;/span&gt;    
         &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetRandomQuote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;Random&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;                
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)];&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetQuotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Putting it in my view&lt;/h2&gt;

&lt;p&gt;To use the service in my view, i add the inject statement on top of the view. &amp;quot;quotes&amp;quot; can be used a variable in this view and is of type &amp;quot;VSCode1.Models.Quotes&amp;quot;.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;@inject VSCode1.Models.Quotes quotes

&lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;@quotes.GetRandomQuote()&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;

@foreach(var quote in quotes.GetQuotes())
{
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;@quote&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Registering your service in your application&lt;/h2&gt;

&lt;p&gt;If i run my application, i would get an error saying that there is no service for type VSCode1.Models.Quotes.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2015-05-22-Injecting-a-service-in-a-view/error.png&quot; alt=&quot;error&quot;&gt;&lt;/p&gt;

&lt;p&gt;When you use &lt;em&gt;@inject&lt;/em&gt;, the runtime will use dependency injection to inject the type you requested. So your type needs to be registered in the dependency injector. This needs to be done in the &lt;em&gt;startup.cs&lt;/em&gt; file.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ConfigureServices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IServiceCollection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;//other stuff ...&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AddTransient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;VSCode1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Models&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Quotes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now the View renders without error.&lt;/p&gt;
</description>
        <pubDate>Mon, 11 May 2015 08:14:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2015/05/11/injecting-a-service-in-an-aspnet-mvc-view</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2015/05/11/injecting-a-service-in-an-aspnet-mvc-view</guid>
        
        <category>ASP.NET</category>
        
        <category>MVC</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>What&#39;s new in C# 6.0: Productivity</title>
        <description>&lt;p&gt;Version 6 of C# contains a bunch of new feature that allows to speed up the productivity of the developer.&lt;/p&gt;

&lt;h2&gt;Feature 1 : static usings&lt;/h2&gt;

&lt;p&gt;The classes are structured by their usings. If you need a specific class, you&amp;#39;ll need to add a using containing the namespace of that class. If you do this, you&amp;#39;ll get access to all the classes that are defined in that using. With static usings, you can bring only a specific class into &amp;quot;scope&amp;quot; which will you access to all of it&amp;#39;s static methods.&lt;/p&gt;

&lt;p&gt;Consider following console application.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ConsoleApplication8&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Program&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Hello world&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If i use a static using for the &amp;quot;Console&amp;quot; class, i can do this.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ConsoleApplication8&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Program&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;WriteLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Hello world&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Feature 2 : ? a.k.a the elvis operator&lt;/h2&gt;

&lt;p&gt;Consider following object graph.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ObjectA&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ObjectB&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ObjectB&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ObjectB&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ObjectC&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ObjectC&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ObjectC&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you want to access the &amp;quot;Name&amp;quot; property of &amp;quot;ObjectC&amp;quot; via ObjectA, i&amp;#39;ll write code that looks like this.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SomeMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectA&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I&amp;#39;ll needs those if-statements because you don&amp;#39;t know if the child object isn&amp;#39;t null. With the elvis operator ? you can remove all the if-statements and make the code a little more readable.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SomeMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectA&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If one of the child objects is null, we&amp;#39;ll get the default value for a &amp;quot;string&amp;quot; object.&lt;/p&gt;

&lt;h2&gt;Feature 3:  Expression Bodied Members&lt;/h2&gt;

&lt;p&gt;Expression Bodies Members give you the possibility to write short methods even shorter.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ObjectC&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &amp;quot;ToString&amp;quot; method can be writing shorter using expressions.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ObjectC&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can only do this for expression on a single line. If you would write it like this, it won&amp;#39;t build and Visual Studio will give build errors.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ObjectC&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt;  
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
        <pubDate>Thu, 02 Apr 2015 08:14:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2015/04/02/what-is-new-in-csharp-6-productivity</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2015/04/02/what-is-new-in-csharp-6-productivity</guid>
        
        <category>C#</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Submitting to different controller actions in ASP.NET MVC</title>
        <description>&lt;p&gt;When you create a view in ASP.NET MVC, you&amp;#39;ll probably have 2 methods for that view in your controller. A HTTPGET method and a HTTPPOST method that will accept your form data. This POST method will do some sort of action. Now sometimes you wish, that you could put 2 submit buttons in the same form that will submit to 2 different actions on your controller.&lt;/p&gt;

&lt;h2&gt;Solution one: Javascript to the rescue&lt;/h2&gt;

&lt;p&gt;You could use javascript to modify the action of your form. You&amp;#39;ll add a click event on the submit button and overwrite the default submit action.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;form&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;myform&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;submit&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;submit1&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;submit&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;submit2&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;#submit1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;myform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;“&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;http&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//localhost/controller/action1”;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;#submit2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;myform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;“&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;http&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//localhost/controller/action2”; &lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Solution two: Creating a custom ActionNameSelector attribute&lt;/h2&gt;

&lt;p&gt;You can create a custom ActionNameSelector. This will select the correct action on your controller based on the value of your submit button. You can find a detailed blog &lt;a href=&quot;http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx&quot;&gt;post&lt;/a&gt; by Maarten Balliauw.&lt;/p&gt;

&lt;h2&gt;Solution three: HTML 5 to the rescue&lt;/h2&gt;

&lt;p&gt;You can add a formaction action attribute on your submit button. This form attribute specifies where the form data needs to be sended when you click on the submit button.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;form&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;myform&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;submit&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;formaction=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/controller/action1&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;submit&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;formaction=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/controller/action2&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The downside of this solution, is that it isn&amp;#39;t supported by all browsers.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2015-03-09-submitting-to-different-controller-actions-in-aspnet-mvc/browsers.png&quot; alt=&quot;browsers supported&quot;&gt;&lt;/p&gt;
</description>
        <pubDate>Mon, 09 Mar 2015 08:14:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2015/03/09/submitting-to-different-controller-actions-in-aspnet-mvc</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2015/03/09/submitting-to-different-controller-actions-in-aspnet-mvc</guid>
        
        <category>ASP.NET</category>
        
        <category>MVC</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>ASP.NET VNext: Routing constraints and custom constraints</title>
        <description>&lt;p&gt;In ASP.NET VNext the routing system has been unified and it is build as a middleware component that easily can be plugged in. This routing system can be found in the “Microsoft.AspNet.Routing” Nuget Package or add &lt;a href=&quot;https://github.com/aspnet/Routing&quot;&gt;github&lt;/a&gt; if you want to work with the source code.&lt;/p&gt;

&lt;h2&gt;Constraints&lt;/h2&gt;

&lt;p&gt;Here is an example how we putted constraints on our route parameters in MVC 5. In this case the parameter “productId” must match an integer.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MapRoute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;s&quot;&gt;&amp;quot;Product/{productId}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;defaults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controller&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Details&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;constraints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;productId&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;@&amp;quot;\d+&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;ASP.NET VNext introduced a new syntax for defining constraints:&lt;/p&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: #c0504d; font-size: medium&quot;&gt;“{parameter:constraint}”&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: #c0504d; font-size: 12pt&quot;&gt;parameter &lt;/span&gt;will contain the name of the parameter you want to constraint and &lt;span style=&quot;color: #c0504d; font-size: 12pt&quot;&gt;constraint &lt;/span&gt;will specify with constraint will be applied.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2014-07-11-aspnet-vnext-routing-constraints-and-custom-constraints/constraints.png&quot; alt=&quot;constraints&quot;&gt;&lt;/p&gt;

&lt;p&gt;For a full overview of the available constraints you can take a look at &lt;a href=&quot;htpps://github.com/aspnet/Routing/tree/dev/src/Microsoft.AspNet.Routing/Constraints&quot;&gt;Microsoft.AspNet.Routing/Constraints&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So the previous constraint will look like this in ASP.NET VNext.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MapRoute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;s&quot;&gt;&amp;quot;Product/{productId:int}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controller&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Details&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You also can make the parameter “nullable” using &lt;span style=&quot;color: #c0504d&quot;&gt;“{parameter:constraint?}” &lt;/span&gt;syntax.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MapRoute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;s&quot;&gt;&amp;quot;Product/{productId:int?}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controller&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Details&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or pass in a default value &lt;span style=&quot;color: #c0504d&quot;&gt;“{parameter:constraint=value}”.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MapRoute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;s&quot;&gt;&amp;quot;Product/{productId:int=1}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controller&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Details&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Adding your custom constraints&lt;/h2&gt;

&lt;p&gt;If the buildin constraints don’t meet your needs, you can also create a custom constraint based on the IRouteConstraint interface.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AreYouLuckyRouteConstraint&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IRouteConstraint&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Match&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HttpContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;httpContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IRouter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;routeKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RouteDirection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;routeDirection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TryGetValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;routeKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;valueString&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CultureInfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InvariantCulture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isInt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TryParse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valueString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NumberStyles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CultureInfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InvariantCulture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;


            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;Random&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
               &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This route constraint will test how lucky you are and will only pass when you passed a correct integer value between the number 0 and 100. Now we need to add our custom constraint to the application route options in the Startup class.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;routeOptions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ApplicationServices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IOptionsAccessor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RouteOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;routeOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstraintMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;areyoulucky&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AreYouLuckyRouteConstraint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now you can use your custom constraint.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MapRoute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;s&quot;&gt;&amp;quot;Product/{productId:areyoulucky}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controller&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Details&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
        <pubDate>Fri, 11 Jul 2014 13:37:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2014/07/11/aspnet-vnext-routing-constraints-and-custom-constraints</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2014/07/11/aspnet-vnext-routing-constraints-and-custom-constraints</guid>
        
        <category>ASP.NET</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>ASP.NET vNext: Managing dependencies using the project.json file</title>
        <description>&lt;p&gt;In ASP.NET vNext your dependencies, configurations aren&amp;#39;t configured in the .csproj file like before. They added a project.json file where all this is done. This file is a simple JSON file that is much more readable than the .csproj file.&lt;/p&gt;

&lt;h2&gt;Adding Dependencies&lt;/h2&gt;

&lt;p&gt;Dependencies are defined using a name and version.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2014-07-02-aspnet-vnext-managing-dependencies-using-the-projectjson/image_5.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;p&gt;And Visual Studio provides full intellisense to add dependencies.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2014-07-02-aspnet-vnext-managing-dependencies-using-the-projectjson/image_6.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2014-07-02-aspnet-vnext-managing-dependencies-using-the-projectjson/image_7.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;p&gt;You can use wildcard to get latest version of a specific set.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2014-07-02-aspnet-vnext-managing-dependencies-using-the-projectjson/image_8.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;p&gt;If you always want the latest version, you&amp;#39;ll need to specify an empty string.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2014-07-02-aspnet-vnext-managing-dependencies-using-the-projectjson/image_9.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;h2&gt;Resolving dependencies in runtime&lt;/h2&gt;

&lt;p&gt;When the runtime need to resolve a dependency, there is a chain of loaders the runtime uses to decide what it needs to load. Their will be a loader that will look for a Nuget Package, another will look for a project and another will look for assemblies on the disk. The main goal was to make Nuget packages the primary goal of reference. If you add Nuget Packages as a reference, ASP.NET will fetch those packages when you first deploy and run your application.&lt;/p&gt;
</description>
        <pubDate>Wed, 02 Jul 2014 08:26:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2014/07/02/aspnet-vnext-managing-dependencies-using-the-projectjson</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2014/07/02/aspnet-vnext-managing-dependencies-using-the-projectjson</guid>
        
        <category>ASP.NET</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Asp.net MVC: The correct way to render a partialview</title>
        <description>&lt;p&gt;When you create an Asp.net mvc application will create partial views in order to use them as &amp;quot;user controls&amp;quot; throughout your application. But you have to use them with caution.&lt;/p&gt;

&lt;p&gt;These are my viewmodels.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ParentViewModel&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Description&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ChildViewModel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Child&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ParentViewModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;Child&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ChildViewModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ChildViewModel&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Description&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The ChildViewModel will be used on other different views, so I&amp;#39;ll create a partial view for it. So my index.cshtml will look like this.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2014-01-24-aspnet-mvc-the-correct-way-to-render-a-partialview/image.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;p&gt;But when I submit my form, I won&amp;#39;t get a Description for my ChildViewModel on the controller site. This is because the partial view for my ChildViewModel doesn&amp;#39;t render like it should. The generate name for the Description field is wrong.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2014-01-24-aspnet-mvc-the-correct-way-to-render-a-partialview/image_1.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;p&gt;The &amp;quot;Child&amp;quot; prefix isn&amp;#39;t added to the Description field so the modelbinder doesn&amp;#39;t know that this is a property of the ChildViewModel.
Now when I convert my partial view to an editor template and use the EditorFor method the prefix will be added automatically.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2014-01-24-aspnet-mvc-the-correct-way-to-render-a-partialview/image_2.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;p&gt;The EditorFor method will actually add a &amp;quot;TemplateInfo&amp;quot; key to the ViewData of the editor template. This key will contain an instance of System.Web.Mvc.TemplateInfo. This object contains a HtmlFieldPrefix property that will be used by the TextBoxFor methode in order to generate the correct name.&lt;/p&gt;

&lt;p&gt;So if we want to use the Partial method we&amp;#39;ll need to pass a ViewDataDictionary with a TemplateInfo key that will contain the correct HtmlFieldPrefix.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2014-01-24-aspnet-mvc-the-correct-way-to-render-a-partialview/image_4.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;If your partialview needs to post data to the controller, I would suggest to transform your partialview to an editor template and use the EditorFor method. This will generate the correct HtmlFieldPrefix for you. But if you need to show a list and you need unique names. I would iterate the list and pass in a custom TemplateInfo to the Partail method for each item in the list. If you only need to show data you can use partial method.&lt;/p&gt;
</description>
        <pubDate>Fri, 24 Jan 2014 13:51:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2014/01/24/aspnet-mvc-the-correct-way-to-render-a-partialview</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2014/01/24/aspnet-mvc-the-correct-way-to-render-a-partialview</guid>
        
        <category>ASP.NET</category>
        
        <category>MVC</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Navigation in a Single page Web application: Sammy.js</title>
        <description>&lt;p&gt;Single page Web applications are becoming more and more frequent. There are a lot of frameworks/technologies that make the development of these applications a lot easier. You can use JQuery to manipulate your page, use Knockout.js to work the ViewModel way and use WebApi to get your data. All these tools make a fine toolbox to create responsive, single page web applications.&lt;/p&gt;

&lt;p&gt;But how about the navigation in these applications? How can this be done in a pluggable, centralized way? You can easily create a JavaScript function on your page that will accept an id of a view that you want to show. This function will then hide all the others views, maybe using JQuery and some sort of animation, and then make the view you requested visible.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;#&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript:navigate(&amp;#39;page_1&amp;#39;)&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Show Page 1&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;#&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript:navigate(&amp;#39;page_2&amp;#39;)&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Show Page 2&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;page&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Div1&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;display: none&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Page 1&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet, eos augue fierent id. Quo nonumes blandit ei, ut sensibus contentiones per.
        Nam te aliquam salutandi, sit at congue pertinax adversarium, eam putent verear laboramus ad. Ut viderer 
        legimus singulis duo, mel propriae volutpat ad. Mea suscipit gloriatur id, vel cu harum scaevola appetere.
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;page&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Div2&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;display: none&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Page 2&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet, eos augue fierent id. Quo nonumes blandit ei, ut sensibus contentiones per. 
        Nam te aliquam salutandi, sit at congue pertinax adversarium, eam putent verear laboramus ad. Ut viderer 
        legimus singulis duo, mel propriae volutpat ad. Mea suscipit gloriatur id, vel cu harum scaevola appetere.
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;navigate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;viewId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;.page&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hide&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

            &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;#&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;viewId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;slow&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now you have a function that is centralized and that actually is responsibly for showing the correct page (view). It acts like an ViewController. I&amp;#39;m not saying that this is a bad way of working, but there is one problem with this solution. The state of witch page was active isn&amp;rsquo;t maintained. This means that you can&amp;#39;t bookmark a specific page or send it to an external user. This is where Sammy.js comes to the rescue.&lt;/p&gt;

&lt;h2&gt;Sammy.js&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://sammyjs.org/intro&quot;&gt;Sammy.js&lt;/a&gt; is a small JavaScript framework that enables you to create routes in your page ( looks a lot like routes in MVC ). You can install it using Nuget.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;#page_1&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Show Page 1&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;#page_2&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Show Page 2&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;page&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;page_1&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;display: none&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Page 1&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet, eos augue fierent id. Quo nonumes blandit ei, ut sensibus contentiones per.
        Nam te aliquam salutandi, sit at congue pertinax adversarium, eam putent verear laboramus ad. Ut viderer 
        legimus singulis duo, mel propriae volutpat ad. Mea suscipit gloriatur id, vel cu harum scaevola appetere.
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;page&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;page_2&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;display: none&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Page 2&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        Lorem ipsum dolor sit amet, eos augue fierent id. Quo nonumes blandit ei, ut sensibus contentiones per. 
        Nam te aliquam salutandi, sit at congue pertinax adversarium, eam putent verear laboramus ad. Ut viderer 
        legimus singulis duo, mel propriae volutpat ad. Mea suscipit gloriatur id, vel cu harum scaevola appetere.
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;Sammy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;#:page&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pageId&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;nx&quot;&gt;navigate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pageId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

        &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;navigate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;viewId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;.page&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hide&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;#&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;viewId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;slow&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With Sammy.js, i created a route &amp;quot;#:page&amp;quot; where &amp;quot;:page&amp;quot; is actually a parameter. In the links, i removed the onclick attribute and added some href information. When you now click on the link, this information will be added to the url. Sammy.js will see this, and will try to match this to its predefined routes. It will then resolve the parameter from the url and pass it to the navigate function.&lt;/p&gt;

&lt;p&gt;The end result is still the same but now the &amp;quot;state&amp;quot;; is added to the url so you can bookmark it or send it as a link.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2013-01-08-navigation-in-a-single-page-web-application-sammyjs/image.png&quot; alt=&quot;result&quot;&gt;&lt;/p&gt;

&lt;p&gt;The solution can be downloaded &lt;a href=&quot;http://dl.dropbox.com/u/41091233/Blog/Navigation%20Single%20Page%20WebApp%20Sammy/Navigation%20Single%20Page%20WebApp%20Sammy.rar&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Tue, 08 Jan 2013 12:15:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2013/01/08/navigation-in-a-single-page-web-application-sammyjs</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2013/01/08/navigation-in-a-single-page-web-application-sammyjs</guid>
        
        <category>Javascript</category>
        
        <category>Sammy.js</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Asp.net MVC MEF: Per Request Lifetime</title>
        <description>&lt;p&gt;Like the title says, i want to use MEF in an Asp.Net MVC application and the lifetime of the object has to be exactly the same as the lifetime of the request. In short, i want 1 single unique object that is recreated per request.&lt;/p&gt;

&lt;p&gt;In MEF there is a way to control the lifetime of the created object. This is specified the PartCreationPolicy attribute that you can use to decorate the object. In my test project i installed the nuget package i created in this &lt;a href=&quot;http://blog.kennytordeur.be/post/2012/11/28/nuget-package-integrated-mef-in-aspnet-mvc-4-and-webapi/&quot;&gt;post&lt;/a&gt; to incorporate MEF in an Asp.net MVC application. I also created an object called MyCustomObject with an interface IMyCustomObject. This object has only one property called Created that contains the date when the object is created. In the constructor of that object i have but an thread.sleep. This way i am sure that the Created property will be different for every created object.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IMyCustomObject&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Created&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;  [Export(typeof(IMyCustomObject))]&lt;/span&gt;    
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyCustomObject&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IMyCustomObject&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Created&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

       &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MyCustomObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;n&quot;&gt;Created&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
           &lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Threading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I also created an object called MyCustomObjectContainer. This object will contain a property of the type IMyCustomObject.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt; [Export]&lt;/span&gt;   
 &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyCustomObjectContainer&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;     [Import]&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IMyCustomObject&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyCustomObject&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The controller will have 2 properties. The first of type IMyCustomObject aand the other of type MyCustomContainer. In the action method of the controller, i will write the Created value that is found in those 2 properties to the viewbag. That way i will be sure if the application is using the same object.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;  [Export]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;  [PartCreationPolicy( CreationPolicy.NonShared)]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HomeController&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Controller&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;      [Import]&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IMyCustomObject&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyCustomObject&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;      [Import]&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyCustomObjectContainer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyCustomObjectContainer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ActionResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ViewBag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyCustomtObjectDateCreated&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyCustomObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Created&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ViewBag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyCustomObjectContainerDateCreated&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyCustomObjectContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyCustomObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Created&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;       
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Choosing the right PartCreationPolicy&lt;/h2&gt;

&lt;p&gt;In MEF there are 3 different type of creation policies that we can choose. More information can be found &lt;a href=&quot;http://mef.codeplex.com/wikipage?title=Parts%20Lifetime&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shared: the part author is telling MEF that at most one instance of the part may exist per container.&lt;/li&gt;
&lt;li&gt;NonShared: the part author is telling MEF that each request for exports of the part will be served by a new instance of it.&lt;/li&gt;
&lt;li&gt;Any or not supplied value: the part author allows the part to be used as either &amp;quot;Shared&amp;quot; or &amp;quot;NonShared&amp;quot;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;The Shared Creation policy&lt;/h2&gt;&lt;/h3&gt;

&lt;p&gt;We will set Creation policy to Shared and run the application.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2012-12-21-aspnet-mvc-mef-per-request-lifetime/image1.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;p&gt;We see that the same instance of the MyCustomOject is used. But when we refresh the page, we will get exactly the same value, over and over again. So this is not the behavior that we want.&lt;/p&gt;

&lt;h3&gt;The NonShared Creation Policy&lt;/h3&gt;

&lt;p&gt;We will set the creation policy to NonShared.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2012-12-21-aspnet-mvc-mef-per-request-lifetime/image2.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;p&gt;With every refresh, we will get a different value for the Created property, but everytime that an IMyCustomObject is asked form the MEF container, a new value is created so this is not a solution for my problem.&lt;/p&gt;

&lt;h2&gt;The solution&lt;/h2&gt;

&lt;p&gt;We can create a solution for this problem using the Proxy-pattern, HttpContext and ExportFactory&lt;T&gt;.&lt;/p&gt;

&lt;p&gt;The ExportFactory&lt;T&gt; class was introduced in MEF 2. It allows us to create new instances and control the lifetime of those instances. This will be perfect to create the MyCustomObject object since we need to control the lifetime of this object.&lt;/p&gt;

&lt;p&gt;The HttpContext contains Http information about an HttpRequest. There is one property that is very interesting in our case, the Items property. This property is in fact a dictionary where you can insert objects into. This collection is cleared with every request (perfect). We will use this dictionary to add the created MyCustomObject. So every time that this object is requested, we will look in this dictionary. When it is not found, the ExportFactory&lt;T&gt; will create it and add it to the dictionary.&lt;/p&gt;

&lt;p&gt;The proxy pattern will be used to make this a transparent as possible.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2012-12-21-aspnet-mvc-mef-per-request-lifetime/image3.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;h3&gt;The Implementation&lt;/h3&gt;

&lt;p&gt;I&amp;#39;ll create an IItemContainer that has one property Container of type IDictionary. Then i&amp;#39;ll create an HttpContextContainer that will implement this interface. This object will map the Container property to the HttpContext.Items property.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IItemContainer&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IDictionary&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;[Export(typeof(IItemContainer))]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;[PartCreationPolicy(CreationPolicy.Shared)]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HttpContextContainer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MEF_Asp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Net_MVC_Per_Request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Models&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IItemContainer&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDictionary&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Container&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HttpContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;       
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next I&amp;#39;ll create the proxy object (MyCustomObjectPerRequest). This object will contain the IItemContainer and the Exportfactory&lt;T&gt; instances and implement the IMyCustomObject. This object will be served by the MefContainer when something is request of type IMyCustomObject.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [Export(typeof(IMyCustomObject))]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyCustomObjectPerRequest&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IMyCustomObject&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;        [Import]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ExportFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MyCustomObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Factory&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;        [Import]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IItemContainer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ItemContainer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IMyCustomObject&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyCustomObject&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ItemContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;MyCustomObject&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;ItemContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;MyCustomObject&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Factory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CreateExport&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IMyCustomObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ItemContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;MyCustomObject&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Created&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyCustomObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Created&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;MyCustomObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Created&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We also need to change the ExportAttribute on the MyCustomObject class so that it doesn&amp;#39;t specify a ContractType.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [Export]&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyCustomObject&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IMyCustomObject&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;The Result&lt;/h3&gt;

&lt;p&gt;When we know run the application, we will use the same MyCustomObject object per request. When we refresh, we&amp;#39;ll get an other instance.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2012-12-21-aspnet-mvc-mef-per-request-lifetime/image4.png&quot; alt=&quot;image&quot;&gt;&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This solution can also be used to use the same instance per Session or to incorporate a caching mechanism.&amp;nbsp; All that needs to been done is to implement the IItemsContainer and map the Container property. The Visual Studio solution can be downloaded &lt;a href=&quot;http://dl.dropbox.com/u/41091233/Blog/MEF%20Asp.Net%20MVC%20Per%20Request/MEF%20Asp.Net%20MVC%20Per%20Request.rar&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Fri, 21 Dec 2012 07:52:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2012/12/21/aspnet-mvc-mef-per-request-lifetime</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2012/12/21/aspnet-mvc-mef-per-request-lifetime</guid>
        
        <category>MEF</category>
        
        <category>MVC 4</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Asp.net MVC: reuse Areas from external project</title>
        <description>&lt;!-- more --&gt;

&lt;p&gt;I created a MVC project that has an Area defined that i would like to use in an other MVC project. Here is the structure.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.ggpht.com/-1nXZfWz0K2A/UM3p80s6egI/AAAAAAAAAIA/1YBv48ELV24/s1600-h/image%25255B37%25255D.png&quot;&gt;&lt;img style=&quot;display: inline; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh5.ggpht.com/-B9ec-BQT0j8/UM3p9uY9jAI/AAAAAAAAAIE/ZDP0NE7wxUk/image_thumb%25255B21%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;327&quot; height=&quot;683&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I created a reference between the AlphaArea and MainApp project. When i run the application is see that the area registration of the AlphaArea is executed. But when i try to go to the area i get an error.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh4.ggpht.com/-sF8-sqivfoI/UM3p-Rv1aTI/AAAAAAAAAIM/6zv7yiTaKL4/s1600-h/image%25255B11%25255D.png&quot;&gt;&lt;img style=&quot;float: none; margin-left: auto; display: block; margin-right: auto; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh5.ggpht.com/-QGz2ETTp5S4/UM3p_NVjXcI/AAAAAAAAAIU/EX-2P4Xp7t8/image_thumb%25255B7%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;1119&quot; height=&quot;302&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What this error says, is that the view for the Index of the AlphaController in the Alpha area isn&amp;rsquo;t found. This makes perfect sense because the Index view isn&amp;rsquo;t declared in the MainApp project and this is where MVC&amp;nbsp; tries to find the view. We need to make sure that the view engine can resolve the view.&lt;/p&gt;

&lt;p&gt;There are a couple solutions to resolve this. We could copy or create a link in the MainApp to the index view that is defined in the AlphaArea. We would need to copy the whole Area structure. Don&amp;rsquo;t know about you but I&#39;m not really found of that solution. An other solution is to compile the AlphaArea views in the DLL. This can be done by a Visual Studio extension and a Nuget package.&lt;/p&gt;

&lt;p&gt;First you need to install the Razor Generator extension for Visual Studio.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh4.ggpht.com/-h0KA5qWiacs/UM3p_1e3ZzI/AAAAAAAAAIc/4vnazwoF7_Y/s1600-h/image%25255B16%25255D.png&quot;&gt;&lt;img style=&quot;float: none; margin-left: auto; display: block; margin-right: auto; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh5.ggpht.com/-NKW_3WIn5Z4/UM3qASbq2iI/AAAAAAAAAIk/YVxXPMLgpGA/image_thumb%25255B10%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;648&quot; height=&quot;270&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This extension will create a .cs file for a view, but in the properties of that view, the Custom Tool needs to be set to &amp;ldquo;RazorGenerator&amp;rdquo;.&amp;nbsp; Otherwise the VS extension will not create a .cs file for that view.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh6.ggpht.com/-xG_eQDy_b0I/UM3qBY5v3CI/AAAAAAAAAIw/EdkLNoArvGc/s1600-h/image%25255B20%25255D.png&quot;&gt;&lt;img style=&quot;display: inline; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh5.ggpht.com/-fxc-c1AuvPM/UM3qCGRT9zI/AAAAAAAAAI4/YrfHTbz5E4k/image_thumb%25255B12%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;383&quot; height=&quot;233&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;a href=&quot;http://lh5.ggpht.com/-MfWFmj3uYv4/UM3qCwwMhOI/AAAAAAAAAI8/lKhAmUFclpg/s1600-h/image%25255B24%25255D.png&quot;&gt;&lt;img style=&quot;display: inline; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh5.ggpht.com/-iSSP9EcfDSM/UM3qDlPxj2I/AAAAAAAAAJE/y_4Da-fIAwM/image_thumb%25255B14%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;311&quot; height=&quot;274&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we compile the project, the view will be added to the DLL of the AlphaArea project. This isn&amp;rsquo;t enough. Now we need a way to tell the view engine that the view can be found in the DLL. Luckily, there is a Nuget package that does the trick. You simply need to install the package &amp;ldquo;RazorGenerator.Mvc&amp;rdquo; in the AlphaArea project.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh5.ggpht.com/-XNY-msYresI/UM3qEkpBwqI/AAAAAAAAAJQ/UaX_bKtF-uA/s1600-h/image%25255B29%25255D.png&quot;&gt;&lt;img style=&quot;display: inline; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh3.ggpht.com/-r1flSGRCPoY/UM3qRjf_NwI/AAAAAAAAAJg/rul0hItf4SA/image_thumb%25255B17%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;659&quot; height=&quot;258&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This package will add a file (RazorGeneratorMvcStart) in the AppStart folder. This file will create a PrecompiledMvcEngine and add it to the ViewEngines. The code will be executed when the applications starts. This is done by using the WebActivator.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;using &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;System.Web;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;using &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;System.Web.Mvc;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;using &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;System.Web.WebPages;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;using &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;RazorGenerator.Mvc;&lt;br /&gt;&lt;br /&gt;[&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;assembly&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;: WebActivator.&lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;PostApplicationStartMethod&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;(AlphaArea.App_Start.&lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;RazorGeneratorMvcStart&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Start&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;)]&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;namespace &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;AlphaArea.App_Start {&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public static class &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;RazorGeneratorMvcStart &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public static void &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;Start() {&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;var &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;engine = &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;PrecompiledMvcEngine&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;RazorGeneratorMvcStart&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;).Assembly) {&lt;br /&gt;                UsePhysicalViewsIfNewer = &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;HttpContext&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;.Current.Request.IsLocal&lt;br /&gt;            };&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;ViewEngines&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;.Engines.Insert(0, engine);&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// StartPage lookups are done by WebPages. &lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;VirtualPathFactoryManager&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;.RegisterVirtualPathFactory(engine);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;When we now run the application and go to the Alpha area, the view is resolved.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh6.ggpht.com/-_3b2QYwwlTk/UM3qSBhMM0I/AAAAAAAAAJk/TOmJ3NCtsqU/s1600-h/image%25255B33%25255D.png&quot;&gt;&lt;img style=&quot;display: inline; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh3.ggpht.com/-WdwDCtwG0Mc/UM3qS_vCJJI/AAAAAAAAAJs/I_N0jwbUxZU/image_thumb%25255B19%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;1018&quot; height=&quot;84&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;The same system can also be used on Controllers in external projects. The example for the post can be downloaded &lt;a href=&quot;http://dl.dropbox.com/u/41091233/Blog/Asp.net%20MVC%204%20Reuse%20Area/Asp.net%20MVC%204%20Reuse%20Area.rar&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Sun, 16 Dec 2012 14:35:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2012/12/16/aspnet-mvc-reuse-areas-from-external-project</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2012/12/16/aspnet-mvc-reuse-areas-from-external-project</guid>
        
        <category>ASP.NET</category>
        
        <category>MVC 4</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Web Api: Passing a Complex type in JSON</title>
        <description>&lt;!-- more --&gt;

&lt;p&gt;In one project i wanted to pass a complex type to the Post method of an Web Api controller. The Post itself will be done using JQuery in the JSON format. I noticed that the parameter was always null. After a bit of searching i found the solution.&lt;/p&gt;

&lt;h1&gt;The Set Up&lt;/h1&gt;

&lt;h2&gt;Server side&lt;/h2&gt;

&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;ValuesController &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;ApiController&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// POST api/values&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public void &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;Post(&lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;MyComplexType &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;value)&lt;br /&gt;        {&lt;br /&gt;        }       &lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;MyComplexType&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public string &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;Name { &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;; }&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;MyComplexSubType &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;MyComplexSubType { &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;MyComplexSubType&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public int &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;Age { &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;; }&lt;br /&gt;    }&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Client Side&lt;/h2&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;script &lt;/span&gt;&lt;span style=&quot;background: white; color: red;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;=&quot;text/javascript&quot;&amp;gt;&lt;br /&gt;&lt;br /&gt;        function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;MyComplexSubType()&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;var &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self = &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;;&lt;br /&gt;&lt;br /&gt;            self.Age = 26;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;MyComplexType()&lt;br /&gt;        {       &lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;var &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self = &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;;&lt;br /&gt;&lt;br /&gt;            self.Name = &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Kenny Tordeur&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;;&lt;br /&gt;            self.MyComplexSubType = &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;MyComplexSubType();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;PostComplexType()&lt;br /&gt;        {&lt;br /&gt;            $.ajax(&lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;/api/values&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, {&lt;br /&gt;                data: JSON.stringify(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;MyComplexType()),&lt;br /&gt;            contentType: &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&#39;application/json&#39;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;,&lt;br /&gt;            type: &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&#39;POST&#39;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;,&lt;br /&gt;            success: SendComplexTypeCallBack&lt;br /&gt;        });&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;SendComplexTypeCallBack(data)&lt;br /&gt;        {&lt;br /&gt;            alert(&lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Complex type sended&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;);&lt;br /&gt;        }&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;The PostComplexType will be trigged by a click event on a button. It will create a javascript object called MyComplexType. This object will map directly to the object we created on the server side.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh5.ggpht.com/-_r5UP0x3fJk/UMj2OLK48uI/AAAAAAAAAHk/s3PQtIfmkro/s1600-h/image%25255B13%25255D.png&quot;&gt;&lt;img style=&quot;float: none; margin-left: auto; display: block; margin-right: auto; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh5.ggpht.com/-g9eHe8J1W-o/UMj2O7ChvdI/AAAAAAAAAHo/_uREauIfrQw/image_thumb%25255B9%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;1121&quot; height=&quot;442&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h1&gt;The missing link&lt;/h1&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;With this set-up the parameter will always be null. We need to add a line of code to the global.asax.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;ValueProviderFactories&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;.Factories.Add(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;JsonValueProviderFactory&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;());&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Value providers are used by the model binding system in MVC to populate the values of model objects. We basically add a provider that enables to bind a model to a JSON model.&amp;nbsp; The solution can be downloaded &lt;a href=&quot;http://dl.dropbox.com/u/41091233/Blog/WebApi%20Complex%20Parameter/WebApi%20Complex%20Parameter.rar&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Wed, 12 Dec 2012 20:25:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2012/12/12/web-api-passing-a-complex-type-in-json</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2012/12/12/web-api-passing-a-complex-type-in-json</guid>
        
        <category>JQuery</category>
        
        <category>JSON</category>
        
        <category>MVC 4</category>
        
        <category>WebApi</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>WebApi: File upload and download using JQuery and submit button</title>
        <description>&lt;!-- more --&gt;

&lt;p&gt;I am going to create a WebApi service called FileService that can be used to upload and download files. The service will only contain 2 methods. A Post to upload and a Get method that accepts an id parameter to identify the file that needs to be downloaded. Let&amp;rsquo;s start with the Post method.&lt;/p&gt;

&lt;h1&gt;The Post method&lt;/h1&gt;

&lt;p&gt;This method will look in the Request object to see if there are any posted files. If so, it will loop over the files and create them on the server side. The server will return a 201 HttpStatus and a list of strings that will contain the full path of the file(s) at server side. When there are no files posted, the service will return a 401 status a.k.a. BadRequest.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;HttpResponseMessage &lt;/span&gt;Post()&lt;br /&gt;       {&lt;br /&gt;           &lt;span style=&quot;color: #2b91af;&quot;&gt;HttpResponseMessage &lt;/span&gt;result = &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;;&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;httpRequest = &lt;span style=&quot;color: #2b91af;&quot;&gt;HttpContext&lt;/span&gt;.Current.Request;&lt;br /&gt;&lt;br /&gt;           &lt;span style=&quot;color: green;&quot;&gt;// Check if files are available&lt;br /&gt;           &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;if &lt;/span&gt;(httpRequest.Files.Count &amp;gt; 0)&lt;br /&gt;           {&lt;br /&gt;               &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;files = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;               &lt;span style=&quot;color: green;&quot;&gt;// interate the files and save on the server&lt;br /&gt;               &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: blue;&quot;&gt;string &lt;/span&gt;file &lt;span style=&quot;color: blue;&quot;&gt;in &lt;/span&gt;httpRequest.Files)&lt;br /&gt;               {&lt;br /&gt;                   &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;postedFile = httpRequest.Files[file];&lt;br /&gt;                   &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;filePath = &lt;span style=&quot;color: #2b91af;&quot;&gt;HttpContext&lt;/span&gt;.Current.Server.MapPath(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;~/&quot; &lt;/span&gt;+ postedFile.FileName);&lt;br /&gt;                   postedFile.SaveAs( filePath);&lt;br /&gt;&lt;br /&gt;                   files.Add(filePath);&lt;br /&gt;               }&lt;br /&gt;&lt;br /&gt;               &lt;span style=&quot;color: green;&quot;&gt;// return result&lt;br /&gt;              &lt;/span&gt;result = Request.CreateResponse(&lt;span style=&quot;color: #2b91af;&quot;&gt;HttpStatusCode&lt;/span&gt;.Created, files);&lt;br /&gt;           }&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;else&lt;br /&gt;           &lt;/span&gt;{&lt;br /&gt;               &lt;span style=&quot;color: green;&quot;&gt;// return BadRequest (no file(s) available)&lt;br /&gt;               &lt;/span&gt;result = Request.CreateResponse(&lt;span style=&quot;color: #2b91af;&quot;&gt;HttpStatusCode&lt;/span&gt;.BadRequest);&lt;br /&gt;           }&lt;br /&gt;&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;result;&lt;br /&gt;       }&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h1&gt;The Get Method&lt;/h1&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;As mentioned before, the Get Method will accept a string file that will identify (= filename) the file that needs to be downloaded.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;HttpResponseMessage &lt;/span&gt;Get(&lt;span style=&quot;color: blue;&quot;&gt;string &lt;/span&gt;id)&lt;br /&gt;       {&lt;br /&gt;           &lt;span style=&quot;color: #2b91af;&quot;&gt;HttpResponseMessage &lt;/span&gt;result = &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;;&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;localFilePath = &lt;span style=&quot;color: #2b91af;&quot;&gt;HttpContext&lt;/span&gt;.Current.Server.MapPath(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;~/&quot; &lt;/span&gt;+ id);&lt;br /&gt;&lt;br /&gt;           &lt;span style=&quot;color: green;&quot;&gt;// check if parameter is valid&lt;br /&gt;           &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;if &lt;/span&gt;(&lt;span style=&quot;color: #2b91af;&quot;&gt;String&lt;/span&gt;.IsNullOrEmpty(id))&lt;br /&gt;           {&lt;br /&gt;               result = Request.CreateResponse(&lt;span style=&quot;color: #2b91af;&quot;&gt;HttpStatusCode&lt;/span&gt;.BadRequest);&lt;br /&gt;           }&lt;br /&gt;               &lt;span style=&quot;color: green;&quot;&gt;// check if file exists on the server&lt;br /&gt;           &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;else if &lt;/span&gt;(!&lt;span style=&quot;color: #2b91af;&quot;&gt;File&lt;/span&gt;.Exists(localFilePath))&lt;br /&gt;           {&lt;br /&gt;               result = Request.CreateResponse(&lt;span style=&quot;color: #2b91af;&quot;&gt;HttpStatusCode&lt;/span&gt;.Gone);&lt;br /&gt;           }&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;else&lt;br /&gt;           &lt;/span&gt;{&lt;span style=&quot;color: green;&quot;&gt;// serve the file to the client&lt;br /&gt;               &lt;/span&gt;result = Request.CreateResponse( &lt;span style=&quot;color: #2b91af;&quot;&gt;HttpStatusCode&lt;/span&gt;.OK);&lt;br /&gt;               result.Content = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;StreamContent&lt;/span&gt;(&lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;FileStream&lt;/span&gt;(localFilePath, &lt;span style=&quot;color: #2b91af;&quot;&gt;FileMode&lt;/span&gt;.Open, &lt;span style=&quot;color: #2b91af;&quot;&gt;FileAccess&lt;/span&gt;.Read));&lt;br /&gt;               result.Content.Headers.ContentDisposition = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;System.Net.Http.Headers.&lt;span style=&quot;color: #2b91af;&quot;&gt;ContentDispositionHeaderValue&lt;/span&gt;(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;attachment&quot;&lt;/span&gt;);&lt;br /&gt;               result.Content.Headers.ContentDisposition.FileName = id;&lt;br /&gt;           }&lt;br /&gt;&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;result;&lt;br /&gt;       }&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h1&gt;Uploading a File&lt;/h1&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Using a submit&amp;nbsp; button&lt;/h2&gt;

&lt;pre class=&quot;code&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;form &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;form1&quot; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;post&quot; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;action&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;api/file&quot; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;enctype&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;multipart/form-data&quot;&amp;gt;&lt;br /&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;label&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;            &lt;/span&gt;Using submit&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;label&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;input &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;myFile&quot; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;file&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;input &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;submit&quot; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;Submit&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;form&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Using JQuery&lt;/h2&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;form &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;enctype&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;multipart/form-data&quot;&amp;gt;&lt;br /&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;label&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &lt;/span&gt;Using JQuery&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;label&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;input &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;file&quot; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;file&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;input &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;button&quot; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;Upload&quot; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;Upload&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;form&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;        &lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;script &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;text/javascript&quot;&amp;gt;&lt;br /&gt;            &lt;/span&gt;$(&lt;span style=&quot;color: blue;&quot;&gt;function &lt;/span&gt;() {&lt;br /&gt;                $(&lt;span style=&quot;color: maroon;&quot;&gt;&#39;#Upload&#39;&lt;/span&gt;).click(&lt;span style=&quot;color: blue;&quot;&gt;function &lt;/span&gt;() {&lt;br /&gt;                    &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;formData = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;FormData($(&lt;span style=&quot;color: maroon;&quot;&gt;&#39;form&#39;&lt;/span&gt;)[0]);&lt;br /&gt;                    $.ajax({&lt;br /&gt;                        url: &lt;span style=&quot;color: maroon;&quot;&gt;&#39;api/file&#39;&lt;/span&gt;, &lt;br /&gt;                        type: &lt;span style=&quot;color: maroon;&quot;&gt;&#39;POST&#39;&lt;/span&gt;,&lt;br /&gt;                        &lt;span style=&quot;color: #006400;&quot;&gt;// Form data&lt;br /&gt;                        &lt;/span&gt;data: formData,&lt;br /&gt;                        &lt;span style=&quot;color: #006400;&quot;&gt;//Options to tell JQuery not to process data or worry about content-type&lt;br /&gt;                        &lt;/span&gt;cache: &lt;span style=&quot;color: blue;&quot;&gt;false&lt;/span&gt;,&lt;br /&gt;                        contentType: &lt;span style=&quot;color: blue;&quot;&gt;false&lt;/span&gt;,&lt;br /&gt;                        processData: &lt;span style=&quot;color: blue;&quot;&gt;false&lt;br /&gt;                    &lt;/span&gt;});&lt;br /&gt;                });&lt;br /&gt;            });&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h1&gt;Downloading a File&lt;/h1&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.ggpht.com/-I5SBqwTkJek/UMHBZeu0iMI/AAAAAAAAAHM/rqrrr9XKnA0/s1600-h/image%25255B7%25255D.png&quot;&gt;&lt;img style=&quot;display: block; float: none; margin-left: auto; margin-right: auto; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh6.ggpht.com/-7q2qvEH6OP4/UMHBakxWI3I/AAAAAAAAAHU/rXO1N3MjK3A/image_thumb%25255B5%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;583&quot; height=&quot;319&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h1&gt;Summary&lt;/h1&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;The solution can be downloaded &lt;a href=&quot;http://dl.dropbox.com/u/41091233/Blog/WebApi%20File%20Upload/WebApi%20FileUpload.rar&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Fri, 07 Dec 2012 09:15:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2012/12/07/webapi-file-upload-and-download-using-jquery-and-submit-button</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2012/12/07/webapi-file-upload-and-download-using-jquery-and-submit-button</guid>
        
        <category>JQuery</category>
        
        <category>MVC 4</category>
        
        <category>WebApi</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Paged Grid with Knockout.js and WebApi</title>
        <description>&lt;!-- more --&gt;

&lt;p&gt;I want to see how easily you can create a custom grid that uses an WebApi as datasource and that allows the user to page the data and also allow them to choose a page size for the grid. &lt;/p&gt;

&lt;h1&gt;&amp;nbsp;&lt;/h1&gt;

&lt;h1&gt;The WebApi&lt;/h1&gt;

&lt;p&gt;The WebApi will return a viewmodel that contains an array of names (string) and a property that specifies how many pages that there are available.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;NamesGridViewModel&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;IEnumerable&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;&amp;gt; Names { &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;; }&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public int &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;NumberOfPages { &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;; }&lt;br /&gt;    }&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;The WebApi itself will contain a method that accepts 2 parameters. The parameters will be page (the page that we request) and pagesize (how many entries a page contains). This method will be responsible for calculating how many pages there are and to serve the requested names based on the 2 parametes.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;ValuesController &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;ApiController&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;IList&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;&amp;gt; Names&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;get &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;{ &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;return new &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;List&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;&amp;gt; { &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Fred&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Barney&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Betty&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Wilma&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Bart&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Lisa&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Maggie&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Homer&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;Marge&quot; &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;}; }&lt;br /&gt;        }&lt;br /&gt;            &lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;NamesGridViewModel &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;Get(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;int &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;page, &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;int &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;pagesize)&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;return new &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;NamesGridViewModel&lt;br /&gt;                       &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;{&lt;br /&gt;                           &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// get the request names for the specific page&lt;br /&gt;                           &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;Names = (0 == page? &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;null &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;: Names.Skip((page - 1)*pagesize).Take(pagesize).ToArray())&lt;br /&gt;                           ,&lt;br /&gt;                           &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// calculated number of pages and ceil the value&lt;br /&gt;                           &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;NumberOfPages = ((&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;background: white; color: #2b91af;&quot;&gt;Math&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;.Ceiling((&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;) Names.Count/pagesize))&lt;br /&gt;                       };&lt;br /&gt;        }&lt;br /&gt;    }&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h1&gt;The client&lt;/h1&gt;

&lt;p&gt;&lt;br /&gt;The client will need a reference to kockout.js. With &lt;a href=&quot;http://knockoutjs.com/&quot; target=&quot;_blank&quot;&gt;knockout.js&lt;/a&gt; it is possible to apply the MVVM pattern and also work with templates in javascript.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;The UI&lt;/h2&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;Page size: &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;select &lt;/span&gt;&lt;span style=&quot;background: white; color: red;&quot;&gt;data-bind&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;=&quot;options: availablePageSize, optionsText: $data, value: selectedPageSize&quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;background: white; color: darkgreen;&quot;&gt;&amp;lt;!-- the grid --&amp;gt;&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;table &lt;/span&gt;&lt;span style=&quot;background: white; color: red;&quot;&gt;data-bind&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;=&quot;with: namesGridViewModel&quot;&amp;gt;&lt;br /&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;thead&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;th&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;Name&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;th&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;thead&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;tbody &lt;/span&gt;&lt;span style=&quot;background: white; color: red;&quot;&gt;data-bind&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;=&quot;foreach: Names&quot;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;td &lt;/span&gt;&lt;span style=&quot;background: white; color: red;&quot;&gt;data-bind&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;=&quot;text: $data&quot;&amp;gt;&lt;br /&gt;            &amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;tbody&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;background: white; color: darkgreen;&quot;&gt;&amp;lt;!--Contains the page links--&amp;gt;&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;div &lt;/span&gt;&lt;span style=&quot;background: white; color: red;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;=&quot;pager&quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;The Javascript&lt;/h2&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;script &lt;/span&gt;&lt;span style=&quot;background: white; color: red;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;=&quot;text/javascript&quot;&amp;gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// Our  ViewModel.&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;pageViewModel() {&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;var &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self = &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;;&lt;br /&gt;        self.namesGridViewModel = ko.observable(); &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// contains the viewmodel returned by the WebApi&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self.selectedPageSize = ko.observable(3); &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// contains the selected page size, default value is 3&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self.availablePageSize = ko.observableArray([1, 2, 3, 4, 5]); &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// contains the available page sizes a user can select&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self.selectedPage = ko.observable(1); &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// contains the selected page&lt;br /&gt;&lt;br /&gt;        // Add a click event to all future element with a class &quot;pageIndex&quot;. This event will fire&lt;br /&gt;        // when the user clicks a specific page.&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;#pager&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;).on(&lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;click&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;.pageIndex&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;(event) {&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// set the selected page in the viewModel&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self.selectedPage($(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;).text());&lt;br /&gt;        });&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// This function will be used to get the data from our WebApi. The requested page and page size are passed&lt;br /&gt;        // as a parameter. The result will be stored in the namesGridViewModel property. This will cause that the subscribe event&lt;br /&gt;        // for the namesGridViewModel will be fired ==&amp;gt; the page links will be created.&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self.navigate = &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;() {&lt;br /&gt;            $.get(&lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;/api/values?page=&quot; &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;+ self.selectedPage() + &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;&amp;amp;pagesize=&quot; &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;+ self.selectedPageSize(), self.namesGridViewModel);&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// Function that will subscribe to all the needed events.&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self.SubscribeToEvents = &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;() {&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// This event will fire when selectedPageSize is changed.&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self.selectedPageSize.subscribe(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;(newValue) {&lt;br /&gt;                self.selectedPage(1);&lt;br /&gt;                self.navigate();&lt;br /&gt;            });&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// This event will be fired when the selectedPage is changed.&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self.selectedPage.subscribe(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;(newValue) {&lt;br /&gt;                self.navigate();&lt;br /&gt;            });&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// This event will fire when a new value is defined for the namesGridViewModel.&lt;br /&gt;            // It will create the page links below the grid.&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self.namesGridViewModel.subscribe(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;(newValue) {&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;var &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;numberOfPages = newValue.NumberOfPages;&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;var &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;$pager = $(&lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;#pager&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;);&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// clear the pager&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;$pager.html(&lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;);&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// created the pages the user can click on&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;for &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;var &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;i = 1; i &amp;lt;= numberOfPages; i++) {&lt;br /&gt;                    &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;var &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;link = $(&lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&#39;&amp;lt;a class=&quot;pageIndex&quot;&amp;gt;&#39; &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;+ i + &lt;/span&gt;&lt;span style=&quot;background: white; color: #a31515;&quot;&gt;&#39;&amp;lt;/a&amp;gt;&#39;&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;);&lt;br /&gt;                    $pager.append(link);&lt;br /&gt;                }&lt;br /&gt;            }, &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;);&lt;br /&gt;        };&lt;br /&gt;        &lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// This function will be used to kick start everything.&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;self.bind = &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;() {&lt;br /&gt;            self.SubscribeToEvents();&lt;br /&gt;            self.navigate();&lt;br /&gt;            ko.applyBindings(self);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: green;&quot;&gt;// Create the viewModel and bind it.&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;function &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;() { &lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;background: white; color: black;&quot;&gt;pageViewModel().bind(); })&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;background: white; color: maroon;&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;background: white; color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h1&gt;The result&lt;/h1&gt;

&lt;p&gt;&lt;br /&gt;&lt;a href=&quot;http://lh5.ggpht.com/-A1kkAPkQ4rQ/ULpj583zJFI/AAAAAAAAAG0/om6t22SmlkE/s1600-h/image%25255B5%25255D.png&quot;&gt;&lt;img style=&quot;display: block; float: none; margin-left: auto; margin-right: auto; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh5.ggpht.com/--oGl5nyMxDQ/ULpj6qkQO2I/AAAAAAAAAG4/j7cYsdDToPY/image_thumb%25255B2%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;625&quot; height=&quot;246&quot; border=&quot;0&quot; /&gt;&lt;/a&gt; &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h1&gt;Summary&lt;/h1&gt;

&lt;p&gt;&lt;br /&gt;You have seen how to create a basic paging grid using Knockout.js.You can easily extend this example to also allow sorting. The solution can be downloaded &lt;a href=&quot;http://dl.dropbox.com/u/41091233/Blog/Paged%20Grid%20with%20Knockout.js%20and%20WebApi/WebApi-Knockout.js-Paging%20Grid.rar&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Sat, 01 Dec 2012 19:09:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2012/12/01/paged-grid-with-knockoutjs-and-webapi</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2012/12/01/paged-grid-with-knockoutjs-and-webapi</guid>
        
        <category>JQuery</category>
        
        <category>Knockout.js</category>
        
        <category>WebApi</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Nuget package: integrated MEF in Asp.Net MVC 4 and WebApi</title>
        <description>&lt;!-- more --&gt;

&lt;p&gt;Today i wanted to create my first Nuget package and i already knew what i would publish to the world. I wanted to make the code that is used to use MEF in Asp.Net MVC 4 and the WebApi ([view this post](http://blog.kennytordeur.be/2012/08/31/mef-in-aspnet-mvc-4-and-webapi)) easy to reuse in other projects. I rewrote the code in a class libary. &lt;br /&gt;After a little google-ing i found a &lt;a href=&quot;http://www.youtube.com/watch?v=t7pYtAjkWUY&quot; target=&quot;_blank&quot;&gt;video&lt;/a&gt; that explained how to create a Nuget package using the Nuget Package Explorer.&amp;nbsp; After that i created a user at the &lt;a href=&quot;http://nuget.org/&quot; target=&quot;_blank&quot;&gt;Nuget gallery&lt;/a&gt; so i got my API Key. With this, i had all the information i needed to published my first Nuget package and i have a feeling that it will not be my last :-)&lt;br /&gt;The &lt;a href=&quot;https://nuget.org/packages/MEF.MVC4&quot; target=&quot;_blank&quot;&gt;package&lt;/a&gt; can be installed from the Package Manager Console by typing: Install-Package MEF.MVC4&lt;/p&gt;
</description>
        <pubDate>Wed, 28 Nov 2012 12:26:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2012/11/28/nuget-package-integrated-mef-in-aspnet-mvc-4-and-webapi</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2012/11/28/nuget-package-integrated-mef-in-aspnet-mvc-4-and-webapi</guid>
        
        <category>ASP.NET</category>
        
        <category>MEF</category>
        
        <category>MVC 4</category>
        
        <category>Nuget</category>
        
        <category>WebApi</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>MEF in Asp.Net MVC 4 and WebApi</title>
        <description>&lt;!-- more --&gt;

&lt;p&gt;MEF stands for &amp;ldquo;Microsoft Extensibility Framework&amp;rdquo;. If you want an overview of MEF, you can look &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/dd460648.aspx#what_mef_provides&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. In this post we will integrated MEF with Asp.Net MVC 4 and with the WebApi.&lt;/p&gt;

&lt;h2&gt;Asp.Net MVC 4&lt;/h2&gt;

&lt;p&gt;First of all you need to reference the &amp;ldquo;System.ComponentModel.Composition.dll&amp;rdquo; assembly which contains the implementation of MEF. Now where are going to create a MEF controller factory. Like the name says, it will be responsible to create the controller that is requested.&lt;/p&gt;

&lt;h1&gt;&amp;nbsp;&lt;/h1&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;MefControllerFactory &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;DefaultControllerFactory&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;       &lt;span style=&quot;color: blue;&quot;&gt;private readonly &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;CompositionContainer &lt;/span&gt;_compositionContainer;&lt;br /&gt;&lt;br /&gt;       &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;MefControllerFactory(&lt;span style=&quot;color: #2b91af;&quot;&gt;CompositionContainer &lt;/span&gt;compositionContainer)&lt;br /&gt;       {&lt;br /&gt;           _compositionContainer = compositionContainer;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       &lt;span style=&quot;color: blue;&quot;&gt;protected override &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IController &lt;/span&gt;GetControllerInstance(System.Web.Routing.&lt;span style=&quot;color: #2b91af;&quot;&gt;RequestContext &lt;/span&gt;requestContext, &lt;span style=&quot;color: #2b91af;&quot;&gt;Type &lt;/span&gt;controllerType)&lt;br /&gt;       {&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;export = _compositionContainer.GetExports(controllerType, &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;, &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;).SingleOrDefault();&lt;br /&gt;&lt;br /&gt;           &lt;span style=&quot;color: #2b91af;&quot;&gt;IController &lt;/span&gt;result;&lt;br /&gt;&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;if &lt;/span&gt;(&lt;span style=&quot;color: blue;&quot;&gt;null &lt;/span&gt;!= export)&lt;br /&gt;           {&lt;br /&gt;               result = export.Value &lt;span style=&quot;color: blue;&quot;&gt;as &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IController&lt;/span&gt;;&lt;br /&gt;           }&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;else&lt;br /&gt;           &lt;/span&gt;{&lt;br /&gt;               result = &lt;span style=&quot;color: blue;&quot;&gt;base&lt;/span&gt;.GetControllerInstance(requestContext, controllerType);&lt;br /&gt;               _compositionContainer.ComposeParts(result);&lt;br /&gt;           }&lt;br /&gt;         &lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;result;&lt;br /&gt;       }&lt;br /&gt;   }&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;The MefcontrollerFactory will inherit from the DefaultControllerFactory and has a constructor that accepts a CompositionContainer. To plug in MEF, the GetControllerInstance methode will be overridden. First will look in our exports if something is defined for the requested controller type. Mark that the SingleOrDefault method is used because if more exports are defined for a controller, an exception will be throw. If this exception is thrown, something is wrong with the configuration of the CompositionContainer. If a single export is found, we&amp;rsquo;ll get the object from the Value property.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If the export isn&amp;rsquo;t found, the base method will be invoked. That way, the controller is created the default-way. When the controller is created, the ComposeParts method on the CompositionContainer is invoked. This will resolve the needed import for the controller.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is the object that will be injected. The GetMessage method will return a string that will be shown on the view.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public interface &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IMyTest&lt;/span&gt;{&lt;br /&gt;    &lt;span style=&quot;color: #2b91af;&quot;&gt;String &lt;/span&gt;GetMessage();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[&lt;span style=&quot;color: #2b91af;&quot;&gt;Export&lt;/span&gt;(&lt;span style=&quot;color: blue;&quot;&gt;typeof&lt;/span&gt;(&lt;span style=&quot;color: #2b91af;&quot;&gt;IMyTest&lt;/span&gt;))]&lt;br /&gt;&lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;MyTest1 &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;IMyTest&lt;/span&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;MyTest1()&lt;br /&gt;    {&lt;br /&gt;        creationDate = &lt;span style=&quot;color: #2b91af;&quot;&gt;DateTime&lt;/span&gt;.Now;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public string &lt;/span&gt;GetMessage()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;String&lt;/span&gt;.Format(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;MyTest1 created at &lt;/span&gt;&lt;span style=&quot;color: mediumseagreen;&quot;&gt;{0}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;&lt;/span&gt;, creationDate.ToString(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;hh:mm:ss&quot;&lt;/span&gt;)) ;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;private &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;DateTime &lt;/span&gt;creationDate;&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;The Export attribute says to MEF that this class can be exported. The type of IMyTest is pasted as a parameter. This indicates the contract that is used. So when an IMyTest is requested from MEF, an object of MyTest1 is returned. &lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af;&quot;&gt;Export&lt;/span&gt;]&lt;br /&gt;&lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;HomeController &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;Controller&lt;/span&gt;{&lt;br /&gt;    [&lt;span style=&quot;color: #2b91af;&quot;&gt;Import&lt;/span&gt;]&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;private &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IMyTest &lt;/span&gt;_myTest;&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;ActionResult &lt;/span&gt;Index()&lt;br /&gt;    {&lt;br /&gt;        ViewBag.Message = _myTest.GetMessage();&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;View();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;The HomeController is marked for export and an import attribute is put on the &amp;ldquo;_mytest&amp;rdquo; property. Of course we still need to configure MVC to use our MefControllerFactorty. To accomplish this, is created a static class with one static public method (RegisterMef). This method will create a Mef CompositionContainer and pass this container to the MefControllerFactory. This MefControllerFactory will be set as the controller factory for MVC. To configure the CompositionContainer an AssemblyCatalog is used.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public static class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;MefConfig&lt;/span&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public static void &lt;/span&gt;RegisterMef()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;container = ConfigureContainer();&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: #2b91af;&quot;&gt;ControllerBuilder&lt;/span&gt;.Current.SetControllerFactory(&lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;MefControllerFactory&lt;/span&gt;(container));&lt;br /&gt;        &lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;dependencyResolver = System.Web.Http.&lt;span style=&quot;color: #2b91af;&quot;&gt;GlobalConfiguration&lt;/span&gt;.Configuration.DependencyResolver;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;private static &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;CompositionContainer &lt;/span&gt;ConfigureContainer()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;assemblyCatalog = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;AssemblyCatalog&lt;/span&gt;(&lt;span style=&quot;color: #2b91af;&quot;&gt;Assembly&lt;/span&gt;.GetExecutingAssembly());&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;container = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;CompositionContainer&lt;/span&gt;(assemblyCatalog);&lt;br /&gt;        &lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;container;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;The RegisterMef method will be called form the Application_Start method that resides in the global.asax.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;MvcApplication &lt;/span&gt;: System.Web.&lt;span style=&quot;color: #2b91af;&quot;&gt;HttpApplication&lt;/span&gt;{&lt;br /&gt;&lt;span style=&quot;color: blue;&quot;&gt;     protected void &lt;/span&gt;Application_Start()&lt;br /&gt;     {&lt;br /&gt;        &lt;span style=&quot;color: #2b91af;&quot;&gt;AreaRegistration&lt;/span&gt;.RegisterAllAreas();&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: #2b91af;&quot;&gt;MefConfig&lt;/span&gt;.RegisterMef();&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: #2b91af;&quot;&gt;WebApiConfig&lt;/span&gt;.Register(&lt;span style=&quot;color: #2b91af;&quot;&gt;GlobalConfiguration&lt;/span&gt;.Configuration);&lt;br /&gt;        &lt;span style=&quot;color: #2b91af;&quot;&gt;FilterConfig&lt;/span&gt;.RegisterGlobalFilters(&lt;span style=&quot;color: #2b91af;&quot;&gt;GlobalFilters&lt;/span&gt;.Filters);&lt;br /&gt;        &lt;span style=&quot;color: #2b91af;&quot;&gt;RouteConfig&lt;/span&gt;.RegisterRoutes(&lt;span style=&quot;color: #2b91af;&quot;&gt;RouteTable&lt;/span&gt;.Routes);&lt;br /&gt;        &lt;span style=&quot;color: #2b91af;&quot;&gt;BundleConfig&lt;/span&gt;.RegisterBundles(&lt;span style=&quot;color: #2b91af;&quot;&gt;BundleTable&lt;/span&gt;.Bundles);&lt;br /&gt;        &lt;span style=&quot;color: #2b91af;&quot;&gt;AuthConfig&lt;/span&gt;.RegisterAuth();&lt;br /&gt;     }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;When the Index action for the HomeController is requested through the browser, everything works just fine but when it is requested it again, an exception is thrown. &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h4&gt;&lt;em&gt;A single instance of controller &#39;MvcApplication5.Controllers.HomeController&#39; cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.&lt;/em&gt;&lt;/h4&gt;

&lt;p&gt;&lt;br /&gt;This exception is thrown because the lifetime of the controller is linked to the lifetime of the CompositionContainer. The container keeps a reference to all the objects that it has created. If the same object is requested, the reference of the previous object is given. There are 2 ways to solve this problem. &lt;br /&gt;&lt;br /&gt;You could pass an instance of a ComposablePartCatalog (in this case the AssemblyCatalog) to the MefControllerFactorty. Then in the &amp;ldquo;GetControllerInstance&amp;rdquo; method always compose a new CompositionContainer to resolve the controller. Personally I&#39;m not really found about that.&lt;br /&gt;&lt;br /&gt;The other solution is to use a CreatonPolicy. If we put a CreationPolicy of &amp;ldquo;NonShared&amp;rdquo; on the HomeController, a new instance of the HomeController is created from scratch every time it gets requested. Since this a standard feature of MEF, I&#39;ll use this solution.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af;&quot;&gt;Export&lt;/span&gt;]&lt;br /&gt;[&lt;span style=&quot;color: #2b91af;&quot;&gt;PartCreationPolicy&lt;/span&gt;(&lt;span style=&quot;color: #2b91af;&quot;&gt;CreationPolicy&lt;/span&gt;.NonShared)]&lt;br /&gt;&lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;HomeController &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;Controller&lt;/span&gt;{&lt;br /&gt;    [&lt;span style=&quot;color: #2b91af;&quot;&gt;Import&lt;/span&gt;]&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;private &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IMyTest &lt;/span&gt;_myTest;&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;ActionResult &lt;/span&gt;Index()&lt;br /&gt;    {&lt;br /&gt;        ViewBag.Message = _myTest.GetMessage();&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;View();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;Now we can request the controller as many times as we want.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h1&gt;&amp;nbsp;&lt;/h1&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h1&gt;WebApi&lt;/h1&gt;

&lt;p&gt;&lt;br /&gt;This is the created WebApi controller. Mark that the controller also has a CreationPolicy defined. This is for the same reason a mentioned above.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;[&lt;span style=&quot;color: #2b91af;&quot;&gt;Export&lt;/span&gt;]&lt;br /&gt;[&lt;span style=&quot;color: #2b91af;&quot;&gt;PartCreationPolicy&lt;/span&gt;(&lt;span style=&quot;color: #2b91af;&quot;&gt;CreationPolicy&lt;/span&gt;.NonShared)]&lt;br /&gt;&lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;HomeController &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;ApiController&lt;/span&gt;{&lt;br /&gt;    [&lt;span style=&quot;color: #2b91af;&quot;&gt;Import&lt;/span&gt;]&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;private &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IMyTest &lt;/span&gt;_myTest;&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;String &lt;/span&gt;GetMessage()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;_myTest.GetMessage();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;To plug in MEF in the WebApi a object needs to be created from the System.Web.Http.Dependencies.IDependencyResolver interface. This is how the object looks like.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;MefDependencyResolver &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;IDependencyResolver&lt;/span&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;private readonly &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;CompositionContainer &lt;/span&gt;_container;&lt;br /&gt;    &lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;MefDependencyResolver(&lt;span style=&quot;color: #2b91af;&quot;&gt;CompositionContainer &lt;/span&gt;container)&lt;br /&gt;    {&lt;br /&gt;        _container = container;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IDependencyScope &lt;/span&gt;BeginScope()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;return this&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public object &lt;/span&gt;GetService(&lt;span style=&quot;color: #2b91af;&quot;&gt;Type &lt;/span&gt;serviceType)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;export = _container.GetExports(serviceType, &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;, &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;).SingleOrDefault();&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;return null &lt;/span&gt;!= export ? export.Value : &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;object&lt;/span&gt;&amp;gt; GetServices(&lt;span style=&quot;color: #2b91af;&quot;&gt;Type &lt;/span&gt;serviceType)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;exports =_container.GetExports(serviceType, &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;, &lt;span style=&quot;color: blue;&quot;&gt;null&lt;/span&gt;);&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;createdObjects = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: blue;&quot;&gt;object&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;if &lt;/span&gt;( exports.Any())&lt;br /&gt;        {&lt;br /&gt;            &lt;span style=&quot;color: blue;&quot;&gt;foreach &lt;/span&gt;(&lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;export &lt;span style=&quot;color: blue;&quot;&gt;in &lt;/span&gt;exports)&lt;br /&gt;            {&lt;br /&gt;                createdObjects.Add(export.Value);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;createdObjects;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public void &lt;/span&gt;Dispose()&lt;br /&gt;    {&lt;br /&gt;        ;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The &amp;ldquo;BeginScope&amp;rdquo; method returns a scope in with the create objects will life. Since we are going to use MEF to control the lifetime of the created objects, we can do a return of &amp;ldquo;this&amp;rdquo;.&amp;nbsp; We can do this because the IDependencyResolver inherits form IDependencyScope. If you really need a limited scope per request, the &amp;ldquo;BeginScope&amp;rdquo; method always needs to return a new object of type IDependencyScope.&lt;br /&gt;&lt;br /&gt;You also need to implement a &amp;ldquo;Dispose&amp;rdquo; method to release the used resources. In the MefDependencyResolver, it isn&amp;rsquo;t implemented. This is because we return the current instance in the &amp;ldquo;BeginScope&amp;rdquo; method. If we disposed the CompositionContainer, we could only request the service once.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Right now, the only thing needed, is the register the create MefDependencyResolver. This will be done in the modified MefConfig class.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public static class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;MefConfig&lt;br /&gt;   &lt;/span&gt;{&lt;br /&gt;       &lt;span style=&quot;color: blue;&quot;&gt;public static void &lt;/span&gt;RegisterMef()&lt;br /&gt;       {&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;container = ConfigureContainer();&lt;br /&gt;&lt;br /&gt;           &lt;span style=&quot;color: #2b91af;&quot;&gt;ControllerBuilder&lt;/span&gt;.Current.SetControllerFactory(&lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;MefControllerFactory&lt;/span&gt;(container));&lt;br /&gt;           &lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;dependencyResolver = System.Web.Http.&lt;span style=&quot;color: #2b91af;&quot;&gt;GlobalConfiguration&lt;/span&gt;.Configuration.DependencyResolver;&lt;br /&gt;           System.Web.Http.&lt;span style=&quot;color: #2b91af;&quot;&gt;GlobalConfiguration&lt;/span&gt;.Configuration.DependencyResolver = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;MefDependencyResolver&lt;/span&gt;(container);&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       &lt;span style=&quot;color: blue;&quot;&gt;private static &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;CompositionContainer &lt;/span&gt;ConfigureContainer()&lt;br /&gt;       {&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;assemblyCatalog = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;AssemblyCatalog&lt;/span&gt;(&lt;span style=&quot;color: #2b91af;&quot;&gt;Assembly&lt;/span&gt;.GetExecutingAssembly());&lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;container = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;CompositionContainer&lt;/span&gt;(assemblyCatalog);&lt;br /&gt;           &lt;br /&gt;           &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;container;&lt;br /&gt;       }&lt;br /&gt;   }&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The example can be downloaded from &lt;a href=&quot;http://dl.dropbox.com/u/41091233/Blog/MEF%20in%20Asp.Net%20MVC%204%20and%20WebApi/MEF%20Asp.Net%20MVC%204%20and%20WebApi.rar&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Fri, 31 Aug 2012 12:00:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2012/08/31/mef-in-aspnet-mvc-4-and-webapi</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2012/08/31/mef-in-aspnet-mvc-4-and-webapi</guid>
        
        <category>ASP.NET</category>
        
        <category>MEF</category>
        
        <category>MVC 4</category>
        
        <category>WebApi</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>Entity Framework: Database Migrations</title>
        <description>&lt;!-- more --&gt;

&lt;p&gt;If you don&amp;rsquo;t like writing SQL DDL statement, like me, than Entity Framework is very handy. With the Code-First, you can write your model and don&amp;rsquo;t need to worry about your database. It is generated for you by Entity Framework. This is very handy but what if you are working with an existing database? Or if you need to support different versions of your database? That is where database migrations come in to play.&lt;/p&gt;

&lt;h1&gt;Before we start&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Create an Asp.Net MVC 4 application&lt;/li&gt;
&lt;li&gt;Use Nuget to install the latest version of the Entity Framework (5.0.0-rc)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Because i am not running an SQL Express or SQL Server on my machine, i am also going to install SqlServerCompact so i can work with an SDF file. This needs a little more configuration.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install EntityFramework.SqlServerCompact form Nuget.&lt;/li&gt;
&lt;li&gt;Change the connectionstring in the web.config&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;connectionStrings&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;add &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;DefaultConnection&lt;/span&gt;&quot; &lt;span style=&quot;color: red;&quot;&gt;connectionString&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;Data Source=|DataDirectory|\DBTest.sdf;Max Database Size=2047&lt;/span&gt;&quot; &lt;span style=&quot;color: red;&quot;&gt;providerName&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&quot;&lt;span style=&quot;color: blue;&quot;&gt;System.Data.SqlServerCe.4.0&lt;/span&gt;&quot; &lt;span style=&quot;color: blue;&quot;&gt;/&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;connectionStrings&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Create your models. In my case i will create as Car and Make class.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;namespace &lt;/span&gt;EF_DatabaseMigrations.Models&lt;br /&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Car&lt;br /&gt;    &lt;/span&gt;{&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Int32 &lt;/span&gt;Id { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;String &lt;/span&gt;Model { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Make &lt;/span&gt;Make { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Make&lt;br /&gt;    &lt;/span&gt;{&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Int32 &lt;/span&gt;Id { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;String &lt;/span&gt;Name { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Next i will create a DbDatasource that will map my entities to the database. This datasource will pass the name of the connectionstring to the base constructor.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;namespace &lt;/span&gt;EF_DatabaseMigrations.DB&lt;br /&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;DbDatasource &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;DbContext&lt;br /&gt;    &lt;/span&gt;{&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;DbDatasource()&lt;br /&gt;            : &lt;span style=&quot;color: blue;&quot;&gt;base&lt;/span&gt;(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;DefaultConnection&quot;&lt;/span&gt;)&lt;br /&gt;        { }&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Car&lt;/span&gt;&amp;gt; Cars { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Make&lt;/span&gt;&amp;gt; Makes { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h1&gt;Let&amp;rsquo;s migrate&lt;/h1&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;To configure your application to use migrations, you have to type &amp;ldquo;enable-migrations&amp;rdquo; command in the Package Manager Console. This will create a Migrations folder that contains a Configuration file. This class contains a seed method that accepts the DbDatasource as a parameter. The seed method will always be execute when you update your database. This method will typically be used to create users in the database or in our case to create the Makes in our database.&amp;nbsp; By default, AutomaticMigrationsEnabled will be set to false. Since we want to automate everything, we need to set it to true :-).&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;blockquote&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;namespace &lt;/span&gt;EF_DatabaseMigrations.Migrations&lt;br /&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;internal sealed class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Configuration &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;DbMigrationsConfiguration&lt;/span&gt;&amp;lt;EF_DatabaseMigrations.DB.&lt;span style=&quot;color: #2b91af;&quot;&gt;DbDatasource&lt;/span&gt;&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;Configuration()&lt;br /&gt;        {&lt;br /&gt;            AutomaticMigrationsEnabled = &lt;span style=&quot;color: blue;&quot;&gt;true&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;protected override void &lt;/span&gt;Seed(EF_DatabaseMigrations.DB.&lt;span style=&quot;color: #2b91af;&quot;&gt;DbDatasource &lt;/span&gt;context)&lt;br /&gt;        {&lt;br /&gt;            context.Makes.AddOrUpdate(m =&amp;gt; m.Name, &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Make &lt;/span&gt;{ Name = &lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Ford&quot; &lt;/span&gt;}&lt;br /&gt;                                                 , &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Make &lt;/span&gt;{ Name = &lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Opel&quot; &lt;/span&gt;}&lt;br /&gt;                                                 , &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Make &lt;/span&gt;{ Name = &lt;span style=&quot;color: #a31515;&quot;&gt;&quot;BMW&quot; &lt;/span&gt;});&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Because the Seed method is always called, we have to make sure that the Makes we are adding, only are getting inserted if they don&amp;rsquo;t exists or are updated when they do exists the database. That is why we are using the AddOrUpdate methode. The first parameter is in fact our condition for the insert or update. If a make with a name &amp;ldquo;Ford&amp;rdquo; doesn&amp;rsquo;t exists in the database, it is inserted. If it does exists, it gets updated.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;When we type &amp;ldquo;update-database&amp;rdquo; command in the Package Manager Console, EF will create our database and will call the Seed method. The location of the database will be determined by the connectionstring that you specify. In our case it will be found in the App_Data folder.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;When we open the database, we see that a Cars and a Makes table are created. The Makes table is pre-filled.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;a href=&quot;http://lh6.ggpht.com/-vKhzBJYgJuc/UComVwfwiyI/AAAAAAAAAGE/OYdpaeOiHR8/s1600-h/image%25255B3%25255D.png&quot;&gt;&lt;img style=&quot;display: block; float: none; margin-left: auto; margin-right: auto; border: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh6.ggpht.com/-jSSpn0Ulqi0/UComWt61psI/AAAAAAAAAGI/lJ_1vASmgjM/image_thumb%25255B1%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;359&quot; height=&quot;124&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;In the table &amp;ldquo;_MigrationHistory&amp;rdquo; all migrations that where performed on this database are stored. That way you can always see what version of database this is. Also Entity Framework will need this information.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Now let&amp;rsquo;s assume that we want to add a BuildDate property to the Car. We can put this in a new migration. This way we can keep track of all our modifications in our database.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Change the Car entity.&lt;/h2&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Car&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Int32 &lt;/span&gt;Id { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;String &lt;/span&gt;Model { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Make &lt;/span&gt;Make { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;DateTime &lt;/span&gt;BuildDate { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;Add Migration&lt;/h2&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Type &amp;ldquo;add-migration AddBuildDate&amp;rdquo; in the Package Manager Console. This will create a new file in the Migration folder. The file name will contain the date + the name that we specified.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;This new file will contain 2 methods that we can override. The &amp;ldquo;Up&amp;rdquo; method will contain all new database modifications. The &amp;ldquo;Down&amp;rdquo; will contain the rollback of the modifications. This way we can easily &amp;ldquo;jump&amp;rdquo; through database versions. We manually add the AddColumn and DropColumn statements.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;namespace &lt;/span&gt;EF_DatabaseMigrations.Migrations&lt;br /&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public partial class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;AddBuildDate &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;DbMigration&lt;br /&gt;    &lt;/span&gt;{&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public override void &lt;/span&gt;Up()&lt;br /&gt;        {&lt;br /&gt;            AddColumn(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;dbo.Cars&quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515;&quot;&gt;&quot;BuildDate&quot;&lt;/span&gt;, c =&amp;gt; c.DateTime(nullable: &lt;span style=&quot;color: blue;&quot;&gt;false&lt;/span&gt;));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public override void &lt;/span&gt;Down()&lt;br /&gt;        {&lt;br /&gt;            DropColumn(&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;dbo.Cars&quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515;&quot;&gt;&quot;BuildDate&quot;&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;When we want to apply those modifications, we need to type &amp;ldquo;update-database -TargetMigration:AddBuildDate&amp;rdquo;. This will modify our database and add the new column.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://lh3.ggpht.com/-mhKvKHmKl40/UComXMyADMI/AAAAAAAAAGQ/Om7QD66ViK0/s1600-h/image%25255B7%25255D.png&quot;&gt;&lt;img style=&quot;display: block; float: none; margin-left: auto; margin-right: auto; border: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh6.ggpht.com/-Drk6QCNjNqw/UComX3YgBoI/AAAAAAAAAGY/SFqsI2KEIho/image_thumb%25255B3%25255D.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;628&quot; height=&quot;116&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;If you want to rollback the database at it&amp;rsquo;s begin state, you type &amp;ldquo;update-database -TargetMigration:0&amp;rdquo; in the Package Console Manager.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;What about the database adminstrators&lt;/h2&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;We can&amp;rsquo;t forget these guys. They probably won&amp;rsquo;t like this way of creating the database and would probably prefer a script. Well you can add an extra parameter to the &amp;ldquo;update-database&amp;rdquo; called script that will create a clean SQL script that performs all the necessary steps to create the database.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;ldquo;update-database &amp;ndash;TargetMigration:AddBuildDate &amp;ndash;script&amp;rdquo; will create this script:&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ALTER TABLE &lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;[Cars] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ADD &lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;[BuildDate] [datetime]&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;[__MigrationHistory] &lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;[MigrationId]&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;[Model]&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: teal;&quot;&gt;[ProductVersion]&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES &lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;201208140904052_AddBuildDate&#39;&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;, &lt;/span&gt;0x1F8B0800000000000400ECBD07601C499625262F6DCA7B7F4AF54AD7E074A10880601324D8904010ECC188CDE692EC1D69472329AB2A81CA6556655D661640CCED9DBCF7DE7BEFBDF7DE7BEFBDF7BA3B9D4E27F7DFFF3F5C6664016CF6CE4ADAC99E2180AAC81F3F7E7C1F3F22FEC7BFF71F7CFC7BBC5B94E9655E3745B5FCECA3DDF1CE4769BE9C56B36279F1D947EBF67CFBE0A3DFE3E8374E1E9FCE16EFD29F34EDF6D08EDE5C369F7D346FDBD5A3BB779BE93C5F64CD78514CEBAAA9CEDBF1B45ADCCD66D5DDBD9D9D83BBBB3B777302F111C14AD3C7AFD6CBB658E4FC07FD79522DA7F9AA5D67E517D52C2F1BFD9CBE79CD50D317D9226F56D934FFECA3D367BFFFD3ACCD2659937F515CD4594B0835E3A74F3E4A8FCB22237C5EE7E5F97B22B7F310C87D64BBA58E4F09C1F6FACDF52AE7CE3FFBE824ABFD06D4E4F7CAAF830FE8A39775B5CAEBF6FA557EAEAF9DCD3E4AEF86EFDDEDBE685FF3DE41CFF4DBB2BDB7F751FA625D96D9A4A40FCEB3B2C93F4A579F3E7ADD5675FE79BECC8904F9EC65D6B679BDC4BB3963AE1478B4FAF47644787877670F44B89B2D9755CB54ED21DE4193E7CA60FABAAD89653E4A9F15EFF2D9F37C79D1CE2DB65F64EFCC27FB3B3BC4395F2D0B62317AABADD7B93F3CF97B73B74FD645392316C84DD7F8FD0D7153844E1D482FB2CBE282C7D61D4AF6965ABFCA4B61A779B1123E1AD3ACFFFEF2E5B3BA5ABCAA00DB7CF6FBBFAED6F51468549D2FDE64F545DE86DD3FBEEB586A23A371773FE2341F4DFCFBB3CD6843F373DC34D5B460349D26F8FD239374BA9CA5430C22982B43119EEBB22D566531A5FE3EFBE85BBDE147401996F240095F86B076C6E3DDEEB8BC11F4198F546F9B1534B73AB8A71368D74610EF6285175EE7ADA343F351EAA8E68FB037A0F055A01E7B5786D479D9C33FE85C05D3FBBA2BB45DA1D93045164333AE1089CD93E2BDAC23EB0A62388608DBD97970B6EFAE183F6324EF0E58C9C75F64AB1589846735F593F4B598CC93EDD7EF6F101702E3EEB489D8458BADED897444769177BEA5AE09D36745DDB4C6667F949ECC16BD66B7E13AD395C77CFD2932FC641AE3777961C86F607EED0072247C46A35A909EE301EAF00C93F4DF4AE1B064655647D4EC4955AE17CB2155BDE96DB5B33E00FDE8F6303CA3E9C3F13EEEC37A7CB743842EB93D8DA92D3B5CDF9DBB5BCDAC48D03732B53125708BB98DBFF6B333B962DAFCF7E5931FFE74842AAA276DAA6D6F4175D7F8164203B5DA214957BDF62971AB796010B1C900216C9FEF818E9A8AAF89CE7B6342EA7156B0DB71D6C071B15ECE2D06D93538FD09EFD99D6E13CB6ED6FE74ECCC63D5F98131626A7443B69E1190261FA534F4CB620603F0FABA69F3C5180DC6AF7F51F93AAF29263DC9C7FB08344DB32FB265719E37ED9BEA6D4EB69EBFFBBA419FF5429B6656FEBF30F22B96ADCACF26CFFBA6806753C4B6BCCCEAE99CDDD1AEDBFC8101D98C7E4778FFFEF8196EF668F0757CF6DE14FEDCC4543F2B7328E6E183A6F01B0C759C5178AFA02480C101C3FB86483447790D126625A9B0A6AD4989F52CE24B0A15A7C52A2B7D6CFBFAFB36F30EAA5970DD6F9EE6AB7C8909F546749B6E3698280BB2C37D378DFB3DC3BDBE7FDE9DA55ECC3714F2895227E99F54349DC27BB1502A1A0C0EC68231A8F118EB67234CF45173AEE10DB1613798FCD98906FBD6979863BDB4695562CAA6B87020E04F2CF369C016B6CDD9F2BC32ECD9C1C834E9AAEABCCD48CF67C7755B9C67D396BE9EE64DC3A9999FCCCA3535395D4CF2D9D9F2CB75BB5AB734E47C3129AF7D6280CB37F5CF216F88F3E32F57EC677E134320340B98AA2F976CC12CDECFFA6A780804C447753BE6B2858EBFB8B6905E54CB5B0252F23D3552FF265FAC281999375F2E5F6797F9306E37D330A4D8E3A74546CEFAA249EF1E09D3787C42EC4739FAA3FF270000FFFFEA4946EC18180000&lt;span style=&quot;color: gray;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;5.0.0-rc.net40&#39;&lt;/span&gt;&lt;span style=&quot;color: gray;&quot;&gt;)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;The Visual Studio solution can be downloaded &lt;a href=&quot;http://dl.dropbox.com/u/41091233/Blog/Entity%20Framework%20Database%20Migrations/EF_DatabaseMigrations.rar&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Tue, 14 Aug 2012 08:21:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2012/08/14/entity-framework-database-migrations</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2012/08/14/entity-framework-database-migrations</guid>
        
        <category>Database migrations</category>
        
        <category>EF</category>
        
        <category>Entity Framework</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>ASP.NET MVC: Set Focus, ViewModel Style</title>
        <description>&lt;!-- more --&gt;

&lt;p&gt;In a recent Asp.Net MVC project i had to set a focus on a specific control, but it wasn&amp;rsquo;t just a specific control. It was calculate in runtime by some conditions. I didn&amp;rsquo;t just want to create an extra string property on the viewmodel where i could store the id of the control. Off course this would also work but i wanted something safer than those magic strings.&lt;/p&gt;

&lt;p&gt;In one of my previous posts [Where is the ClientID?](http://blog.kennytordeur.be/post/2011/05/10/aspnet-mvc-where-is-the-clientid), i was able to determine the Id that is created by the HtmlHelper to create a textbox, checkbox, &amp;hellip;&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public static class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;HtmlHelper&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;public static &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;String &lt;/span&gt;GetId&amp;lt;TModel,TProperty&amp;gt;( &lt;span style=&quot;color: blue;&quot;&gt;this &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;HtmlHelper&lt;/span&gt;&amp;lt;TModel&amp;gt; htmlHelper, &lt;span style=&quot;color: #2b91af;&quot;&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Func&lt;/span&gt;&amp;lt;TModel, TProperty&amp;gt;&amp;gt; expression )&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;TagBuilder&lt;/span&gt;.CreateSanitizedId(&lt;span style=&quot;color: #2b91af;&quot;&gt;ExpressionHelper&lt;/span&gt;.GetExpressionText(expression));&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;So i created a property called Focus on my ViewModelBase class with the exacte same type,&amp;nbsp; but for the return type of the function i take Object.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public abstract class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;ViewModelBase&lt;/span&gt;&amp;lt;T&amp;gt;&lt;br /&gt;   {&lt;br /&gt;       &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;Func&lt;/span&gt;&amp;lt;T,&lt;span style=&quot;color: #2b91af;&quot;&gt;Object&lt;/span&gt;&amp;gt;&amp;gt;  Focus { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;   }&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;&amp;nbsp;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Then my PersonViewModel inherits from ViewModelBase&amp;lt;T&amp;gt; where T equals the PersonViewModel type. That way i have complete intellisence when i want to set the Focus property.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;PersonViewModel &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;ViewModelBase&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;PersonViewModel&lt;/span&gt;&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;String &lt;/span&gt;Name { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;String &lt;/span&gt;LastName { &lt;span style=&quot;color: blue;&quot;&gt;get&lt;/span&gt;; &lt;span style=&quot;color: blue;&quot;&gt;set&lt;/span&gt;; }&lt;br /&gt;    }&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;At the view side, i&amp;rsquo;ll use the GetId extension method to determine the id off the control i want to set the focus to.&amp;nbsp; I&amp;rsquo;ll pas the Focus property as parameter for this extension method. Then i&amp;rsquo;ll use it&amp;rsquo;s result in a JQuery selector and invoke the .focus() method.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: yellow;&quot;&gt;@model &lt;/span&gt;ASP.NET_MVC_Set_Focus_ViewModel_Style.Models.&lt;span style=&quot;color: #2b91af;&quot;&gt;PersonViewModel&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background: yellow;&quot;&gt;@{&lt;br /&gt;&lt;/span&gt;    ViewBag.Title = &lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Index&quot;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;background: yellow;&quot;&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;h2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;    &lt;/span&gt;Index&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;h2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background: yellow;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;using &lt;/span&gt;(Html.BeginForm())&lt;br /&gt;{&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;                &lt;/span&gt;Name&lt;br /&gt;            &lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: yellow;&quot;&gt;@&lt;/span&gt;Html.TextBoxFor(m =&amp;gt; m.Name)&lt;br /&gt;            &lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;                &lt;/span&gt;Lastname&lt;br /&gt;            &lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: yellow;&quot;&gt;@&lt;/span&gt;Html.TextBoxFor(m =&amp;gt; m.LastName)&lt;br /&gt;            &lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;td&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;        &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;tr&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: yellow;&quot;&gt;@&lt;/span&gt;Html.HiddenFor(m =&amp;gt; m.Focus)&lt;br /&gt;    &lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;input &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;submit&quot; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;ChangeFocus&quot; /&amp;gt;&lt;br /&gt;&lt;/span&gt;}&lt;br /&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;script &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&quot;text/javascript&quot;&amp;gt;&lt;br /&gt;    &lt;/span&gt;$(document).ready(&lt;span style=&quot;color: blue;&quot;&gt;function &lt;/span&gt;() {&lt;br /&gt;        $(&lt;span style=&quot;color: maroon;&quot;&gt;&quot;#&lt;/span&gt;&lt;span style=&quot;background: yellow; color: maroon;&quot;&gt;@&lt;/span&gt;Html.GetId(Model.Focus)&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;).focus();&lt;br /&gt;    });&lt;br /&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;At the Controller side, on the HttpGet method, i&amp;rsquo;ll set the focus on the Name. On the HttpPost method, i&amp;rsquo;ll set the focus on LastName. That way you&amp;rsquo;ll see the difference.&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;public class &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;HomeController &lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;Controller&lt;br /&gt;    &lt;/span&gt;{&lt;br /&gt;        [&lt;span style=&quot;color: #2b91af;&quot;&gt;HttpGet&lt;/span&gt;]&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;ActionResult &lt;/span&gt;Index()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style=&quot;color: blue;&quot;&gt;var &lt;/span&gt;personViewModel = &lt;span style=&quot;color: blue;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;PersonViewModel&lt;/span&gt;();&lt;br /&gt;&lt;br /&gt;            personViewModel.Focus = m =&amp;gt; m.Name;&lt;br /&gt;&lt;br /&gt;            &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;View(personViewModel);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        [&lt;span style=&quot;color: #2b91af;&quot;&gt;HttpPost&lt;/span&gt;]&lt;br /&gt;        &lt;span style=&quot;color: blue;&quot;&gt;public &lt;/span&gt;&lt;span style=&quot;color: #2b91af;&quot;&gt;ActionResult &lt;/span&gt;Index(&lt;span style=&quot;color: #2b91af;&quot;&gt;PersonViewModel &lt;/span&gt;personViewModel)&lt;br /&gt;        {&lt;br /&gt;            personViewModel.Focus = m =&amp;gt; m.LastName;&lt;br /&gt;&lt;br /&gt;            &lt;span style=&quot;color: blue;&quot;&gt;return &lt;/span&gt;View(personViewModel);&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;The main advantage is that you don&amp;rsquo;t have to work with those magic strings. When the property is renamed or removed, i&amp;rsquo;ll get a compiler error. And your code is cleaner.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;This code isn&amp;rsquo;t rocket science but can be very handy. The solution can be downloaded from &lt;a href=&quot;http://dl.dropbox.com/u/41091233/Blog/Asp.Net%20MVC%203%20Set%20Focus%20ViewModel%20Style/ASP.NET%20MVC%20Set%20Focus%20ViewModel%20Style.rar&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Wed, 16 May 2012 15:58:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2012/05/16/aspnet-mvc-set-focus-viewmodel-style</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2012/05/16/aspnet-mvc-set-focus-viewmodel-style</guid>
        
        <category>ASP.NET</category>
        
        <category>JQuery</category>
        
        <category>MVC</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>ASP .NET MVC and Unity: Navigating through Interfaces</title>
        <description>&lt;!-- more --&gt;

&lt;p&gt;In this previous [post](http://blog.kennytordeur.be/post/2011/05/03/aspnet-mvc-3-and-unity-using-an-idependencyresolver) you saw how i used an IDependencyResolver in combination with an IUnityContainer to created a loosely coupled application. But there is still one dependency that i want to cut and that is the url that is used to navigate.&lt;/p&gt;

&lt;p&gt;Normally if we want to navigate to the Index action of our HomeController we use something like this in the url: http://localhost/Home/Index&lt;br /&gt;That is an dependency that i want to cut, because we ask to use a controller of type &amp;ldquo;HomeController&amp;rdquo;&lt;br /&gt;.&lt;br /&gt;What i want to do is to navigate through interfaces, http://localhost/IHome/Index for example. When we type this, it will be up to Unity to resolve the requested controller. I will use the solution used in this [post](http://blog.kennytordeur.be/post/2011/05/03/aspnet-mvc-3-and-unity-using-an-idependencyresolver) as base.&lt;/p&gt;

&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;

&lt;h2&gt;Let&amp;rsquo;s get started&lt;/h2&gt;

&lt;p&gt;The first thing that we need to do is to create an IHomeController interface. This interface will be implemented by our HomeController.&lt;/p&gt;

&lt;div id=&quot;scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bc87133d-0758-48cd-8659-58a50aae6752&quot; class=&quot;wlWriterEditableSmartContent&quot; style=&quot;margin: 0px; display: inline; float: none; padding: 0px;&quot;&gt;
&lt;div style=&quot;border: #000080 1px solid; color: #000; font-family: &#39;Courier New&#39;, Courier, Monospace; font-size: 10pt;&quot;&gt;
&lt;div style=&quot;background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px;&quot;&gt;IHomeController interface&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; System.Web.Mvc;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;namespace&lt;/span&gt; MVCWebsite.Controllers&lt;br /&gt; {&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;interface&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;IHomeController&lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;IController&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Web.Mvc.&lt;span style=&quot;color: #2b91af;&quot;&gt;ActionResult&lt;/span&gt; Index();&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;br /&gt; }&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;div id=&quot;scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4828018a-72fc-4710-bf45-4a43cff950c0&quot; class=&quot;wlWriterEditableSmartContent&quot; style=&quot;margin: 0px; display: inline; float: none; padding: 0px;&quot;&gt;
&lt;div style=&quot;border: #000080 1px solid; color: #000; font-family: &#39;Courier New&#39;, Courier, Monospace; font-size: 10pt;&quot;&gt;
&lt;div style=&quot;background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px;&quot;&gt;HomeController&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; System.Web.Mvc;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; MVCWebsite.Repository;&lt;br /&gt; &lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;namespace&lt;/span&gt; MVCWebsite.Controllers&lt;br /&gt; {&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;HomeController&lt;/span&gt;: &lt;span style=&quot;color: #2b91af;&quot;&gt;Controller&lt;/span&gt;, MVCWebsite.Controllers.&lt;span style=&quot;color: #2b91af;&quot;&gt;IHomeController&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #2b91af;&quot;&gt;ITitleRepository&lt;/span&gt; &lt;em&gt;titleRepository = &lt;span style=&quot;color: #0000ff;&quot;&gt;null&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; HomeController(&lt;span style=&quot;color: #2b91af;&quot;&gt;ITitleRepository&lt;/span&gt; titleRepository)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/em&gt;titleRepository = titleRepository;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;ActionResult&lt;/span&gt; Index()&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ViewBag.Title = &lt;em&gt;titleRepository.GetTitle(&lt;span style=&quot;color: #a31515;&quot;&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; View();&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; }&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that we created an interface, we will register it with our Unitycontainer. This mains i will have to adapt my UnityConfigurator class.&lt;/p&gt;
&lt;div id=&quot;scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4f7d99fc-db1a-4fa2-a274-d6e7989e4fe1&quot; class=&quot;wlWriterEditableSmartContent&quot; style=&quot;margin: 0px; display: inline; float: none; padding: 0px;&quot;&gt;
&lt;div style=&quot;border: #000080 1px solid; color: #000; font-family: &#39;Courier New&#39;, Courier, Monospace; font-size: 10pt;&quot;&gt;
&lt;div style=&quot;background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px;&quot;&gt;UnityConfigurator&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; System.Web.Mvc;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; Microsoft.Practices.Unity;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; MVCWebsite.Controllers;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; MVCWebsite.IoC;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; MVCWebsite.Repository;&lt;br /&gt; &lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;namespace&lt;/span&gt; MVCWebsite.Ioc&lt;br /&gt; {&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;UnityConfigurator&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;IUnityContainer&lt;/span&gt; GetContainer()&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #2b91af;&quot;&gt;IUnityContainer&lt;/span&gt; container = &lt;span style=&quot;color: #0000ff;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;UnityContainer&lt;/span&gt;();&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;container.RegisterType&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IControllerFactory&lt;/span&gt;, &lt;span style=&quot;color: #2b91af;&quot;&gt;UnityControllerFactory&lt;/span&gt;&amp;gt;();&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;container.RegisterType&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IHomeController&lt;/span&gt;, &lt;span style=&quot;color: #2b91af;&quot;&gt;HomeController&lt;/span&gt;&amp;gt;();&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;container.RegisterType&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;ITitleRepository&lt;/span&gt;, &lt;span style=&quot;color: #2b91af;&quot;&gt;TitleRepository2&lt;/span&gt;&amp;gt;(&lt;span style=&quot;color: #0000ff;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;HttpContextLifetimeManager&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;ITitleRepository&lt;/span&gt;&amp;gt;());&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; container;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; }&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When we type an url in our browser the ControllerFactory is used to return an instance of the requested controller. We already use a custom ControllerFactory ( UnityControllerFactory ) so we&amp;rsquo;ll need to change it&amp;rsquo;s implementation a bit.&lt;/p&gt;
&lt;p&gt;You can override a couple of methods of a ControllerFactory. But we are only interested in 2 of them:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;GetControllerType(System.Web.Routing.RequestContext requestContext, string controllerName)&lt;/li&gt;
&lt;li&gt;IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The GetControllerType is actually responsible for converting the controllername, that is typed in an url, to a specific type. So we will have to override this method so that we can write our custom logic.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;div id=&quot;scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:72d486e9-50d2-4b3c-943d-e0a814f390e0&quot; class=&quot;wlWriterEditableSmartContent&quot; style=&quot;margin: 0px; display: inline; float: none; padding: 0px;&quot;&gt;
&lt;div style=&quot;border: #000080 1px solid; color: #000; font-family: &#39;Courier New&#39;, Courier, Monospace; font-size: 10pt;&quot;&gt;
&lt;div style=&quot;background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px;&quot;&gt;UnityControllerFactory&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;Type&lt;/span&gt; GetControllerType(System.Web.Routing.&lt;span style=&quot;color: #2b91af;&quot;&gt;RequestContext&lt;/span&gt; requestContext, &lt;span style=&quot;color: #0000ff;&quot;&gt;string&lt;/span&gt; controllerName)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #008000;&quot;&gt;// use the base method&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #2b91af;&quot;&gt;Type&lt;/span&gt; type = &lt;span style=&quot;color: #0000ff;&quot;&gt;base&lt;/span&gt;.GetControllerType(requestContext, controllerName);&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #008000;&quot;&gt;// If the base method can&amp;#39;t resolve a controllername, we &amp;#39;ll have to help it a hand.&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: #0000ff;&quot;&gt;null&lt;/span&gt; == type)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #2b91af;&quot;&gt;Assembly&lt;/span&gt;[] assemblies = &lt;span style=&quot;color: #2b91af;&quot;&gt;AppDomain&lt;/span&gt;.CurrentDomain.GetAssemblies();&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;foreach&lt;/span&gt; (&lt;span style=&quot;color: #0000ff;&quot;&gt;var&lt;/span&gt; assembly &lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt; assemblies)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;type = assembly.GetTypes().SingleOrDefault(t =&amp;gt; t.Name == &lt;span style=&quot;color: #2b91af;&quot;&gt;String&lt;/span&gt;.Format(&lt;span style=&quot;color: #a31515;&quot;&gt;&amp;quot;{0}Controller&amp;quot;&lt;/span&gt;, controllerName));&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: #0000ff;&quot;&gt;null&lt;/span&gt; != type)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;break&lt;/span&gt;;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; type;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;If the base method can&amp;rsquo;t resolve the requested controller type, we&amp;rsquo;ll look in the current loaded assemblies to see if we can find the type of the requested controller. I still use the base method so that the default way of navigation still works. If you completely want to navigate through interfaces, you don&amp;rsquo;t make a call to it&amp;rsquo;s base method. So when we type /IHome/Index, this method will return IHomeController as type.&lt;/p&gt;
&lt;p&gt;Now that we have the requested controller type, we&amp;rsquo;ll need to create an instance of that controller. That&amp;rsquo;s why we&amp;rsquo;ll need to override the GetControllerInstance of our ControllerFactory ( UnityControllerFactory ).&lt;/p&gt;
&lt;p&gt;The current DependecyResolver will be used create the requested controller. Remember that the current DependencyResolver is our UnityDependencyResolver and that it will use Unity to create an instance of the requested controller type. When we have an instance of the controller, we&amp;rsquo;ll just need to change the &amp;ldquo;controller&amp;rdquo; value in the routedata. If we don&amp;rsquo;t to this, MVC will try to resolve the requested view in the IHome folder, instead of the Home folder.&lt;/p&gt;
&lt;div id=&quot;scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a88a3d0c-b403-46a0-b0b1-5ef9e4178b1d&quot; class=&quot;wlWriterEditableSmartContent&quot; style=&quot;margin: 0px; display: inline; float: none; padding: 0px;&quot;&gt;
&lt;div style=&quot;border: #000080 1px solid; color: #000; font-family: &#39;Courier New&#39;, Courier, Monospace; font-size: 10pt;&quot;&gt;
&lt;div style=&quot;background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px;&quot;&gt;UnityControllerFactory&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;IController&lt;/span&gt; GetControllerInstance(System.Web.Routing.&lt;span style=&quot;color: #2b91af;&quot;&gt;RequestContext&lt;/span&gt; requestContext, &lt;span style=&quot;color: #2b91af;&quot;&gt;Type&lt;/span&gt; controllerType)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #2b91af;&quot;&gt;IController&lt;/span&gt; result = &lt;span style=&quot;color: #0000ff;&quot;&gt;null&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: #0000ff;&quot;&gt;null&lt;/span&gt; != controllerType)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #008000;&quot;&gt;// resolve the requested controller type through the DependencyResolver&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;result = &lt;span style=&quot;color: #2b91af;&quot;&gt;DependencyResolver&lt;/span&gt;.Current.GetService(controllerType) &lt;span style=&quot;color: #0000ff;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;IController&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; (result != &lt;span style=&quot;color: #0000ff;&quot;&gt;null&lt;/span&gt;)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #008000;&quot;&gt;// change the route data so that we&amp;#39;ll look in the right place to resolve the requested view&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;requestContext.RouteData.Values[&lt;span style=&quot;color: #a31515;&quot;&gt;&amp;quot;controller&amp;quot;&lt;/span&gt;] = result.GetType().Name.Replace(&lt;span style=&quot;color: #a31515;&quot;&gt;&amp;quot;Controller&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #a31515;&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;);&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; result;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
&lt;h2&gt;Let&amp;rsquo;s test it&lt;/h2&gt;
&lt;p&gt;So when we type /IHome/Index, we&amp;rsquo;ll get the index page of the HomeController.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://lh3.ggpht.com/-u67JuSS0RI8/TmtCDzsg5LI/AAAAAAAAAFE/-EZtsut0nMc/s1600-h/image1%25255B1%25255D.png&quot;&gt;&amp;lt;img style=&amp;quot;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;&amp;quot; title=&amp;quot;image&amp;quot; src=&amp;quot;http://lh4.ggpht.com/-EOAey&lt;/em&gt;xvkhw/TmtCEd3zr5I/AAAAAAAAAFI/2Lizr5dVLEc/image&lt;em&gt;thumb.png?imgmax=800&amp;quot; alt=&amp;quot;image&amp;quot; width=&amp;quot;680&amp;quot; height=&amp;quot;174&amp;quot; border=&amp;quot;0&amp;quot; /&amp;gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
&lt;h2&gt;Let&amp;rsquo;s go a step further&lt;/h2&gt;
&lt;p&gt;It&amp;rsquo;s common for applications that for certain users you need to display a different page with more/less functionality. Let&amp;rsquo;s assume that users who are in the role &amp;rdquo;Admin&amp;rdquo; needs to see a different page as their home page. Since we removed the dependency between the url and controller we can easily achieve this functionality.&lt;/p&gt;
&lt;p&gt;We create a controller called &amp;#39;&amp;rdquo;AdminHomeController&amp;rdquo; which will implement the IHomeController interface. We also create an Index view for the AdminHomeController.&lt;/p&gt;
&lt;div id=&quot;scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:519b524f-c558-4cbd-850e-39a5cc6714ff&quot; class=&quot;wlWriterEditableSmartContent&quot; style=&quot;margin: 0px; display: inline; float: none; padding: 0px;&quot;&gt;
&lt;div style=&quot;border: #000080 1px solid; color: #000; font-family: &#39;Courier New&#39;, Courier, Monospace; font-size: 10pt;&quot;&gt;
&lt;div style=&quot;background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px;&quot;&gt;AdminHomeController&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; System.Web.Mvc;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; MVCWebsite.Repository;&lt;br /&gt; &lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;namespace&lt;/span&gt; MVCWebsite.Controllers&lt;br /&gt; {&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;AdminHomeController&lt;/span&gt; : &lt;span style=&quot;color: #2b91af;&quot;&gt;Controller&lt;/span&gt;, MVCWebsite.Controllers.&lt;span style=&quot;color: #2b91af;&quot;&gt;IHomeController&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #2b91af;&quot;&gt;ITitleRepository&lt;/span&gt; _titleRepository = &lt;span style=&quot;color: #0000ff;&quot;&gt;null&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; AdminHomeController(&lt;span style=&quot;color: #2b91af;&quot;&gt;ITitleRepository&lt;/span&gt; titleRepository)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/em&gt;titleRepository = titleRepository;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #008000;&quot;&gt;//&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #008000;&quot;&gt;// GET: /Home/&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;ActionResult&lt;/span&gt; Index()&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ViewBag.Title = &lt;em&gt;titleRepository.GetTitle(&lt;span style=&quot;color: #a31515;&quot;&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; View();&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; }&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we have to change our UnityConfigurator a little. In Unity is possible to create an &amp;ldquo;InjectionFactory&amp;rdquo;. You can link a function with an Injectionfactory. This means that the function will be responsible to return an instance of the requested type.&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;ll create an InjectionFactory for the IHomeController type. This injectionfactory will return an AdminHomeController for users who have an &amp;ldquo;Admin&amp;rdquo; role, for all other users a HomeController will be returned.&lt;/p&gt;
&lt;div id=&quot;scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:304c41c0-043d-412d-a2fd-e8fbf5471578&quot; class=&quot;wlWriterEditableSmartContent&quot; style=&quot;margin: 0px; display: inline; float: none; padding: 0px;&quot;&gt;
&lt;div style=&quot;border: #000080 1px solid; color: #000; font-family: &#39;Courier New&#39;, Courier, Monospace; font-size: 10pt;&quot;&gt;
&lt;div style=&quot;background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px;&quot;&gt;UnityConfigurator&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffff; max-height: 500px; overflow: auto; padding: 2px 5px; white-space: nowrap;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; System.Web;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; System.Web.Mvc;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; Microsoft.Practices.Unity;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; MVCWebsite.Controllers;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; MVCWebsite.IoC;&lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;using&lt;/span&gt; MVCWebsite.Repository;&lt;br /&gt; &lt;br /&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;namespace&lt;/span&gt; MVCWebsite.Ioc&lt;br /&gt; {&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;UnityConfigurator&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;IUnityContainer&lt;/span&gt; GetContainer()&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #2b91af;&quot;&gt;IUnityContainer&lt;/span&gt; container = &lt;span style=&quot;color: #0000ff;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;UnityContainer&lt;/span&gt;();&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;container.RegisterType&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IControllerFactory&lt;/span&gt;, &lt;span style=&quot;color: #2b91af;&quot;&gt;UnityControllerFactory&lt;/span&gt;&amp;gt;();&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;container.RegisterType&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;IHomeController&lt;/span&gt;&amp;gt;(&lt;span style=&quot;color: #0000ff;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;InjectionFactory&lt;/span&gt;(c =&amp;gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: #2b91af;&quot;&gt;HttpContext&lt;/span&gt;.Current.User.IsInRole(&lt;span style=&quot;color: #a31515;&quot;&gt;&amp;quot;Admin&amp;quot;&lt;/span&gt;))&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; (c.Resolve&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;AdminHomeController&lt;/span&gt;&amp;gt;());&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;else&lt;/span&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; (c.Resolve&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;HomeController&lt;/span&gt;&amp;gt;());&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}));&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;container.RegisterType&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;ITitleRepository&lt;/span&gt;, &lt;span style=&quot;color: #2b91af;&quot;&gt;TitleRepository2&lt;/span&gt;&amp;gt;(&lt;span style=&quot;color: #0000ff;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #2b91af;&quot;&gt;HttpContextLifetimeManager&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #2b91af;&quot;&gt;ITitleRepository&lt;/span&gt;&amp;gt;());&lt;br /&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; container;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt; }&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When a user who is in the &amp;ldquo;Admin&amp;rdquo; role types /IHome/Index as url, an instance of AdminHomeController will be returned and he&amp;rsquo;ll see the Index view from the AdminHome folder.&lt;/p&gt;
&lt;p&gt;&amp;lt;a href=&amp;quot;http://lh3.ggpht.com/-t8XLO6u1CJY/TmtCFM3jE2I/AAAAAAAAAFM/Dssi&lt;/em&gt;WTQgLw/s1600-h/image11.png&amp;quot;&amp;gt;&lt;img style=&quot;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;&quot; title=&quot;image&quot; src=&quot;http://lh6.ggpht.com/-h2LFpbGb0Ic/TmtCF5p3UPI/AAAAAAAAAFQ/OeUrV8QDuTc/image1_thumb.png?imgmax=800&quot; alt=&quot;image&quot; width=&quot;687&quot; height=&quot;165&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Hope you enjoyed reading it. The solution can be downloaded &lt;a href=&quot;http://dl.dropbox.com/u/41091233/Blog/Asp.Net%20MVC%203%20%26%20Unity%20Navigating%20through%20Interfaces/Asp.Net%20MVC%203%20%26%20Unity%20Navigating%20through%20Interfaces.rar&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/p&gt;
</description>
        <pubDate>Sat, 10 Sep 2011 08:55:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2011/09/10/asp-net-mvc-and-unity-navigating-through-interfaces</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2011/09/10/asp-net-mvc-and-unity-navigating-through-interfaces</guid>
        
        <category>ASP.NET</category>
        
        <category>MVC</category>
        
        <category>Unity</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>ASP.NET MVC: Where is the ClientID?</title>
        <description>&lt;p&gt;For those who are used to program in ASP.NET know this scenario. You have a textbox and you want to do some custom validation in Javascript. You have to be able to access the textbox from the Javascript. The first thing you need to know is the Id for that textbox. You can&amp;#39;t hard code it, because you are not sure how ASP.NET will generate the Id. If the control exists in an INamingContainer, the INamingContainer will add some prefix to the Id of the control.&lt;/p&gt;

&lt;p&gt;That is where the ClientID property comes to the rescue. This property contains the Id that will be used in HTML. Take a look at this example.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;err&quot;&gt;&amp;lt;&lt;/span&gt;%@ Page Title=&amp;quot;Home Page&amp;quot; Language=&amp;quot;C#&amp;quot; MasterPageFile=&amp;quot;~/Site.master&amp;quot; AutoEventWireup=&amp;quot;true&amp;quot;
    CodeBehind=&amp;quot;Default.aspx.cs&amp;quot; Inherits=&amp;quot;WebApplication1._Default&amp;quot; %&amp;gt;

&lt;span class=&quot;nt&quot;&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ID=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;HeaderContent&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;runat=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;HeadContent&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ID=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;BodyContent&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;runat=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;MainContent&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;language=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ButtonClicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;lt;%= MyTextBox.ClientID %&amp;gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Value: &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

   &lt;span class=&quot;nt&quot;&gt;&amp;lt;asp:TextBox&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ID=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;MyTextBox&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;runat=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript:ButtonClicked()&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Click Here&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example the Id of MyTextBox will be showed in a messagebox. Because MyTextBox is added in a Asp:Content control, the Id of this textbox will be prefixed with something. When we run the site and look at the source of the webpage, we will see it&amp;#39;s id.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;language=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ButtonClicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;MainContentMyTextBox&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Value: &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;    
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;   
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;ctl00$MainContent$MyTextBox&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;dfd&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;MainContentMyTextBox&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript:ButtonClicked()&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Click Here&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;But how do we do this in ASP.NET MVC?&lt;/h2&gt;

&lt;p&gt;Take a look at this example. We use the TextBoxFor extension method to generate a textbox for our model. How do we figure out the Id that is used for the textbox?&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;@model MvcApplication2.Models.Person
@{
    ViewBag.Title = &amp;quot;Home Page&amp;quot;;
}

&lt;span class=&quot;nt&quot;&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;@ViewBag.Message&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;language=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
       &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ButtonClicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;????&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
           &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Value: ??&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
           &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

   @Html.TextBoxFor(m =&amp;gt; m.Name);
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript:ButtonClicked()&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Click Here&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can hardcode the Id of the textbox ( &amp;quot;Name&amp;quot; ) in the javascript, but would it not be a lot more beautiful or easier ( intellisense) if you could pas a lambda expression?&lt;/p&gt;

&lt;p&gt;After looking in Reflector i saw that MVC uses this static class for generating an Id and it passes this Id to a static method of the TagBuilder class called &amp;quot;CreateSanitizedId&amp;quot;.&lt;/p&gt;

&lt;p&gt;Now that we know this, the only thing left to do is to create an HtmlHelper extension that will do exactly the same.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Web.Mvc.Html&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IdHtmlHelper&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetIdFor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HtmlHelper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;htmlHelper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TagBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CreateSanitizedId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ExpressionHelper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetExpressionText&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can write our code like this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;@model MvcApplication2.Models.Person
@{
    ViewBag.Title = &amp;quot;Home Page&amp;quot;;
  }

&lt;span class=&quot;nt&quot;&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;@ViewBag.Message&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;language=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
       &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ButtonClicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;@Html.GetIdFor( m=&amp;gt; m.Name)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
           &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Value: &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;        
       &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

  @Html.TextBoxFor( m =&amp;gt; m.Name)
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript:ButtonClicked()&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Click Here&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When we render the page and look at the source, we can see that the Id is put in the getElementById method.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;language=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
       &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ButtonClicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
           &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
           &lt;span class=&quot;nx&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Value: &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;        
       &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;John Doe&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class=&quot;nt&quot;&gt;&amp;lt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onclick=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;javascript:ButtonClicked()&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Click Here&lt;span class=&quot;nt&quot;&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
        <pubDate>Tue, 10 May 2011 14:36:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2011/05/10/aspnet-mvc-where-is-the-clientid</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2011/05/10/aspnet-mvc-where-is-the-clientid</guid>
        
        <category>ASP.NET</category>
        
        <category>MVC</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>ASP.NET MVC 3 and Unity using an IDependencyResolver</title>
        <description>&lt;p&gt;In this &lt;a href=&quot;http://blog.kennytordeur.be/post/2011/04/23/aspnet-mvc-and-unity&quot;&gt;post&lt;/a&gt; i used a custom ControllerFactory for dependency injection. I created a custom ControllerFactory where i overrided the GetControllerInstance method. In this method i used the UnityContainer for creating instances of the requested controller.&lt;/p&gt;

&lt;p&gt;In ASP.NET MVC 3 their is some build in abstraction for doing dependency injection (&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.web.mvc.idependencyresolver.aspx&quot;&gt;IDependencyResolver&lt;/a&gt;). I will change the example used in this &lt;a href=&quot;http://blog.kennytordeur.be/post/2011/04/23/aspnet-mvc-and-unity&quot;&gt;post&lt;/a&gt; to use the IDependencyResolver.&lt;/p&gt;

&lt;h2&gt;The IDependencyResolver&lt;/h2&gt;

&lt;p&gt;The IDependencyResolver is a fairly easy interface. It contains only 2 methods:&lt;/p&gt;
&lt;ul&gt;
* object GetService (Type serviceType)
  * The GetService method is used to resolve a single registered service
* IEnumerable&lt;object&gt; GetServices(Type serviceType)
  * The GetServices method resolves all registered services&lt;/p&gt;

&lt;p&gt;In our case, we will create an UnityDependencyResolver. This implementation will accept an IUnityContainer and will use this container for resolving the requested types. If the DependencyResolver is unable to resolve a type, it should always return null for the GetService method and an empty collection for the GetServices method. That is why a try catch is used in the UnityDependencyResolver.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UnityDependencyResolver&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDependencyResolver&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;IUnityContainer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;UnityDependencyResolver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IUnityContainer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;cp&quot;&gt;#region IDependencyResolver Members&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serviceType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serviceType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetServices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serviceType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ResolveAll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serviceType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;cp&quot;&gt;#endregion&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Registering the UnityDependencyResolver&lt;/h2&gt;

&lt;p&gt;Now that we created this dependency resolver, we have to register it so that the MVC framework will use it. This is done in the Global.asax, in the Application_Start method.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;  &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ApplicationStart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;AreaRegistration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisterAllAreas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

      &lt;span class=&quot;n&quot;&gt;RegisterGlobalFilters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GlobalFilters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;RegisterRoutes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RouteTable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

      &lt;span class=&quot;n&quot;&gt;DependencyResolver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SetResolver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UnityDependencyResolver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UnityConfigurator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()));&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You could also register it inside a WebActivator.PreApplicationStartMethod.&lt;/p&gt;

&lt;h2&gt;Changing the UnityControllerFactory&lt;/h2&gt;

&lt;p&gt;In the UnityControllerFactory we now can use the IDependencyResolver that we registered to resolve the requested controllers. It doesn&amp;#39;t need to work with an IUnityContainer any more.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UnityControllerFactory&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DefaultControllerFactory&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;        
        &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IController&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetControllerInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Routing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RequestContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requestContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controllerType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;IController&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controllerType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DependencyResolver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;controllerType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Changing the UnityConfigurator&lt;/h2&gt;

&lt;p&gt;As you might have noticed the Application_Start method of the Global.asax has changed a bit. The line for registering our UnityControllerFactory has been removed (see &lt;a href=&quot;http://blog.kennytordeur.be/post/2011/04/23/aspnet-mvc-and-unity&quot;&gt;this&lt;/a&gt;). The MVC 3 framework will use it&amp;#39;s current DependencyResolver to resolve a ControllerFactory, so we have to register our UnityControllerFactory with the IUnityContainer. Now the MVC 3 framework will use our UnityControllerFactory as it&amp;#39;s default ControllerFactory.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UnityConfigurator&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IUnityContainer&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;IUnityContainer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UnityContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisterType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IControllerFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UnityControllerFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisterType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TitleRepository2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HttpContextLifetimeManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;());&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
        <pubDate>Tue, 03 May 2011 14:08:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2011/05/03/aspnet-mvc-3-and-unity-using-an-idependencyresolver</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2011/05/03/aspnet-mvc-3-and-unity-using-an-idependencyresolver</guid>
        
        <category>ASP.NET</category>
        
        <category>MVC</category>
        
        <category>Unity</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>ASP.NET MVC and Unity</title>
        <description>&lt;h2&gt;What is Unity?&lt;/h2&gt;

&lt;p&gt;The Unity Application Block (Unity) is a lightweight extensible dependency injection container with support for constructor, property, and method call injection. Dependency injection is used to create loosely coupled applications. These kind of applications are easier to maintain and easier to test.&lt;/p&gt;

&lt;h2&gt;What do we want to achieve?&lt;/h2&gt;

&lt;p&gt;We want to make our controllers loosely coupled and break any hard dependencies it has. Every object/resource that our controller needs, has to be automatically populated and correctly initiated by Unity.&lt;/p&gt;

&lt;h2&gt;The Asp.net mvc application&lt;/h2&gt;

&lt;p&gt;This is the HomeController that we have in our application.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HomeController&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Controller&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_titleRepository&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;HomeController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_titleRepository&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TitleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// GET: /Home/&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ActionResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ViewBag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Title&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_titleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetTitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see, it uses a TitleRepository. This repository is used to determine which title has to be rendered on a specific page.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;titleRepository&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TitleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is the line that we want to remove. With this, we make a hard dependency between our HomeController and the TitleRepository.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetTitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TitleRepository&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MVCWebsite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Repository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetTitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot; This is a Title for the page: {0}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Using Unity&lt;/h2&gt;

&lt;h3&gt;Adding references&lt;/h3&gt;

&lt;p&gt;Add the Microsoft.Practices.Unity dll to you project. Unity can be found at http://unity.codeplex.com/.&lt;/p&gt;

&lt;h3&gt;Creating a custom LifetimeManager&lt;/h3&gt;

&lt;p&gt;When you register a type with Unity, you can also specify a lifetime of this type. If you don&amp;#39;t specify a lifetime, Unity will create a new instance of that type each time that the type is requested. In an Asp.Net application, we can make the LifetimeManager cache or store the created objects in the HttpContext HttpSession or HttpApplication. In my case, i want a singleton per request.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HttpContextLifetimeManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LifetimeManager&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SetValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;HttpContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AssemblyQualifiedName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HttpContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AssemblyQualifiedName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;RemoveValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;HttpContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Remove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AssemblyQualifiedName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;  
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Creating the UnityContainer&lt;/h3&gt;

&lt;p&gt;Next in line is the creation of a UnityContainer. This container will be used to resolve certain types. In order to work, we have to register these types. In our case, we our going to register the ITitleRepository type with the TitleRepository type. This will mean that if we ask the container to resolve the ITitleRepository, it will return a TitleRepository object. As you see, i pass the HttpContextLifetimeManager as parameter.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UnityConfigurator&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IUnityContainer&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;IUnityContainer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UnityContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisterType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TitleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HttpContextLifetimeManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;());&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we are registering our type hardcoded. It is also possible doing this in the config file.&lt;/p&gt;

&lt;h3&gt;Creating a Controller Factory&lt;/h3&gt;

&lt;p&gt;We need to create a custom Controller factory to replace the default factory that is used by the MVC framework. Our factory accepts a IUnityContainer as parameter. This container contains all the types we registered.&lt;/p&gt;

&lt;p&gt;We only need to override the GetControllerInstance method. In this method we have to use the container to create an instance of the requested controller. This way, Unity will automatically resolve the requested types for this controller. In our case: ITitleRepository.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UnityControllerFactory&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DefaultControllerFactory&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;IUnityContainer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;UnityControllerFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IUnityContainer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IController&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetControllerInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Routing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RequestContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requestContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controllerType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;IController&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;controllerType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;controllerType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we have to tell the MVC framework to use the UnityControllerFactory as it&amp;#39;s default controller factory. This is done best in the global.asax, in the Application_Start method.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ApplicationStart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;AreaRegistration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisterAllAreas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;RegisterGlobalFilters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GlobalFilters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;RegisterRoutes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RouteTable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ControllerBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SetControllerFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UnityControllerFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UnityConfigurator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Breaking the dependency&lt;/h2&gt;

&lt;p&gt;Now we have to modify our HomeController. We change it&amp;#39;s constructor to accept an object of the ITitleRepository type.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HomeController&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Controller&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_titleRepository&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;HomeController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;titleRepository&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// GET: /Home/&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ActionResult&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ViewBag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Title&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetTitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;The result&lt;/h2&gt;

&lt;p&gt;When we go to the index of the HomeController, our UnityControllerFactory comes in action and Untity will inject the TitleRepository in the constructor of the HomeController.&lt;/p&gt;

&lt;p&gt;When i want to use another implementation of the ITitleRepository, i only have to change the GetContainer method of the UnityConfigurator and the HomeController will automatically be injected with the new implementation.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TitleRepository2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MVCWebsite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Repository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetTitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;{0} is the Title for this page!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ToUpper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UnityConfigurator&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IUnityContainer&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;IUnityContainer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UnityContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisterType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TitleRepository2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HttpContextLifetimeManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITitleRepository&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;());&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
        <pubDate>Sat, 23 Apr 2011 07:40:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2011/04/23/aspnet-mvc-and-unity</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2011/04/23/aspnet-mvc-and-unity</guid>
        
        <category>ASP.NET</category>
        
        <category>MVC</category>
        
        <category>Unity</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>NHibernate in combination with a webservice</title>
        <description>&lt;p&gt;In this post i&amp;#39;ill try to explain how you can make NHibernate interact with a webservice to fill in a certain object.&lt;/p&gt;

&lt;h2&gt;Current situation&lt;/h2&gt;

&lt;p&gt;This is the current situation that we have.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2011-04-07-nhibernate-in-combination-with-a-webservice/situation.gif&quot; alt=&quot;Current situation&quot;&gt;&lt;/p&gt;

&lt;p&gt;We have a webservice called &amp;quot;Marial State service&amp;quot;. This service is responsible for all marial states available. This service has only 2 methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GetAll()

&lt;ul&gt;
&lt;li&gt;Gives all the available marial states&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;GetById(Int32 id)

&lt;ul&gt;
&lt;li&gt;Gives a specific marial state by it&amp;#39;s id&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The client is an application that has it&amp;#39;s own database and uses NHibernate as ORM. The client is responsible for creating/updating/deleting persons in his database. So in the database we find a table called &amp;quot;Person&amp;quot; with contains all the data.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2011-04-07-nhibernate-in-combination-with-a-webservice/database.gif&quot; alt=&quot;database schema&quot;&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the person, has a marial state which comes from the MarialState service.
For those who noticed, yes i am using an Access as database. &lt;a href=&quot;https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.JetDriver/&quot;&gt;Here&lt;/a&gt; you can find the needed DLL.&lt;/p&gt;

&lt;h2&gt;The MarialState service&lt;/h2&gt;

&lt;p&gt;I created 2 new projects in visual studio. One which contains the model off the service.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2011-04-07-nhibernate-in-combination-with-a-webservice/webservicemodel.gif&quot; alt=&quot;webservice model&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;NHibernateWebservices.WebServiceProxy.Model&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MarialState&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Description&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And another project for the service logic. As you can see this isn&amp;#39;t a real service. I am mocking it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2011-04-07-nhibernate-in-combination-with-a-webservice/webserviceproxy.gif&quot; alt=&quot;webservice model&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;NHibernateWebservices.WebServiceProxy&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MarialState&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetAll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
                                &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                                    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Description&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Single&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                                    &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Description&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Married&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                                    &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Description&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;widower&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                                &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetAll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SingleOrDefault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;The client&lt;/h2&gt;

&lt;p&gt;At client side the we created a business object called &amp;quot;Person&amp;quot; which will map at the Person table in it&amp;#39;s database.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FullName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MarialStateId&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is the mapping file for the &amp;quot;Person&amp;quot; entity.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xmlversion=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;hibernate-mappingxmlns&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;classname&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;NHibernateWebservices.Model.Person,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;NHibernateWebservices.Model&amp;quot;&lt;/span&gt;
         &lt;span class=&quot;na&quot;&gt;table=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Person&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;idname&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;column=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;unsaved-value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;generatorclass&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;increment&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;propertycolumn&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;FullName&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;FullName&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;String&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;propertyname&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;MarialStateId&amp;quot;&lt;/span&gt;
             &lt;span class=&quot;na&quot;&gt;column=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;MarialStateId&amp;quot;&lt;/span&gt;
             &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;int&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/class&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Now How to get the data?&lt;/h2&gt;

&lt;h3&gt;First try&lt;/h3&gt;

&lt;p&gt;The most easy way to get the MarialState entity would be to write something like this.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FullName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;      

    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialStateId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MarialStateId&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialStateId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;marialStateId&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;marialStateId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;marialStateId&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This solution will work, but I can see several things that I am not happy with. First of all it&amp;#39;s a lot off extra code. In this case the &amp;quot;Person&amp;quot; entity is very simple, but what if it was complex and you needed to get other data from a service, you would be writing a lot of code looking like this.&lt;/p&gt;

&lt;h3&gt;Second&lt;/h3&gt;

&lt;p&gt;In this approach you need to create a reference between the WebServiceProxy and the business Model of the client. At this point the business Model of the client is also responsible to fill it&amp;#39;s own data instead of only validating against business rules.&lt;/p&gt;

&lt;h4&gt;Using NHibernate IUserType&lt;/h4&gt;

&lt;p&gt;The ideal situation would that we ask NHibernate to give use a &amp;quot;Person&amp;quot; and that everything, including MarialState object, would be filled automatically. You can do this if you create a new NHibernate IUserType object. IUserType is an interface which allows you to plug in into the NHibernate mapping process and handle it your self.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MarialStateUserType&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IUserType&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Assemble&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cached&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;owner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DeepCopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cached&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DeepCopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Disassemble&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DeepCopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetHashCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetHashCode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsMutable&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NullSafeGet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDataReader&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;owner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NHibernateUtil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NullSafeGet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ToInt32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NullSafeSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDbCommand&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDataParameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DBNull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ReturnedType&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SqlType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SqlTypes&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SqlType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NHibernateUtil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SqlType&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;original&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;owner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;original&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReferenceEquals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This it the custom IUsertype that is created for the MarialState entity. In this example, these are the most interesting parts of code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ReturnedType&lt;/li&gt;
&lt;li&gt;SqlTypes&lt;/li&gt;
&lt;li&gt;NullSafeGet&lt;/li&gt;
&lt;li&gt;NullSafeSet&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;ReturnedType/SqlTypes properties&lt;/h5&gt;

&lt;p&gt;The ReturnedType and SqlTypes properties tells NHibernate which type to expect. The ReturnedType property contains the type of object that this IUsertype has to return. In our case it is a MarialState object.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ReturnedType&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The SqlTypes property tells how the object is mapped in the database. Here the MarialState object is mapped to a numerical column.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SqlType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SqlTypes&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SqlType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NHibernateUtil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SqlType&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h5&gt;NullSafeGet/NullSafeSet methods&lt;/h5&gt;

&lt;p&gt;The NullSafeSet method is called when we want to save/update our object. In our case, the parameter &amp;quot;value&amp;quot; will contain our MarialState entity. The parameter &amp;quot;cmd&amp;quot; will contain the command that is used to update/insert the &amp;quot;Person&amp;quot; entity in the database.&lt;/p&gt;

&lt;p&gt;What we now have to do, is to insert the Id of the MarialState entity in the command at the index place. We know the index because it is passed to the NullSafeSet method. If the MarialState entity is empty, we insert the DBNull value.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NullSafeSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDbCommand&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDataParameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DBNull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The NullSafeSet method is called when we want to extract the data from the database. This method will return a MarialState entity. As you can see, this method has a IDataReader object as parameter. From this IDataReader we have to extract the MarialStateId column and use it&amp;#39;s value to get the MarialState entity from the MarialState service. We also do a check to see if the value isn&amp;#39;t null because it could be &lt;em&gt;NULL&lt;/em&gt; in the database.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NullSafeGet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDataReader&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;owner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NHibernateUtil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NullSafeGet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ToInt32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marialState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h5&gt;Putting the pieces together&lt;/h5&gt;

&lt;p&gt;Now that everything is in place, we can start to glue it together. First of all, we can change the &amp;quot;Person&amp;quot; entity at the client. The property MarialStateId can now be replaced by the MarialState property. The class looks a lot cleaner this way.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FullName&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WebServiceProxy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MarialState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next in line is to change the mapping file for the &amp;quot;Person&amp;quot; entity. In this file we are going to make use of the MarialStateUserType that we created.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xmlversion=&amp;quot;1.0&amp;quot; ?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;hibernate-mappingxmlns&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;classname&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;NHibernateWebservices.Model.Person,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;NHibernateWebservices.Model&amp;quot;&lt;/span&gt;
         &lt;span class=&quot;na&quot;&gt;table=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Person&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;idname&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;column=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;unsaved-value=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;generatorclass&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;increment&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;propertycolumn&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;FullName&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;FullName&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;String&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class=&quot;nt&quot;&gt;&amp;lt;propertyname&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;MarialState&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;column=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;MarialStateId&amp;quot;&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;NHibernateWebservices.NHibernateUserType.MarialStateUserType, NHibernateWebservices&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/class&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we run the client and get a &amp;quot;Person&amp;quot; from the database, the MarialState will be filled automatically be NHibernate.&lt;/p&gt;

&lt;h3&gt;What about the performance?&lt;/h3&gt;

&lt;p&gt;If you have some performance problems, you can always but lazy loading on the MarialState property. This way we will only go to the service if we use the MarialState property.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;propertyname&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&amp;quot;MarialState&amp;quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;column=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;MarialStateId&amp;quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;lazy=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;NHibernateWebservices.NHibernateUserType.MarialStateUserType, NHibernateWebservices&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
        <pubDate>Thu, 07 Apr 2011 04:38:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2011/04/07/nhibernate-in-combination-with-a-webservice</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2011/04/07/nhibernate-in-combination-with-a-webservice</guid>
        
        <category>NHibernate</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
      <item>
        <title>WCF KnownType</title>
        <description>&lt;p&gt;In object-oriented programming the concept of polymorphism is frequently used. This can cause problems in a WCF service if no action is taken. This is where &lt;strong&gt;KnownType&lt;/strong&gt; comes to the rescue.&lt;/p&gt;

&lt;p&gt;This is the WCF service that we are going to use in this example.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [ServiceContract(&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;        Namespace=&amp;quot;http://localhost/WeatherForeCast&amp;quot;&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;        ,Name=&amp;quot;WeatherForeCastService&amp;quot;)]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IWeatherForeCastService&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;        [OperationContract]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;WeatherForeCast&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetWeatherForeCast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;day&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WeatherForeCastService&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IWeatherForeCastService&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WeatherForeCast&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetWeatherForeCast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;day&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WeatherForeCast&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Celsius&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Day&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;day&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This service has only 1 method. The GetWeatherForeCast method gives us a weather forecast for a specific day ( = parameter ).&lt;/p&gt;

&lt;p&gt;This is how the WeatherForeCast object looks like.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [DataContract]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WeatherForeCast&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;        [DataMember]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Celsius&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;        [DataMember]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Day&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Fore some reason, the service method must also return Farenheit as property on all the uneven days. So we decide to create a new class WeatherForeCastExtra that inherits from WeatherForeCast and add the extra property.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [DataContract]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WeatherForeCastExtra&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WeatherForeCast&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;        
&lt;span class=&quot;na&quot;&gt;        [DataMember]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Farenheit&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Off course we also have to modify our service so that it could return a WeatherForeCastExtra object on all the uneven days.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WeatherForeCastService&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IWeatherForeCastService&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WeatherForeCast&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetWeatherForeCast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;day&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;day&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Day&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WeatherForeCast&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Celsius&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Day&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;day&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WeatherForeCastExtra&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Celsius&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Farenheit&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;86&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Day&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;day&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we now create a client for this and ask for a forecast on a even day, it will work fine. If you ask for a forecast on an uneven day, you will get an error.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;    &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;WeatherForeCastClient&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Program&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WeatherForeCastService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WeatherForeCastServiceClient&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;WeatherForeCastService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WeatherForeCastServiceClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;WeatherForeCastService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WeatherForeCast&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weatherForeCast&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetWeatherForeCast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2011&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;weatherForeCast&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetWeatherForeCast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2011&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The problem is that the client doesn&amp;#39;t have any idea how the WeatherForeCastExtra object looks like or how it has to be deserialized. If we look at the WSDL our service generates, we can&amp;#39;t find a definition for the WeatherForeCastExtra type.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2011-03-24-wcf-knowntype/wsdl.png&quot; alt=&quot;wsdl&quot;&gt;&lt;/p&gt;

&lt;p&gt;It&amp;#39;s kind of logic that we can&amp;#39;t find a definition for the WeatherForeCastExtra type. If we look at our service interface, we see that the service returns the WeatherForeCast type. We have to find some way to tell the client that the service also returns something off the WeatherForeCastExtra type. This is were KnownType comes into action.&lt;/p&gt;

&lt;p&gt;The KnownType attribute lets you specify types that should be include during serialization/deserialization. We have to use this attribute in our base class ( WeatherForeCast ).&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [KnownType(typeof(WeatherForeCastExtra))]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;    [DataContract]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WeatherForeCast&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;        [DataMember]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Celsius&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sdssdsds&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;        [DataMember]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Day&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we now look at the generate WSDL file of our service, we will find a definition for our WeatherForeCastExtra type and the DataContractSerializer will know how the serialize or deserialize our WeatherForeCastExtra type.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.kennytordeur.be/images/2011-03-24-wcf-knowntype/wsdl2.png&quot; alt=&quot;wdsl&quot;&gt;&lt;/p&gt;

&lt;p&gt;If we run our client now, we won&amp;#39;t get an exception on uneven days.&lt;/p&gt;

&lt;p&gt;You can imagine that, if you have to include 10 types, your class would be decorated with 10 KnownType attributes and would be a little hard to read. This is why the KnownType attribute has another constructor. This constructor accepts a method name, as a string, of a static method that returns an array of types. These types will be include in the WDSL definition of the service.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;na&quot;&gt;    [KnownType(&amp;quot;GetKnowTypes&amp;quot;)]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;    [DataContract]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WeatherForeCast&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;        [DataMember]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Celsius&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;        [DataMember]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DateTime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Day&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetKnowTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WeatherForeCastExtra&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will return the same WSDL as before and our client will still work.&lt;/p&gt;
</description>
        <pubDate>Thu, 24 Mar 2011 05:14:00 +0000</pubDate>
        <link>http://blog.kennytordeur.be/post/2011/03/24/wcf-knowntype</link>
        <guid isPermaLink="true">http://blog.kennytordeur.be/post/2011/03/24/wcf-knowntype</guid>
        
        <category>WCF</category>
        
        
        <category>blog</category>
        
        <category>archives</category>
        
      </item>
    
  </channel>
</rss>
