繁体   English   中英

Net Core:使用Xunit为工作单元模式和DBContext执行依赖注入

[英]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();

参考问题: Net Core:在Appun,Repository等的Xunit测试中执行所有依赖注入

您正在将服务注册为临时服务,这意味着DI容器每次需要服务来解析或注入时都会创建新的实例,因此注入IRepository<>IUnitOfWork实例与GetService<IUnitOfWork>()获取的实例不同GetService<IUnitOfWork>() 您可以通过向services.AddScoped注册服务来解决此问题,以便为每个打开的范围创建每个服务的单个实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM