[英]Generic repository implementation with base repository as abstract class
凈核心項目。 我正在嘗試實現通用存儲庫模式。 我有抽象的基礎存儲庫。 所以我正在覆蓋基礎存儲庫並創建我的其他存儲庫。 實現如下所示。
BaseRepository.cs
public abstract class BaseRepository<T>: IBaseRepository<T>
where T : class
{
private readonly DbContext dbContext;
private readonly DbSet<T> dbSet;
public BaseRepository(DbContext dbContext)
{
this.dbContext = dbContext;
this.dbSet = dbContext?.Set<T>();
}
public async Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, params Expression<Func<T, object>>[] includes)
{
IQueryable<T> query = this.dbSet;
foreach (Expression<Func<T, object>> include in includes)
{
query = query.Include(include);
}
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
query = orderBy(query);
}
return await query.ToListAsync().ConfigureAwait(false);
}
}
}
存儲庫.cs
public class Repository<T> : BaseRepository<T>, IRepository<T>
where T : class
{
private readonly DbContext dbContext;
private readonly DbSet<T> dbSet;
public Repository(DbContext context) : base(context)
{
}
public async Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, params Expression<Func<T, object>>[] includes)
{
IQueryable<T> query = this.dbSet;
foreach (Expression<Func<T, object>> include in includes)
{
query = query.Include(include);
}
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
query = orderBy(query);
}
return await query.ToListAsync().ConfigureAwait(false);
}
}
}
UnitOfWork.cs
public class UnitOfWork<TContext> : IUnitOfWork<TContext>
where TContext : DbContext, IDisposable
{
private readonly MyContext context;
private Dictionary<(Type type, string name), object> _repositories;
public UnitOfWork(MyContext myContext)
{
if (this.context == null)
{
this.context = myContext;
}
}
public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class
{
return (IRepository<TEntity>)GetOrAddRepository(typeof(TEntity), new Repository<TEntity>(Context));
}
public async Task<int> CompleteAsync()
{
try
{
return await this.context.SaveChangesAsync().ConfigureAwait(false);
}
catch (DbUpdateConcurrencyException ex)
{
ex.Entries.Single().Reload();
return await this.context.SaveChangesAsync().ConfigureAwait(false);
}
}
internal object GetOrAddRepository(Type type, object repo)
{
_repositories ??= new Dictionary<(Type type, string Name), object>();
if (_repositories.TryGetValue((type, repo.GetType().FullName), out var repository)) return repository;
_repositories.Add((type, repo.GetType().FullName), repo);
return repo;
}
}
}
然后在我注冊時在我的 startup.cs 文件中
services.AddDbContext<MyContext>(options =>
options.UseSqlServer(this.Configuration["AzureSQLConnectionString"]));
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped<IUnitOfWork, UnitOfWork>();
UnitofWork 使用泛型類型“UnitofWork”會引發錯誤,需要 1 種類型的參數。 然后我添加了services.AddScoped<IUnitOfWork, UnitOfWork<MyContext>>()
。 現在它在 Repository.cs 的 GetAsync 方法中拋出錯誤。 錯誤是 Parameter: source is required, 它不能為空。 然后我發現
IQueryable 查詢 = this.dbSet;
查詢為空。 所以我的理解是我在建立與數據庫的連接時遇到了一些問題。 有人可以幫助我在這里到底做錯了什么嗎? 任何幫助將不勝感激。 謝謝
更改這些類如下:
public abstract class BaseRepository<T>: IBaseRepository<T>
where T : class
{
protected readonly DbContext dbContext;
protected readonly DbSet<T> dbSet;
public BaseRepository(DbContext dbContext)
{
this.dbContext = dbContext;
this.dbSet = dbContext?.Set<T>();
}
public virtual async Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, params Expression<Func<T, object>>[] includes)
{
IQueryable<T> query = this.dbSet;
foreach (Expression<Func<T, object>> include in includes)
{
query = query.Include(include);
}
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
query = orderBy(query);
}
return await query.ToListAsync().ConfigureAwait(false);
}
}
public class Repository<T> : BaseRepository<T>, IRepository<T>
where T : class
{
public Repository(DbContext context) : base(context)
{
}
//override GetAsync if different than base. In your case it is not.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.