简体   繁体   English

OO设计用于业务逻辑

[英]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. 所以我想问一下我是否应该在Discharge方法中调用存储库函数,或者我最好在Discharge方法之外调用存储库save方法。 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? 使用Service类还是更好的方法?

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. 假设您只需要释放oneBuyOperation,那么也可以释放两个购买操作。 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. 如果每次调用Discharge都会通过Save方法保持salesOperation,如果第二个失败,您将无法可靠地回滚更改。

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

    UnitOfWork.CurrentSession.Save(sellOperation);
}

For instance. 例如。

Apply Observer Pattern in this case. 在这种情况下应用观察者模式

Let all buy objects register themselves to listen on Sell object's Discharge() method call. 让所有购买对象注册自己来监听Sell对象的Discharge()方法调用。 And call discharge this way: 并以这种方式呼叫放电:

sellOperation.Discharge();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM