简体   繁体   English

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

[英]applying unit of work in a generic repository

I'm learning on doing repository with unit of work. 我正在学习使用工作单元进行存储库。 I also know how to do DI/IOC. 我也知道如何做DI / IOC。 But my problem is I can't figure out where to apply Unit of Work in my code. 但是我的问题是我不知道在我的代码中将Unit of Work应用于何处。 Below is a Generic Repository. 以下是通用存储库。

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
    }

What I had tried so far: 到目前为止我尝试过的是:

  1. Make a Unit of Work class 制作Unit of Work

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

In my service: 为我服务:

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
}

There's no error when I build it. 构建它时没有错误。 And when I try it apply in my Controller it doesn't give me some error. 当我尝试将其应用于我的Controller它不会给我一些错误。 But when I try to run and debug it, in the Intellitrace, It doesn't give me an Insert statement and again, it does not give me an error.. Where have I gone wrong? 但是,当我尝试运行和调试它时,在Intellitrace中,它没有给我Insert语句,也没有给我一个错误。

Any help would be much appreciated. 任何帮助将非常感激。 Thanks! 谢谢!

We saw together you should separate your service from your repository. 我们在一起看到,您应该将服务与存储库分开。

Here, your problem seems to come from the fact you use two different dbcontext instances. 在这里,您的问题似乎来自使用两个不同的dbcontext实例的事实。 one in your repository and one in your UnitOfWork. 一个在您的存储库中,另一个在您的UnitOfWork中。

Instead, you should inject the same Dbcontext instance in your repository and your UnitOfWork. 相反,您应该在存储库和UnitOfWork中注入相同的Dbcontext实例。

EDIT: You should write your different layers like this: 编辑:您应该这样编写您的不同层:

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.
    }
}

The controller layer should do this: 控制器层应执行以下操作:

public class ProductController : Controller
{
     private readonly IProductService prodService;

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

Here is your corrected UnitOfWork: 这是您更正后的UnitOfWork:

public class UnitOfWork : IUnitOfWork
{
    private readonly IDbContext _dbContext;

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

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

    // Dispose method
}

Here is an example of Repository 这是一个仓库的例子

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
}

And you create a context class that inherits from DbContext, and that you inject in UoW and Repos. 然后创建一个上下文类,该类继承自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