We have a table of persons in which different types of persons are stored (buyer, seller, agent, etc.). Our ORM is Entity Framework CodeFirst (CTP5). We use the repository template for good TDD and mockingly. In PersonRepository, I want to return a specific type so that I can do things like this:
Agent a = repository.Get<Agent>(5005);
a.SomeAgentProperty = someValue;
Buyer b = repository.Get<Buyer>(253);
b.SomeBuyerProperty = someOtherValue;
The idea is that I know what kind of person I get when I get it from the repository. And, yes, I could just create X different Get methods called GetBuyer (int PersonId), GetSeller (int PersonId) and so on. But it has a smell of code.
What will the general function look like?
Here is my repository interface:
public interface IPersonRepository
{
Person Get(int PersonId);
T Get<T>(int PersonId);
void Save(Person person);
void Delete(int p);
}
And my specific implementation:
public T Get<T>(int PersonId)
{
}
source