The following are some of the patterns available that can be used for DI:
- Constructor injection
- Property injection
- Method injection
- Service Locator
The following section provides details on these various patterns.
Constructor injection
Constructor injection is a technique in which dependencies are passed through a class's constructor. This is an excellent way to inject dependencies because the dependencies are made explicit. The object cannot be instantiated without its dependencies.
If a class can be instantiated and its methods called, but the functionality does not work properly because one or more dependencies have not been provided, classes are being dishonest with their clients. It is a better practice to explicitly require ...