快速Q为你模式专家。

我想要一个与实际数据访问技术分离的存储库模式,因为我还没有决定,我希望它具有灵活性。所以,这可能是L2S,L2E,NHibernate,Lightspeed或其他任何东西。

但是我对这个UnitOfWork事情感到困惑。

在L2S世界中,这似乎是您的DataContext。

但是对于非L2S世界,假设我使用的是手写SQL。

我的问题是谁做了什么?在我的Repo.Save()方法中,它应该调用UnitOfWork.Commit,然后生成所需的INSERT / UPDATE SQL吗?

不期待一个明确的答案,但一些讨论会很好,只是为了确保我走在正确的轨道上!

由于

有帮助吗?

解决方案

存储库肯定可以在工作对象单元上调用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 DataContext
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top