Inversion of Control Containers in C#

IoC Container

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 which 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 to 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 container 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 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 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 will discuss Unity Container.

SUMMARY

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 need. 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 *