UnitOfWork 模式和原子操作
我不能 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我不认为这是苹果和橙色实体之间的关系。
只是 Apple 有一个名为 OrangeID 的数字属性。 您的代码应该如下所示:
然后,您可以检查数据库关系是否已正确建立。
因此,现在没有证据证明您的工作单元实施是否有效。
此外,如果您想节省一些时间,还有许多可用的实现。
虽然,我已经重新发明了轮子,并且使用了我的实现:)
如果你想看一下它托管在谷歌代码上:
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:
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.