Extension Method to split services registrations – Dependency Injection – .NET CORE PART III
Hi! This is part three of the dependency injection series, if you want to see the previous articles, just check:
- Dependency Injection Net Core – Part 1 – What is DI ? Applying DI in a Console Core Project
- Dependency Injection Net Core – Part 2 – Applying DI in a ASP.NET CORE Project
Problem – a lot of services 🙁
If you can remember, we only have 3 services in our app:
If we have only a few services, we don’t have any problem but can you imagine if we have a lot of services? Our ConfigureServices would be huge and maintenance issues can appear.
Let me show you:
We will add ten services more – Interface + Concrete Service and then, we’ll register each service in the configureServices method.
Ok, you can see it, we have a lot of services. In this case, we have ten services but in a real application, we would be able to have more than thirty 😮
Maybe, we need to evaluate splitting our services into separate files because our main principle is: keep the configureServices method as short and clear as possible.
Applying Extension Methods to split our services registrations – Hello clean code 😀
Ok, our first step will be to create an “Infraestructure” folder and then, we’ll create two classes inside it:
- SchoolServiceCollection : we will put here all services are related to school.
- FakeServiceCollection we will put here all fake services.
Ok, let’s started 😉
1 – Creating an extension class: Name + “ServiceCollection” (naming convention)
2 – Set class as Static class and changing the namespace to Microsoft.Extensions.DependencyInjection
3 – Add our extension method
We are extending IServiceCollection and also, we need to return the IServiceCollection because of the service registration implements the CHAIN PATTERN.
4 – Add our service registrations
5 – Finally we need to call our extension method in the configureServices Method
Perfect, we need to do the same with FakeServices, let’s code:
Finally, we need to call this method in the configurationServices method and remove old registrations:
Magic 🙂 we can split our services registrations accord several criteria for instead, we can have:
- ConfigurationServices ==> methods that retrieve information from a specifying config section
- BusinessServices ==> All business service registrations
- RepositoryServices ==> All repository service registrations
- Split by modules for example: AddMembershipServices() – AddCatching() – AddAuditing() – etc.
In other words, you can split the service registrations whatever you want according to your business 😉
That’s all !
Big hug 😉
Code on my github 😉
2 thoughts on “Extension Method to split services registrations – Dependency Injection – .NET CORE PART III”
Thank you! Nice pattern and very well explained as usual!
Fantastic beat ! I would like to apprentice even as you amend your website, how could i subscribe for a weblog website? The account aided me a acceptable deal. I were tiny bit acquainted of this your broadcast offered shiny clear concept