Tuesday, November 29, 2022
HomeBusiness AnalyticsBaseController With Dependency Injection - John Daniel Associates, Inc.

BaseController With Dependency Injection – John Daniel Associates, Inc.

Just lately I’ve inherited an ASP.NET core utility, which can be my first time working with the framework. Working with the applying initializing controllers felt very repetitive since all controllers used a couple of widespread providers like loggers. ASP.NET core makes service entry straightforward with dependency injection, however it’s difficult to remain true to the DRY precept when each controller is initialized with the identical few providers. 

ASPNET Services Injected Used In Controllers

Within the code above, a couple of of the providers being injected are additionally being utilized in different controllers. So, it turns into very repetitive to need to initialize all controllers the identical approach. However transferring the injection of widespread providers to a base controller is not going to work, as seen under. 

ASPNET Moving Injection Common Services To Base Controller

Having the widespread service injected in a base controller constructor will defeat the aim of a base controller and change into redundant. The providers nonetheless must be outlined in every little one controller. 


Create Properties As an alternative

What I discovered to work finest for my wants and the applying is to outline all widespread providers as properties. With ASP.NET Core the Microsoft.Extensions.DependencyInjection identify area offers us entry to the next extension technique HttpContext.RequestServices.GetService<T>.


With this strategy, one factor to remember is that it makes use of the HttpContext object, and if it isn’t obtainable, you won’t be able to make use of the service. And bear in mind the providers nonetheless must be registered within the Startup.cs > ConfigureServices technique.

Base Controller

ASPNET Define Common Services Properties

Youngster Controller

ASPNET Controllers Inject Services Specific

Now controllers are solely required to inject the providers particular to them. Thus, sticking to the DRY precept and protecting the controller constructors clear. 

Facet word, Microsoft appears to desire injection over RequestServices:

The providers obtainable inside an ASP.NET Core request are uncovered by way of the HttpContext.RequestServices assortment. When providers are requested from inside a request, the providers and their dependencies are resolved from the RequestServices assortment.

The framework creates a scope per request and RequestServices exposes the scoped service supplier. All scoped providers are legitimate for so long as the request is energetic.

Be aware: Favor requesting dependencies as constructor parameters to resolving providers from the RequestServices assortment. This ends in lessons which might be simpler to check.

Preserve Studying: C# Home windows Service Debug Hack >>



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments