数据库对象的工作单元是什么?

发布于 2024-08-05 08:37:51 字数 86 浏览 6 评论 0原文

通俗地说,什么是数据库对象的工作单元?我正在研究如何将数据库表转换为 C# 类,并且经常遇到这个术语,但每个人似乎都在描述它,好像您应该已经知道它是什么一样。

In layman's terms, what is a unit of work in regards to database objects? I'm researching how to convert database tables into C# classes and I frequently come across this term, but everyone seems to describe it as if you should already know what it is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没︽人懂的悲伤 2024-08-12 08:37:51

我将在这里引用Martin Fowler,因为我认为他的含义是最清晰、最容易理解的之一我见过:

工作单元会跟踪您在业务事务期间所做的所有可能影响数据库的操作。当您完成后,它会计算出由于您的工作而需要执行的所有更改数据库的操作。

I'll quote Martin Fowler here, since I think his meaning is one of the clearest, most comprehensible I've seen:

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.

玩心态 2024-08-12 08:37:51

基本上,它意味着完成原子操作所需的工作,例如在两个支票账户之间转账。

示例(伪代码)

Procedure TransferBetweenAccounts(Amount, Account source, Account target)
   Begin Transaction
      Debit source account By (Amount)   }----Unit of
      Credit target account By (amount)  }----Work 
   End Transaction

   If Transaction Failed
       Roll Back

这里有一篇描述工作单元和持久性无知的优秀 MSDN 文章:
http://msdn.microsoft.com/en-us/magazine/dd882510。 ASPX

Basically it means the work required to complete an atomic action, e.g. transferring money between two checking accounts.

Example (in pseudocode)

Procedure TransferBetweenAccounts(Amount, Account source, Account target)
   Begin Transaction
      Debit source account By (Amount)   }----Unit of
      Credit target account By (amount)  }----Work 
   End Transaction

   If Transaction Failed
       Roll Back

A good MSDN article describing Unit of Work and Persistence Ignorance is here:
http://msdn.microsoft.com/en-us/magazine/dd882510.aspx

空宴 2024-08-12 08:37:51

在 LINQ to SQL 中,工作单元定义为 (http://msdn. microsoft.com/en-us/library/bb546187.aspx):

数据上下文的实例应该
一生中只有一个“工作单元”。
在松耦合环境中,
工作单元通常很小,
也许是一笔乐观的交易,
包括一次调用
提交更改。因此,数据
上下文是在以下位置创建和处置的
方法范围。如果工作单位
包括对业务规则的调用
逻辑,那么通常你会想要
为此保留 DataContext 实例
整个操作过程

了解工作单元对于成功使用 LINQ to SQL 非常重要。查看此页面(更新场景),了解工作单元模式如何适应 LINQ to SQL:

http ://aspalliance.com/1414_LINQ_to_SQL_Part_4__Updating_our_Database.3

Martin Fowler 的《企业架构模式》一书有相当多的页面专门讨论该主题(第 184-194 页)。他的简短定义是:

“维护受影响对象的列表
通过商业交易和
协调更改的写入
以及并发的解决
问题。”

In LINQ to SQL a unit of work is defined as (http://msdn.microsoft.com/en-us/library/bb546187.aspx):

An instance of a data context should
have a lifetime of one "unit of work."
In a loosely-coupled environment, a
unit of work is typically small,
perhaps one optimistic transaction,
including a single call to
SubmitChanges. Therefore, the data
context is created and disposed at
method scope. If the unit of work
includes calls to business rules
logic, then generally you will want to
keep the DataContext instance for that
whole operation

Understanding the unit of work is important for success with LINQ to SQL. Have a look at this page (A update scenario), for how the unit of work pattern fits into LINQ to SQL:

http://aspalliance.com/1414_LINQ_to_SQL_Part_4__Updating_our_Database.3

Martin Fowler's Patterns of Enterprise Architecture book has quite a few pages devoted to the topic (pg 184-194). His brief definition is:

"Maintains a list of objects affected
by a business transaction and
coordinates the writing out of changes
and the resolution of concurrency
problems."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文