Back to: Design Patterns in C# With Real-Time Examples
Inversion of Control Containers in C#
In the last article, I discussed how to implement the 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 the most commonly used DI containers for .NET Framework.
- Unity
- Ninject
- Structuremap
- Autofac
- Spring.Net
- 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; the 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.
Can you explain Ninject for mvc5 as you have explained Unity.
Best Blog which i read about IoC
Nice article, simple and understandable