This repository contains slides and code samples for "DI Why? Getting a Grip on Dependency Injection". The code is written in C# in .NET 7.
Many of our modern frameworks have Dependency Injection (DI) built in. But how do you use that effectively? We need to look at what DI is and why we want to use it. We’ll look at the problems caused by tight coupling. Then we’ll use some DI patterns such as constructor injection and property injection to break that tight coupling. We’ll see how loosely-coupled applications are easier to extend and test. With a better understanding of the basic patterns, we’ll remove the magic behind DI containers so that we can use the tools appropriately in our code.
What you will learn:
- See the problems that DI can solve
- Understand DI by using it without a container
- See how a DI container can add some magic and reduce some code
DI Patterns
- Dependency Injection: The Property Injection Pattern
- Property Injection: Simple vs. Safe
- Dependency Injection: The Service Locator Pattern
Decorators and Async Interfaces
- Async Interfaces, Decorators, and .NET Standard
- Async Interfaces
- Adding Retry with the Decorator Pattern
- Unit Testing Async Methods
- Adding Exception Logging with the Decorator Pattern
- Adding a Client-Side Cache with the Decorator Pattern
- The Real Power of Decorators -- Stacking Functionality
Challenges
- Static Objects: Mocking Current Time with a Simple Time Provider
Additional Resources
- Steve Smith: New is Glue
- jeremybytes.com/Demos.aspx#DI