Open farlee2121 opened 5 years ago
If I can set the connection string via constructor, then I can use the accessors without a config file (like for quick scripts). This is especially helpful for experimental clients that I just want to use an in-memory db.
Being able to specify in-memory or sql via connection string also simplifies builds, since I don't need to migrate a real sql db to run tests.
Here is an interesting thread. With DI, you can set the db connection string per call. This would allow for per-client databases (or regional databases, etc) with only one server. https://stackoverflow.com/questions/36816215/dynamically-change-connection-string-in-asp-net-core
The idea is that all information needed to run a class is declared via the constructor.
This may seem like excess work and doesn't feel much different than things like Entity framework's configuration defaults.
However, trying to reuse a class outside of the DI setting shows complications. The component cannot be used without configuration that has no explicit ties. Knowledge of the ORM is required to understand why the code doesn't run. Further, configuration via a config file may not always be preferable.
By injecting connection strings, we make this dependency explicit. Within the DI setting, the configuration still happens automagically.