Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample code for workflows with visitor pattern
public class Claim
{
public Workflow Workflow { get; }
}
public abstract class Workflow
{
public abstract void Fire(Transition transition, Claim claim);
}
public class FullWF : Workflow
{
public override void Fire(Transition transition, Claim claim)
=> transition.ExecuteFor(this, claim);
}
public class WF1 : Workflow
{
public override void Fire(Transition transition, Claim claim)
=> transition.ExecuteFor(this, claim);
}
public class WF2 : Workflow
{
public override void Fire(Transition transition, Claim claim)
=> transition.ExecuteFor(this, claim);
}
public abstract class Transition
{
protected virtual void BaseExecute(Workflow workflow, Claim claim)
{
// do something common to all wf and all transition e.g. save something to db
}
public virtual void ExecuteFor(FullWF fullWF, Claim claim) => BaseExecute(fullWF, claim);
public virtual void ExecuteFor(WF1 wf1, Claim claim) => BaseExecute(wf1, claim);
public virtual void ExecuteFor(WF2 wf2, Claim claim) => BaseExecute(wf2, claim);
}
public class T_start : Transition { }
public class T_end : Transition { }
public class T_1_2 : Transition { }
public class T_1_5 : Transition { }
public class ClaimDto
{
public WorkflowDto Workflow { get; set; }
}
public class WorkflowDto
{
public TransitionDto[] AvailableTransitions { get; set; }
}
public enum TransitionDto
{
T_start,
T_end,
T_1_2,
T_1_5
}
public class TransitionResolver
{
public Transition Resolve(TransitionDto dto)
{
return dto switch
{
TransitionDto.T_start => new T_start(),
TransitionDto.T_end => new T_end(),
TransitionDto.T_1_2 => new T_1_2(),
TransitionDto.T_1_5 => new T_1_5(),
// so on
_ => throw new InvalidOperationException("Unexpected transition dto value")
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment