UnitOfWork 是否等于事务?或者它不止于此? [英] Is UnitOfWork equals Transaction? Or it is more than that?

查看:24
本文介绍了UnitOfWork 是否等于事务?或者它不止于此?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

互联网上充斥着关于UnitOfWork模式的信息;即便如此也不例外.

The internet is full of information about UnitOfWork pattern; even SO is not an exception.

我还是不明白.以我的理解UnitOfWork = Transaction in DB.就这样;不多不少.

I still do not understand something about it. In my understanding UnitOfWork = Transaction in DB. Thats all; nothing more, nothing less.

这是正确的吗?

我的困惑在于它是如何在不同的 ORM 中实现的.NHibernate 使用 ISession 不仅仅是一个 Transaction.Dapper 把一切交给你.

My confusion is due to how it is implemented in different ORMs. NHibernate uses ISession for more than just a Transaction. Dapper leaves everything to you.

我这里的问题只是关于设计模式,没有考虑任何 ORM 或技术.

My question here is about design pattern only without considering any ORM or technology.

如果不仅仅是Transaction,请解释如何.

If it is more than just Transaction, please explain how.

编辑 1

参考@David Osborne 在回答中建议的this 链接.

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.

所以这意味着 UnitOfWorkDBTransaction and More.

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.

我现在的理解正确吗?

推荐答案

It 起源, AFAIK,因为需要 ORM 工具来跟踪逻辑/业务事务期间对象的 [持久性] 状态.

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.

中间有许多 SQL 语句的数据库事务可以说也是一个工作单元.然而,我认为关键的区别在于模式中定义的工作单元已将该细节级别抽象为对象级别.

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屋!

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