これは「コーディングのスタイル」の質問かもしれませんが、この時点で本当に混乱しています。現在、MVVM パターン (ViewModel、Repository、Controller など) に従おうとしています。
しかし、誰がデータ ソースへの接続を開始する必要があるのでしょうか? 特に、複数のコントローラーがアクティブな接続を必要とする場合は?
そこにはそれほど多くの可能性はありません-すべてのコントローラー自体が新しい接続を開くか、対応するViewModelが接続を開き、それをリポジトリに渡し、リポジトリがそれをコントローラーに渡します-または、接続がさらに早くインスタンス化されます(たとえば、スタートアップ.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
}
}