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.
