Tuesday, September 30, 2014

MVC 4 : Dependency Injection

Dependency Injection
ASP.NET MVC has included a dependency resolver in earlier version, which dramatically improves the ability of an application to participate in dependency injection for both services consumed by MVC and commonly created classes like controllers and view pages.
The dependency injection (DI) pattern is another form of the inversion of control pattern, wherein there is no intermediary object like the service locator. Instead, components are written in a way that allows their dependencies to be stated explicitly, usually by way of constructor parameters or property setters.
Constructor Injection
The most common form of dependency injection is called constructor injection. This technique involves creating a constructor for your class that expresses all of its dependencies explicitly
Sample:
    public class NotificationSystem
    {
        private IMessagingService svc;

        public NotificationSystem(IMessagingService service)
        {
            this.svc = service;
        }
        public void InterestingEventHappened()
        {
            svc.SendMessage();
        }
    }
In this code, the first benefit is that the implementation of the constructor is dramatically simplified. The component is always expecting whoever creates it to pass the required dependencies. It only needs to store the instance of IMessagingService for later use. Another benefit is that you’ve reduced the number of things NotificationSystem needs to know about.

Property Injection
A less common form of dependency injection is called property injection. As the name implies, dependencies for a class are injected by setting public properties on the object rather than through the use of constructor parameters.
Sample:
    public class NotificationSystem
    {
        public IMessagingService MessagingService
        {
            get;
            set;
        }
        public void InterestingEventHappened()
        {
            MessagingService.SendMessage();
        }
    }
This code removes the constructor arguments (in fact, it removes the constructor entirely) and replaces it with a property. This class expects any consumers to provide required dependencies via properties rather than the constructor.

Dependency Resolution
Using a dependency injection container is one way to make the resolution of these dependencies simpler. A dependency injection container is a software library that acts as a factory for components, automatically inspecting and fulfi lling their dependency requirements. The consumption portion of the API for a dependency injection container looks a lot like a service locator because the primary action user ask it to perform is to provide user with some component, usually based on its type. The primary way that MVC talks to containers is through an interface created for MVC applications: IDependencyResolver.
The interface is defined as follows:
    public interface IDependencyResolver
    {
        object GetService(Type serviceType);
        IEnumerable<object> GetServices(Type serviceType);

    }
This interface is consumed by the MVC framework itself. If user want to register a dependency injection container (or a service locator, for that matter), need to provide an implementation of this interface. User can typically register an instance of the resolver inside your Global.asax file as below:
       DependencyResolver.Current = new MyDependencyResolver();

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.