BaseController With Dependency Injection – John Daniel Associates, Inc.


Not too long ago I’ve inherited an ASP.NET core utility, which can also 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 companies like loggers. ASP.NET core makes service entry straightforward with dependency injection, but it surely’s difficult to remain true to the DRY precept when each controller is initialized with the identical few companies. 

ASPNET Services Injected Used In Controllers

Within the code above, a couple of of the companies being injected are additionally being utilized in different controllers. So, it turns into very repetitive to should initialize all controllers the identical method. However transferring the injection of widespread companies to a base controller won’t work, as seen beneath. 

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 develop into redundant. The companies nonetheless must be outlined in every youngster controller. 


Create Properties As a substitute

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


With this method, one factor to remember is that it makes use of the HttpContext object, and if it’s not obtainable, you will be unable to make use of the service. And bear in mind the companies nonetheless must be registered within the Startup.cs > ConfigureServices technique.

Base Controller

ASPNET Define Common Services Properties

Baby Controller

ASPNET Controllers Inject Services Specific

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

Facet observe, Microsoft appears to choose injection over RequestServices:

The companies obtainable inside an ASP.NET Core request are uncovered by the HttpContext.RequestServices assortment. When companies are requested from within a request, the companies 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 companies are legitimate for so long as the request is energetic.

Be aware: Favor requesting dependencies as constructor parameters to resolving companies from the RequestServices assortment. This ends in courses which can be simpler to check.

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


Please enter your comment!
Please enter your name here

Share post:




More like this

Easy methods to Open a Financial institution Account On-line at Union Financial institution: A Step-by-Step Information

Union Financial institution February 15, 2023Union Financial...

TaxProf Weblog

Thursday, March 23, 2023 Stark Presents Retrofitting Prop 13...

Web Buddies Receives SOC 2 Sort II Attestation for 4th Consecutive 12 months

Web Buddies Receives SOC 2 Sort II Attestation...

Donald Glover Accused of Hating Black Girls After Evaluating ‘Swarm’ Character to ‘Animal’

Donald Glover’s “Swarm” has the multifaceted entertainer going through...
%d bloggers like this: