さまざまなタイプの人 (買い手、売り手、エージェントなど) を格納する Persons テーブルがあります。私たちの ORM は Entity Framework CodeFirst (CTP5) です。適切な TDD とモッキングのためにリポジトリ パターンを使用しています。PersonRepository では、特定の型を返したいので、次のようなことができます。
Agent a = repository.Get<Agent>(5005); // Where 5005 is just an example Id for the person
a.SomeAgentProperty = someValue;
Buyer b = repository.Get<Buyer>(253); // Again, a simple PersonId.
b.SomeBuyerProperty = someOtherValue;
リポジトリから取得したときに、どのような人が取得されているかを知っているという考えです。そして、はい、GetBuyer(int PersonId)、GetSeller(int PersonId) などと呼ばれる X 個の異なる Get メソッドを作成することもできます。しかし、それにはコードの匂いがあります。
ジェネリック関数はどのように見えるでしょうか?
これまでの私のリポジトリインターフェースは次のとおりです。
public interface IPersonRepository
{
Person Get(int PersonId); // To get a generic person
T Get<T>(int PersonId); // To get a specific type of person (buyer, agent, etc.)
void Save(Person person);
void Delete(int p);
}
そして私の具体的な実装:
public T Get<T>(int PersonId)
{
//Here's my question: What goes here?
}