1

これは「コーディングのスタイル」の質問かもしれませんが、この時点で本当に混乱しています。現在、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
 }
}
4

2 に答える 2

4

MVCとMVVMを混同していると思います:

MVVM の概要 MVC の概要

ViewModelは、データベースからデータを取得するリポジトリを使用して、モデルから情報を取得する責任があります。ここではコントローラーは必要ありません。

 public ViewModel()
    {
       Person.PersonRepository pR;
       Department.DepartmentRepository dR;
     }

または、ViewModel にリポジトリ インターフェイスを挿入して、クリーンで分離されたテスト可能な実装を取得することもできます。

public ViewModel(IPersonRepository personRepo, IDepartmentRepository depRepo)
    {
       Person.PersonRepository pR = personRepo;
       Department.DepartmentRepository dR = depRepo;
     }
于 2015-01-29T09:59:15.857 に答える
3

MVVM パターンを誤解していると思います。この記事を読む:

https://msdn.microsoft.com/en-us/magazine/dd419663.aspx

MVVM をよりよく理解するのに役立つはずです。

アップデート:

リポジトリのオープン接続。ORM を使用してデータベース (EF、NHibernate) にアクセスする場合、通常は接続プールが使用されます。ORM を使用しない場合は、プールを実装できます。

http://martinfowler.com/eaaCatalog/repository.html - この記事では、パターン「リポジトリ」について説明します。彼はコレクションのようなインターフェースを実装し、データアクセスの機能を隠します。したがって、リポジトリ内に接続を作成する必要があります。

于 2015-01-29T08:04:30.113 に答える