Thursday, April 18, 2024

BaseController With Dependency Injection – John Daniel Mates, Inc.

-


Just lately I’ve inherited an ASP.NET core utility, which could also be my first time running with the framework. Running with the appliance initializing controllers felt very repetitive since all controllers used a couple of commonplace services and products like loggers. ASP.NET core makes carrier get entry to simple with dependency injection, however it’s difficult to stick true to the DRY theory when each controller is initialized with the similar few services and products. 

ASPNET Services Injected Used In Controllers

Within the code above, among the services and products being injected also are being utilized in different controllers. So, it turns into very repetitive to must initialize all controllers the similar approach. However shifting the injection of commonplace services and products to a base controller is not going to paintings, as observed underneath. 

ASPNET Moving Injection Common Services To Base Controller

Having the typical carrier injected in a base controller constructor will defeat the aim of a base controller and turn out to be redundant. The services and products nonetheless want to be outlined in every kid controller. 

Resolution

Create Houses As a substitute

What I discovered to paintings best possible for my wishes and the appliance is to outline all commonplace services and products as houses. With ASP.NET Core the Microsoft.Extensions.DependencyInjection title house offers us get entry to to the next extension approach HttpContext.RequestServices.GetService<T>.

Warning

With this means, something to bear in mind is that it makes use of the HttpContext object, and if it’s not to be had, you’re going to now not be capable to use the carrier. And consider the services and products nonetheless want to be registered within the Startup.cs > ConfigureServices approach.

Base Controller

ASPNET Define Common Services Properties

Kid Controller

ASPNET Controllers Inject Services Specific

Now controllers are simplest required to inject the services and products particular to them. Thus, sticking to the DRY theory and protecting the controller constructors blank. 

Aspect notice, Microsoft turns out to desire injection over RequestServices:

The services and products to be had inside of an ASP.NET Core request are uncovered in the course of the HttpContext.RequestServices assortment. When services and products are asked from within a request, the services and products and their dependencies are resolved from the RequestServices assortment.

The framework creates a scope in line with request and RequestServices exposes the scoped carrier supplier. All scoped services and products are legitimate for so long as the request is lively.

Notice: Want inquiring for dependencies as constructor parameters to resolving services and products from the RequestServices assortment. This ends up in categories which might be more uncomplicated to check.

Stay Studying: C# Home windows Carrier Debug Hack >>

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related Stories