2011-03-01 7 views
0

リポジトリとアクティブレコードのパターンを以下のように混在させるとどう思いますか?継承に関するリポジトリとアクティブレコードのパターンの混合

GetAll()メソッドを実装するにはどのように継承がenvolvedですか?

class Person 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public Person(int id) 
    { 
     this.Id = id; 
     new PersonRepository().Load(this); 
    } 

    public virtual void Save() 
    { 
     new PersonRepository().Save(this); 
    } 
} 

class Employee : Person 
{ 
    public int RegistrationNumber { get; set; } 

    public Employee(int id) : base(id) 
    { 
     new EmployeeRepository().Load(this); 
    } 

    public override void Save() 
    { 
     base.Save(); 
     new EmployeeRepository().Save(this); 
    } 
} 

interface IRepository<T> 
{ 
    void Save(T x); 
    bool Load(T x); 
    IEnumerable<T> GetAll(); 
    // Other methods removed to keep code clean 
} 

class PersonRepository : IRepository<Person> 
{ 
    public void Save(Person x) 
    { 
     throw new NotImplementedException(); 
    } 

    public bool Load(Person x) 
    { 
     throw new NotImplementedException(); 
    } 

    public IEnumerable<Person> GetAll() 
    { 
     throw new NotImplementedException(); 
    } 
} 

class EmployeeRepository : IRepository<Employee> 
{ 
    public void Save(Employee x) 
    { 
     throw new NotImplementedException(); 
    } 

    public bool Load(Employee x) 
    { 
     throw new NotImplementedException(); 
    } 

    public IEnumerable<Employee> GetAll() 
    { 
     // How to return Person data???? 
     throw new NotImplementedException(); 
    } 
} 

答えて

1

すべてPersonオブジェクトをロードするオーバーヘッドが発生心配している場合は、実際にデータが必要になるまで多分それらをロードしない - などレイジーロードアプローチを介するなど。

「人」のリストを作成する必要があるが、すべてのデータを戻したくない場合は、リストに必要なもの(ID、名字など)のみを使用してくださいPersonオブジェクトはあまりにも重いので、適切な方法ではありませんか?

私がする傾向があるのは、(人のような)概念を念頭に置き、それらを表す2つのクラスがあります。 - リストなどのために設計された軽量クラスです(通常は読み取り専用です)。 - "Person"で操作を実行するときの "フル機能"オブジェクト。

あなたの行っていることには何も問題はありませんが、厳密にはOOベースです。ソフトウェアが実際に実行する環境によって物理的な制限がある場合のデータ取得の意味には関係しません。

あなたが持っているアプローチは基本的には素晴らしいですが、セットベースのアプローチではなく、「クラスごとの」アプローチに基づいています。

関連する問題