Repository<T>
을 추상 클래스로 만들 수 있습니다. 당신이 기본 저장소의 인스턴스를 초기화 할 수있게하려면이,
unitofwork.People = new PeopleRepository();
unitofwork.People.Add(new Person()); // Can access the base class
Console.WriteLine(unitofwork.People.GetPersonName()); // People-specific methods
unitofwork.Spaceships = new SpaceshipRepository();
:
abstract class RepositoryBase<TEntity> where TEntity : class, IEntity {
void Add(TEntity entity);
...
}
class PeopleRepository : RepositoryBase<Person> {
string GetPersonName();
}
class SpaceshipRepository : RepositoryBase<Spaceship> {
void Fly();
}
당신은 다음과 같이 클래스를 구현하는 것 : 그런 다음 각 엔티티 해야 자신의 구현을
class Repository<TEntity> where TEntity : class, IEntity {
void Add(TEntity entity);
}
class PeopleRepository : Repository<Person> {
string GetPersonName();
}
당신이 자신의 저를 제공하는 것을 PeopleRepository
을 중지하려면 : abstract
로 표시 할 필요가 없습니다 그런 다음 sealed
수정자를 사용할 수 있습니다.
class Repository<TEntity> where TEntity : class, IEntity {
sealed protected void Add(TEntity entity);
}
여기에는 일반적인 하나 크기에 맞는 모든 클래스의 아이디어가 실패하는 곳이 있습니다. 관심사를 분리하고 리포지토리와 별도로 쿼리 클래스를 만듭니다. – trailmax