Inversion of Control Containers in C#

Inversion of Control Containers in C#

In the last article, I discussed how to implement Dependency Injection design pattern to achieve fully loosely coupled classes. IoC Container (DI Container) is a great framework that implements dependency injection automatically. That means this framework creates dependency object and inject them automatically when required.

Note: The terms Dependency Injection (DI) & Inversion of Control (IoC) are generally used together to achieve fully loosely coupled systems. Hence some people say IoC Container while some other people say it as DI container but both terms indicate the same thing. So don’t be confused with the terminology.

IoC container (DI container) creates an object of the specified class and then automatically injects all the dependency objects through a class constructor, property or method at runtime. This is done automatically by the Ioc (DI) container so that we don’t have to create and manage these dependency objects manually.

Today, there are a lot of excellent open sources or commercial containers available for .NET. Here I am giving you the list of most commonly used DI containers for.Net Framework.

  1. Unity
  2. Ninject
  3. Structuremap
  4. Autofac
  5. Spring.Net
  6. Castle Windsor

All the above containers provide easy support for the following DI lifecycle.

Register:

The DI container must know which dependency to instantiate when it encounters a particular type. This process is called registration. Basically, it must include some way to register type-mapping.

Resolve:

When using the IoC container, we don’t need to create objects manually as the container does it for us. This is called resolution. The container includes some methods to resolve the specified type; container creates an object of the specified type, injects required dependencies if any and returns it.

Dispose:

The container must manage the lifetime of dependent objects. Most IoC containers include different lifetime managers to manage an object’s lifecycle and dispose of it.

In the next article, I am going to discuss the Unity Container. Here, in this article, I try to explain Ioc Container which is also known as DI Container. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *