It looks like one of the proposed new features of the next version of ASP.NET is a library or API simply titled Dependency Injection. In this post, I will provide feedback to the team on that particular
sub-project, in the form of an open blog post. The contents of this post
was originally posted on my blog.
Dependency Injection support
The details on the motivation for the Dependency Injection library are sparse, but I assume that the purpose is provide 'Dependency Injection support' to ASP.NET. If so, that motivation is laudable, because Dependency
Injection (DI) is the proper way to write loosely coupled code when using Object-Oriented Design.
Some parts of the ASP.NET family already have DI support; personally, I'm most familiar with ASP.NET MVC and ASP.NET Web API. Other parts have proven rather hostile towards DI - most notably ASP.NET Web Forms. The problem with Web Forms is that Constructor
Injection is impossible, because the Web Forms framework doesn't provide a hook for creating new Page objects.
My interpretation
As far as I can tell, the current ASP.NET Dependency Injection code defines an interface for creating objects:
In addition to this central interface, there are other interfaces that enable you to configure a 'service collection', and then there are Adapters for
Autofac
Ninject
StructureMap
Unity
Caste Windsor
As far as I can tell, there's no web code in the ASP.NET Dependency Injection code base. In other words, this is a poster example of a Conforming
Container.
My recommendations
It's an excellent idea to add 'Dependency Injection support' to ASP.NET, for the few places where it's not already present. However, as I've previously explained, a Conforming Container isn't the right solution. The right solution is to put
the necessary extensibility points into the framework:
ASP.NET MVC already has a good extensibility point in the IControllerFactory interface. I recommend keeping this interface, and other interfaces in MVC that play a similar role.
ASP.NET Web API already has a good extensibility point in the IHttpControllerActivator interface. I recommend keeping this interface, and other interfaces in Web API that play a similar role.
ASP.NET Web Forms have no extensibility point that enables you to create custom Page objects. I recommend adding an IPageFactory interface, as described in my article about DI-Friendly frameworks.
Other object types related to Web Forms, such as Object Data Sources, suffer from the same shortcoming, and should have similar factory interfaces.
There may be other parts of ASP.NET with which I'm not particularly familiar (SignalR), but they should all follow the same pattern of defining Abstract Factories for user classes, in the
cases where these don't already exist.
In addition to adding these required extensibility points, I recommend completely abandoning the project of defining a Conforming Container. The extensibility points should be added where they're used - the MVC Factories as part of MVC, the Web
Form Factories as part of Web Forms, etc. This will have the added benefit of making the ASP.NET Dependency Injection project redundant. Less code is better than more code.
These are my general recommendations to the team, but if desired, I'd also like to offer my assistance with any particular issues the team might encounter.
None
0 Points
17 Posts
Feedback on ASP.NET vNext Dependency Injection
May 26, 2014 04:28 PM|ploeh|LINK
It looks like one of the proposed new features of the next version of ASP.NET is a library or API simply titled Dependency Injection. In this post, I will provide feedback to the team on that particular sub-project, in the form of an open blog post. The contents of this post was originally posted on my blog.
Dependency Injection support
The details on the motivation for the Dependency Injection library are sparse, but I assume that the purpose is provide 'Dependency Injection support' to ASP.NET. If so, that motivation is laudable, because Dependency Injection (DI) is the proper way to write loosely coupled code when using Object-Oriented Design.
Some parts of the ASP.NET family already have DI support; personally, I'm most familiar with ASP.NET MVC and ASP.NET Web API. Other parts have proven rather hostile towards DI - most notably ASP.NET Web Forms. The problem with Web Forms is that Constructor Injection is impossible, because the Web Forms framework doesn't provide a hook for creating new Page objects.
My interpretation
As far as I can tell, the current ASP.NET Dependency Injection code defines an interface for creating objects:
In addition to this central interface, there are other interfaces that enable you to configure a 'service collection', and then there are Adapters for
As far as I can tell, there's no web code in the ASP.NET Dependency Injection code base. In other words, this is a poster example of a Conforming Container.
My recommendations
It's an excellent idea to add 'Dependency Injection support' to ASP.NET, for the few places where it's not already present. However, as I've previously explained, a Conforming Container isn't the right solution. The right solution is to put the necessary extensibility points into the framework:
In addition to adding these required extensibility points, I recommend completely abandoning the project of defining a Conforming Container. The extensibility points should be added where they're used - the MVC Factories as part of MVC, the Web Form Factories as part of Web Forms, etc. This will have the added benefit of making the ASP.NET Dependency Injection project redundant. Less code is better than more code.
These are my general recommendations to the team, but if desired, I'd also like to offer my assistance with any particular issues the team might encounter.
DI