tags:

views:

236

answers:

1

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?

A: 

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.

David Schmitt