BaseController With Dependency Injection – John Daniel Mates, Inc.

[ad_1]

Not too long ago I’ve inherited an ASP.NET core software, which may be my first time running with the framework. Running with the applying initializing controllers felt very repetitive since all controllers used a couple of not unusual products and services like loggers. ASP.NET core makes provider get entry to simple with dependency injection, nevertheless it’s difficult to stick true to the DRY idea when each controller is initialized with the similar few products and services. 

ASPNET Services Injected Used In Controllers

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

ASPNET Moving Injection Common Services To Base Controller

Having the typical provider injected in a base controller constructor will defeat the aim of a base controller and develop into redundant. The products and services nonetheless wish to be outlined in every kid controller. 

Resolution

Create Houses As an alternative

What I discovered to paintings highest for my wishes and the applying is to outline all not unusual products and services as houses. With ASP.NET Core the Microsoft.Extensions.DependencyInjection identify house provides us get entry to to the next extension approach HttpContext.RequestServices.GetService<T>.

Warning

With this way, something to remember is that it makes use of the HttpContext object, and if it isn’t to be had, you’ll no longer be capable of use the provider. And take into account the products and services nonetheless wish 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 products and services explicit to them. Thus, sticking to the DRY idea and preserving the controller constructors blank. 

Facet observe, Microsoft turns out to want injection over RequestServices:

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

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

Be aware: Favor asking for dependencies as constructor parameters to resolving products and services from the RequestServices assortment. This leads to categories which can be more uncomplicated to check.

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

[ad_2]


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *