Tuesday, March 19, 2019

Install Unity in Web API

This blog discuss about the unity and its feature and it demonstrates how to install unity in web api project by using nugget package and explain about the type mapping registration

Unity Container :

The Unity is a full feature, a light weight, extensible dependency injection container, it helps to build the loosely coupled program and simplified object creation.

Unity Container Features:

1.       Abstraction the dependency of program and on demand it injects dependency.
2.       Simplified type-mapping registration for interface type or base type.
3.       Automatically injects registered type at run-time through constructor, property or method.
4.       Automatically manages the scope of instance and if it goes outside scope, it automatically dispose the instance.


Installation of Unity in Web API Project:

Here are steps to install unity package in our web API application.

1.       Open the nuget package manager



2.       Search ‘Unity’ package


3.       Click on install button to install unity package.

Unity.WebAPI package allows the simple Integration of the Unity IoC container with ASP.NET Web API. 

Here are steps to install unity.WebAPI package in our web api application.

1.       Open the nuget package manager
2.       Search ‘Unity.WebAPI’ package
3.       Click on install button to install unity.webapi package.


after two packages installations, there are few dll reference are added into project 



after unity.webapi package installation, the UnityConfig.cs class file is added under App_Start folder. 



In UnityConfig file, it contains the mapping for register types eg ( IOrderEngine ) and having information dependence resolver ( we are using Unity.WebAPI assembly as resolver)


C# Code :

public static class UnityConfig
    {
        public static void RegisterComponents()
        {
            var container = new UnityContainer();
           
            container.RegisterType<IOrderEngine, OrderEngine>();
           
            GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        }
    }


Add line in Global.asax.cs to configure unity container

C# Code :

public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            UnityConfig.RegisterComponents();  ß------------ Add line
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
    }



OrdersController is WEB API  Controller, which create and update the order information. You can see, the IOrderEngine type is being passed as parameter into API controller’s constructor and when API constructor is being called; it uses unity to resolve the IOrderEngine type.

C# Code :

    [RoutePrefix("api/Orders")]
    public class OrdersController : ApiController
    {
        IOrderEngine _orderEngine;

        public OrdersController(IOrderEngine orderEngine)
        {
            _orderEngine = orderEngine;
        }

        [HttpPost]
        [ApiVersion("1.0", Deprecated = true)]
        [ApiVersion("2.0")]
        [Route("v{version:apiVersion}/Create")]
        public IHttpActionResult Create(Order Order)
        {
            return Ok(_orderEngine.Create(Order));
        }

        [HttpPut]
        [ApiVersion("1.0", Deprecated = true)]
        [ApiVersion("2.0")]
        [Route("v{version:apiVersion}/Update")]
        public IHttpActionResult Update(Order Order)
        {
            return Ok(_orderEngine.Update(Order));
        }

    }

other dependency injection and unity container  related posts : 

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...