Closed stamahto closed 6 months ago
Hi @stamahto,
Not sure of your internal infrastructure, but let me provide some general feedback.
Here is the standard approach. You have a generic repository, where T is your entity. Also, in Repository
implementation you pass your concrete DbContext to the base.
public interface IRepository<T> : IRepositoryBase<T> where T : class
{
}
public class Repository<T> : RepositoryBase<T>, IRepository<T> where T : class
{
public Repository(AppDbContext dbContext) : base(dbContext)
{
}
}
If you want to work with multiple contexts, then define an additional generic parameter as follows. Notice the where TDbContext : DbContext
constraint.
public interface IRepository<TDbContext, T> : IRepositoryBase<T> where T : class
{
}
public class Repository<TDbContext, T> : RepositoryBase<T>, IRepository<TDbContext, T>
where T : class where TDbContext : DbContext
{
public Repository(TDbContext dbContext) : base(dbContext)
{
}
}
Hi, does anyone have created generic repository with ardalis specifications like this?
I struggle with the genericity of
RepositoryBase
along with the use ofISpecification
param in methods. I can't seem to get it to work together. Thank you.