RESTful Day #3: Resolve Dependency of Dependencies Using Inversion of Control & Dependency Injection in ASP.Net Web APIs with Unity Container and Managed Extensibility Framework (MEF)

In this article we’ll try to resolve the dependency (data model) of a dependency (services) from our existing solution.

Table of Contents

  • Introduction
  • Roadmap
  • Existing Design and Problem
  • Managed Extensibility Framework (MEF)
  • Creating a Dependency Resolver with Unity and MEF
  • Setup Business Services
  • Setup DataModel
  • Setup REST endpoint / WebAPI project
  • Running the application
  • Advantages of this design
  • Conclusion

Introduction

In my last two articles I explained how to create a RESTful service using ASP.Net Web API working with Entity Framework and resolving dependencies using Unity Container. This article explains how to create a loosely-coupled system with Unity Container and Managed Extensibility Framework (MEF) using Inversion of Control. I'll not be explaining much theory but rather focus more on practical implementations. For the readers who are following this series, they can use their existing solution that they have created previously. For my new readers of this article, I have provided the download link for the previous source code and current source code as well.

For theory and understanding of DI and IOC you can use the following links: Unity and Inversion of Control(IOC).

Roadmap



Here is my roadmap for learning RESTful APIs:

 

I'll intentionally use Visual Studio 2010 and .NET Framework 4.0 because there are a few implementations that are very hard to find in .NET Framework 4.0, but I'll make it easy by showing how to do it.

Existing Design and Problem

We already have an existing design. If you open the solution, you'll get to see the structure as shown below:



We tried to design a loosely-coupled architecture in the following way:

DataModel (responsible for communication with database): Only talks to the service layer.

Services (acting as a business logic layer between the REST endpoint and data access)

communicates between the REST endpoint and DataModel.

REST API, in other words Controllers: Only talks to services via the interfaces exposed.

But when we tried to resolve the dependency of UnitOfWork from Services, we had to reference the DataModel DLL in our WebAPI project. This violated our system as shown in the following image.



In this article we'll try to resolve the dependency (data model) of a dependency (services) from our existing solution. My controller depended on services and my services depended on the data model. Now we'll design an architecture in which components will be independent of each other in terms of object creation and instantiation. To do this we'll use the Managed Extensibility Framework (MEF) along with Unity Container and reflection.

Image source : http://o5.com/wp-content/uploads/2010/08/dreamstime_15583711-550x371.jpg

Ideally, we should not have the following code in our Bootstrapper class:
  1. container.RegisterType<IProductServices, ProductServices>().RegisterType<UnitOfWork>(new HierarchicalLifetimeManager());   
Managed Extensibility Framework (MEF)

You can have a read about Unity from the MSDN link. The following is a quote from the MSDN:

“The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications. It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well.”

“MEF is an integral part of the .NET Framework 4 and is available wherever the .NET Framework is used. You can use MEF in your client applications, whether they use Windows Forms, WPF, or any other technology, or in server applications that use ASP.NET.”

Creating a Dependency Resolver with Unity and MEF

Open your Visual Studio, I am using Visual Studio 2010, you can use Visual Studio version 2010 or above. Load the solution.

Step 1

Right-click the Solution Explorer and add a new project named Resolver as in the following:


I have intentionally chosen this name and you already know it why.

Step 2

Right-click the Resolver project and click on ManageNugetPackage, in the interface for adding the new package, search for Unity.MVC3 in the online library as in the following:



Install the package into your solution.

Step 3

Right-click the resolver project and add a reference for System.ComponentModel.Composition.
You can find the DLL in your GAC. I am using Framework 4.0, so referring to the same version DLL.



This DLL is part of MEF and is already installed with .NET Framework 4.0 in the system GAC. This DLL provides classes that are the very core of MEF.

Step 4

Just add an interface named IComponent to the Resolver project that contains the initialization method named Setup. We'll try to implement this interface in our Resolver class that we'll create in our other projects, like DataModel, Services and WebApI.
  1. namespace Resolver  
  2. {  
  3.     /// <summary>  
  4.     /// Register underlying types with unity.  
  5.    /// </summary>  
  6.    public interface IComponent  
  7.    {  
  8.    }  
  9. }  
Step 5

