Dependency Injection (DI)
Dependency Injection (DI) is a software design pattern. It allows us to develop loosely-coupled code. The intent of Dependency Injection is to make code maintainable. Dependency Injection helps to reduce the tight coupling among software components. Dependency Injection reduces the hard-coded dependencies among your classes by injecting those dependencies at run time instead of design time technically. This article explains how to implement Dependency Injection in C# and .NET code.
The process of injecting (converting) coupled (dependent) objects into decoupled (independent) objects is called Dependency Injection.
Types of Dependency Injection
1. Constructor Injection
Construction injection is the most commonly used dependency pattern in Object Oriented Programming. The constructor injection normally has only one parameterized constructor, so in this constructor dependency there is no default constructor and we need to pass the specified value at the time of object creation. We can use the injection component anywhere within the class. It addresses the most common scenario where a class requires one or more dependencies.
2. Setter Injection
Getter and Setter Injection injects the dependency by using default public properties procedure such as Gettter(get(){}
)
and Setter(set(){}
).
3. Interface Injection
Interface Injection is similar to Getter and Setter DI, the Getter and Setter DI uses default getter and setter but
Interface Injection uses support interface a kind of explicit getter and setter which sets the interface property.
The following code sample illustrates the concept, ISetService
is a support interface which has method setServiceRunService
which set the interface property.
//Support interface interface ISetService { void setServiceRunService(IService client); } //Implementing Interface Injection public class BusinessLogicImplementationInterfaceDI : ISetService { IService _client1; public void setServiceRunService(IService client) { _client1 = client; Console.WriteLine("Interface Injection ==> Current Service : {0}", _client1.ServiceMethod()); } }
Consuming Interface Injection
BusinessLogicImplementationInterfaceDI objInterfaceDI = new BusinessLogicImplementationInterfaceDI(); objInterfaceDI= new ClaimService(); objInterfaceDI.setServiceRunService(serviceObj);
4. Service Locator Injection
Service Locator Injection is also known as Dependency Absorption. It is used to replacement of new operator. It hides the class dependency by invoking methods directly (without creating object). The following code sample illustrates the concept, in the consumption setService method is invoked by class name BusinessLogicImplementation without creating object.
//Implementing Service Locator Injection public class BusinessLogicImplementation { private static IService _clientLocator; public static IService getService() { return _clientLocator; } public static void setService(IService clientSL) { _clientLocator = clientSL; } }
Consuming Service Locator Injection
BusinessLogicImplementation.setService(new PaymentService()); IService client2 = BusinessLogicImplementation.getService(); Console.WriteLine("Service Locator => Current Service : {0}" ,client2.ServiceMethod());
Advantages of DI
Drawback
The main drawback of dependency injection is that using many instances together can become a very difficult if there are too many instances and many dependencies that need to be resolved.