I have been looking at learning dependency injections (i think i have now grasped the basics) and am looking to implement it into a webform application.
My question is, what dependency injection framework should i use for a webforms project, or is it a question of what works best for you?
I Have currently looked at Spring.Net, Ninject, Unity and StructureMap, i tend to have no preference in the configuration, whether its XML or fluent interfaces. However is XML configuration becoming less favourable?
Most of the information i come across relates to dependency injection whilst in a MVC environment. And have also read that some frameworks such as Structure Map only work with webforms using version 2.0 or earlier. So the kind of things i need to consider are whether webforms will be continuous support, and the ease of configuration for someone relatively new to the pattern.
Thankyou in advance.
ASP.NET Web Forms isn't supported in ASP.NET Core (nor are ASP.NET Web Pages). Typically, the functionality of these pages must be rewritten when porting to ASP.NET Core. There are, however, some strategies you can apply before or during such migration to help reduce the overall effort required.
ASP.NET Web Forms is a part of the ASP.NET web application framework and is included with Visual Studio. It is one of the four programming models you can use to create ASP.NET web applications, the others are ASP.NET MVC, ASP.NET Web Pages, and ASP.NET Single Page Applications.
Spring.NET is one of the popular open source frameworks for Dependency Injection. Spring.NET supports . NET 4.0, . NET Client Profile 3.5 and 4.0, Silverlight 4.0 and 5.0, and Windows Phone 7.0 and 7.1.
There are a lot of existing Web Forms applications out there still being supported, maintained and developed further, and a lot of developers are still working with Web Forms. But web development is moving on.
It doesn't really matter which framework you pick, the only trick is to allow classes such as your System.Web.UI.Page
classes to be injected with their dependencies. When you look at ASP.NET MVC you see that they specially designed it to play nice with dependency injection frameworks. ASP.NET WebForms clearly isn't designed for this. Some frameworks do have support for WebForms out of the box, but for all others, it isn't that hard to make this.
In a WebForms application, the 'thing' that creates pages for you is the PageHandlerFactory
. What you must do is override the PageHandlerFactory
base class, implement some injecting behavior in this type, and register this new type in the web.config:
<?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've written an article about how to create a PageHandlerFactory
to work with the Common Service Locator, but you can pick your favorite IoC framework and change just one line of code to get it to work.
Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With