Wednesday 8 November 2017

Dependency Injection in ASP.NET MVC using DependencyResolver and ControllerActivator

In my previous examples for dependency injection in ASP.NET MVC, I have used an approach where creating a custom controller factory deriving from DefaultControllerFactory and override the GetControllerInstance method for dependency injection. But ASP.NET MVC 3 is providing great support for dependency injection with very nice abstraction. In this post, I will demonstrate how to achieve dependency injection using two interfaces introduced in ASP.NET MVC 3 - IDependencyResolver and IControllerActivator. Unity 2.0 will be using as the dependency injection  container for this demo app.
Custom Controller Activator class using IControllerActivator
ASP.NET MVC 3 has introduced a new interface IControllerActivator which lets you activate controllers with custom behavior and can be use it for dependency  injection purpose.The IControllerActivator interface is discoverable using the dependency resolver. Let’s create a custom controller activator class by deriving from IControllerActivator intreface 
  1. public class CustomControllerActivator : IControllerActivator
  2.     {        
  3.         IController IControllerActivator.Create(
  4.             System.Web.Routing.RequestContext requestContext,
  5.             Type controllerType)
  6.         {
  7.             return DependencyResolver.Current
  8.                 .GetService(controllerType) as IController;
  9.         }      
  10.     }
 Custom Dependency Resolver class using IDependencyResolver
ASP.NET MVC 3 has introduced a new interface IDependencyResolver which exposes two methods - GetService and GetServices.The GetService method resolves singly registered services that support arbitrary object creation and the GetServices resolves multiply registered services. Implementations of the IDependencyResolver interface should delegate to the underlying dependency injection container to provide the registered service for the requested type. When there are no registered services of the requested type, the ASP.NET MVC framework expects implementations of this interface to return null from GetService and to return an empty collection from GetServices. Let’s create a custom dependency resolver class by deriving from IDependencyResolver intreface in order to working with Unity to providing dependency injection. 

  1. public class UnityDependencyResolver : IDependencyResolver
  2. {        
  3.     IUnityContainer container;       
  4.     public UnityDependencyResolver(IUnityContainer container)
  5.     {
  6.         this.container = container;
  7.     }
  8.  
  9.     public object GetService(Type serviceType)
  10.     {
  11.         try
  12.         {
  13.             return container.Resolve(serviceType);
  14.         }
  15.         catch
  16.         {               
  17.             return null;
  18.         }
  19.     }
  20.  
  21.     public IEnumerable<object> GetServices(Type serviceType)
  22.     {
  23.         try
  24.         {
  25.             return container.ResolveAll(serviceType);
  26.         }
  27.         catch
  28.         {                
  29.             return new List<object>();
  30.         }
  31.     }
  32. }
 Configure the Unity container with DependencyResolver.SetResolver Method
The below code in the Global.asax.cs configure the Unity container using SetResolver method of DependencyResolver class 
  1. protected void Application_Start()
  2. {
  3.     AreaRegistration.RegisterAllAreas();
  4.     RegisterGlobalFilters(GlobalFilters.Filters);
  5.     RegisterRoutes(RouteTable.Routes);
  6.     IUnityContainer container = GetUnityContainer();
  7.     DependencyResolver.SetResolver(new UnityDependencyResolver(container));
  8. }
The SetResolver method of DependencyResolver class provides a registration point for dependency injection containers. In this method, we configure the UnityDependencyResolver class for providing dependency injection with Unity 2.0. The SetResolver method will be working with any dependency injection container.If you want to use StructureMap  as the dependency injection container, you can create a dependency resolver class in order to working with StructureMap  by deriving IDependencyResolver intreface and later you can configure this class with SetResolver method. The ASP.NET MVC 3 is providing an excellent support for working  with dependency injection containers.
The GetUnityContainer method is shown below 
  1. private IUnityContainer GetUnityContainer()
  2.  {
  3.      //Create UnityContainer          
  4.      IUnityContainer container = new UnityContainer()
  5.      .RegisterType<IControllerActivatorCustomControllerActivator>()
  6.      .RegisterType<IFormsAuthenticationServiceFormsAuthenticationService>()
  7.      .RegisterType<IMembershipServiceAccountMembershipService>()
  8.      .RegisterInstance<MembershipProvider>(Membership.Provider)
  9.      .RegisterType<IDatabaseFactoryDatabaseFactory>(newHttpContextLifetimeManager<IDatabaseFactory>())
  10.      .RegisterType<IUnitOfWorkUnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>())
  11.      .RegisterType<ICategoryRepositoryCategoryRepository>(newHttpContextLifetimeManager<ICategoryRepository>())
  12.      .RegisterType<IExpenseRepositoryExpenseRepository>(newHttpContextLifetimeManager<IExpenseRepository>())
  13.      .RegisterType<ICategoryServiceCategoryService>(newHttpContextLifetimeManager<ICategoryService>())
  14.      .RegisterType<IExpenseServiceExpenseService>(new HttpContextLifetimeManager<IExpenseService>());
  15.      return container;         
  16.  }
 In the above GetUnityContainer method, I added the registration for IControllerActivator type into the Unity container along with other types. This will use concrete class CustomControllerActivator for the type IControllerActivator.
Source Code

You can download the source code from my demo app for ASP.NET MVC 3 and EF Code First at http://efmvc.codeplex.com/

No comments:

Post a Comment

Recent Post

Parallel Task in .Net 4.0