In this blog, we will discuss
about the inversion of control (IoC) pattern and Dependency Injection and it
will demonstrates how to implement dependency injection in ASP.NET Web API by
using unity.
As Per Ioc Definition - a component should not depend directly upon
any component rather it depends upon abstract
Dependency Injection is one way
to implement of inversion of control (IoC) pattern and Dependency Injection
makes component very loosely couple and loosely coupled application are easier
to maintain and extend.
The
Unity Container (Unity) is a lightweight, extensible dependency injection
container. It facilitates building loosely coupled applications and provides
developers with the following advantages:
• Simplified object creation, especially for hierarchical
object structures and dependencies
• Abstraction of requirements; this allows developers
to specify dependencies at run time or in configuration and simplify
Basically in dependency Injection
process, the Unity Container is usually used to resolve the dependencies and
injects through constructor or property and we register the type in container
and retrieve the object related to type from constructor
Here is an example how to use the
unity container in ASP.NET Web API to resolve the controller or business object
dependency and inject dependency through constructor.
Here is a Web API Controller - TulipController which depends
on business object – TulipBusiness, to get tulip sales data.
public class TulipController : ApiController
{
TulipBusiness _tulip;
public TulipController()
{
tulip =new TulipBusiness();
}
public decimal GetYearSales(int year)
{
return _tulip.GetTulipSales(year);
}
}
First we need to create an
abstract type or interface of business object - TulipBusiness and then
we need to inject this abstract type into object.
public interface ITulipBusiness
{
decimal GetYearSales(int year);
}
public class TulipBusiness : ITulipBusiness
{
public decimal
GetYearSales(int year)
{
}
}
I modified above Web API
Controller, now Web API Controller depends on interface in place of a concrete
class
public class TulipController : ApiController
{
ITulipBusiness _tulip;
public TulipController(ITulipBusiness tulip)
{
_tulip =
tulip;
}
public decimal GetYearSales(int year)
{
return _tulip.GetTulipSales(year);
}
}
and we need to define the mapping
of dependency type ITulipBusiness and its class TulipBusiness in unity
Container class so whenever a dependency of type ITulipBusiness is
required an instance of TulipBusiness is created and is
injected into the dependent type by unit container.
public class UnityConfig
{
UnityContainer _container;
public UnityContainer CurrentContainer
{ get { return _container; } }
public UnityConfig()
{
_container = new UnityContainer();
_container.RegisterType<ITulipBusiness, TulipBusiness>();
}
}
Now whenever TulipController
object will be created, Unity will be automatically resolved dependencies and
inject object.
Here is an example how to
manually resolve the dependency by Unity Container:
Simply call the Resolve method of
unity container to create/instantiate the TulipController and its dependency
TulipBusines object,
var controller = _container.Resolve<TulipController>();
here before instantiate the
tulipController object, unity container resolve all dependencies behind scenes
and it first construct a TulipBusiness object and then passes it to thee
constructor of the TulipController class.
In same way you can create
instance of class which implemented specific Interface type by unit container.
var tulipBusiness = _container.Resolve<ITulipBusiness>();
There are other post related to
dependency injection and unity container
Thanks for Visiting!!
No comments:
Post a Comment