给你一个模式专家的快速问答。
我想要一个从实际的数据访问技术中解耦出来的Repository模式,因为我还没有决定这一点,我希望它是灵活的。所以,这可以是L2S,L2E,NHibernate,Lightspeed或任何东西。
但我对UnitOfWork这件事感到困惑。
在L2S世界中,这似乎就是您的DataContext。
但是,假设我使用的是手工编写的SQL,那么对于非L2S世界又会怎样呢?
我的问题是谁做什么?在我的Repo.Save()方法中,是否应该调用UnitOfWork.Commit,然后由它生成所需的插入/更新SQL?
不期待一个明确的答案,但一些讨论会很好,只是为了确保我在正确的轨道上!
谢谢
发布于 2009-06-22 09:47:45
存储库当然可以在工作单元对象上调用commit/save/submit,或者它们可以将此留给使用者。我更喜欢后一种情况,因为它使使用者能够控制工作单元实例的生命周期,从而允许使用者使用多个存储库:
// outside the repository layer
// int productId defined elsewhere
// int quantity defined elsewhere
IUnitOfWork unitOfWork = ... instantiate/get a new instance of your DataContext ...
ProductRepository productRepository = new ProductRepository(unitOfWork);
Product product = productRepository.GetById(productId);
Order order = new Order();
order.AddOrderLine(product, quantity);
OrderRepository orderRepository = new OrderRepository(unitOfWork);
orderRepository.Add(order);
unitOfWork.Save(); // This calls SubmitChanges() on the DataContexthttps://stackoverflow.com/questions/1024606
复制相似问题