Skip to content

Implementing IStatefulObject

tkellogg edited this page May 9, 2011 · 3 revisions

All objects that are sent through stateful workflows must implement IStatefulObject. This interface has two methods:

interface IStatefulObject {
    object GetStateId(object workflowId);
    void SetStateId(object workflowId, object stateId);
}

As far as implementing IStatefulObject, there are basically two levels you can do. You can assume that the object will never be in multiple workflows simultaneously, in which case the implementation is simple:

class SiteVisit : IStatefulObject
{
    public int SiteVisitId { get; set; }
    public string WorkflowState { get; set; }
    public object GetState(object workflow) { 
        return WorkflowState;
    }
    public object SetState(object workflow, object state) {
        WorkflowState = state;
    }
}

This is the simplest [and recommended] implementation if it makes sense.

If you need to allow the possibility that an object can be in several workflows simultaneously, you can do this by introducing a second object (WorkflowState in this case) that maps workflows and states:

class SiteVisit : IStatefulObject
{
    public int SiteVisitId { get; set; }
    public IList<WorkflowState> WorkflowStateMapping { get; set; }
    public object GetState(object workflow) { 
        return WorkflowStateMapping.First(x => x.WorkflowId == (int)workflow.WorkflowId).State;
    }
    public object SetState(object workflow, object state) {
        WorkflowStateMapping.First(x => x.WorkflowId == (int)workflow.WorkflowId).State = state
    }
}

class WorkflowState {
    public int WorkflowId { get; set; }
    public string State { get; set; }
}

It all depends on your business requirements.

Clone this wiki locally