繁体   English   中英

在通用存储库中应用工作单元

[英]applying unit of work in a generic repository

我正在学习使用工作单元进行存储库。 我也知道如何做DI / IOC。 但是我的问题是我不知道在我的代码中将Unit of Work应用于何处。 以下是通用存储库。

public abstract class Repository<T, C> : //IDisposable,
        IRepository<T> where T : class
        where C : DbContext, new()

    {
        private C entities = new C();

        public C Context 
        {
            get
            {
                return this.entities;
            }
            set
            {
                this.entities = value;
            }
        }

       public virtual void Insert(T entity)
       {
         this.entities.Set<T>().Add(entity);
       }

       // remove some code for brevity
    }

到目前为止我尝试过的是:

  1. 制作Unit of Work

     public class UnitOfWork : IUnitOfWork { private readonly FooContext _dbContext; public UnitOfWork() { _dbContext = new DataContext; } public void Save() { _dbContext.SaveChanges(); } // Dispose method } 

为我服务:

public class ProductService : Repository<Product, FooContext>, IProductService
{
  private readonly IProductRepository _prodRepo;
  private readonly IUnitOfWork _uow;
  public ProductService(IUnitOfWork uow, IProductRepository prodRepo)
  {
    _uow = uow;
    _prodRepo = prodRepo;
  }

  public override void Insert(Item entity)
  {
    base.Insert(entity);
    Save();
  }

  public void Save()
  {
    uow.Save();
  }

  // remove some code for brevity
}

构建它时没有错误。 当我尝试将其应用于我的Controller它不会给我一些错误。 但是,当我尝试运行和调试它时,在Intellitrace中,它没有给我Insert语句,也没有给我一个错误。

任何帮助将非常感激。 谢谢!

我们在一起看到,您应该将服务与存储库分开。

在这里,您的问题似乎来自使用两个不同的dbcontext实例的事实。 一个在您的存储库中,另一个在您的UnitOfWork中。

相反,您应该在存储库和UnitOfWork中注入相同的Dbcontext实例。

编辑:您应该这样编写您的不同层:

public class ProductService
{
    private readonly IProductRepository productRepository;

    private readonly IUnitOfWork unitOfWork;

    public ProductService(IProductRepository productRepository, IUnitOfWork unitOfWork)
    {
        this.productRepository = productRepository;
        this.unitOfWork = unitOfWork;
    }

    public IEnumerable<Product> GetCurrentProductsOnOrderForCustomer(int customerId)
    {
        // etc.
    }
}

控制器层应执行以下操作:

public class ProductController : Controller
{
     private readonly IProductService prodService;

     public ProductController(IProductService prodService)
     {
         this.prodService = prodService;
     }
}

这是您更正后的UnitOfWork:

public class UnitOfWork : IUnitOfWork
{
    private readonly IDbContext _dbContext;

    public UnitOfWork(IDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public void Save()
    {
        _dbContext.SaveChanges();
    }

    // Dispose method
}

这是一个仓库的例子

public class ProductRepository
{
    private readonly IDbContext _context;

    public ProductRepository(IDbContext dbContext)
    {
        this._context = context;
    }    

    public virtual void Insert(T entity)
    {
        this._context.Products.Add(entity);
    }

       // remove some code for brevity
}

然后创建一个上下文类,该类继承自DbContext,并注入UoW和Repos。

public class MyApplicationContext : DbContext
{
    public MyApplicationContext(string connectionString)
    {
        // Configure context as you want here
    }

    public DbSet<Product> Products { get; set; }
}

暂无
暂无

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

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