<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="html">on code</title>
  <icon>http://suhair.in/Content/icons/flame.ico</icon>
  <logo>http://suhair.in/Content/icons/flame.png</logo>
  <updated>2010-01-06T08:00:00</updated>
  <id>http://suhair.in/atom</id>
  <link rel="alternate" type="text/html" hreflang="en" href="/atom"/>
  <link rel="self" type="application/atom+xml" href="http://suhair.in/ATOM"/>
  <generator uri="http://oxite.net" version="1.0">Oxite</generator>
  <entry>
    <title type="html">Dependency Injection in Asp.net MVC using MVC Contrib</title>
    <link rel="alternate" type="text/html" href="http://suhair.in/Blog/Dependency-Injection-in-Aspnet-MVC-using-MVC-Contrib"/>
    <id>http://suhair.in/Blog/Dependency-Injection-in-Aspnet-MVC-using-MVC-Contrib</id>
    <updated>2010-01-07T05:23:10.037</updated>
    <published>2010-01-06T08:00:00</published>
    <author>
      <name>Admin</name>
    </author>
    <category term="structuremap" />
    <category term="dependencyinjection" />
    <category term="csharp" />
    <category term="mvccontrib" />
    <category term="aspnet" />
    <category term="mvc" />
    <content type="html" xml:lang="en">
      &lt;p&gt;
	For the sake of creating the sample let&amp;#39;s introduce the dependency of a ILinkRepository in our brand new asp.net MVC application. Eventually this repository will be used by our HomeController to display a number of Links and finally we will inject the dependency by StructureMap dependency framework..&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
	&lt;img alt=&quot;Hosted by imgur.com&quot; src=&quot;http://imgur.com/ZAy6c.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	Next we will add the dependency of ILinkRepository on our HomeController. Since the default ControllerFactory cannot cope with controllers having no default constructor we will hard code the initialization of this dependency on the default constructor. Finally we will pass the list of links to the view and the view will display it for the user. This is the common scenario.&lt;/p&gt;
&lt;p&gt;
	&lt;img alt=&quot;Hosted by imgur.com&quot; src=&quot;http://imgur.com/A1Mze.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	Since view is strongly typed, it will just iterate over the collection producing a list of links. We are now done with the basic structure of the application. We have just used a type of dependency injection infamously known as Poor Man&amp;#39;s dependency injection since the HomeController constructor hard coded the creation of the repository.&lt;/p&gt;
&lt;p&gt;
	&lt;img alt=&quot;Hosted by imgur.com&quot; src=&quot;http://imgur.com/VRO0a.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	Now, it&amp;#39;s time to modify the controller for dependency injection. Since we are using dependency injection, controller is not concerned with creating the repository as in previous version. Dependency injection framework will automatically provide the dependent repository to the controller.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
	&lt;img alt=&quot;Hosted by imgur.com&quot; src=&quot;http://imgur.com/O1iki.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	Running the application now will definitely produce an error since the default controller factory cannot create controllers having no parameterless constructor as shown below.&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;
	&lt;span class=&quot;inlinecode&quot;&gt;&lt;i&gt;No parameterless constructor defined for this object.&lt;/i&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;
	Finally we are in a position to introduce the StructureMap dependency injection framework to the MVC application. It could be configured in the Global.asax.cs file as follows and the application will run as expected.&lt;/p&gt;
&lt;p&gt;
	&lt;img alt=&quot;Hosted by imgur.com&quot; src=&quot;http://imgur.com/8ISjh.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
	Source code could be downloaded from &lt;a href=&quot;http://github.com/suhair/DependencyInjection-Demo&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
	&amp;nbsp;&lt;/p&gt;
&lt;a rev=&quot;vote-for&quot; href=&quot;http://dotnetshoutout.com/Dependency-Injection-in-Aspnet-MVC-using-MVC-Contrib-on-code&quot;&gt;&lt;img alt=&quot;Shout it&quot; src=&quot;http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fsuhair.in%2FBlog%2FDependency-Injection-in-Aspnet-MVC-using-MVC-Contrib&quot; style=&quot;border:0px&quot;/&gt;&lt;/a&gt;
    </content>
  </entry>
  <entry>
    <title type="html">DataContract Serializer Essentials</title>
    <link rel="alternate" type="text/html" href="http://suhair.in/Blog/DataContract-Serializer-Essentials"/>
    <id>http://suhair.in/Blog/DataContract-Serializer-Essentials</id>
    <updated>2010-01-07T05:25:16.507</updated>
    <published>2010-01-05T08:00:00</published>
    <author>
      <name>Admin</name>
    </author>
    <category term="csharp" />
    <category term="DataContractSerializer" />
    <content type="html" xml:lang="en">
      &lt;p&gt;
	DataContractSerializer could be used to serialize and deserialize instances of a type into an XML stream or document. The first step in the process is to prepare the type for the serialization. In order to apply the DataContract attributes we have to add reference to the assembly&amp;nbsp;&lt;span class=&quot;inlinecode&quot;&gt;System.Runtime.Serialization.dll&lt;/span&gt;. Decorate the Type and the members for the Type to be serialized as shown below.&lt;/p&gt;
