Sick Gaming
Use Dependency Injection In WebForms Application - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: C#, Visual Basic, & .Net Frameworks (https://www.sickgaming.net/forum-79.html)
+--- Thread: Use Dependency Injection In WebForms Application (/thread-85079.html)



Use Dependency Injection In WebForms Application - xSicKxBot - 06-05-2018

Use Dependency Injection In WebForms Application

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/use-dependency-injection-in-webforms-application.png" width="1024" height="576" title="" alt="" /></div><div><p><span><a href="https://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection</a></span> design pattern is widely used in modern applications.  It decouples objects to the extent that no client code needs to be changed simply because an object it depends changes to a different one.  It brings you a lot of <span><a href="http://tutorials.jenkov.com/dependency-injection/dependency-injection-benefits.html">benefits</a></span>, like reduced dependency, more reusable code, more testable code, etc.  in the past, it was very difficult to use Dependency Injection in WebForms application before.  Starting from .NET 4.7.2, it is now easy for developers to use Dependency Injection in WebForms applications.  With the UnityAdapter, you can add it to your existing WebForms application in 4 simple steps.</p>
<h2>How to enable Dependency Injection in your existing WebForms application</h2>
<p>Suppose you have a movie website which lists most popular movies in history.  You use repository pattern to separate the logic that retrieves the data and maps it to the business entity.  Currently you are creating business logic object and repository object in default.aspx page.  The code looks like bellow.</p>
<p>Now simply follow 4 steps below, you will be able to adopt Dependency Injection to decouple the MovieManager from default.aspx page. The sample web application is on <span><a href="https://github.com/Jinhuafei/examples/tree/master/DependencyInjection/PopularMovies">this Github repo</a></span>.  And you can use tag to retrieve the code change in each step.</p>
<h4>1. Retarget the project to .NET Framework 4.7.2. (Git Tag: step-1)</h4>
<p>Open project property and change the targetFramework of the project to .NET Framework 4.7.2. You would also need to change targetFramework in httpRuntime section in web.config file as illustrated below.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/06/step1-1.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/use-dependency-injection-in-webforms-application.png" alt="" class="aligncenter size-large wp-image-14095" width="879" height="494" /></a><a href="https://msdnshared.blob.core.windows.net/media/2018/06/step1-2.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/use-dependency-injection-in-webforms-application-1.png" alt="" class="aligncenter size-large wp-image-14105" width="879" height="494" /></a></p>
<h4>2. Install AspNet.WebFormsDependencyInjection.Unity NuGet package. (Git Tag: step-2)</h4>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/06/step2-1.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/use-dependency-injection-in-webforms-application-2.png" alt="" class="aligncenter size-large wp-image-14115" width="879" height="497" /></a></p>
<h4>3. Register types in Global.asax. (Git Tag: step-3)</h4>
<h4>4. Refactor Default.aspx.cs. (Git Tag: step-4)</h4>
<h2>Areas that Dependency Injection can be used</h2>
<p>There are many areas you can use Dependency Injection in WebForms applications now. Here is a complete list.</p>
<ul>
<li>Pages and controls
<ul>
<li>WebForms page</li>
<li>User control</li>
<li>Custom control</li>
</ul>
</li>
<li>IHttpHandler and IHttpHandlerFactory</li>
<li>IHttpModule</li>
<li>Providers
<ul>
<li>BuildProvider</li>
<li>ResourceProviderFactory</li>
<li>Health monitoring provider</li>
<li>Any ProviderBase based provider created by System.Web.Configuration.ProvidersHelper.InstantiateProvider. e.g. custom sessionstate provider</li>
</ul>
</li>
</ul>
<h2>Summary</h2>
<p>Using <a href="https://www.nuget.org/packages/Microsoft.AspNet.WebFormsDependencyInjection.Unity/">Microsoft.AspNet.WebFormsDependencyInjection.Unity</a> NuGet package on .net framework 4.7.2, Dependency Injection can be easily added into your existing WebForms application. Please give it a try and let us know your feedback.</p>
</div>