首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >工作单元和L2S DataContext

工作单元和L2S DataContext
EN

Stack Overflow用户
提问于 2009-06-21 19:41:37
回答 1查看 604关注 0票数 0

给你一个模式专家的快速问答。

我想要一个从实际的数据访问技术中解耦出来的Repository模式,因为我还没有决定这一点,我希望它是灵活的。所以,这可以是L2S,L2E,NHibernate,Lightspeed或任何东西。

但我对UnitOfWork这件事感到困惑。

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

但是,假设我使用的是手工编写的SQL,那么对于非L2S世界又会怎样呢?

我的问题是谁做什么?在我的Repo.Save()方法中,是否应该调用UnitOfWork.Commit,然后由它生成所需的插入/更新SQL?

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-06-22 09:47:45

存储库当然可以在工作单元对象上调用commit/save/submit,或者它们可以将此留给使用者。我更喜欢后一种情况,因为它使使用者能够控制工作单元实例的生命周期,从而允许使用者使用多个存储库:

代码语言:javascript
复制
// 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1024606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档