Dependency Injection
Dependency Injection: What is a Dependency, Constructor Injection, Layers: Models, Services, Controllers.
Updated
- What is a Dependency (Apprentice): A dependency is an object a class needs to do its job. When a class creates its own dependencies with new, it's wired to them for good, and hard to test.
- Constructor Injection (Journeyman): A class asks for its dependencies in its constructor and keeps them in readonly fields. Whoever creates it passes them in, so a test can pass a fake.
- Layers: Models, Services, Controllers (Journeyman): Apps are split into layers (controllers, services, data access and models), each with one job, each handed the layer below it through its constructor.