Friday, May 22, 2015

Inject Dependency in WebAPI using Unity IoC container

I had a misconception about Unity IoC container. I was trying to manage dependency of one Api Controller using Unity container, which was specific for MVC. So, In obvious reason It did not worked.

Then I checked in Google and realize that Unity has different package for  WebAPI. If you are interested to inject dependency in WebAPI controller , please use following package.

Install-Package Unity.AspNet.WebApi -Version 3.5.1404

It will scaffold necessary setting in your application. In this article we will see where to register your dependency in  for API controller.

Here is unity Activator class which will automatically generate for you.

public static class UnityWebApiActivator
    {
        /// <summary>Integrates Unity when the application starts.</summary>
        public static void Start()
        {
            var resolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
        }

        public static void Shutdown()
        {
            var container = UnityConfig.GetConfiguredContainer();
            container.Dispose();
        }
    }

The Start and ShutDown functions are to up and down container in application. Here is definition of UnityConfig class.

    public class UnityConfig
    {
        #region Unity Container
        private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
        {
            var container = new UnityContainer();
            RegisterTypes(container);
            return container;
        });
        /// <summary>
        /// Gets the configured Unity container.
        /// </summary>
        public static IUnityContainer GetConfiguredContainer()
        {
            return container.Value;
        }
        #endregion

        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
        public static void RegisterTypes(IUnityContainer container)
        {
            container.RegisterType<IService, Service>();
        }
    }

The RegisterType function is important one. Here we have to register our dependency. In this example we will implement IService interface in Service class and will inject to API Controller. For that, we are registering Dependency here.

Create controller to inject dependency.

public interface IService
    {
        void hello();
    }
    public class Service : IService
    {
        void IService.hello()
        {
            throw new NotImplementedException();
        }
    }

    public class DummyController : ApiController
    {
        IService obj;
        public DummyController(IService obj)
        {
            this.obj = obj;
        }
       
        public void Get()
        {

        }
    }

Once we invoke Dummy controller ,we will see that Dependency has injected to constructor.


No comments:

Post a Comment