How to Decouple Your Code Using Dependency Injection

dependency injection how to decouple your code n.w
1 / 18
Embed
Share

Learn about the concept of dependency injection and how it helps decouple your code modules. Explore SOLID principles, such as Single Responsibility Principle and Dependency Inversion Principle. Understand the importance of Dependency Injection in software development, its benefits such as decoupling, flexibility, and maintainability. Dive into Pure DI and Composition Root concepts to enhance your understanding.

  • Decoupling
  • Dependency Injection
  • SOLID Principles
  • Software Development
  • Pure DI

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Dependency Injection How to Decouple Your Code Modules

  2. What is dependency injection? Put appropriate instances in don t let the object create them. Well, it can be a bit more

  3. What is SOLID? S - Single responsibility principle (SRP) O - Open/closed principle (OCP) L - Liskov substitution principle (LSP) I - Interface segregation principle (ISP) D - Dependency inversion principle (DIP)

  4. Single responsibility principle A class should only have one responsibility having problems finding a name is a sign of a violation creating things is a responsibility -> factory related to separation of concerns (SoC)

  5. Dependency inversion principle Abstractions should not depend on details. Details should depend on abstractions "Code against interfaces (i.e. abstractions) not implementations (details)"

  6. What is dependency injection "Dependency Injection is a 25-dollar term for a 5-cent concept." "Don't look for things - ask for them!" (Misko Hevery)

  7. Why Dependency Injection? Decoupling of software pieces (classes) SRP Flexibility Composability Exchangeability Testability (mocking) Maintainability

  8. Pure DI Learn and understand how DI works, what patterns and practices are helpful before using an automated way Try putting your dependencies together manually to get a feeling for how to design your classes and interfaces Once you ve written pages and pages of constructor calls, consider using a DI container

  9. Composition root The point where DI starts ideally there is only one and it s at the very start of the application When trying to fit DI into an existing application you might have multiple at first Move them closer to the start of the application and eventually they will converge

  10. Types of injection Constructor Injection constructor parameters mandatory dependencies Property Injection properties/setter optional dependencies Null-Object pattern Field Injection Use only in exceptional cases this is not possible with pure DI

  11. Testing with Dependency Injection 1. Refactor classes for DI 2. For each dependency: 1. Extract a Testable interface 2. Have concrete class implement the interface 3. Update the original class to use the interface 4. Create a test class that implements the interface called a Mock object 5. Use the mock object for testing

  12. Refactor the class for DI (before) public class View { private Model model; public View () { model = new Model(); } public Model getModel() { return model; } }

  13. Refactor the class for DI (after) public class View { private Model model; public View (Model m) { model = m; } public Model getModel() { return model; } }

  14. Extract a "testable" interface What does a model have? public interface IModel { Expected<Boolean> addClass (String name); Expected<Boolean> renameClass (String oldName, String newName); Expected<Boolean> removeClass (String name); ... }

  15. Have the concrete class implement the interface public class Model implements IModel { public Expected<Boolean> addClass (String name) { ... } public Expected<Boolean> renameClass (String oldName, String newName) { ... } ... }

  16. Update the Original Class public class View { private IModel model; public View (IModel m) { model = m; } public IModel getModel() { return model; } }

  17. Create a Mock Object public class AlwaysErrorModel implements IModel { public Expected<Boolean> addClass (String name) { return Expected.of(new Exception( duplicate )); } public Expected<Boolean> renameClass (String oldName, String newName) { return Expected.of(new Exception( old class name does not exist )); } ... }

  18. When to Actually Use Mock Objects? Testing complex controller logic Testing UIs (can t always run the UI) When you cannot reproduce When the real object is slow/complex

Related


More Related Content