Web API Architecture And Dependency Injection Best Practices


This article explains the Web API best practices for architecture and Dependency Injection, using Unity and other options available for Dependency Injection.

From last few years, Web API is becoming very popular and these days, a lot of projects are going on with Web API. Thousands of projects have been developed using Web API.

If someone is working on Web API, then its architecture and best practices are the most important things, which enable the developer to create one of the best applications. There are more than a hundred rules and recommendations for Web API best practices but in this article, I am going to explain only about the architecture and Dependency Injection.

Web API Architecture

Hence, before discussing about Dependency Injection, it would be better to discuss Web API architecture best practices because Dependency Injection is based on the architecture. Here, I am going to refer to one of my previous articles.

It is recommended to go through my previous article because I will be using source code and concepts of my previous article. You can download the source from the attachment section of my previous article.

Step 1

Create Layered Architecture

Generally, Web API has 3 layers, which are given below.

  1. Web API Layer
  2. Business Layer (Business Logic Layer)
  3. Repository Layer (Data Access Layer)

You can see that Business Layer interfaces do not have any dependency on repository layer interfaces. Thus, the repository layer interface will be referenced only in the classes of business layer.


Step 2

Add POCO (Plain Old CLR Object).

Step 3

Add repository layer project to implement Repository layer interfaces and Business layer project to implement Business layer interfaces.

In the Solution Explorer, it will look, as shown below.


Now, I am going to implement Dependency Injection, using Unity. Right click on API project and open NuGet Package Manager and search for unity.


Install the two packages, as shown in the preceding screenshot.

After installing Unity at Web API layer, it will add some files and codes, which are shown below.

UnityConfig.cs 

  1. public static class UnityConfig {  
  2.     public static void RegisterComponents() {  
  3.         var container = new UnityContainer();  
  4.         GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);  
  5.     }  
  6. }   

UnityResolver.cs

  1. public class UnityResolver: IDependencyResolver  
  2. {  
  3.     protected IUnityContainer container;  
  4.     public UnityResolver(IUnityContainer container) {…………………………}  
  5.     public IDependencyScope BeginScope() {…………………………}  
  6.     public void Dispose() {…………………………}  
  7.     public object GetService(Type serviceType) {…………………………}  
  8.     public IEnumerable < object > GetServices(Type serviceType) {…………………………}  
  9. }  

Add the code given below to the file.

WebApiConfig.cs

  1. public static void Register(HttpConfiguration config)  
  2. {  
  3.     var container = new UnityContainer();  
  4.     container.RegisterType < IUserRepository, UserRepository > (new HierarchicalLifetimeManager());  
  5.     container.RegisterType < IUserBusiness, UserBusiness > (new HierarchicalLifetimeManager());  
  6.     container.RegisterType < IBaseRepository < User > , BaseRepository < User >> (new HierarchicalLifetimeManager());  
  7.     config.DependencyResolver = new UnityResolver(container);  
  8. }  

I have seen many people add unity at 2 layers i.e. at Business layer and API layer. Write the codes given below.

API Layer

  1. container.RegisterType<IUserBusiness, UserBusiness>(new HierarchicalLifetimeManager());  

Business Layer

  1. container.RegisterType<IUserRepository, UserRepository>(new HierarchicalLifetimeManager());container.RegisterType<IBaseRepository<User>, BaseRepository<User>>(new HierarchicalLifetimeManager());  

I do not endorse the preceding approach due to multiple reasons. Now, I am going to compare pros and cons of 2 approaches.

Approach 1

API Layer

  1. container.RegisterType<IUserBusiness, UserBusiness>(new HierarchicalLifetimeManager());container.RegisterType<IUserRepository, UserRepository>(new HierarchicalLifetimeManager());container.RegisterType<IBaseRepository<User>, BaseRepository<User>>(new HierarchicalLifetimeManager());  

Approach 2

API Layer

  1. container.RegisterType<IUserBusiness, UserBusiness>(new HierarchicalLifetimeManager());  

Business Layer

  1. container.RegisterType<IUserRepository, UserRepository>(new HierarchicalLifetimeManager());container.RegisterType<IBaseRepository<User>, BaseRepository<User>>(new HierarchicalLifetimeManager());  

The screenshot given below explains why the first approach is a better approach,

Injecting Dependencies without any third-party framework

It is not necessary to use any other framework for injecting the dependencies. We can inject dependencies without Unity or any other third party framework.

Go to the class startup.cs and inside the method “ConfigureServices(IServiceCollection services)”, add 2 lines of code given below. 

  1. services.AddTransient<IUserManager, UserManager>();  
  2. services.AddTransient<IUserRepository, UserRepository>();   

You can also inject those dependencies from web.config file or from appsettings.json files.


Similar Articles