mercredi 22 septembre 2021

What is the best practice for ASP.NET Core MVC dependency injection?

Here's my dilemma: every controller in my ASP.NET Core MVC project "automatically" gets the IConfiguration, which can be used to get any value from my appsettings.json including my connection strings. Note that I would love to get values from my appsettings.json in almost every class.

So, I have repository classes (or DAO, whatever) that do the respective queries to the database(s), the problem is, that in order to get the connection string in this classes I need to check the IConfiguration object that the controller has, so far I've found two options:

  1. Pass the configuration as a parameter, or
  2. Put it in a static class so that every class can access it (my preferred solution)

My dilemma is this: imagine that from the controller to the repository class I need to pass through 6 more classes, so if I implement solution (1) DO I need to pass the configuration as a parameter to each class or is there another solution?

Aucun commentaire:

Enregistrer un commentaire