ASP.NET Core 2.2 REST API #3 — Dependency Injection & Service Registration
Up Next: Creating Resources using POST
Startup.cs
is the class which allows us to configure our pipeline, middleware and register our services. This is all about clean code and maintainability.
The problem with our current startup file format is that although it’s all nice and centralised, there are still many tweaks we are going to do in this series and unless we find a decoupled way to make a configuration changes, this will be a mess really fast.
For this, we are going to take advantage of the orthogonality of service registration: Service installations and configuration changes are for the most part, independent of each other registration. We can split Startup.cs
into chunks called Installers
.
Let’s take a look at the installers that we come up with:
MvcInstaller
DbInstaller
You can tell, from the semantic meaning perspective, what is each installer responsible for. We just need to detect all the available installers, instantiate them and call InstallServices
in Startup/ConfigureServices()
.
The API is fully up and running, and this is what you call a clean Startup.cs
. For some extra points, you can collapse the snippet above as an extension acting on IServiceCollection
.
Up Next: Creating Resources using POST
Code is available on Github and the instructional videos are located on YouTube.
Keep Coding