Before we declare our Setup method, just add one more interface responsible for serving as a contract to register types. I name this interface IRegisterComponent as in the following:
  1. namespace Resolver  
  2. {  
  3.     /// <summary>  
  4.     /// Responsible for registering types in unity configuration by implementing IComponent  
  5.     /// </summary>  
  6.     public interface IRegisterComponent  
  7.     {  
  8.         /// <summary>  
  9.         /// Register type method  
  10.         /// </summary>  
  11.         /// <typeparam name="TFrom"></typeparam>  
  12.         /// <typeparam name="TTo"></typeparam>  
  13.         /// <param name="withInterception"></param>  
  14.         void RegisterType<TFrom, TTo>(bool withInterception = false) where TTo : TFrom;  
  15.         /// <summary>  
  16.         /// Register type with container controlled life time manager  
  17.         /// </summary>  
  18.         /// <typeparam name="TFrom"></typeparam>  
  19.         /// <typeparam name="TTo"></typeparam>  
  20.         /// <param name="withInterception"></param>  
  21.         void RegisterTypeWithControlledLifeTime<TFrom, TTo>(bool withInterception = false) where TTo : TFrom;  
  22.     }  
  23. }  
In this interface I have declared two methods, one RegisterType and other into RegisterType with Controlled life time of the object, in other words the life time of an object will be hierarchal in manner. This is kind of the same as we do in Unity.

Step 6

Now declare a Setup method on our previously created IComponent interface, that takes an instance of IRegisterComponent as a parameter as in the following:

void SetUp(IRegisterComponent registerComponent);

So our IComponent interface becomes:
  1. namespace Resolver  
  2. {  
  3.     /// <summary>  
  4.     /// Register underlying types with unity.  
  5.     /// </summary>  
  6.     public interface IComponent  
  7.     {  
  8.         void SetUp(IRegisterComponent registerComponent);  
  9.     }  
  10. }  
Step 7

Now we'll write a packager, or you can say a wrapper, over MEF and Unity to register types/ components. This is the core MEF implementation. Create a class named ComponentLoader and add the following code to it:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Microsoft.Practices.Unity;  
  6. using System.ComponentModel.Composition.Hosting;  
  7. using System.ComponentModel.Composition.Primitives;  
  8. using System.Reflection;  
  9.   
  10. namespace Resolver  
  11. {  
  12.     public static class ComponentLoader  
  13.     {  
  14.         public static void LoadContainer(IUnityContainer container, string path, string pattern)  
  15.         {  
  16.             var dirCat = new DirectoryCatalog(path, pattern);  
  17.             var importDef = BuildImportDefinition();  
  18.             try  
  19.             {  
  20.                 using (var aggregateCatalog = new AggregateCatalog())  
  21.                 {  
  22.                     aggregateCatalog.Catalogs.Add(dirCat);  
  23.   
  24.                     using (var componsitionContainer = new CompositionContainer(aggregateCatalog))  
  25.                     {  
  26.                         IEnumerable<Export> exports = componsitionContainer.GetExports(importDef);  
  27.   
  28.                         IEnumerable<IComponent> modules =  
  29.                             exports.Select(export => export.Value as IComponent).Where(m => m != null);  
  30.   
  31.                         var registerComponent = new RegisterComponent(container);  
  32.                         foreach (IComponent module in modules)  
  33.                         {  
  34.                             module.SetUp(registerComponent);  
  35.                         }  
  36.                     }  
  37.                 }  
  38.             }  
  39.             catch (ReflectionTypeLoadException typeLoadException)  
  40.             {  
  41.                 var builder = new StringBuilder();  
  42.                 foreach (Exception loaderException in typeLoadException.LoaderExceptions)  
  43.                 {  
  44.                     builder.AppendFormat("{0}\n", loaderException.Message);  
  45.                 }  
  46.   
  47.                 throw new TypeLoadException(builder.ToString(), typeLoadException);  
  48.             }  
  49.         }  
  50.   
  51.         private static ImportDefinition BuildImportDefinition()  
  52.         {  
  53.             return new ImportDefinition(  
  54.                 def => truetypeof(IComponent).FullName, ImportCardinality.ZeroOrMore, falsefalse);  
  55.         }  
  56.     }  
  57.   
  58.     internal class RegisterComponent : IRegisterComponent  
  59.     {  
  60.         private readonly IUnityContainer _container;  
  61.   
  62.         public RegisterComponent(IUnityContainer container)  
  63.         {  
  64.             this._container = container;  
  65.             //Register interception behaviour if any  
  66.         }  
  67.   
  68.         public void RegisterType<TFrom, TTo>(bool withInterception = false) where TTo : TFrom  
  69.         {  
  70.             if (withInterception)  
  71.             {  
  72.                 //register with interception   
  73.             }  
  74.             else  
  75.             {  
  76.                 this._container.RegisterType<TFrom, TTo>();  
  77.             }  
  78.         }  
  79.   
  80.         public void RegisterTypeWithControlledLifeTime<TFrom, TTo>(bool withInterception = false) where TTo : TFrom  
  81.         {  
  82.             this._container.RegisterType<TFrom, TTo>(new ContainerControlledLifetimeManager());  
  83.         }  
  84.     }  
  85. }  
