tags:

views:

66

answers:

2

I have one Sell Operation object and two Buy Operation objects, i want to implement such behavior that one Sell Operation discharges two Buy Operation, it looks like:

sellOperation.Discharge(oneBuyOperation);
sellOperation.Discharge(twoBuyOperation);


so i want to ask whether i should call the repository function in the Discharge method, or i'd better call the repository save method outside Discharge method. like:

opRepository.Save(sellOpertion);

So anyone could give me some advise what are you going to implement in this scenario? using Service class or anything better way?

+4  A: 

Save should not be part of business logic - it removes the ability to wrap numerous operations within a single unit of work.

Say you only want oneBuyOperation to be discharged if twoBuyOperation can be discharged as well. If either fail, you don't want either to be persisted.

If each time you called Discharge it persisted the sellOperation through the Save method, you wouldn't be able to reliably rollback changes should the second one fail.

using(UnitOfWork.Start())
{
    sellOperation.Discharge(oneBuyOperation);
    sellOperation.Discharge(twoBuyOperation);  

    UnitOfWork.CurrentSession.Save(sellOperation);
}

For instance.

Michael Shimmins
thanks for your advice
Feel free to vote up and/or accept if you think this answer was helpful or 'the answer' to your problem.
Michael Shimmins
A: 

Apply Observer Pattern in this case.

Let all buy objects register themselves to listen on Sell object's Discharge() method call. And call discharge this way:

sellOperation.Discharge();

http://en.wikipedia.org/wiki/Observer_pattern
http://www.dofactory.com/Patterns/PatternObserver.aspx

this. __curious_geek