пятница, 24 августа 2012 г.

Dependency Injection in ASP.NET MVC 4 and WebAPI using Unity

In this post, I will demonstrate how to use Dependency Injection in ASP.NET MVC 4 application and WebAPI using Unity. The new ASP.NET MVC 4 is a great framework for building web applications and WebAPI is a great for building HTTP services. I will use Unity IoC container to provide the integration with ASP.NET MVC 4 and WebAPI for applying dependency injection.

Using Unity in ASP.NET MVC 4


The following command in the Package Manager console will install Unity.Mvc3 package into your ASP.NET MVC 4 WebAPI application.

PM > Install-Package Unity.Mvc3

Unity.Mvc3 is a package that allows simple Integration of Microsoft's Unity IoC container with ASP.NET MVC 3. It also works great for ASP.NET MVC 4.

After installation of the package you will find the Bootstrapper.cs file in the root of your ASP.NET MVC 4 WebAPI project. The course code of that file is:

    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
            // e.g. container.RegisterType();            

            return container;
        }
    }

We will return to this class a little bit later.

To get started, we should add a call to Bootstrapper.Initialize() in the end of the Application_Start method of Global.asax.cs and the MVC framework will then use the Unity.Mvc3 DependencyResolver to resolve your components.

        protected void Application_Start()
        {
            // Default code of the method
            // . . .

            // Initialization of our Unity container
            Bootstrapper.Initialise();
        }


Using Unity in ASP.NET MVC 4 WebAPI


Unfortunately Unity.Mvc3 doesn't change the DependencyResolver for WebAPI. I hope it will be implemented in Unity.Mvc4.
However, there is the nuget package that has already implemented needed functionality - Unity.WebAPI.

The following command in the Package Manager console will install Unity.WebAPI package into your ASP.NET MVC 4 WebAPI application.

PM > Install-Package Unity.WebAPI

Unity.WebAPI is a library that allows simple Integration of Microsoft's Unity IoC container with ASP.NET's WebAPI. This project includes a bespoke DependencyResolver that creates a child container per HTTP request and disposes of all registered IDisposable instances at the end of the request.

After installation of the package you just have to add one line of code to your Bootstrapper class into the Initialise method:

        public static void Initialise()
        {
            var container = BuildUnityContainer();

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            // register dependency resolver for WebAPI RC
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
            // if you still use the beta version - change above line to:
            //GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new Unity.WebApi.UnityDependencyResolver(container));
        }

After that you are ready to create your business logic.

Create service


Let's start with the service class that we want to use in our containers.

First, we will create the interface:

    public interface IValuesService
    {
        int Create(string value);
        string Update(int id, string value);
        void Delete(int id);

        IDictionary GetAll();
        string Get(int id);
    }

And the service class which contains realization of this interface:

    public class ValuesService : IValuesService
    {
        private Dictionary _values;

        public ValuesService()
        {
            _values = new Dictionary
                          {
                              {1, "Value 1"},
                              {2, "Value 2"},
                              {3, "Value 3"},
                              {4, "Value 4"},
                              {5, "Value 5"},
                          };
        }

        private int GetNextId()
        {
            return _values.Keys.Max() + 1;
        }

        public int Create(string value)
        {
            var id = GetNextId();
            _values.Add(id, value);
            return id;
        }

        public string Update(int id, string value)
        {
            _values[id] = value;
            return Get(id);
        }

        public void Delete(int id)
        {
            _values.Remove(id);
        }

        public IDictionary GetAll()
        {
            return _values;
        }

        public string Get(int id)
        {
            return _values[id];
        }
    }


This is the really simple service with some predefined values in the constructor (just for this simple example). Of course in the real life your services will be much more complex.

Register service


The next step is register our service and interface in Unity container. This is very simple, you just have to add one line of code to the Bootstrapper class into the BuildUnityContainer method:

        private static IUnityContainer BuildUnityContainer()
        {
            var container = new UnityContainer();

            // register values service
            container.RegisterType();

            return container;
        }

As you can see it is really simple.
Also, if you read this method carefully you should see that Unity.Mvc3 registered all of our controllers by default in the Unity container. This is the line where it's happened:

            container.RegisterControllers();


Inject service to Controller


So, let's get our service in the home controller:

    public class HomeController : Controller
    {
        private readonly IValuesService _valuesService;

        public HomeController(IValuesService valuesService)
        {
            _valuesService = valuesService;
        }

        public ActionResult Values()
        {
            var viewModel = _valuesService.GetAll();

            return View(viewModel);
        }


        // old code
        public ActionResult Index()
        {
            return View();
        }
    }

I've just added the constructor which receive IValuesService as a parameters. And Unity will do all the "magic" of resolving out service interface for us.
Then. to test it, let's create a simple view named as the same as the action - Values.cshtml (tip: you can create a view just clicking the right mouse button on the action name, and selecting 'Add View...' in the context menu):

@model Dictionary

@{
    ViewBag.Title = "Values";
}

Values

    @foreach(var item in Model) {
  • ID: @item.Key, Value: @item.Value
  • }

Then, compile our solution and run it.
Then, navigate to http://localhost:/Home/Values. You should see the list of the five values with ID.
If you see it then injection for our Controllers works properly.

Inject service to ApiController


Next, let's inject our service to ApiControllers:

    public class ValuesController : ApiController
    {
        private readonly IValuesService _valuesService;
        
        public ValuesController(IValuesService valuesService)
        {
            _valuesService = valuesService;
        }

        // GET /api/values
        public IEnumerable> Get()
        {
            return _valuesService.GetAll();
        }

        // GET /api/values/5
        public string Get(int id)
        {
            return _valuesService.Get(id);
        }

        // POST /api/values
        public void Post(string value)
        {
            _valuesService.Create(value);
        }

        // PUT /api/values/5
        public void Put(int id, string value)
        {
            _valuesService.Update(id, value);
        }

        // DELETE /api/values/5
        public void Delete(int id)
        {
            _valuesService.Delete(id);
        }
    }

To test is everything fine with the ApiController, just compile the solution and run it. 
After that, open the IE developers tools and click the 'Start capturing' button in the Network tab.
Then, navigate to http://localhost:/api/values. 
You should see something like this in the Response body tab:

[{"key":1,"value":"Value 1"},{"key":2,"value":"Value 2"},{"key":3,"value":"Value 3"},{"key":4,"value":"Value 4"},{"key":5,"value":"Value 5"}]

So, for now all of your Controllers and ApiControllers can use your services.

That's all.

Комментариев нет: