What is the dependency of Injection Framework with WebForms

I was looking at dependency injection training (I think I now understand the basic principles) and I want to implement it in a web application.

My question is, which dependency injection infrastructure should I use for the webforms project, or is it a question of what works best for you?

I am currently looking at Spring.Net, Ninject, Unity, and StructureMap; I have no preference in the configuration, be it XML or free interfaces. However, XML configuration is becoming less favorable?

Most of the information I come across is related to dependency injection in an MVC environment. And they also read that some structures, such as Structure Map, only work with web forms using version 2.0 or earlier. Thus, I need to consider whether web forms will be continuous support and ease of setup for someone relatively new to the template.

Thank you in advance.

+7
c # dependency-injection webforms
source share
1 answer

It doesn't really matter which structure you choose, the only trick is to allow classes, such as your System.Web.UI.Page classes, to inject their dependencies. When you look at ASP.NET MVC, you see that they specifically designed it to play well with dependency injection frameworks. ASP.NET WebForms is clearly not intended for this. Some frameworks have WebForms support out of the box, but for everyone else it is not so difficult to do.

In a WebForms application, the β€œthing” that creates pages for you is PageHandlerFactory . What you have to do is redefine the base PageHandlerFactory class, implement some kind of injection behavior in this type, and register this new type in the web.config file:

 <?xml version="1.0"?> <configuration> <system.web> <httpHandlers> <add verb="*" path="*.aspx" type="MyPageHandlerFactory, MyAsm"/> </httpHandlers> </system.web> <system.webServer> <handlers> <add name="CSLPageHandler" verb="*" path="*.aspx" type="MyPageHandlerFactory, MyAsm"/> </handlers> </system.webServer> </configuration> 

I wrote an article on how to create a PageHandlerFactory to work with the Common Service Locator , but you can choose your favorite IoC structure and change only one line of code to make it work.

Good luck.

+10
source share

All Articles