0

We currently use a mix of either approach. Is there a (performance) benefit of one vs the other?

example 1: ILogicAPI injected into controller constructor:

services.AddScoped<ILogicAPI, LogicAPI>(sp =>
{
    var httpClient = sp.GetRequiredService<IHttpClientFactory>().CreateClient();
    return new LogicAPI(surveyConfig.SurveyApiProxyConfiguration(httpClient));
});

Controller:

 public AssessmentController(
     SurveyUIConfiguration configuration,
     IAssessmentAPI assessmentApi,
     ISurveyReviewAPI surveyReviewApi,
     IAnswerAPI answerApi,
     ITakerScreenAPI screenApi,
     ILogicAPI logicApi,
     IMasterSurveyErrorMessageTextsAPI errorAPI
     )
 {
     this.assessmentApi = assessmentApi;
     this.surveyReviewApi = surveyReviewApi;
     this.answerApi = answerApi;
     viewModelService = new ViewModelService(configuration, answerApi, screenApi, logicApi, errorAPI);
 }

or example 2: new LogicAPI is instantiated in the controller

public AssessmentController(
    IMasterSurveyErrorMessageTextsAPI errorAPI,
    IBaseSurveyControllerServices services
    ) : base(services)
{
    this.assessmentApi = new AssessmentAPI(SurveyApiSettings);
    this.surveyReviewApi = new SurveyReviewAPI(SurveyApiSettings);
    this.answerApi = new AnswerAPI(SurveyApiSettings);
    var screenApi = new TakerScreenAPI(SurveyApiSettings);
    var logicApi = new LogicAPI(SurveyApiSettings);
    viewModelService = new ViewModelService(Configuration, answerApi, screenApi, logicApi, errorAPI);
}
3
  • Example 2 is tending toward the 'control freak' anti-pattern, which negates the benefits IoC and DI bring. Good summary here: davidguida.net/… Commented Jan 10 at 9:02
  • That makes sense. I have this mix of both throughout the applications. The reason it came to my attention was the CreateClient() that I needed to add to each interface. So, I have about twenty places where I'm now calling this: var httpClient = sp.GetRequiredService<IHttpClientFactory>().CreateClient(); It's another question... but is there a way to get httpClient outside of this (without BuildServiceProvider() which seems discouraged), and apply it to each. Commented Jan 10 at 16:49
  • Can't you just take a dependency on HttpClient in those classes and then let the IoC container resolve it for you? Seems like you're doing too much heavy lifting that the container can do on your behalf. Commented Jan 11 at 11:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.