About Hasmukh patel

My Photo
Harrow, London, United Kingdom
Dot-Net developer with expertise in Web, WPF, Win-form applications. Have worked on Asp.net,mvc , WPF and Win-forms projects in c#.net language having Sql-Server/Oracle as database with service oriented architecture using test driven development. Having complete knowledge of SDLC and have successfully worked and implemented it on projects.

Setting up Dependency Resolver with MVC 3 and 4


In order to setup DependencyResolver, you have to create a class which should implementation of IDependencyResolver Interface. First you need a Dependency Injection Containers (IOC) like Unity, Castle Windsor, Ninject, StructureMap, Spring.NET, Autofac, Puzzle.NFactory, PicoContainer.NET, LinFu  or you can create your own IOC based on .Net Reflection. 
 
Example of DependencyResolver with unity as below;
    public class UnityDependencyResolver : IDependencyResolver
    {
        private readonly IUnityContainer _unityContainer;
 
        private readonly IDependencyResolver _previousResolver;
 
        public UnityDependencyResolver(IUnityContainer container)
        {
            _unityContainer = container;
            _previousResolver = DependencyResolver.Current;
        }
        public object GetService(Type serviceType)
        {
            try
            {
                return _unityContainer.Resolve(serviceType);
            }
            catch (Exception)
            {
                return _previousResolver.GetService(serviceType);
            }
        }
 
        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _unityContainer.ResolveAll(serviceType);
        }
    }
 
    public class CustomControllerActivator : IControllerActivator
    {
        public IController Create(RequestContext requestContext, Type controllerType)
        {
            return DependencyResolver.Current.GetService(controllerType) as IController;
        }
 
    }
 
N.B. install Unity using Nuget Package manager by following command in PM command prompt;

To install Unity, run the following command in the Package Manager Console

PM> Install-Package Unity -Version 2.1.505.2

After creating the DependencyResolver class, need to setup Dependencies and its concrete class.
Create a static class with some static method which can be invoke from the application startup event. 
Example of bootstrapper class;
    public static class Bootstrapper
    {
        public static void Initialise()
        {
            var container = BuildUnityContainer();
 
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        }
 
        private static IUnityContainer BuildUnityContainer()
        {
            var container = new UnityContainer();
 
            // register all your components with the container here
            // it is NOT necessary to register your controllers
            
            // e.g. container.RegisterType<ITestService, TestService>();            
            container.RegisterType<IControllerActivatorCustomControllerActivator>();
            
            container.RegisterType<IUserServiceUserServiceClient>();
            container.RegisterType<IEmailServiceEmailServiceClient>();
            container.RegisterType<IAuthoServiceAuthoServiceClient>();
            return container;
        }
    }
 
Now invoke Initialise method from the application start event as below;
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        { // your other code
            Bootstrapper.Initialise();
        }
    }