是工作单位,将自动生成新的对象(AUTO_INCREMENT ID)交易的良好格局? [英] Is unit of work a good pattern for transactions that will auto generate new objects (auto_increment id)?

查看:191
本文介绍了是工作单位,将自动生成新的对象(AUTO_INCREMENT ID)交易的良好格局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从工作模式的单位我uderstand做基于某些领域repostiries typic交易的方法(使用每域对象存储库)。例如:在UOW对象定义一些资源库对象后,提交基于theyr状态的那些库。

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 .

当插入()导致发生一个创建一个新对象的什么(自动生成的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提交是RAN对象产生具体而复杂的事务。

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奥姆斯知道如何处理调用数据库的顺序。例如。 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'.

一旦实体被添加到DB可以检索结果的 @@ IDENTITY / SCOPE_IDENTITY ,然后更新它的编号使用任何合适的解决方案。

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天全站免登陆