MVC 3 introduces the IControllerActivator and IDependencyResolver interfaces. An example implementation can be found here which uses Unity. I've shown you the relevant parts below:
internal class UnityDependencyResolver : IDependencyResolver { private IUnityContainer _container = null; public UnityDependencyResolver(IUnityContainer container) { this._container = container; } public object GetService(Type serviceType) { try { return this._container.Resolve(serviceType); } catch { // Return null to indicate that we were unable to create an instance return null; } } public IEnumerable<object> GetServices(Type serviceType) { try { return this._container.ResolveAll(serviceType); } catch { // Return null to indicate that we were unable to create an instance return null; } } }Then you just have to register this custom dependency resolver when your application starts.
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); // Register DI container ConfigureContainer(); } private void ConfigureContainer() { IUnityContainer container = new UnityContainer(); // Register other services here // Set the current resolver DependencyResolver.SetResolver(new UnityDependencyResolver(container)); }Nothing ground breaking here. It's the simplicity of it that makes it nice. What I really like is that almost everything is loaded through the current dependency resolver. So what does the IControllerActivator have to do with it? Well, I'm not sure to be honest. If you put a break point in a custom resolver at line 14, you'll see the following things attempting to be loaded:
- IControllerFactory
- IControllerActivator
- HomeController (which controller you have set as your starting point)
- IFilterProvider
- IViewEngine
- IViewPageActivator
- ...
So in the end, if you're using a respectable DI framework, IDependencyResolver is all you need.
Tags: ASP.NET MVC
, IoC
Subscribe to:
Post Comments (Atom)
IDependencyResolver has a serious design flaw: No Release() method. See here:
ReplyDeletehttp://mikehadlow.blogspot.co.uk/2011/02/mvc-30-idependencyresolver-interface-is.html
Thanks for you comment. I read your blog posting on the issue and while I agree it's a shortfall, I would hardly classify it as a serious design flaw. There are too many workarounds for this to be considered serious.
ReplyDelete