Dependency inversion keeps high-level code focused on its work by depending on abstractions instead of low-level implementation details. Dependency injection is a related technique, but the two concepts are not identical. section 8.1
In an ASP.NET Core application, a registration handler can require an EmailSender without constructing it or knowing how its dependencies are created. The container supplies the implementation, reducing wiring in application code and helping control complexity as the application grows. section 8.1
The practical contrast is:
- Construct dependencies directly when the application is simple and the extra wiring is not warranted. section 8.1
- Declare required services such as EmailSender and let the container create them when dependencies become more complex. 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 small registration handler that receives EmailSender with the manual construction of MessageFactory, NetworkClient, and email settings. section 8.1





