user192415
user192415

Reputation: 561

OO design for business logic

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?

Upvotes: 3

Views: 162

Answers (2)

this. __curious_geek
this. __curious_geek

Reputation: 43207

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

Upvotes: 0

Michael
Michael

Reputation: 20049

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.

Upvotes: 4

Related Questions