工作单元是否是自动生成新对象(auto_increment id)的事务的好模式? [英] Is unit of work a good pattern for transactions that will auto generate new objects (auto_increment id)?

查看:18
本文介绍了工作单元是否是自动生成新对象(auto_increment id)的事务的好模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从工作单元模式中,我理解了一种基于某些域 repostiries(使用每个域对象的存储库)进行典型事务的方法.示例:在 UoW 对象中定义一些存储库对象后,根据它们的状态提交这些存储库.

From the unit of work pattern i uderstand a method of doing typic transactions based on some domain repostiries (using a repository per domain object) . Example : after defining some repository objects in the UoW object , commit those repositories based on theyr state .

此外,存储库不应包含任何事务逻辑.

Also the repositories should not contain any transaction logic .

当 insert() 导致创建一个新对象(自动生成的 id)时会发生什么,该对象稍后被同一事务中的另一个对象需要?

What happens when an insert() leads to a creation of a new object (auto generated id) that later on is needed by another object in the same transaction ?

工作单元似乎不适用于这种情况.运行 UoW 提交时可能会生成更具体和更复杂的事务.

Unit of work does not seem to work for this case . There could be even more specific and complex transaction where objects are generated when the UoW commit is ran .

在这种情况下应该如何处理交易?

How should the transactions be treated in this case ?

推荐答案

通常像 NHibernate 或 EntityFramework 这样的 ORM 知道如何处理对 DB 的调用顺序.例如.NHibernate的逆用于指定谁负责双向关系.

Usually ORMs like NHibernate or EntityFramework know how to handle the order of calls to DB. E.g. NHibernate's inverse is used in order to specify who is responsible for bidirectional relationship.

如果您开发自己的 DataAccessLayer/ORM,则您有责任指定调用顺序.最简单的解决方案是添加所有新实体"=>删除所有已删除的实体"=>更新所有脏实体".

If you develop your own DataAccessLayer/ORM it is your responsibility to specify the invocation order. The simplest solution is 'Add all new entities' => 'Delete all deleted entities' => 'Update all dirty entities'.

将实体添加到数据库后,您可以检索其@@IDENTITY/SCOPE_IDENTITY,然后使用任何适当的解决方案更新其 Id.

Once an entity is added to DB you can retrieve as a result its @@IDENTITY/ SCOPE_IDENTITY and then update its Id using any appropriate solution.

这篇关于工作单元是否是自动生成新对象(auto_increment id)的事务的好模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