The 3 Types of Dependency Injection
Dependency Injection solves problems such as:
- How can an application or class be independent of how its objects are created?
- How can the way objects are created be specified in separate configuration files?
- How can an application support different configurations?
Creating objects directly within the class is inflexible because it commits the class to particular objects and makes it impossible to change the instantiation later independently from (without having to change) the class. It stops the class from being reusable if other objects are required, and it makes the class hard to test because real objects can not be replaced with mock objects.
A class is no longer responsible for creating the objects it requires, and it does not have to delegate instantiation to a factory object as in the Abstract Factory design pattern.
There are three types of dependency injection — constructor injection, method injection, and property injection.
Constructor Injection
- Constructor injection is the process of using the constructor to pass in the dependencies of a class.
- You should use constructor injection when your class has a dependency that the class requires in order to work properly.
- If your class cannot work without a dependency, then inject it via the constructor.
- you should use constructor injection when the dependency in question has a lifetime longer than a single method. Dependencies passed into the constructor should be useful to the class in a general way, with its use spanning multiple methods in the class. If a dependency is used in only one spot, method injection
- Checking for null is necessary and is boilerplate code. Protecting against null being passed as a parameter is called the guard pattern
Property Injection (aka setter injection)
- You should use property injection in case the dependency is truly optional
- Property Injection however causes Temporal Coupling and when writing Line of Business applications, your dependencies should never be optional: you should instead apply the Null Object pattern.
- property injection is considered bad in 98% of all scenarios because it hides dependencies and there is no guarantee that the object will be injected when the class is created. (ref)
- The built-in IoC container does not support property injection. You will have to use a third-party IoC container.
Method Injection
- Thus method injection is useful in two scenarios: when the implementation of dependency will vary, and when the dependency needs to be renewed after each use. In both cases, it’s up to the caller to decide what implementation to pass to the method.
Conclusion
Dependency injection is a powerful, useful, and critical technique to use in order to write clean, loosely coupled, easy to maintain code. There are three ways to do dependency injection, each having its own use case. Learn when to use these three techniques, and you will be well on your way to writing excellent, testable, and lovely code.
Manager IT Dept(Software Development Team) | .Net Core | Restful API | Microservice | SQL | DB Performance Tuning | Agile Methodology | Scrum Master
1 年Very Nice.
Software Engineer | C# .Net Core MVC, SQL, Web API, Entity Framework
3 年?? ??? Service Locator ?? ???????