Use Dependency Injection In WebForms Application
Dependency Injection 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 benefits, 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.
How to enable Dependency Injection in your existing WebForms application
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.
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 this Github repo. And you can use tag to retrieve the code change in each step.
1. Retarget the project to .NET Framework 4.7.2. (Git Tag: step-1)
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.
2. Install AspNet.WebFormsDependencyInjection.Unity NuGet package. (Git Tag: step-2)
3. Register types in Global.asax. (Git Tag: step-3)
4. Refactor Default.aspx.cs. (Git Tag: step-4)
Areas that Dependency Injection can be used
There are many areas you can use Dependency Injection in WebForms applications now. Here is a complete list.
- Pages and controls
- WebForms page
- User control
- Custom control
- IHttpHandler and IHttpHandlerFactory
- Health monitoring provider
- Any ProviderBase based provider created by System.Web.Configuration.ProvidersHelper.InstantiateProvider. e.g. custom sessionstate provider
Using Microsoft.AspNet.WebFormsDependencyInjection.Unity 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.