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.
Steven
source share