&lt;p&gt;
	&lt;img alt=&quot;Hosted by imgur.com&quot; src=&quot;http://imgur.com/BhrdP.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	In addition to the implicit serialization, an instance of DataContractSerializer can be used to explicitly serialize the object by using an appropriate object to write the format (an instance of XmlWriter, fro example). The details are illustrated in the following picture.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
	&lt;img alt=&quot;Hosted by imgur.com&quot; src=&quot;http://imgur.com/aWT6D.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	The resulting XML file could be customized by passing the properties such as Name and Namespace to the DataContract and DataMember attributes.&lt;/p&gt;
&lt;p&gt;
	&lt;img alt=&quot;Hosted by imgur.com&quot; src=&quot;http://imgur.com/O0A7O.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;
	&amp;nbsp;&lt;/p&gt;
&lt;a rev=&quot;vote-for&quot; href=&quot;http://dotnetshoutout.com/DataContract-Serializer-Essentials-on-code&quot;&gt;&lt;img alt=&quot;Shout it&quot; src=&quot;http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fwww.suhair.in%2FBlog%2FDataContract-Serializer-Essentials&quot; style=&quot;border:0px&quot;/&gt;&lt;/a&gt;
    </content>
  </entry>
  <entry>
    <title type="html">Getting Started with Clojure in .net</title>
    <link rel="alternate" type="text/html" href="http://suhair.in/Blog/Getting-Started-with-Clojure-in-net"/>
    <id>http://suhair.in/Blog/Getting-Started-with-Clojure-in-net</id>
    <updated>2009-11-05T05:49:47.357</updated>
    <published>2009-11-04T08:00:00</published>
    <author>
      <name>Admin</name>
    </author>
    <category term="net" />
    <category term="functionalprogramming" />
    <category term="clojure" />
    <category term="clojureclr" />
    <content type="html" xml:lang="en">
      &lt;p&gt;
	Before getting started here is the pre-requisites for installing ClojureCLR&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;
		Microsoft .&lt;span class=&quot;caps&quot;&gt;NET&lt;/span&gt; Framework Version 3.5 SP1&lt;/li&gt;
	&lt;li&gt;
		Visual Studio 2008 SP1&lt;/li&gt;
	&lt;li&gt;
		&lt;a href=&quot;http://dlr.codeplex.com/&quot; target=&quot;_blank&quot;&gt;Dynamic Language Runtime&lt;br /&gt;
		&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;
		&lt;a href=&quot;http://www.nunit.org/index.php?p=home&quot; target=&quot;_blank&quot;&gt;NUnit&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;
		&lt;a href=&quot;http://ayende.com/projects/rhino-mocks/downloads.aspx&quot; target=&quot;_blank&quot;&gt;RhinoMocks&lt;br /&gt;
		&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
	These are the steps followed by me for the new installation&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;
		Created a folder called ClojureCLR&lt;/li&gt;
	&lt;li&gt;
		Downloaded the Dynamic Language Runtime and extracted the DLR_Main folder into the above folder&lt;/li&gt;
	&lt;li&gt;
		Compiled the DLR by opening the visual studio solution file inside DLR_Main&lt;/li&gt;
	&lt;li&gt;
		Downloaded the &lt;a href=&quot;http://github.com/richhickey/clojure-clr/&quot; target=&quot;_blank&quot;&gt;ClojureCLR&lt;/a&gt; by using the git (Windows version can be downloaded &lt;a href=&quot;http://code.google.com/p/msysgit/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;)&amp;nbsp;and Opened the ClojureCLR.sln file&lt;/li&gt;
	&lt;li&gt;
		Added references for NUnit and Rhinomocks (with castle dependencies download from &lt;a href=&quot;http://ayende.com/20/section.aspx/download/227&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;)&amp;nbsp; Clojure.Tests project&lt;/li&gt;
	&lt;li&gt;
		Set Clojure.Main as startup project and then Crl + F5 or F5 will start the interactive Clojure section&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
	Clojure is dynamic i.e,&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;
		Clojure can be embedded in .net applications and can be interacted with&lt;/li&gt;
	&lt;li&gt;
		Can be used as a scripting language inside .net&lt;/li&gt;
	&lt;li&gt;
		Can be interacted with the primary programming interface called Read-Eval-Print (REPL) loop&lt;/li&gt;
	&lt;li&gt;
		Supports dynamic compilation&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
	Part of clojure called &lt;em&gt;Reader &lt;/em&gt;will read the clojure program in chunks called forms and translates them in to clojure data structures. when run for the first time, the clojure REPL will display the following prompt.&lt;/p&gt;
&lt;pre class=&quot;prettyprint paper&quot;&gt;&lt;code&gt;
Clojure 1.1.0-alpha-SNAPSHOT
user=&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;
	Prompt displays the default namespace and is ready to evaluate the clojure programs. Let&amp;#39;s feed it with a number.&lt;/p&gt;
&lt;pre class=&quot;prettyprint paper&quot;&gt;&lt;code&gt;
Clojure 1.1.0-alpha-SNAPSHOT
user=&amp;gt; 42 &lt;span class=&quot;inlinecode box&quot;&gt;1&lt;/span&gt;
42

user=&amp;gt; (+ 2 3)&lt;span class=&quot;inlinecode box&quot;&gt;2&lt;/span&gt;
5
&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;
	Number literal &lt;span class=&quot;inlinecode box&quot;&gt;1&lt;/span&gt; is a Form in clojure and evaluates to itself. Let&amp;#39;s feed the REPL with a function that calculates the sum of two numbers &lt;span class=&quot;inlinecode box&quot;&gt;2&lt;/span&gt;. Several deviations from the imperative programming languages are evident from this single expression.&lt;/p&gt;
&lt;p&gt;
	&lt;img align=&quot;top&quot; alt=&quot;&quot; height=&quot;211&quot; src=&quot;http://imgs.xkcd.com/comics/lisp_cycles.png&quot; width=&quot;640&quot; /&gt;&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;
		All program codes are written in parenthesized lists&lt;/li&gt;
	&lt;li&gt;
		A function call is written as a list with the function or operator&amp;#39;s name (+ in the above code) first, and the arguments following (2 and 3 in above code)&lt;/li&gt;
	&lt;li&gt;
		White space is the commas in Clojure (2 3 in the above code as arguments instead of 2, 3)&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;prettyprint paper&quot;&gt;&lt;code&gt;
Clojure 1.1.0-alpha-SNAPSHOT
user=&amp;gt; (+ 4 6) &lt;span class=&quot;inlinecode box&quot;&gt;3&lt;/span&gt;
10
user=&amp;gt; (- 5 4) &lt;span class=&quot;inlinecode box&quot;&gt;4&lt;/span&gt;
1
user=&amp;gt; (/ 2.0 3) &lt;span class=&quot;inlinecode box&quot;&gt;5&lt;/span&gt;
0.666666666666667
user=&amp;gt; (* 5 6) &lt;span class=&quot;inlinecode box&quot;&gt;6&lt;/span&gt;
30
user=&amp;gt; (inc 4) &lt;span class=&quot;inlinecode box&quot;&gt;7&lt;/span&gt;
5
user=&amp;gt; (dec 4) &lt;span class=&quot;inlinecode box&quot;&gt;8&lt;/span&gt;
3
user=&amp;gt; (quot 12 5) &lt;span class=&quot;inlinecode box&quot;&gt;9&lt;/span&gt;
2
user=&amp;gt; (rem 12 5) &lt;span class=&quot;inlinecode box&quot;&gt;10&lt;/span&gt;
2
user=&amp;gt; (min 2 3 4 1) &lt;span class=&quot;inlinecode box&quot;&gt;11&lt;/span&gt;
1
user=&amp;gt; (max 2 3 4 1) &lt;span class=&quot;inlinecode box&quot;&gt;12&lt;/span&gt;
4

&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;
	Add, Subtract, Multiply, Divide, Increment, Decrement, quot[ient] of dividing numerator by denominator, remainder of dividing numerator by denominator, least of the nums and the greatest of the nums are the computations supported by the clojure on Numbers. In addition to the bitwise operations the numbers support the following comparison operations.&lt;/p&gt;
&lt;pre class=&quot;prettyprint paper&quot;&gt;&lt;code&gt;
Clojure 1.1.0-alpha-SNAPSHOT
user=&amp;gt; (== 1 1)
True
user=&amp;gt; (&amp;lt; 1 2)
True
user=&amp;gt; (&amp;lt;= 3 4)
True
user=&amp;gt; (&amp;gt; 3 2)
True
user=&amp;gt; (&amp;gt;= 4 3)
True
user=&amp;gt; (zero? 4)
False
user=&amp;gt; (pos? -2)
False
user=&amp;gt; (neg? -2)
True

&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;
	Other data structures will be covered in future posts.&lt;/p&gt;
&lt;p&gt;
	&lt;a href=&quot;http://dotnetshoutout.com/Getting-Started-with-Clojure-in-net-on-code&quot; rev=&quot;vote-for&quot;&gt;&lt;img alt=&quot;Shout it&quot; src=&quot;http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fsuhair.in%2FBlog%2FGetting-Started-with-Clojure-in-net&quot; style=&quot;border: 0px none ;&quot; /&gt;&lt;/a&gt;
&amp;nbsp;
&lt;a href=&quot;http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsuhair.in%2fBlog%2fGetting-Started-with-Clojure-in-net&quot;&gt;&lt;img src=&quot;http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsuhair.in%2fBlog%2fGetting-Started-with-Clojure-in-net&quot; border=&quot;0&quot; alt=&quot;kick it on DotNetKicks.com&quot; /&gt;&lt;/a&gt;
&lt;/p&gt;
    </content>
  </entry>
  <entry>
    <title type="html">Asp.net MVC Areas in depth - Part 2</title>
    <link rel="alternate" type="text/html" href="http://suhair.in/Blog/Aspnet-MVC-Areas-in-depth-Part-2"/>
    <id>http://suhair.in/Blog/Aspnet-MVC-Areas-in-depth-Part-2</id>
    <updated>2009-10-12T08:42:07.097</updated>
    <published>2009-10-10T08:00:00</published>
    <author>
      <name>Admin</name>
    </author>
    <category term="csharp" />
    <category term="aspnet" />
    <category term="mvc" />
    <content type="html" xml:lang="en">
      &lt;p&gt;
	One of the first gotchas that will come across while using the asp.net MVC areas is in the usage of same controller name across different areas and the main project. For example the default asp.net MVC template has a &lt;span classs=&quot;inlinecode&quot;&gt;HomeController&lt;/span&gt; and if we add the &lt;span classs=&quot;inlinecode&quot;&gt;HomeController&lt;/span&gt; to one of the areas, accessing the home part of the areas will work fine but accessing the main home url will result in the following error&lt;/p&gt;
&lt;pre&gt;	&lt;i&gt;The controller name &amp;#39;home&amp;#39; is ambiguous between the following types: 
        AreasInDepth.Controllers.HomeController 
        AreasInDepth.Areas.Blog.Controllers.HomeController&lt;/i&gt;

&lt;/pre&gt;
&lt;p&gt;
	The easiest solution is to come up with unique controller names. Since the routing for the controllers in the areas works fine in this case and the only thing we have to fix is the main controllers we can use another concept to make this code work. The following code in Global.asax file is all that is required to fix this mess&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt; protected void Application_Start()
 {
      AreaRegistration.RegisterAllAreas();
      RegisterRoutes(RouteTable.Routes);
      ControllerBuilder.Current.DefaultNamespaces.Add(&amp;quot;AreasInDepth.Controllers&amp;quot;); &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt;
 }
&lt;/pre&gt;
&lt;p&gt;
	Simply, the above code instructs the &lt;span class=&quot;inlinecode&quot;&gt;DefaultControllerFactory&lt;/span&gt; to give priority for the controller inside the main project over those in areas and do not throw the previous &lt;span classs=&quot;inlinecode&quot;&gt;InvalidOperationException&lt;/span&gt; if the controller name is happened to be unique among these default name spaces &lt;span class=&quot;box&quot;&gt;1&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;
	It&amp;#39;s time to continue exploring the areas in asp.net MVC 2 preview 2&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;public Route MapRoute(string name, string url, object defaults, object constraints, string[] namespaces) 
{
    if (namespaces == null &amp;amp;&amp;amp; Namespaces != null) {
        namespaces = Namespaces.ToArray();
    }

    Route route = Routes.MapRoute(name, url, defaults, constraints, namespaces); &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt;
    route.DataTokens[&amp;quot;area&amp;quot;] = AreaName;&lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt;

    bool useNamespaceFallback = (namespaces == null || namespaces.Length == 0);
    route.DataTokens[&amp;quot;UseNamespaceFallback&amp;quot;] = useNamespaceFallback; &lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt;

    return route;
}

&lt;/pre&gt;
&lt;p&gt;
	&lt;span class=&quot;inlinecode&quot;&gt;MapRoute&lt;/span&gt; function of the &lt;span class=&quot;inlinecode&quot;&gt;AreaRegistrationContext&lt;/span&gt; class, discussed in the previous &lt;a href=&quot;http://suhair.in/Blog/aspnet-areas-in-depth&quot;&gt;post&lt;/a&gt;, creates and adds &lt;span class=&quot;box&quot;&gt;1&lt;/span&gt; a Route to the global static &lt;span class=&quot;inline&quot;&gt;RouteTable.Routes&lt;/span&gt;. It also places two data tokens namely area &lt;span class=&quot;box&quot;&gt;2&lt;/span&gt; and UseNamespaceFallback &lt;span class=&quot;box&quot;&gt;3&lt;/span&gt; which are later utilized in the route mapping workflow. If namespaces are not provided UseNameSpaceFallBack data token will have the value of true and the &lt;span classs=&quot;inlinecode&quot;&gt;DefaultControllerFactory&lt;/span&gt; will probe the rest of the namespaces to get a valid controller type. This is a useful hook to prevent the &lt;span classs=&quot;inlinecode&quot;&gt;DefaultControllerFactory&lt;/span&gt; from searching for controller in unintended areas. Rest of the magic happens during the request processing and we have to look the code inside &lt;span classs=&quot;inlinecode&quot;&gt;DefaultControllerFactory&lt;/span&gt;, and here it is&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;protected internal virtual Type GetControllerType(RequestContext requestContext, string controllerName) {
    if (String.IsNullOrEmpty(controllerName)) {
        throw new ArgumentException(MvcResources.Common_NullOrEmpty, &amp;quot;controllerName&amp;quot;);
    }

   
    object routeNamespacesObj;
    Type match;

     &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt;
    if (requestContext != null &amp;amp;&amp;amp; requestContext.RouteData.DataTokens.TryGetValue(&amp;quot;Namespaces&amp;quot;, out routeNamespacesObj)) 
    {
        IEnumerable&lt;string&gt; routeNamespaces = routeNamespacesObj as IEnumerable&lt;string&gt;;
        if (routeNamespaces != null) {
            HashSet&lt;string&gt; nsHash = new HashSet&lt;string&gt;(routeNamespaces, StringComparer.OrdinalIgnoreCase);
            match = GetControllerTypeWithinNamespaces(controllerName, nsHash);

            
            if (match != null || false.Equals(requestContext.RouteData.DataTokens[&amp;quot;UseNamespaceFallback&amp;quot;])) {
                
                return match; &lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt;
            }
        }
    }

    
    HashSet&lt;string&gt; nsDefaults = new HashSet&lt;string&gt;(ControllerBuilder.DefaultNamespaces, StringComparer.OrdinalIgnoreCase);
    match = GetControllerTypeWithinNamespaces(controllerName, nsDefaults); &lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt;
    if (match != null) {
        return match;
    }

    
    return GetControllerTypeWithinNamespaces(controllerName, null /* namespaces */); &lt;span class=&quot;box nocode&quot;&gt;4&lt;/span&gt;
}


