开发者

What dependency Injection Framework with WebForms

开发者 https://www.devze.com 2023-01-27 09:06 出处:网络
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.

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 hav开发者_高级运维e 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消