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

时间:2014-06-26 07:57:46

标签: c# asp.net-mvc asp.net-mvc-4 repository-pattern unit-of-work

我正在学习如何用工作单元做存储库。我也知道如何做DI / IOC。但我的问题是我无法确定在我的代码中应用Unit of Work的位置。下面是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
    }

到目前为止我尝试了什么:

  1. 制作Unit of Work课程

    public class UnitOfWork : IUnitOfWork
    {
      private readonly FooContext _dbContext;
      public UnitOfWork()
      {
        _dbContext = new DataContext;
      }
    
      public void Save()
      {
        _dbContext.SaveChanges();
    
      }
    
      // Dispose method
    }
    
  2. 在我的服务中:

    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语句,而且它再也没有给我一个错误..我哪里出错?

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

1 个答案:

答案 0 :(得分:1)

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

在这里,您的问题似乎来自于您使用两个不同的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; }
}