繁体   English   中英

MVC 5 EF6工作单元和存储库模式

[英]MVC 5 EF6 unit of work and repository pattern

如何在MVC 5和EF6中实现工作单元和存储库模式? 以前我通过使用注入我的控制器的单个存储库来避免对工作单元的任何需求,如下所示:

public class ProductController : BaseController
{
    private IShopRepository _repository;


    public ClassController()
        : this(new ShopRepository())
    {
    }

    public ClassController(IShopRepository repository)
    {
        _repository = repository;
    }


    ....

}

但现在我想重构代码,以便为每个实体类型设置一个单独的存储库,例如。 ProductRepository,CustomerRepository等能够将多个存储库注入控制器,同时确保使用相同的dbcontext。

阅读http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/advanced-entity-framework-scenarios-for-an-mvc-上的微软教程Web应用程序 ,架构师现在建议存储库和工作单元模式不再需要,但它们没有提供如何在其示例中实现或构建存储库的任何示例?

有些人甚至开始将存储库重命名为服务?

如何使用EF6构建您的存储库并在MVC5中实现工作单元,也许使用Unity等IOC? 或者是另一种解决方案?

我正在努力以下几行,但不确定它是否是最佳解决方案,我如何添加工作单元?

public class ShopContext : DbContext
{
    public ShopContext() : base("name=ShopContext")
    {
    }

    public DbSet<Product> Products { get; set; }
    public DbSet<Customer> Customers { get; set; }
    ...

}


public interface IProductRepository
{
   IEnumerable<Product> GetAll();
   ...
}


public interface ICustomerRepository
{
   IEnumerable<Customer> GetAll();
   ...
}

public class ProductRepository : IDisposable, IProductRepository
{
     private ShopContext _context;

     public ProductRepository()
     {
    _context  = new ShopContext();

     }

     public IEnumerable<Product> GetAll()
     {
        return _context.Products;
     }

     // Other methods not displayed

     protected void Dispose(bool disposing)
     {
       if (disposing)
       {
           if (_context != null)
           {
              _context.Dispose();
              _context = null;
           }
       }
     }

     public void Dispose()
     {
       Dispose(true);
       GC.SuppressFinalize(this);
     }

}

public class CustomerRepository : IDisposable, ICustomerRepository
{
     private ShopContext _context;

     public CustomerRepository()
     {
        _context  = new ShopContext();

     }

     public IEnumerable<Customer> GetAll()
     {
        return _context.Customers;
     }

     // Other methods not displayed

     protected void Dispose(bool disposing)
     {
       if (disposing)
       {
           if (_context != null)
           {
              _context.Dispose();
              _context = null;
           }
       }
     }

     public void Dispose()
     {
       Dispose(true);
       GC.SuppressFinalize(this);
     }
}


public class ProductsController : BaseController
{

   private IProductRepository _productRepository;
   private ICustomerRepository _customerRepository;


   public ProductsController()
        : this(new ProductRepository(), new CustomerRepository())
    {
    }

   public ProductsController(IProductRepository productRepository, ICustomerRepository customerRepository)  
   {
       _productRepository = productRepository;
       _customerRepository = customerRepository;

   }

   // Other controller methods not shown.
} 

示例代码会有所帮助。

最好的工作示例,可以发现在该系列米塔尔的codeproject他使用Entity FrameworkGeneric Repository patternUnit of Work 跟上他,你会知道这一切是如何工作的米塔尔系列

正如@Thomas在评论中已经说过的那样,我认为你在这里所说的是服务层而不是存储库。 这两个术语经常互换使用,但它们不是一回事。

存储库模式旨在提供数据库的抽象,以便数据库可以更改而不会影响其余代码。 EF6中的DBContext已经为您做了,例如,一个名为one thing的表,但映射到具有不同名称的类。 DBContext还已经实现了工作单元模式,因为它将在单个事务中执行所有操作,直到您在上下文中调用SaveChanges / SaveChangesAsync

然后,服务层向用户界面层提供方法。 它从存储库调用方法来执行此操作。

使用简单的模型,服务层和存储库看起来是相同的,但是您的存储库通常会映射到一个业务对象(例如,联系人),因为您的服务层可能封装了许多对象(例如,客户业务实体) ,在保存时将数据存储到Contact和CustomerProfile存储库中,使用工作单元确保两个更改都一起提交或回滚。

@ ken2k的这个优秀的现有堆栈溢出回答更加详细。

暂无
暂无

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

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