&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;&lt;/pre&gt;
&lt;p&gt;
	&lt;span classs=&quot;inlinecode&quot;&gt;GetControllerType&lt;/span&gt;, first searches &lt;span class=&quot;box&quot;&gt;1&lt;/span&gt; the route namesapaces for the matching controller type. It will exit the search if a match is found or the namespace fallback option &lt;span class=&quot;box&quot;&gt;2&lt;/span&gt; is set to false. If the previous two conditions were not satisfied the function will search for the type in the application&amp;#39;s default namespaces collection &lt;span class=&quot;box&quot;&gt;3&lt;/span&gt;. This is the scenario explained at the beginning of this post. Finally there will be a fallback option to search all of the namespaces for valid controller type. &lt;span class=&quot;box&quot;&gt;4&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
	&amp;nbsp;&lt;/p&gt;
&lt;div class=&quot;shoutout&quot;&gt;
	&lt;a href=&quot;http://dotnetshoutout.com/Aspnet-MVC-Areas-in-depth-Part-2-on-code&quot; rev=&quot;vote-for&quot;&gt;&lt;img alt=&quot;Shout it&quot; src=&quot;http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fsuhair.in%2FBlog%2FAspnet-MVC-Areas-in-depth-Part-2&quot; style=&quot;border: 0px none ;&quot; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href=&quot;http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fsuhair.in%2fBlog%2fAspnet-MVC-Areas-in-depth-Part-2&quot;&gt;&lt;img alt=&quot;kick it on DotNetKicks.com&quot; border=&quot;0&quot; src=&quot;http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fsuhair.in%2fBlog%2fAspnet-MVC-Areas-in-depth-Part-2&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
    </content>
  </entry>
  <entry>
    <title type="html">Asp.net MVC Areas in depth</title>
    <link rel="alternate" type="text/html" href="http://suhair.in/Blog/aspnet-areas-in-depth"/>
    <id>http://suhair.in/Blog/aspnet-areas-in-depth</id>
    <updated>2009-10-04T12:10:05.617</updated>
    <published>2009-10-02T08:00:00</published>
    <author>
      <name>Admin</name>
    </author>
    <category term="csharp" />
    <category term="aspnet" />
    <category term="mvc" />
    <content type="html" xml:lang="en">
      &lt;pre class=&quot;prettyprint&quot;&gt;protected void Application_Start()
{
     AreaRegistration.RegisterAllAreas(); &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt;
     RegisterRoutes(RouteTable.Routes);
}

