I m developing sales promotion system and I just stepped on something that could be probably handled with state machine pattern, but I have no experiences with state machines yet. Maybe the state machine is totally useless in this situation :) So I have a sales promotion which has some duration, some assigned customers, products, discounts etc. Each promotion also has it s state. There s about 5 states. The transitions between states are strictly defined - it s not possible to change state 1 to state 3 directly - user has to change state to 2 first. There re some limitations like "it s not possible to add m或e products when promotion is in state 3-5". Or limitations like "only super-users can edit promotion costs when it s in state 3-5".
我刚读到关于http://www.codeplex.com/SimpleStateMachine,但我不确定对于这种情况来说是否太复杂。我可以使用以下内容来处理服务层中的状态逻辑:
if (promotion.state == statesReposit或y.GetState3() && false == loggedUser.IsInRole("superUser")){
throw new PromotionStateException("user not allowed to edit promotion in this status");
}
...
或
public void ChangePromotionStatus(promotion, newStatus){
if (promotion.Status == status1 && newStatus != statesRepo.GetState2()){
throw new StateTransitionException("unable to change from status 1 to " + newStatus);
}
}
但我不喜欢这种代码——一定有更好的方法:)有人有建议吗?当然,我可以将这些问题分开,开发PromotionStatusChangeReviewService、PromotionEditPermissionService等服务,以减少代码的耦合,但可能有一些更好的解决方案我目前看不到。