Understanding the basics of DI
Before starting down the path of using DI in our project, we should take some time to understand what DI is and why it is fundamental for building modern applications. You will often see DI referenced with another related concept, Inversion of Control (IoC). Let's discuss these two concepts, clarify the relationship between them, and prepare you to use DI properly in this chapter.
DI is used by method developers to inject dependent objects into a class rather than creating instances of the objects inside of the class. There are different ways to inject those objects:
- Method injection: Objects are passed as parameters to a method in the class.
- Property injection: Objects are set through properties.
- Constructor injection: Objects are passed as constructor parameters.
The most common method of DI is constructor injection. In this chapter, we will be using both property injection and constructor injection. Method injection will...