&lt;/pre&gt;
&lt;p&gt;
	In the &lt;span class=&quot;inlinecode&quot;&gt;Application_Start&lt;/span&gt; event, the Asp.net MVC application registers the areas by calling a static function on the &lt;span class=&quot;inlinecode&quot;&gt;AreaRegistration&lt;/span&gt; abstract class &lt;span class=&quot;box&quot;&gt;1&lt;/span&gt;.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;public static void RegisterAllAreas() 
{
      RegisterAllAreas(null);
}

public static void RegisterAllAreas(object state) 
{
                                 &lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt;                        &lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt;
      RegisterAllAreas(RouteTable.Routes, new BuildManagerWrapper(), state);
}
&lt;/pre&gt;
&lt;p&gt;
	&lt;span class=&quot;inlinecode&quot;&gt;RegisterAllAreas&lt;/span&gt; function was finally invoked with a &lt;span class=&quot;inlinecode&quot;&gt;RouteCollection&lt;/span&gt; &lt;span class=&quot;box&quot;&gt;2&lt;/span&gt; and an &lt;span class=&quot;inlinecode&quot;&gt;IBuildManager&lt;/span&gt; instance &lt;span class=&quot;box&quot;&gt;3&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;
	&lt;span class=&quot;inlinecode&quot;&gt;Buildmanager&lt;/span&gt; provides the methods to manage the Asp.net compilation. &lt;span class=&quot;inlinecode&quot;&gt;GetReferencedAssemblies&lt;/span&gt; method of the &lt;span class=&quot;inlinecode&quot;&gt;Buildmanager&lt;/span&gt; will return a collection of assemblies which includes those specified in assemblies element of web.config, assemblies built from App_Code folder, and assemblies from other top level folders.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;internal static void RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, object state) 
{
                                                                                               &lt;span class=&quot;box nocode&quot;&gt;4&lt;/span&gt;
        List&lt;type&gt; areaRegistrationTypes = TypeHelpers.FilterTypesInAssemblies(buildManager, IsAreaRegistrationType);
        foreach (Type areaRegistrationType in areaRegistrationTypes) 
        {
            AreaRegistration registration = (AreaRegistration)Activator.CreateInstance(areaRegistrationType);&lt;span class=&quot;box nocode&quot;&gt;5&lt;/span&gt;
            registration.CreateContextAndRegister(routes, state);
        }
}
&lt;/type&gt;&lt;/pre&gt;
&lt;p&gt;
	From the referenced assemblies, the application filters, using a predicate &lt;span class=&quot;box&quot;&gt;4&lt;/span&gt;, those of &lt;span class=&quot;inlinecode&quot;&gt;AreaRegistration&lt;/span&gt; Type and creates an instance for each item found &lt;span class=&quot;box&quot;&gt;5&lt;/span&gt;. In asp.net MVC preview 2 a class that is derived from &lt;span class=&quot;inlinecode&quot;&gt;AreaRegistration&lt;/span&gt; Type should be present in the corresponding area&amp;#39;s folder and the instances of these classes are the ones created in the above step.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;public class Routes: AreaRegistration &lt;span class=&quot;box nocode&quot;&gt;6&lt;/span&gt;
{
                                           &lt;span class=&quot;box nocode&quot;&gt;7&lt;/span&gt;
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(&amp;quot;blog_default&amp;quot;, &amp;quot;Blog/{controller}/{action}/{id}&amp;quot;,
                             new {controller = &amp;quot;Post&amp;quot;, action = &amp;quot;index&amp;quot;, id = &amp;quot;&amp;quot;});&lt;span class=&quot;box nocode&quot;&gt;8&lt;/span&gt;
    }

    public override string AreaName &lt;span class=&quot;box nocode&quot;&gt;5&lt;/span&gt;
    {
        get { return &amp;quot;blog&amp;quot;; }
    }
}
&lt;/pre&gt;
&lt;p&gt;
	&lt;span class=&quot;inlinecode&quot;&gt;CreateContextAndRegister&lt;/span&gt; method of &lt;span class=&quot;inlinecode&quot;&gt;AreaRegistration&lt;/span&gt; instance packages the &lt;span class=&quot;inlinecode&quot;&gt;RouteCollection&lt;/span&gt;, object state, and area name &lt;span class=&quot;box&quot;&gt;5&lt;/span&gt; (which is obtained from the user defined AreaRegistration derived class &lt;span class=&quot;box&quot;&gt;6&lt;/span&gt;) in to an &lt;span class=&quot;inlinecode&quot;&gt;AreaRegistrationContext&lt;/span&gt; and passes the context &lt;span class=&quot;box&quot;&gt;7&lt;/span&gt; to the &lt;span class=&quot;inlinecode&quot;&gt;RegisterArea&lt;/span&gt; method which is implemented in the user defined &lt;span&gt;AreaRegistration&lt;/span&gt; class. The custom defined AreaRegistration class then can add the required routes &lt;span class=&quot;box&quot;&gt;8&lt;/span&gt; to the RoutesCollection and the internals of these will be further explored in the next post.&lt;/p&gt;


