我知道这可能是一个“编码风格”的问题,但在这一点上我真的很困惑。目前我正在尝试遵循 MVVM 模式(ViewModel、Repository、Controller 等)
但是谁应该发起与数据源的连接呢?特别是当多个控制器需要活动连接时?
那里没有那么多可能性 - 每个控制器本身打开一个新连接,相应的 ViewModel 打开连接并将其传递给存储库,然后将其传递给它的控制器 - 或者连接被实例化甚至更早(例如 StartUp 。CS)。
我知道没有“完美”的解决方案,但我希望能得到一些灵感,也许是一个好的/最佳实践。
更新 1
示例代码:
namespace Question {
class ViewModel {
Person.Person p;
Department.Department d;
Person.PersonRepository pR;
Department.DepartmentRepository dR;
// Here in the VM both classes (Person and Department) intersect - should I inject an instance of a "IDataProvider" from here into the Repositorys?
// If so, I'd have to pass it to the repository which has to pass it to it's controller.
}
}
namespace Question.Person {
class Person {
// Person Model
}
class PersonRepository {
// This class does whatever a repository does and delegates database query to it's controller
}
class PersonController {
// Or should the Controller itself instantiate a new "IDataProvider" ?
// This class needs a connection to the databse to execute querys
}
}
namespace Question.Department {
class Department {
// Department Model
}
class DepartmentRepository {
// This class does whatever a repository does and delegates database query to it's controller
}
class DepartmentController {
// This class needs a connection to the databse to execute querys
}
}