简体   繁体   中英

What dependency Injection Framework with WebForms

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM