- Report this article
Mohammad Ramezani
Mohammad Ramezani
Senior Software Engineer at Knab
Published Sep 5, 2020
+ Follow
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 fornull is necessary andis boilerplate code. Protecting againstnull being passed as a parameter is called theguard pattern
Property Injection (aka setter injection)
- You should use property injection in case the dependency is truly optional
- Property Injection however causesTemporal Couplingand when writing Line of Business applications, your dependencies should never be optional: you should instead apply theNull 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.
Help improve contributions
Mark contributions as unhelpful if you find them irrelevant or not valuable to the article. This feedback is private to you and won’t be shared publicly.
Contribution hidden for you
This feedback is never shared publicly, we’ll use it to show better contributions to everyone.
Like
Celebrate
Support
Love
Insightful
Funny
74
5 Comments
Arifur Rahman
Manager IT Dept(Software Development Team) | .Net Core | Restful API | Microservice | SQL | DB Performance Tuning
1w
- Report this comment
Very Nice.
1Reaction
Nandhakumar Selvaraj
Senior Software Engineer at Smitiv Technologies
2mo
- Report this comment
Best Explanation. Thank you.
1Reaction
Elaheh Mahmoodi
Software Developer
2y
- Report this comment
پس چرا Service Locator رو نگفتین؟
1Reaction
See more comments
To view or add a comment, sign in
More articles by this author
No more previous content
- What's the Hi/Lo algorithm? Oct 14, 2020
- Specification pattern: what’s that? Oct 13, 2020
- Screaming Architecture Apr 8, 2020
- Premature optimization is the root of all evil Apr 7, 2020
- Fluent Interface: Simple Concept To Code Legibility Jan 13, 2020
- It was announced that .NET 5 will be the next release of .NET Core and represents a unification of the platform. Dec 15, 2019
- monolithic to microservices migration Nov 30, 2019
- Monolith First (By Martin Fowler) Jan 31, 2019
- Common Mistakes in Dot Net Framework Concepts (MSIL, CLI, Source Code, and managed or unmanaged Code) Jan 18, 2019
- Can Quality Guarantee Success? Mar 25, 2017
No more next content
Sign in
Stay updated on your professional world
Sign in
By clicking Continue, you agree to LinkedIn’s User Agreement, Privacy Policy, and Cookie Policy.
New to LinkedIn? Join now
Insights from the community
- Object Oriented Design What's the best way to identify key classes and attributes in object-oriented analysis?
- Object Oriented Design How do you debug inheritance and polymorphism issues?
- Multithreading How do you choose the appropriate multithreading design pattern for your problem domain?
- Object Oriented Design How do you use dependency injection and inversion of control to reduce coupling and increase security?
- Domain-Driven Design (DDD) What are some best practices to document and visualize your domain model and its evolution?
Others also viewed
- Modelling: Is it Art or Math Koos Brandt 4y
- Modelling: Is it Art or Math Koos Brandt 4y
- Build a simple DI container for Dependency Injection Ke An NGUYEN 4mo
- Build a simple DI container for Dependency Injection Ke An NGUYEN 4mo
- YOUser Centricity Mark Weinberg 7y
- YOUser Centricity Mark Weinberg 7y
- Looking back at The Unified Modeling Language (UML) Alvin Brathwaite, MSc 7y
- Visualize Sharad Bhushan Kandikonda 3y
- Use the Mediator design pattern Fahed Aljghine 6mo
- Seven ways to communicate better using Box-and-Arrow diagrams Mauro Morelli 1y
Explore topics
- Sales
- Marketing
- Business Administration
- HR Management
- Content Management
- Engineering
- Soft Skills
- See All