Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12201

DI in constructor or [FromService] in method? [closed]

$
0
0

I am wondering what is the better way to inject a service. I have two ways in mind:

This:

Public async Task<IActionResult> CheckOut([FromBody] FooModel model, [FromService] Config config){    //do stuff with config here...}

Or:

Public class FooClass{    private readonly IConfig _config;    public FooClass(IConfig config)    {        _config = config;    }    //methods interacting with config...}

Is there a scenario where one is better to use than the other, e.g. if multiple methods use the Config is it then better to inject it in the constructor?


Viewing all articles
Browse latest Browse all 12201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>