UnitOfWork是否等于事务?还是比这更多? [英] Is UnitOfWork equals Transaction? Or it is more than that?
问题描述
互联网上有关于 UnitOfWork
模式的信息;即使是这样也不例外。
我还是不了解它。在我的理解 UnitOfWork = DB中的事务
。就这样;
这是否正确?
我的困惑是因为它是如何实现的不同的 ORM
s。 NHibernate
使用 ISession
不仅仅是一个交易
。
我的问题是关于设计模式,不考虑任何 Dapper
> ORM 或技术。
如果不仅仅是交易
,请说明如何。
编辑1
参考这个链接,由@David Osborne回答。
工作单元跟踪可能影响数据库的业务
事务中执行的所有操作。完成后,它将
的所有数据都改成数据库作为结果
的所有工作。
所以这意味着 UnitOfWork
是 DBTransaction
和更多。 / p>
以下是额外的责任: -
-
保持什么状态您在本次工作中已更改,插入,删除。
-
根据此状态,工作完成后修改数据库。
尽管上面的引用没有明确提及,但也可以控制查询的批处理。
我的理解正确吗?
它发起,AFAIK,需要ORM工具来跟踪逻辑/业务交易中对象的[持久性]状态。
单位如何的工作管理这个,并且它与底层存储技术和存储的对象的关系是一个实现细节。
数据库事务之间有多个SQL语句,也可以说是一个工作单位。然而,我认为,关键的区别在于,在模式中定义的工作单元已将该细节层次提升到对象级别。
The internet is full of information about UnitOfWork
pattern; even SO is not an exception.
I still do not understand something about it. In my understanding UnitOfWork = Transaction in DB
. Thats all; nothing more, nothing less.
Is this correct?
My confusion is due to how it is implemented in different ORM
s. NHibernate
uses ISession
for more than just a Transaction
. Dapper
leaves everything to you.
My question here is about design pattern only without considering any ORM
or technology.
If it is more than just Transaction
, please explain how.
Edit 1
Reference to this link as suggested in answer by @David Osborne.
A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work.
So this means UnitOfWork
is DBTransaction
and More.
Following are it's additional responsibilities: -
Maintain state of what you have changed, inserted, deleted in this session of work.
Based on this state, modify the database when work is done.
Although not clearly mentioned in quote above, but it also may control batching of queries.
Is my understanding correct now?
It originates, AFAIK, from the need for ORM tools to track the [persistence] state of objects during a logical/business transaction.
How a unit of work manages this, and its relationship with the underlying storage technology and the objects stored, is an implementation detail.
A database transaction with a number of SQL statements in between, is arguably also a unit of work. However, the key difference, I suppose, is that the unit of work, as defined in the pattern, has abstracted that level of detail to an object level.
这篇关于UnitOfWork是否等于事务?还是比这更多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!