What is dependency injection?
Dependency injection eliminates tight coupling between objects to make both the objects and applications that use them more flexible, reusable, and easier to test. It facilitates the creation of loosely coupled objects and their dependencies. The basic idea behind Dependency Injection is that you should isolate the implementation of an object from the construction of objects on which it depends. Dependency Injection is a form of the Inversion of Control Pattern where a factory object carries the responsibility for object creation and linking. The factory object ensures loose coupling between the objects and promotes seamless testability.
Dependency Injection to be very advantageous for doing Development without change any component.
Dependency Injection is an important pattern for creating classes that are easier to unit test in isolation
Promotes loose coupling between classes and subsystems
Can enable better code reuse
There are a couple of different flavors of Dependency Injection
Constructor Injection – Attach the dependencies through a constructor function at object creation
public interface IService
{
string GetMethod();
}
Here Constructor is used to interface parameter that exposed through the parameterized contractor. It injects the dependencies through a contractor method as object creation other classes.Here given below the service implementation
{
public string ServiceMethod()
{
return "Runnig";
}
}
Code implementation services Locator
public class BusinessServices
{
private static IService _servicer;
public static IService GetService()
{
return _servicer;
}
public static void SetService(IService serviceImplemented)
{
_servicer= serviceImplemented;
}
}
Setter Injection – Attach the dependencies through setter properties
Getter and Setter Injection injects the dependency by using default public properties procedure such as
Gettter(get(){})
and Setter(set(){})
.
Interface Injection – Interface Injection uses support interface a kind of explicit getter and setter which sets the interface property.
No comments:
Post a Comment