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

Taught in

References