Sample code for workflows with visitor pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
For huge Bussiness Logic, any good Workflow patterns or ROP pattern ?
Many tasks like https://github.com/aelassas/Wexflow/wiki/Samples#flowchart-workflows
Sample: Connect to sFTP, download file CSV using WinSCP
read CSV using FileHelpers
Get List of Entity, Insert in SQL Server (transactional)
Move file CSV to another directory in sFTP
Send Email OK
Error handling if fails in any part of process...send email priority High for errors, logging in text file or database ?