Step 8

Now our Resolver wrapper is ready. Build the project and add its reference to the DataModel, BusinessServices and WebApi projects like shown below.



Setup Business Services

We have already added a reference of Resolver in the BusinessServices project. We agreed to implement the IComponent interface in each of our projects.

So create a class named DependencyResolver and implement an IComponent interface into it. We use reflection too to import the IComponent type. So add a class and add the following code to that DependencyResolver class:
  1. using System.ComponentModel.Composition;  
  2. using DataModel;  
  3. using DataModel.UnitOfWork;  
  4. using Resolver;  
  5.   
  6. namespace BusinessServices  
  7. {  
  8.     [Export(typeof(IComponent))]  
  9.     public class DependencyResolver : IComponent  
  10.     {  
  11.         public void SetUp(IRegisterComponent registerComponent)  
  12.         {  
  13.             registerComponent.RegisterType<IProductServices, ProductServices>();  
  14.   
  15.         }  
  16.     }  
  17. }   


Note that we have implemented the SetUp method and in the same method we registered the type for my ProductService.

All of the existing codebase remains the same. We don't need to touch the IProductServices interface or ProductServices class.

Setup DataModel

We have added a Resolver project reference to the DataModel project as well. So we'll try to register the type of UnitOfWork in this project. We proceed in the same fashion, just add a DependencyResolver class and implement its Setup method to register a type of UnitOfWork. To make the code more readable and standard, I made a change. I just added an interface for UnitOfWork and named it IUnitOfWork. Now my UnitOfWork class derives from this, you can do this exercise in earlier versions of projects we discussed in the first two articles.

So my IUnitOfWork contains a declaration of a single public method in UnitOfWork as in the following:
  1. namespace DataModel.UnitOfWork  
  2. {  
  3.     public interface IUnitOfWork  
  4.     {  
  5.         /// <summary>  
  6.         /// Save method.  
  7.         /// </summary>  
  8.         void Save();  
  9.     }  
  10. }  
Now register the type for UnitOfWork in the DepenencyResolver class. Our class becomes as shown below:
  1. using System.ComponentModel.Composition;  
  2. using System.Data.Entity;  
  3. using DataModel.UnitOfWork;  
  4. using Resolver;  
  5.   
  6. namespace DataModel  
  7. {  
  8.     [Export(typeof(IComponent))]  
  9.     public class DependencyResolver : IComponent  
  10.     {  
  11.         public void SetUp(IRegisterComponent registerComponent)  
  12.         {  
  13.             registerComponent.RegisterType<IUnitOfWork,UnitOfWork.UnitOfWork>();  
  14.         }  
  15.     }  


Again, there is no need to touch any existing code of this project.

Setup REST endpoint / WebAPI project

90% of the job is done.

We now need to set up the WebAPI project.We'll not add any DependencyResolver class to this project. We'll invert the calling mechanism of layers in the Bootstrapper class that we already have, so when you open your bootstrapper class, you'll get the code, something like:
  1. using System.Web.Http;  
  2. using System.Web.Mvc;  
  3. using BusinessServices;  
  4. using DataModel.UnitOfWork;  
  5. using Microsoft.Practices.Unity;  
  6. using Unity.Mvc3;  
  7.   
  8. namespace WebApi  
  9. {  
  10.     public static class Bootstrapper  
  11.     {  
  12.         public static void Initialise()  
  13.         {  
  14.             var container = BuildUnityContainer();  
  15.   
  16.             DependencyResolver.SetResolver(new UnityDependencyResolver(container));  
  17.   
  18.             // register dependency resolver for WebAPI RC  
  19.             GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);  
  20.         }  
  21.   
  22.         private static IUnityContainer BuildUnityContainer()  
  23.         {  
  24.             var container = new UnityContainer();  
  25.   
  26.             // register all your components with the container here  
  27.             // it is NOT necessary to register your controllers  
  28.               
  29.             // e.g. container.RegisterType<ITestService, TestService>();          
  30.             container.RegisterType<IProductServices, ProductServices>().RegisterType<UnitOfWork>(new HierarchicalLifetimeManager());  
  31.   
  32.             return container;  
  33.         }  
  34.     }  
  35. }  
Now, we need to change the codebase a bit to make our system loosely-coupled. Just remove the reference of DataModel from the WebAPI project.
Let me revise what is the job of  "HierarchicalLifetimeManager",
 
