What is IoC Container or DI Container

22 Aug 2022
Beginner
145K Views

The terms Dependency Injection (DI) & Inversion of Control (IoC) are generally used interchangeably to describe the same design pattern. Hence some people say IoC Container and some people say DI container but both terms indicate the same thing. So don't be confused by the terminology.

The IoC container that is also known as a DI Container is a framework for implementing automatic dependency injection very effectively. It manages the complete object creation and its lifetime, as well as it also injects the dependencies into the classes.

For more help about DI and IoC see Understanding Inversion of Control, Dependency Injection, and Service Locator, and Dependency Injection in ASP.NET MVC using Unity IoC Container.

What is DI Container

A DI Container is a framework to create dependencies and inject them automatically when required. It automatically creates objects based on the request and injects them when required. DI Container helps us to manage dependencies within the application in a simple and easy way.

The DI container creates an object of the defined class and also injects all the required dependencies as an object a constructor, a property, or a method that is triggered at runtime and disposes itself at the appropriate time. This process is completed so that we don't have to create and manage objects manually all the time.

We can also manage application dependencies without a DI Container, but it will be like as POOR MAN’S DI and we have to do more work, to make it configured and manageable.

Lifecycle of the DI Containers

All the containers of DI should be passed through the different sets of statuses until the final conclusion is made, which includes the below lifecycle stages.

  • Register: The container should be registered or initiated as soon as it gets the specific type and this is called the register stage
  • Resolve: While using the DI containers, we do not have to create manual objects. The container does it for us. This is called the resolution phase. The DI container must include some of the methods to resolve the specified type as soon as it identifies. The respective container creates an object of the specified type, along with injecting the required dependencies if any of them are required, and at last, it returns the object
  • Dispose: The DI containers must be able to manage the lifetime of the dependent objects accordingly. Most of the DI containers include different lifetime managers and their responsibility for managing an object's lifecycle and disposing of it

List of popular DI Container for .Net

Today, there are a lot of excellent DI Containers that are available for .NET. Here, I am sharing the list of the most useful DI containers for the .Net framework.

Castle Windsor

  1. Based on the Castle MicroKernel.

  2. Well documented and used by many.

  3. Understands Decorator

  4. Typed factories

  5. Commercial support available

StructureMap

  1. It has been around since June 2004

  2. It is actively developed and works in many cases

Spring.NET

  1. INTERCEPTION

  2. Comprehensive documentation

  3. Commercial support available

Autofac

  1. Easy to learn API

  2. second-generation DI Container

  3. Commercial support available

Unity

  1. INTERCEPTION

  2. Good documentation

  3. Consistent API

Ninject

  1. Easy to learn API

  2. Second-generation DI Container

Read More Articles Related to Dependency Injection
What do you think?

DI is one of the robust design principles that require the introduction of the different types of controls in the object oriented designs (OODs) to achieve a loosely coupled relationship between the application classes. When it comes to the coupling, the control refers to any additional responsibilities of a class has, and other than that, its main responsibility is to control the application flow accordingly.

I hope you will use one of the IoC containers within your .NET application. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Learn to Crack Your Technical Interview
Accept cookies & close this