[英]Net Core: Execute Dependency Injection with Xunit for Unit of Work Pattern and DBContext
我正在嘗試使用工作單元模式對Base Repository進行依賴注入,我們使用的是通用存儲庫(由公司架構師決定)。
出於某種原因,沒有什么可以保存,只是想知道在下面的依賴注入中,將DBContext與工作單元相關聯的正確方法是什么。 當前在測試時收到空值,實體框架中沒有任何保存。
它可以使用Regular Dbcontext保存,但在測試后無法使用工作單元模式保存值。
在Xunit test.cs中,似乎我需要將TestContext作為參數傳遞給UnitofWork Constructor,不知道如何在依賴注入,接收錯誤,
工作單位
public class UnitOfWork : IUnitOfWork
{
private readonly DbContext context;
public UnitOfWork(DbContext context)
{
this.context = context;
}
public DbSet<TEntity> DBSet<TEntity, TPrimaryKey>() where TEntity : class, IEntity<TPrimaryKey> => context.Set<TEntity>();
public void SetAsModified<TPrimaryKey>(IEntity<TPrimaryKey> entity) => Task.Run(() => context.Entry(entity).State = EntityState.Modified);
public async Task Save()
{
await context.SaveChangesAsync();
}
// IDisposable pattern support
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed && disposing)
{
context.Dispose();
}
this.disposed = true;
}
基礎知識庫:
public class BaseRepository<T, TPrimaryKey> : IRepository<T, TPrimaryKey> where T : class, IEntity<TPrimaryKey>
{
private readonly IUnitOfWork unitOfWork;
protected virtual DbSet<T> DbSet { get; }
protected IQueryable<T> All => DbSet.AsNoTracking();
public IUnitOfWork UnitOfWork => unitOfWork;
public BaseRepository(IUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
DbSet = unitOfWork.DBSet<T, TPrimaryKey>();
}
public void Insert(T entity)
{
DbSet.Add(entity);
}
public IQueryable<T> GetAll()
{
return All;
}
public class BaseRepository<T> : BaseRepository<T, int>, IRepository<T> where T : class
{
public BaseRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
{
}
}
XUnit測試:
var services = new ServiceCollection();
services.AddDbContext<TestContext>(a => a.UseInMemoryDatabase("Test"));
services.AddTransient<DbContext, TestContext>();
services.AddTransient<IUnitOfWork, UnitOfWork>();
services.AddTransient(typeof(IRepository<>), typeof(BaseRepository<>));
ServiceProvider serviceProvider = services.BuildServiceProvider();
var scope = serviceProvider.CreateScope();
var testdbContext = scope.ServiceProvider.GetServices<TestContext>();
var unitOfWork = scope.ServiceProvider.GetService<IUnitOfWork>();
var ProductRepository = scope.ServiceProvider.GetService<IRepository<Product>>();
ProductRepository.Insert(new Product {ProductId = 1, ProductName = "ABC";
ProductRepository.Insert(new Product {ProductId = 2, ProductName = "DEF");
await unitOfWork.Save();
public Product()
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription{ get; set; }
}
什么都沒有儲蓄。 在調試期間 - 我在DBSet綠色中的值,但在進行ProductRepository.GetAll()
時沒有,但是沒有意義,試圖弄清楚
更新 :
只使用DbContext保存時,它可以正常工作。 但是,無法保存工作單元。
TestContext.Product.Add(expectedResult[0]);
TestContext.Product.Add(expectedResult[1]);
TestContext.SaveChanges();
您正在將服務注冊為臨時服務,這意味着DI容器每次需要服務來解析或注入時都會創建新的實例,因此注入IRepository<>
的IUnitOfWork
實例與GetService<IUnitOfWork>()
獲取的實例不同GetService<IUnitOfWork>()
。 您可以通過向services.AddScoped
注冊服務來解決此問題,以便為每個打開的范圍創建每個服務的單個實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.