How Dependency Injection Containers Work in C#?

Dependency Injection (DI) containers, such as Unity or DryIoc, help manage the creation and lifetime of object dependencies in C#. They facilitate the Inversion of Control (IoC) principle, allowing you to focus on writing clean, maintainable code without worrying about the complexities of instantiating dependencies manually. How DI Containers Work? Registration:  You define which concrete classes should be used to fulfill specific interface contracts. This allows the DI container to know what to instantiate when a class requests a particular dependency. Resolution:  When an instance of a class is requested, the DI container looks at the registered services, resolves the dependencies, and creates the object with the required dependencies injected. Lifetime Management:  The container manages the lifecycle of the dependencies. You can specify whether instances should be singleton (one instance for the entire application), transient (a new instance each time), or scoped (one instance per r

What is IoC Container?

 IoC Container

IoC Container is a framework for implementing automatic dependency injection.
before I explain IoC Container first lets know what is dependency injection

Dependency Injection

It is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. 


The Dependency Injection pattern involves three things which are client, service and the injector so, there will be 
three types of classes involves in this process.

  1. Client Class: The client class is a class which depends on the service class.
  2. Service Class: The service class is a class that provides service to the client class.
  3. Injector Class: The injector class injects the service class object into the client class.
The injector class injects dependencies in three ways: through a constructor, or through a property, or through a method. so, there are three types of Dependency Injection which are:

  1. Constructor Injection: In the constructor injection, the injector supplies the dependency through the client class constructor.
  2. Property Injection: In the property injection, the injector supplies the dependency through a public property of the client class.
  3. Method Injection: In the method injection, the client class implements an interface which declares the method(s) to supply the dependency and the injector uses this interface to supply the dependency to the client class.
Let come back to the IoC Container.

IoC Container manages object creation and it's life-time, and also injects dependencies to the class. The IoC container creates an object of the specified class and also injects all the dependency objects through a constructor or a property or a method at run time and disposes it at the appropriate time. This is done so that we don't have to create and manage objects manually.

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

Register: The 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. The container does it for us. This is called resolution. The container must include some methods to resolve the specified type, the container creates an object of the specified type, injects the required dependencies if any and returns the object.
Dispose: The container must manage the lifetime of the dependent objects. Most IoC containers include different lifetime managers to manage an object's lifecycle and dispose it.

There are many open source containers available for .NET. Some are listed below.
DryIoc
Unity
StructureMap
Ninject
Autofac
Simple Injector
Light Inject




Comments

Popular posts from this blog

Filter DataGrid and ListView in wpf using ICollectionView

Pagination of DataGrid in WPF using MVVM

How to Create TabControl using Prism Region