C
C#2y ago
surwren

MVC dependency injections vs sql queries

From a c# industry standpoint, when would it be preferable to retrieve data via dependency injections and when would it be preferable to retrieve data via sql queries?
9 Replies
FroH.LVT
FroH.LVT2y ago
Dependency Injection has nothing that related to SQL Queries
surwren
surwren2y ago
??? Yes it does, you can perform a DI to retrieve an object and place it into a controller, then use it alongside a DB class which has its own methods The alternative is to open a new sqlconnection to the db and use MVC's @sql query syntax to query
FroH.LVT
FroH.LVT2y ago
You said use DI to get DB Class to get data from database ==> Your DB Class will deal with database and retrieve data ==> not your DI maybe u mean use DBContext vs legacy ADO.NET to retrieve data from database ?
surwren
surwren2y ago
Doesnt the DB class just contain static methods? DI is needed to ensure loose coupling between the controller and the DB class, no?
FroH.LVT
FroH.LVT2y ago
what do you mean by " DB class just contain static method" ?
surwren
surwren2y ago
The actual db object will be deal with in the controller
FroH.LVT
FroH.LVT2y ago
are you using EF Core?
surwren
surwren2y ago
No, ADO and ASP
FroH.LVT
FroH.LVT2y ago
Can you elaborate more using code? I'm confused