bbv.Common.StateMachine
class是我见过的最好的状态机器代码。但它缺乏一件事:获得现状。如何从bbv.Common.StateMachine(现在是Appccelerate.StateMachine)类获取当前状态?
这是一个订单跟踪系统:
fsm = new ActiveStateMachine<States, Events>();
fsm.In(States.OrderCreated)
.On(Events.Submitted)
.Goto(States.WaitingForApproval);
fsm.In(States.WaitingForApproval)
.On(Events.Reject)
.Goto(States.Rejected);
fsm.In(States.WaitingForApproval)
.On(Events.Approve)
.Goto(States.BeingProcessed);
fsm.In(States.BeingProcessed)
.On(Events.ProcessFinished)
.Goto(States.SentByMail);
fsm.In(States.SentByMail)
.On(Events.Deliver)
.Goto(States.Delivered);
fsm.Initialize(States.OrderCreated);
fsm.Start();
fsm.Fire(Events.Submitted);
// Save this state to database
你可以看到它是如何工作的轻松。
但我想将订单状态保存在数据库中。因此,我将能够显示订单处于哪种状态。
我需要一个
fsm.GetCurrentState()
//show this state in the a table
方法。其实有一种方法:我可以使用ExecuteOnEntry
并在每个州的条目上更改本地值。但为每个州写ExecuteOnEntry
会很麻烦,因为我会重复自己!
必须有一个微妙的方式来做到这一点。
难道你知道bbv.Common已被重命名为Appccelerate。看看我们的新主页http://www.appccelerate.com –