Do you want to master Clean Architecture?
Grab the 33% Off .NET Engineer Clean Architecture Masterclass
Discount code : BF2023
Here’s what’s inside:
Clean Architecture
Architectural principles
Domain-Driven Design
CQRS pattern blueprint
Validation & Logging solved
Concurrency control with EF
Authentication with KeyCloak
Minimal APIs versus Controllers
Reliable messaging with Outbox
Testing best practices using xUnit
About today’s newsletter :
Today we are going to discuss :
Three different approaches of registering dependencies
Inline Dependency Injection
Extension Method Dependency Injection
Modularized Dependency Registration
In the world of .NET development, it's a common practice to establish registrations for dependencies, ranging from basic services to third-party components obtained through NuGet packages.
1/ Inline Dependency Registration
Consider a straightforward example of dependency registration. Below is an illustration of how dependencies would be registered in a Program.cs file.
2/ Extension Method Dependency Registration
The previous approach is suitable but becomes less efficient as the number of dependencies increases.
A more effective strategy involves relocating these dependencies into an extension method and then invoking that extension method within the Program file.
Add in Program
3/ Modularized Dependency Registration
I prefer to create distinct extension methods for each concern.
Then, I consolidate the invocation of these methods into a single method, and only this method is called in the Program file.
This ensures a more organized and modular approach to dependency registration.
Add in Program file
This way dependencies becomes :
- More clean
- Easy to manage
Whenever you’re ready, there are 2 ways I can help you
Promote yourself to 8000+ subscribers by Sponsoring my Newsletter
Become a Patron and get access to 160+ .NET Questions and Answers
Great approach! I just think the distinct extension methods created could have their modifier as private, minimizing the available options on intellisense when registering services in Program.cs, what do you thing?