工作单元问题
在以下场景中我应该如何使用工作单元模式:
我正在编写一个 winforms 应用程序。 我有一个屏幕,用户可以在其中编辑单个订单。 在此屏幕上,用户可以打开另一个表单来选择送货公司。 在进行选择之前,用户还可以在此子表单中添加/编辑现有的送货公司。
如何使用工作单元模式来实现此场景? 目前我有一个订单输入屏幕的工作单元。 我的第一个想法是将子表单也包含在这个工作单元中。 问题是,交付公司的更改应该在子表单中保留。 但是,当我保留对送货公司的更改时,这也会保留订单中的更改。
我是否应该创建第二个工作单元来对交付公司进行任何编辑? 在这种情况下,如何使该工作单元中的更改在第一个工作单元中可见?
How should I use the unit of work pattern in the following scenario:
I'm writing a winforms app.
I have one screen where the user can edit a single order.
On this screen, the user can open another form to select the delivery company. The user can also add/edit existing delivery companies in this child form before doing the selection.
How can I implement this scenario using the Unit of work pattern?
Currently I have one unit of work for the order entry screen. My first thought was to include the child form in this unit of work too. The problem is, delivery company changes should be persisted when in the child form . But when I persist changes to the delivery companies, this will also persist the changes in the order.
Should I create a second unit of work for any edits to the delivery companies? In that case, how can I make the changes in that unit of work visible in the first unit of work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的描述来看,您似乎确实有两个工作单元。 第一个是“订单输入”,第二个是“编辑送货公司”。 每个工作单元都有某种底层类型的会话或事务。 要从子表单与订单条目进行通信,您必须将公司的对象与子表单的会话分离,然后将其重新附加到父表单。 如何实际实现这一点取决于您使用的数据访问层,但一个简单的方法是传递公司的 ID。
From your description it sounds as if you really have two units of work here. The first is "Order Entry" and the second one is "Edit Delivery Company". Each unit of work has some underlying kind of session or transaction. To communicate from the child form to the order entry you'll have to detach the company's object from the child form's session an re-attach it to the parent. How to actually implement this depends on the data access layer you're using, but an easy way is to pass around the ID of the company.
OrderEditForm
和DeliveryCompanyCollectionEditForm
应共享相同的DeliveryCompanyRepository
并共享相同的工作单元。DeliveryCompanyRepository
应从数据库返回公司 + 在工作单元期间添加的公司。这样,新的送货公司对象应该可以在
OrderEditForm
中使用,而无需保留更改。并行使用的两个或多个工作单元需要特别注意数据同步。 当一个工作单元提交时,某个调解器通常会更新所有其他工作单元,正如另一个答案中已经指出的那样。
OrderEditForm
andDeliveryCompanyCollectionEditForm
should share the sameDeliveryCompanyRepository
that shares the same unit of work.DeliveryCompanyRepository
should return companies from database + companies that were added during unit of work.This way a new delivery company object should be available in
OrderEditForm
without persisting changes.Two or more units of work used in parallel require special attention in data synchronization. When one unit of work commits, some mediator usually updates all other units of work, as already stated in the other answer.