{"id":23521,"date":"2018-06-05T18:13:17","date_gmt":"2018-06-05T18:13:17","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/?p=14085"},"modified":"2018-06-05T18:13:17","modified_gmt":"2018-06-05T18:13:17","slug":"use-dependency-injection-in-webforms-application","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2018\/06\/05\/use-dependency-injection-in-webforms-application\/","title":{"rendered":"Use Dependency Injection In WebForms Application"},"content":{"rendered":"<p><span><a href=\"https:\/\/en.wikipedia.org\/wiki\/Dependency_injection\">Dependency Injection<\/a><\/span> design pattern is widely used in modern applications. \u00a0It decouples objects to the extent that no client code needs to be changed simply because an object it depends changes to a different one. \u00a0It 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.\u00a0 in the past, it was very difficult to use Dependency Injection in WebForms application before.\u00a0 Starting from .NET 4.7.2, it is now easy for developers to use Dependency Injection in WebForms applications. \u00a0With the UnityAdapter, you can add it to your existing WebForms application in 4 simple steps.<\/p>\n<h2>How to enable Dependency Injection in your existing WebForms application<\/h2>\n<p>Suppose you have a movie website which lists most popular movies in history. \u00a0You use repository pattern to separate the logic that retrieves the data and maps it to the business entity. \u00a0Currently you are creating business logic object and repository object in default.aspx page. \u00a0The code looks like bellow.<\/p>\n<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>. \u00a0And you can use tag to retrieve the code change in each step.<\/p>\n<h4>1. Retarget the project to .NET Framework 4.7.2. (Git Tag: step-1)<\/h4>\n<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>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2018\/06\/step1-1.png\"><img loading=\"lazy\" decoding=\"async\" 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 loading=\"lazy\" decoding=\"async\" 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>\n<h4>2. Install AspNet.WebFormsDependencyInjection.Unity NuGet package. (Git Tag: step-2)<\/h4>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2018\/06\/step2-1.png\"><img loading=\"lazy\" decoding=\"async\" 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>\n<h4>3. Register types in Global.asax. (Git Tag: step-3)<\/h4>\n<h4>4. Refactor Default.aspx.cs. (Git Tag: step-4)<\/h4>\n<h2>Areas that Dependency Injection can be used<\/h2>\n<p>There are many areas you can use Dependency Injection in WebForms applications now. Here is a complete list.<\/p>\n<ul>\n<li>Pages and controls\n<ul>\n<li>WebForms page<\/li>\n<li>User control<\/li>\n<li>Custom control<\/li>\n<\/ul>\n<\/li>\n<li>IHttpHandler and IHttpHandlerFactory<\/li>\n<li>IHttpModule<\/li>\n<li>Providers\n<ul>\n<li>BuildProvider<\/li>\n<li>ResourceProviderFactory<\/li>\n<li>Health monitoring provider<\/li>\n<li>Any ProviderBase based provider created by System.Web.Configuration.ProvidersHelper.InstantiateProvider. e.g. custom sessionstate provider<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<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>\n","protected":false},"excerpt":{"rendered":"<p>Dependency Injection design pattern is widely used in modern applications. \u00a0It decouples objects to the extent that no client code needs to be changed simply because an object it depends changes to a different one. \u00a0It brings you a lot of benefits, like reduced dependency, more reusable code, more testable code, etc.\u00a0 in the past, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":23522,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[80,94,95,85],"class_list":["post-23521","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-webdev","tag-aspnet","tag-dependency-injection","tag-ioc","tag-webforms"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/23521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=23521"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/23521\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/23522"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=23521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=23521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=23521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}