liveBook · Manning

Implicit dependency

As explained in ASP.NET Core in Action, 3e

An implicit dependency is a required object or service obtained indirectly rather than declared through a component's parameters or constructor arguments.

An implicit dependency is a required object or service obtained indirectly rather than declared through parameters or constructor arguments. RegisterUser has an implicit dependency on EmailSender because it creates the object itself; its dependency is visible only by reading the method body. section 8.1

Implicit dependencies make code harder to maintain:

  • They hide a method's real requirements and make the code harder to reason about and test. section 8.1
  • They tie callers to concrete implementations, so refactoring EmailSender or its dependencies requires changing every manual construction site. section 8.1
  • They force callers to manage object creation and prevent straightforward reuse of expensive instances. section 8.1

Prefer explicit dependencies instead. An endpoint handler can declare EmailSender as a parameter, allowing the ASP.NET Core DI container to construct the dependency graph and provide the required instance. section 8.1 section 8.6

Manual creation versus injection

Without DI, RegisterUser creates EmailSender directly. With DI, it declares `string RegisterUser(string username, EmailSender emailSender)`, calls `emailSender.SendEmail(username)`, and leaves construction to the container.

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines implicit dependencies, demonstrates their maintenance costs, and contrasts manual construction with injected handler parameters.
Examples
Uses RegisterUser, EmailSender, NetworkClient, MessageFactory, and EmailServerSettings to compare manual construction with container injection.
What it assumes
Assumes basic C# methods and classes while introducing dependency injection, service registration, and dependency-graph construction.
What you can do after
Apply it via ASP.NET Core DI by declaring required services as endpoint parameters and avoiding manual construction inside handlers.
How it connects
Connects implicit dependencies with dependency injection, explicit dependencies, dependency graphs, DI containers, and service location.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.