Cześć, próbuję przebrnąć przez ten ciężki temat. W jaki sposób mogę uzyskać ten sam context bazy danych dla wielu repozytoriów. Próbuję uzyskać to po przez generyczne repozytorium no i w zasadzie działa tylko jest jeden problem, konkretne repozytoria nie mogą zostać rozszerzone o dodatkowe metody.
Interfejs
public interface IRepository<T> where T:class
{
IQueryable<T> GetAll();
T GetById(object id);
void Add(T entity);
void Update(T entity);
void Delete(T entity);
void DeleteById(object id);
void Save();
}
Generyczne Repozytorium
public class Repository<T> : IRepository<T> where T:class
{
private Baza db = null;
private DbSet<T> table = null;
public Repository()
{
this.db = new Baza ();
table = db.Set<T>();
}
public Repository(Baza db)
{
this.db = db;
table = db.Set<T>();
}
public IQueryable<T> GetAll()
{
return table;
}
public T GetById(object id)
{
return table.Find(id);
}
public void Add(T entity)
{
table.Add(entity);
}
public void Update(T entity)
{
table.Attach(entity);
db.Entry(entity).State = EntityState.Modified;
}
public void Delete(T entity)
{
T exist = table.Find(entity);
table.Remove(exist);
}
public void DeleteById(object id)
{
throw new NotImplementedException();
}
public void Save()
{
db.SaveChanges();
}
}
Mając przykładowo Repository<User>, Repository<Customer>, Repository<Employer >, jak zaimplementować różne metody dla każdego repozytorium? Bo na tę chwilę w każdym z nich są takie same metody.