<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>mvccontrib - on code</title>
        <description></description>
        <link>/Tags/mvccontrib/RSS</link>
        <language>en</language>
        <image>
            <url>http://suhair.in/Content/icons/flame.png</url>
            <title>on code</title>
            <link>/Tags/mvccontrib/RSS</link>
            <width>64</width>
            <height>64</height>
        </image>
        <item>
            <dc:creator>Admin</dc:creator>
            <title>Dependency Injection in Asp.net MVC using MVC Contrib</title>
            <description>&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;</description>
            <link>http://suhair.in/Blog/Dependency-Injection-in-Aspnet-MVC-using-MVC-Contrib</link>
            <guid isPermaLink="true">http://suhair.in/Blog/Dependency-Injection-in-Aspnet-MVC-using-MVC-Contrib</guid>
            <pubDate>Wed, 06 Jan 2010 08:00:00 GMT</pubDate>
            <category>structuremap</category>
            <category>dependencyinjection</category>
            <category>csharp</category>
            <category>mvccontrib</category>
            <category>aspnet</category>
            <category>mvc</category>
        </item>
    </channel>
</rss>
