0

This a general question. I am designing a system using the Unit of Work pattern. Are there any established patterns which lays out how to share logic between multiple units of work in a unit testable way?

Here is a more concrete scenario:

There is a PurchaseOrder which

UnitOfWork1 -> submits PO for approval

UnitOfWork2 -> approves or denies PO and sent it back to submitter

Both Unit of works shares same code such as:

ShareLogic1 -> User needs to have access to PO

SharedLogic2 -> Record the last action to PO

What pattern(s) can I use where such logic can be shared between multiple units of work. Though inheritance can solve the problem in this case, I don't want to use inheritance as it won't fit in every case.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Anish
  • 3,045
  • 3
  • 27
  • 29
  • This pattern is called methods -) Method is a reusable piece of code. – Alex Burtsev Dec 23 '11 at 07:53
  • That is not what I was asking. Is there pattern for sharing logic between multiple units of work? The shared code needs to be mockable. Its easy to dump the code into a bunch of helper classes but that is not what I was looking for. The beset solution I was able to come up with is to have a business logic factory which classes for for specific logic the unit of work calls for. I am not sure if this is the best pattern. I am pretty sure this is a common scenario and I wanted to know how others tackled this problem. – Anish Dec 23 '11 at 13:51
  • I think you misunderstood UnitOfWork pattern, UOF is a SCOPE in which a series of operations can be considered atomic (in simple words), .NET TransactionScope class is an example of UOC for data access. So it's a SCOPE. From what you wrote it looks like that you understood UOC as some sort of WorkFlow elemnt. – Alex Burtsev Dec 23 '11 at 14:00
  • 1
    Either you have a mess in you head, or I misunderstood you. If it's first case, then try looking at examples of Domain Driven Design applications. http://stackoverflow.com/questions/540130/good-domain-driven-design-samples – Alex Burtsev Dec 23 '11 at 14:06
  • I understand the UnitOfWork pattern. I think you misunderstood my question. If you do not understand the question, please do not comment. – Anish Dec 23 '11 at 14:28

1 Answers1

0

Put that logic in a class and let both units of work have access to it. Both examples seem to belong to a PurchaseOrderRepository class. Using inversion of control you can mock it.

Ivo
  • 8,172
  • 5
  • 27
  • 42