Skip to content

Implementing IStatefulObject

AnBeug edited this page Feb 28, 2013 · 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 GetStateId(object workflow) { 
        return WorkflowState;
    }
    public object SetStateId(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) { 
        var item = WorkflowStateMapping.First(x => x.WorkflowId == (int)workflow.WorkflowId);
        return item.State
    }
    public void SetState(object workflow, object state) {
        var item = WorkflowStateMapping.First(x => x.WorkflowId == (int)workflow.WorkflowId);
        item.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