&lt;a rev=&quot;vote-for&quot; href=&quot;http://dotnetshoutout.com/Aspnet-MVC-Areas-in-depth-on-code&quot;&gt;&lt;img alt=&quot;Shout it&quot; src=&quot;http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fsuhair.in%2FBlog%2Faspnet-areas-in-depth&quot; style=&quot;border:0px&quot;/&gt;&lt;/a&gt;
    </content>
  </entry>
  <entry>
    <title type="html">Sequential Workflow - Part 2</title>
    <link rel="alternate" type="text/html" href="http://suhair.in/Blog/sequential-workflow-part-2"/>
    <id>http://suhair.in/Blog/sequential-workflow-part-2</id>
    <updated>2009-09-28T11:29:29.82</updated>
    <published>2009-09-28T16:59:42</published>
    <author>
      <name>Admin</name>
    </author>
    <category term="csharp" />
    <category term="wf" />
    <content type="html" xml:lang="en">
      &lt;pre class=&quot;prettyprint&quot;&gt;partial class ATMSimulator
{
      #region Designer generated code

      /// &lt;summary&gt; 
      /// Required method for Designer support - do not modify 
      /// the contents of this method with the code editor.
      /// &lt;/summary&gt;
      [System.Diagnostics.DebuggerNonUserCode]
      private void InitializeComponent()
      {
          this.CanModifyActivities = true;
          System.Workflow.Activities.CodeCondition codecondition1 = new System.Workflow.Activities.CodeCondition();
          this.DispatchAmount = new System.Workflow.Activities.CodeActivity();
          this.GatherAmountInput = new System.Workflow.Activities.CodeActivity();
          this.CashWithdrawalSequence = new System.Workflow.Activities.SequenceActivity();
          this.WhileUserNeedsATMService = new System.Workflow.Activities.WhileActivity();
          // 
          // DispatchAmount
          // 
          this.DispatchAmount.Name = &amp;quot;DispatchAmount&amp;quot;;
          this.DispatchAmount.ExecuteCode += new System.EventHandler(this.DispatchAmount_ExecuteCode);
          // 
          // GatherAmountInput
          // 
          this.GatherAmountInput.Name = &amp;quot;GatherAmountInput&amp;quot;;
          this.GatherAmountInput.ExecuteCode += new System.EventHandler(this.GatherAmountInput_ExecuteCode);
          // 
          // CashWithdrawalSequence
          // 
          this.CashWithdrawalSequence.Activities.Add(this.GatherAmountInput); &lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt;
          this.CashWithdrawalSequence.Activities.Add(this.DispatchAmount);
          this.CashWithdrawalSequence.Name = &amp;quot;CashWithdrawalSequence&amp;quot;;
          // 
          // WhileUserNeedsATMService
          // 
          this.WhileUserNeedsATMService.Activities.Add(this.CashWithdrawalSequence);&lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt;
          codecondition1.Condition += new System.EventHandler&lt;system.workflow.activities.conditionaleventargs&gt;  ( his.CheckUserNeedsATMService);
          this.WhileUserNeedsATMService.Condition = codecondition1; &lt;span class=&quot;box nocode&quot;&gt;4&lt;/span&gt;
          this.WhileUserNeedsATMService.Name = &amp;quot;WhileUserNeedsATMService&amp;quot;;
          // 
          // ATMSimulator
          // 
          this.Activities.Add(this.WhileUserNeedsATMService);
          this.Name = &amp;quot;ATMSimulator&amp;quot;;
          this.CanModifyActivities = false;

      }

      #endregion

      private CodeActivity DispatchAmount;
      private SequenceActivity CashWithdrawalSequence;
      private WhileActivity WhileUserNeedsATMService; &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt;
      private CodeActivity GatherAmountInput;

}

