Dependency Injection in C#

Dependency Injection (DI) is a software design pattern that allows us to develop loosely coupled code. When designing an object oriented application, a major concern of design is "Design must be loosely coupled". Loose coupling offers us greater re-usability, maintainability and test-ability.

There are three basic type of Dependency Injection
Construction Injection
Setter Injection
Interface based Injection.

Constructor Injection
The basic idea of constructor-injection is that the object has no defaults or single constructor; instead specified values are required at the time of creation to instantiate the object. In other words Constructor injection uses parameters to inject dependencies.

Setter Injection
Setter Injection does not require the constructor to be change but dependencies are passed through public properties that are exposed. Setter Injection allows us costly resources or services to be created as late as possible and only when required.

Interface Based Injection
Interface based Injection is implemented using common interfaces that other classes need to implement to inject dependencies. This type of injected from both the way either constructor injection or setter injection.

No comments:

Post a Comment