Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Currently I have one unit of work for the order entry screen. The problem is, delivery company changes should be persisted when in the child form.

How can I implement this scenario using the Unit of work pattern?

  1. OrderEditForm and DeliveryCompanyCollectionEditForm should share the same DeliveryCompanyRepository that shares the same unit of work.

  2. 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.

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?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top