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 more 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".
I just read about http://www.codeplex.com/SimpleStateMachine , but I'm not sure if it isn't too complex for this case. I could handle the state logic in my service layer using things like:
if (promotion.state == statesRepository.GetState3() && false == loggedUser.IsInRole("superUser")){
throw new PromotionStateException("user not allowed to edit promotion in this status");
}
...
or
public void ChangePromotionStatus(promotion, newStatus){
if (promotion.Status == status1 && newStatus != statesRepo.GetState2()){
throw new StateTransitionException("unable to change from status 1 to " + newStatus);
}
}
But I don't like this kind of code - there must be some better approach :) Does anybody have an advice? I could separate the concerns of course and develop services like PromotionStatusChangeReviewService, PromotionEditPermissionService etc to make the code less coupled, but there's probably some better solution I can't see at the moment.