&lt;/system.workflow.activities.conditionaleventargs&gt;&lt;/pre&gt;
&lt;p&gt;
	The major differnce from the Part 1 in this workflow is the inclusion of &lt;span class=&quot;inlinecode&quot;&gt;WhileActivity&lt;/span&gt; &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt; so that the user can make more than one transaction in one go. &lt;span class=&quot;inlinecode&quot;&gt;WhileActivity&lt;/span&gt; is a &lt;span class=&quot;inlinecode&quot;&gt;CompositeActivity&lt;/span&gt; so it can conatin other activities &lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt;.&amp;nbsp; Since &lt;span class=&quot;inlinecode&quot;&gt;WhileActivity&lt;/span&gt; can execute only single activity, the previous activities are packaged in to a single &lt;span class=&quot;inlinecode&quot;&gt;SequenceActivity&lt;/span&gt; &lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt; and given to the &lt;span class=&quot;inlinecode&quot;&gt;WhileActivity&lt;/span&gt; &lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt;. The &lt;span class=&quot;inlinecode&quot;&gt;Condition&lt;/span&gt; property of &lt;span class=&quot;inlinecode&quot;&gt;WhileActivity&lt;/span&gt;, which is set to an instance of &lt;span class=&quot;inlinecode&quot;&gt;CodeCondition&lt;/span&gt; &lt;span class=&quot;box nocode&quot;&gt;4&lt;/span&gt; in this code, determines whether the WhileActivity should proceed or halt.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;public sealed partial class ATMSimulator : SequentialWorkflowActivity
{
      private int amountToWithdraw = 0;
      private bool userNeedsService = true;

      public ATMSimulator()
      {
          InitializeComponent();
      }
      private void GatherAmountInput_ExecuteCode(object sender, EventArgs e) &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt;
      {
          Console.WriteLine(&amp;quot;please sepecify the amount to withdraw&amp;quot;);
          amountToWithdraw = int.Parse(Console.ReadLine());
      }

      private void DispatchAmount_ExecuteCode(object sender, EventArgs e) &lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt;
      {
          Console.WriteLine(String.Format(&amp;quot;Please collect {0:c} from the ATM&amp;quot;, amountToWithdraw));
          Console.WriteLine(&amp;quot;Enter Q to quit&amp;quot;);
          userNeedsService = !Console.ReadLine().Equals(&amp;quot;Q&amp;quot;, StringComparison.OrdinalIgnoreCase); &lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt;
       }

      private void CheckUserNeedsATMService(object sender, ConditionalEventArgs e) &lt;span class=&quot;box nocode&quot;&gt;4&lt;/span&gt;
      {
          e.Result = userNeedsService; &lt;span class=&quot;box nocode&quot;&gt;5&lt;/span&gt;
      }


}

