Constructor injection supplies a class's required dependencies through its constructor. A dependency-injection container inspects that constructor and creates or provides the required objects, so application code can use a service without constructing its dependency graph manually. section 8.1
Injecting an email sender
An email service can declare its collaborators directly: EmailSender(MessageFactory factory, NetworkClient client). The endpoint then declares EmailSender as a parameter, calls SendEmail, and leaves object creation to the container. This avoids manually nesting new expressions for MessageFactory, NetworkClient, and EmailServerSettings. section 8.1
Listing 8.4 Sending an email using DI to inject dependencies
string RegisterUser(string username, EmailSender emailSender) #1
{
emailSender.SendEmail(username); #2
return $"Email sent to {username}!"; #2
}
Compare the injected endpoint signature with manual construction in the related listings. section 8.1