"HierarchicalLifetimeManager" , for this lifetime manager, as for the ContainerControlledLifetimeManager, Unity returns the same instance of the registered type or object each time you call the Resolve or ResolveAll method or when the dependency mechanism injects instances into other classes. The distinction is that when there are child containers, each child resolves its own instance of the object and does not share one with the parent. When resolving in the parent, the behavior is like a container controlled lifetime; when resolving the parent and the child you have different instances with each acting as a container-controlled lifetime. If you have multiple children, each will resolve its own instance.

We don't want our DataModel to be exposed to the WebAPI project. That was our intent though, so we cut down the dependency of the DataModel project now.



Add the following code of the Bootstrapper class to the existing Bootstarpper class:
  1. using System.Web.Http;  
  2. //using DataModel.UnitOfWork;  
  3. using Microsoft.Practices.Unity;  
  4. using Resolver;  
  5. using Unity.Mvc3;  
  6.   
  7. namespace WebApi  
  8. {  
  9.     public static class Bootstrapper  
  10.     {  
  11.         public static void  Initialise()  
  12.         {  
  13.             var container = BuildUnityContainer();  
  14.   
  15.             System.Web.Mvc.DependencyResolver.SetResolver(new UnityDependencyResolver(container));  
  16.   
  17.             // register dependency resolver for WebAPI RC  
  18.             GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);  
  19.         }  
  20.   
  21.         private static IUnityContainer BuildUnityContainer()  
  22.         {  
  23.             var container = new UnityContainer();  
  24.   
  25.             // register all your components with the container here  
  26.             // it is NOT necessary to register your controllers  
  27.               
  28.             // e.g. container.RegisterType<ITestService, TestService>();          
  29.            // container.RegisterType<IProductServices, ProductServices>().RegisterType<UnitOfWork>(new HierarchicalLifetimeManager());  
  30.   
  31.             RegisterTypes(container);  
  32.   
  33.             return container;  
  34.         }  
  35.   
  36.         public static void RegisterTypes(IUnityContainer container)  
  37.         {  
  38.   
  39.             //Component initialization via MEF  
  40.             ComponentLoader.LoadContainer(container, ".\\bin""WebApi.dll");  
  41.             ComponentLoader.LoadContainer(container, ".\\bin""BusinessServices.dll");  
  42.   
  43.         }  
  44.     }  
  45. }  
It is kind of redefining the Bootstrapper class without touching our existing controller methods. We now don't even need to register the type for ProductServices as well, we already did this in the BusinessServices project.



Note that in the RegisterTypes method we load the components/DLLs using reflection using ComponentLoader.We wrote two lines, first to load the WebAPI.dll and another one to load Business Services.dll.

Had the name of the BusinessServicess.dll been WebAPI.Services.dll then we would have only written one line of code to load both the WebAPI and the BusinessService DLL as shown below.
  1. ComponentLoader.LoadContainer(container, ".\\bin""WebApi*.dll");  
Yes we can use a Regex.

Running the application

Just run the application. We get:



We already have our test client added, but for new readers, just go to Manage Nuget Packages by right-clicking the WebAPI project and type WebAPITestClient into the searchbox in online packages as in the following:



You'll get “A simple Test Client for ASP.NET Web API”, just add it. You'll get a help controller in Areas -> HelpPage as shown below.



I have already provided the database scripts and data in my previous article, you can use that.

Append “/help” to the application URL and you'll get the test client as in the following:



You can test each service by clicking on it.

Service for GetAllProduct:



To create a new product:



In the database, we get a new product as in the following:



Update product:



We get in the database:

Delete product:



In the database:



Advantages of this design

In my earlier articles I focussed more on design flaws, but our current design has emerged with a few added advantages.

We got an extensible and loosely-coupled application design that can go far with more new components added in the same way.

Registering types automatically using reflection. Assume we want to register any Interface implementation to our REST endpoint, we just need to load that DLL into our Bootstrapper class, or if the DLLs share a common suffix of the names then we just need to place that DLL in the bin folder and that will automatically be loaded at run time.


Image source: http://a.pragprog.com/magazines/2013-07/images/iStock_000021011143XSmall__1mfk9b__.jpg

Database transactions or any of such module is now not exposed to the service endpoint, this makes our service more secure and maintains the design structure too.

Conclusion

We now know how to use a Unity container to resolve dependency and perform inversion of control using MEF too. In my next article I'll try to explain how to open multiple endpoints to our REST service and create custom URLs in the true REST fashion in my WebAPI. Until then Happy Coding. You can also download the source code from GitHub. Add the required packages, if they are missing in the source code.

Read more:

For more technical articles you can reach out to CodeTeddy

My other series of articles: