如何在MVC5和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-application上的微软教程,架构师现在建议不再需要存储库和工作单元模式,但他们在示例中没有提供任何如何实现或构建存储库的示例?
有些人甚至开始将存储库重命名为服务?
您如何使用EF6来构建存储库和实现MVC5中的工作单元呢?或者另一种解决方案是什么?
我正在做以下工作,但不确定这是否是最好的解决方案,以及如何添加工作单元?
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.
} 示例代码会很有帮助。
发布于 2016-06-02 19:11:13
人们能找到的最好的工作例子是米塔尔在codeproject的系列文章,他使用了Entity Framework、Generic Repository pattern和Unit of Work。跟上他,你会了解到这一切是如何工作的,这里是链接Mittal Series
发布于 2017-09-05 03:29:03
正如@Thomas已经在评论中声明的那样,我认为您在这里需要的是一个服务层,而不是一个存储库。这两个术语经常互换使用,但它们不是一回事。
存储库模式旨在提供数据库的抽象,以便数据库可以在不影响其余代码的情况下更改。EF6中的DBContext已经为您完成了这项工作,例如,您可以拥有一个名为one thing的表,但它映射到一个具有不同名称的类。DBContext还已经实现了工作单元模式,因为在您对上下文调用SaveChanges/SaveChangesAsync之前,它将执行单个事务内的所有操作。
然后,服务层向用户接口层提供方法。它调用存储库中的方法来完成此操作。
在一个简单的模型中,看起来服务层和存储库是一回事,但是您的存储库通常会映射到一个业务对象(例如,联系人),服务层可能会封装许多对象(例如,客户业务实体,保存时会将数据存储到Contact和CustomerProfile存储库),使用一个工作单元来确保这两个更改提交或回滚在一起。
这篇excellent existing stack overflow answer by @ken2k更详细地介绍了这一点。
https://stackoverflow.com/questions/37589990
复制相似问题