&lt;/pre&gt;
&lt;p&gt;
	The sequence activities are executed &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt; in the first go and sets whether the user needs to do further transactions &lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt;. Based on the user&amp;#39;s preference, &lt;span class=&quot;inlinecode&quot;&gt;CodeCondition&lt;/span&gt; event handler &lt;span class=&quot;box nocode&quot;&gt;4&lt;/span&gt; sets the &lt;span class=&quot;inlinecode&quot;&gt;Result&lt;/span&gt; to a boolean value to signal whether the while loop should halt or proceed &lt;span class=&quot;box nocode&quot;&gt;5&lt;/span&gt;.&lt;/p&gt;
    </content>
  </entry>
  <entry>
    <title type="html">Sequential Workflow - Part 1</title>
    <link rel="alternate" type="text/html" href="http://suhair.in/Blog/sequential-workflow-part-1"/>
    <id>http://suhair.in/Blog/sequential-workflow-part-1</id>
    <updated>2009-09-28T12:32:21.18</updated>
    <published>2009-09-27T08:00:00</published>
    <author>
      <name>Admin</name>
    </author>
    <category term="csharp" />
    <category term="wf" />
    <content type="html" xml:lang="en">
      &lt;pre class=&quot;prettyprint&quot;&gt;public sealed partial class ATMSimulator : SequentialWorkflowActivity &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt;
{
      private int amountToWithdraw = 0;
      public ATMSimulator()
      {
          InitializeComponent();
      }

      private void GatherAmountInput_ExecuteCode(object sender, EventArgs e)&lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt;
      {
          Console.WriteLine(&amp;quot;please sepecify the amount to withdraw&amp;quot;);
          amountToWithdraw = int.Parse(Console.ReadLine());
      }

      private void DispatchAmount_ExecuteCode(object sender, EventArgs e)&lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt;
      {
          Console.WriteLine(String.Format(&amp;quot;Please collect {0:c} from the ATM&amp;quot;, amountToWithdraw));
      }

}
&lt;/pre&gt;
&lt;p&gt;
	Activities are the primary building blocks in Windows Workflow Foundation (WF) and the sequential workflow itself is an activity called &lt;span class=&quot;inlinecode&quot;&gt;SequentialWorkflowActivity&lt;/span&gt; &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt;. As the &lt;span class=&quot;inlinecode&quot;&gt;SequentialWorkflowActivity&lt;/span&gt; inherits from the &lt;span class=&quot;inlinecode&quot;&gt;CompositeActivity&lt;/span&gt;, it can execute the code in the child activities &lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt; in a forward-only direction.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;partial class ATMSimulator
{
      #region Designer generated code

      /// &lt;summary&gt; 
      /// Required method for Designer support - do not modify 
      /// the contents of this method with the code editor.
      /// &lt;/summary&gt;
      [System.Diagnostics.DebuggerNonUserCode]
      private void InitializeComponent()
      {
          this.CanModifyActivities = true;
          this.DispatchAmount = new System.Workflow.Activities.CodeActivity();
          this.GatherAmountInput = new System.Workflow.Activities.CodeActivity();
          // 
          // DispatchAmount
          // 
          this.DispatchAmount.Name = &amp;quot;DispatchAmount&amp;quot;;
          this.DispatchAmount.ExecuteCode += new System.EventHandler(this.DispatchAmount_ExecuteCode);
          // 
          // GatherAmountInput
          // 
          this.GatherAmountInput.Name = &amp;quot;GatherAmountInput&amp;quot;;
          this.GatherAmountInput.ExecuteCode += new System.EventHandler(this.GatherAmountInput_ExecuteCode);
          // 
          // ATMSimulator
          // 
          this.Activities.Add(this.GatherAmountInput);
          this.Activities.Add(this.DispatchAmount);
          this.Name = &amp;quot;ATMSimulator&amp;quot;;
          this.CanModifyActivities = false;

      }

      #endregion

      private CodeActivity DispatchAmount; &lt;span class=&quot;box nocode&quot;&gt;4&lt;/span&gt;
      private CodeActivity GatherAmountInput; &lt;span class=&quot;box nocode&quot;&gt;5&lt;/span&gt;

} 
&lt;/pre&gt;
&lt;p&gt;
	In the designer generated file of the simulator program there exists two code activities &lt;span class=&quot;box nocode&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;box nocode&quot;&gt;5&lt;/span&gt;. The primary goal of the code activities are to invoke the code-beside methods &lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt;. These methods will be used to examine and change the workflow instance states.&lt;/p&gt;
    </content>
  </entry>
  <entry>
    <title type="html">EntityConnection code walkthrough</title>
    <link rel="alternate" type="text/html" href="http://suhair.in/Blog/entity-connection-code-walkthrough"/>
    <id>http://suhair.in/Blog/entity-connection-code-walkthrough</id>
    <updated>2009-09-27T10:57:21.487</updated>
    <published>2009-09-24T08:00:00</published>
    <author>
      <name>Admin</name>
    </author>
    <category term="csharp" />
    <category term="EntityFramework" />
    <content type="html" xml:lang="en">
      &lt;pre class=&quot;prettyprint&quot;&gt;// Specify the provider name, server and database.
