liveBook · Manning

Dependency graph

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

A dependency graph is a directed graph representing objects or components and the dependencies required to construct or operate them.

A dependency graph is the set of objects that must be created to create a requested root object. In an ASP.NET Core minimal API, an endpoint handler can be the root, while its services and their constructor dependencies form the rest of the graph. section 8.1 section 8.5

An email-sending example builds the graph incrementally:

  • RegisterUser needs EmailSender. section 8.1
  • EmailSender needs MessageFactory and NetworkClient. section 8.1
  • NetworkClient needs EmailServerSettings. section 8.1
  • The DI container creates the nested objects first, then supplies the completed EmailSender to the handler. section 8.5

Injecting the graph at the endpoint

string RegisterUser(string username, EmailSender emailSender) { emailSender.SendEmail(username); return $"Email sent to {username}!"; } The handler declares only its immediate dependency. The container resolves EmailSender and the objects beneath it, removing manual construction from application logic. section 8.1 section 8.5

Figure 8.2 Dependency diagram using DI . RegisterUser indirectly depends on all the other classes but doesn’t need to know how to create them. The RegisterUser handler declares that it requires EmailSender, and the container provides it.
CH08_F02_Lock3

Compare the dependency graph before and after DI: manual construction forces RegisterUser to know every indirect dependency, while DI lets the container provide the graph. section 8.1

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

How deep it goes
Defines the graph and works through nested email-service construction, but defers service lifetimes to later treatment.
Examples
Uses RegisterUser, EmailSender, MessageFactory, NetworkClient, EmailServerSettings, and LinkGenerator to demonstrate graph construction and resolution.
What it assumes
Assumes basic C# classes and constructors while teaching dependency injection, explicit dependencies, and service registration.
What you can do after
Understand how the DI container constructs requested services from their declared dependencies and supplies them to handlers.
How it connects
Connects dependency graphs with dependency injection, constructor injection, endpoint handlers, service registration, and service lifetimes.

Also explained in

Compare how 5 books explain dependency graph →

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.