Verify Unity Container For Resolving The Hierarchical Register Type

Unity Container is used in Web API/MVC with hierarchical solution architecture to achieve the dependency injection (IOC).
 
Let us suppose the solution architecture is as in the below image.
 
Verify Unity Container Resolving The Hierarchical Register Type
 
The solution contains different layers. In order to achieve the Dependency Injection, let us suppose we configure Unity Container by adding the reference using NuGet Package Manager.
 
Below is the configuration to map Interfaces with concrete Classes for Unity Container in WebAPIConfig.cs of API Solution.
  1. public static class WebApiConfig  
  2.     {  
  3.         public static void Register(HttpConfiguration config)  
  4.         {  
  5.             // Web API configuration and services  
  6.             // Configure Web API to use only bearer token authentication.  
  7.             config.SuppressDefaultHostAuthentication();  
  8.             config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));  
  9.   
  10.             RegisterInterfacesWithUnity(config);  
  11.             // Web API routes  
  12.             config.MapHttpAttributeRoutes();  
  13.   
  14.             config.Routes.MapHttpRoute(  
  15.                 name: "DefaultApi",  
  16.                 routeTemplate: "api/{controller}/{id}",  
  17.                 defaults: new { id = RouteParameter.Optional }  
  18.             );  
  19.         }  
  20.   
  21.         public static void RegisterInterfacesWithUnity(HttpConfiguration config)  
  22.         {  
  23.             // Web API configuration and services  
  24.             IUnityContainer container = new UnityContainer();  
  25.   
  26.             container.RegisterType<ICustomerManager, CustomerManager>(new HierarchicalLifetimeManager());  
  27.             container.RegisterType<ICustomerData, CustomerData>(new HierarchicalLifetimeManager());  
  28.   
  29.             config.DependencyResolver = new UnityResolver(container);  
  30.         }  
  31.     }  
Our Services communicate as Controller - BusinessLayer - DataAccessLayer.
 
In the above image, we register both the layer interfaces, like ICustomerManager, and ICustomerData. Suppose, if the developer missed adding any of the interfaces, then at runtime, we will get an exception saying "An error occurred when trying to create a controller of type 'CustomerController'. Make sure that the controller has a parameterless public constructor."
 
It is very difficult to understand what’s gone wrong. In order to solve this problem, let us use the test methods to verify whether all the hierarchy interfaces are getting resolved by the Unity Container.
 
The below snippet shows the test method to verify the container register resolve.
  1. [TestClass]  
  2.     public class VerifyContainerResolving  
  3.     {  
  4.         [TestMethod]  
  5.         public void TestMethod1()  
  6.         {  
  7.             IUnityContainer container = new UnityContainer();  
  8.   
  9.   
  10.             container.RegisterType<ICustomerManager, CustomerManager>(new HierarchicalLifetimeManager());  
  11.             container.RegisterType<ICustomerData, CustomerData>(new HierarchicalLifetimeManager());  
  12.   
  13.             /* Verification for container resolving*/  
  14.             container.AddExtension(new Diagnostic());  
  15.             foreach (var registration in container.Registrations)  
  16.             {  
  17.                 try  
  18.                 {  
  19.                     container.Resolve(registration.RegisteredType, registration.Name);  
  20.                 }  
  21.                 catch (ResolutionFailedException ex)  
  22.                 {  
  23.                     Assert.Fail(string.Concat(ex.Message, "\n", ex.StackTrace.ToString()));  
  24.                 }  
  25.             }  
  26.             /* Verification for container resolving*/  
  27.         }  
  28.     }  
Test method output result.
 
Verify Unity Container Resolving The Hierarchical Register Type
 
Let us comment any one of the registries. Then, you can get where exactly it is breaking. 
  1. [TestClass]  
  2.     public class VerifyContainerResolving  
  3.     {  
  4.         [TestMethod]  
  5.         public void TestMethod1()  
  6.         {  
  7.             IUnityContainer container = new UnityContainer();  
  8.   
  9.   
  10.             container.RegisterType<ICustomerManager, CustomerManager>(new HierarchicalLifetimeManager());  
  11.             //container.RegisterType<ICustomerData, CustomerData>(new HierarchicalLifetimeManager());  
  12.   
  13.             /* Verification for container resolving*/  
  14.             container.AddExtension(new Diagnostic());  
  15.             foreach (var registration in container.Registrations)  
  16.             {  
  17.                 try  
  18.                 {  
  19.                     container.Resolve(registration.RegisteredType, registration.Name);  
  20.                 }  
  21.                 catch (ResolutionFailedException ex)  
  22.                 {  
  23.                     Assert.Fail(string.Concat(ex.Message, "\n", ex.StackTrace.ToString()));  
  24.                 }  
  25.             }  
  26.             /* Verification for container resolving*/  
  27.         }  
  28.     }  

Test method results after commenting one registry map.

Verify Unity Container Resolving The Hierarchical Register Type
 
By using the above test method logic, we can easily find out the missing register of the interfaces while using Unity Container with WebAPI multilayered architecture. 
 
Please refer to the code sample attached to the articles. 


Similar Articles