dependency injection
Giving an object the collaborators it needs from outside, usually through its constructor, instead of letting it create or look them up itself.
In more detail
Constructor injection makes requirements visible in the signature: the object cannot exist without them. Production wiring passes real implementations while tests pass fakes, so decisions can be tested without a database or clock.
Examples
builder.Services.AddScoped<IOrderReader, OrderReader>();public GreetingService(Clock clock) { this.clock = clock; }In Spring, a class with a single constructor gets its arguments injected automatically.In this course: It is what makes a seam: production passes the real gateway, a test passes a double. Prefer it to patching, which reaches into a module from outside and is easy to get wrong.Related terms
- service lifetime — How long the container keeps and reuses an instance of a service: transient, scoped or singleton.
- IServiceCollection — The list of service registrations the dependency injection container is built from, reached in ASP.NET Core as builder.Services.
- service provider — The built dependency injection container, which creates and hands out services according to their registrations.
- abstraction — A simplified description of what something does, such as an interface, that hides how it does it.
- inversion of control — A design in which a piece of code gives up control of some decisions, such as how its dependencies are created or what it renders, to the code around it.
- Spring bean — An object that the Spring container creates, configures and manages, and can inject into other beans.
- test double — Any object that replaces a real dependency in a test: a stub returns canned data, a fake is a small working implementation, and a mock checks interactions.
- Clock (java.time) — The java.time class that supplies the current time, which code can receive as a dependency so tests can control what now means.
- seam — A place in the code where you can swap in different behaviour without editing the code itself, such as a parameter.
- monkeypatch — Replacing a name inside a module while a test runs, such as swapping a clock function, and restoring it afterwards.
- constructor — The special method that runs when a new object is created from a class and sets up its starting data.
- ConstructorParameters<T> — The utility type that gives a class constructor's parameter list as a tuple.
- NestJS — A TypeScript framework for structured Node.js server applications, organised into modules, controllers and dependency injection.
- factory function — A function whose job is to create and return new objects, often choosing or configuring the class to use.
Taught in
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- The Dependency Injection Container: Scopes and Lifetimes — Modern .NET Core, C# & Enterprise Microservices
- The Options Pattern: Strongly Typed Configuration — Modern .NET Core, C# & Enterprise Microservices
- Building Routes with Minimal APIs — Modern .NET Core, C# & Enterprise Microservices
- Comparing Controllers and Minimal APIs — Modern .NET Core, C# & Enterprise Microservices
- Entity Framework Core: Code First and Migrations — Modern .NET Core, C# & Enterprise Microservices
- Model Validation and FluentValidation — Modern .NET Core, C# & Enterprise Microservices
- Working With Jackson — Modern Java: Virtual Threads & High-Throughput Services
- Designing Testable Code — Modern Java: Virtual Threads & High-Throughput Services
- Dependency Injection And The IoC Container — Modern Java: Virtual Threads & High-Throughput Services
- The Spring Bean Lifecycle — Modern Java: Virtual Threads & High-Throughput Services
- Tying It All Together — Modern Java: Virtual Threads & High-Throughput Services
- What Test Doubles Are For — Automated Testing, TDD & Quality Engineering
- Testing Time And Randomness — Automated Testing, TDD & Quality Engineering
- Function Utilities: ReturnType, Parameters And Constructor — Production TypeScript