string providerName = &amp;quot;System.Data.SqlClient&amp;quot;;
string serverName = &amp;quot;.&amp;quot;;
string databaseName = &amp;quot;AdventureWorks&amp;quot;;

// Initialize the connection string builder for the
// underlying provider.
SqlConnectionStringBuilder sqlBuilder =
    new SqlConnectionStringBuilder(); &lt;span class=&quot;box nocode&quot;&gt;1&lt;/span&gt;

// Set the properties for the data source.
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
sqlBuilder.IntegratedSecurity = true;

// Build the SqlConnection connection string.
string providerString = sqlBuilder.ToString();&lt;span class=&quot;box nocode&quot;&gt;2&lt;/span&gt;

&lt;/pre&gt;
&lt;p&gt;
	SqlConnectionStringBuilder class &lt;span class=&quot;box&quot;&gt;1&lt;/span&gt; is used to create the Sql Server specific connection string &lt;span class=&quot;box&quot;&gt;2&lt;/span&gt;. Later this generated connection string will be passed to the EntityConnectionStringBuilder instance. This gives the following advantages&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;
		Strongly typed connection string properties&lt;/li&gt;
	&lt;li&gt;
		Support for synonyms&lt;/li&gt;
	&lt;li&gt;
		Security&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder =
    new EntityConnectionStringBuilder();

//Set the provider name.
entityBuilder.Provider = providerName;&lt;span class=&quot;box nocode&quot;&gt;3&lt;/span&gt;

// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;

// Set the Metadata location.
entityBuilder.Metadata = @&amp;quot;res://*/AdventureWorksModel.csdl|
                            res://*/AdventureWorksModel.ssdl|
                            res://*/AdventureWorksModel.msl&amp;quot;;&lt;span class=&quot;box nocode&quot;&gt;4&lt;/span&gt;
Console.WriteLine(entityBuilder.ToString());


&lt;/pre&gt;
&lt;p&gt;
	Unlike the SqlConnectionStringBuilder, EntityConnectionStringBuilder is vendor agnostic and hence we need to specify the provider name &lt;span class=&quot;box&quot;&gt;3&lt;/span&gt; before generating the Entity connection string. Metadata file locations &lt;span class=&quot;box&quot;&gt;4&lt;/span&gt; should also be specified before constructing the connection string.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;using (EntityConnection conn =
    new EntityConnection(entityBuilder.ToString()))
{
    conn.Open();
    Console.WriteLine(&amp;quot;Just testing the connection.&amp;quot;);
    conn.Close();
}

&lt;!-- pre--&gt;&lt;/pre&gt;
    </content>
  </entry>
</feed>
