UnitOfWork 模式和原子操作

发布于 2024-07-30 04:26:57 字数 763 浏览 8 评论 0原文

我不能 100% 确定我已经正确实现了我的 Repository 和 UnitOfWork 模式,但是我不知道这还能如何工作。

例如,我有两个对象,Apple 和 Orange。

Apple 通过 OrangeID 加入 Orange,如下所示:

public class Apple
{
  public int OrangeID { get; set; }
}

我想创建一个新的 Apple 和一个新的 Orange,并且我想适当地设置 ID 链接。 但我有一个问题。 在将 Orange 保存到数据库之前,我不会知道 OrangeID。

因此,这意味着我将具有以下内容:

var unitOfWork = new UnitOfWork();
Orange newOrange = new Orange();
OrangeRepository.Insert(newOrange);
unitOfWork.Commit();

//newOrange will have been updated with the actual ID
Apple newApple = new Apple(newOrange.ID);
etc...

这不是原子操作,除非我有一个位于上述之外的事务。 但我认为这就是 UnitOfWork 应该处理的事情? 或者我的 UnitOfWork.Commit() 在写入数据库时​​应该分配适当的值?

任何帮助/提示将不胜感激, 谢谢 邓肯

I'm not 100% sure that I've implemented my Repository and UnitOfWork patterns correctly, but then I can't see how else this will work.

For example, I have two objects, Apple and Orange.

Apple is joined to Orange via an OrangeID like so:

public class Apple
{
  public int OrangeID { get; set; }
}

I want to create a new Apple and a new Orange, and I want to set the ID link up appropriately. But I have a problem. I won't know the OrangeID until I have saved the Orange to the database.

So this means I will have the following:

var unitOfWork = new UnitOfWork();
Orange newOrange = new Orange();
OrangeRepository.Insert(newOrange);
unitOfWork.Commit();

//newOrange will have been updated with the actual ID
Apple newApple = new Apple(newOrange.ID);
etc...

This is not an atomic operation, unless I have a transaction that sits outside the above. But I thought that was what UnitOfWork was supposed to handle? Or should my UnitOfWork.Commit() assign the appropriate values when it's writing to the database?

Any help/tips would be appreciated,
thanks
Duncan

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

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

发布评论

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

评论(1

要走就滚别墨迹 2024-08-06 04:26:57

首先,我不认为这是苹果和橙色实体之间的关系。
只是 Apple 有一个名为 OrangeID 的数字属性。 您的代码应该如下所示:

Apple apple = new Apple(newOrange);
AppleRepository.Insert(apple);
unitOfWork.Commit();

然后,您可以检查数据库关系是否已正确建立。
因此,现在没有证据证明您的工作单元实施是否有效。

此外,如果您想节省一些时间,还有许多可用的实现。

虽然,我已经重新发明了轮子,并且使用了我的实现:)

如果你想看一下它托管在谷歌代码上:
ws-helpers项目。 我需要时间将其作为一个独立的项目,因为它实际上是以前项目的一部分。

当然,最著名的实现是 Rhino-Tools/UnitOfWork。
但是,我更喜欢我的,因为我可以根据需要创建任意数量的单元,每个单元都有一个单独的事务,而在 Rhino-Tools 中始终只有一个当前单元。 但我还不确定当前实现的线程安全性。

First, I can't see this as a relation between an Apple and Orange entities.
It's just an Apple has a numeric attribute called OrangeID. Your code should be like:

Apple apple = new Apple(newOrange);
AppleRepository.Insert(apple);
unitOfWork.Commit();

Then, you may check If db relation has been established correctly.
So now there is no proof If your unit-of-work implementation works or not.

Moreover, There are many available implementations, If you want to save some of your time.

Although, I already re-invented the wheel and I use an implementation of mine :)

If you'd like to have a peek It's hosted on google code:
ws-helpers project. I need time to make this as a stand-alone project because It was actually part of a previous project.

Sure, the most famous implementation is Rhino-Tools/UnitOfWork.
But, I prefer mine because I can create as many units as I need, each with a separate transaction, while in Rhino-Tools there is always only one current unit. But I'm not yet sure about the thread-safety of current implementation.

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