1

Is there a design pattern that can be applied where I can build sophisticated workflows without having to write multiple conditional and nested conditional statements - I think the answer may lie somewhere in employing something like the Specification Pattern but am unsure how it comes together in C#?

   static void Main()
    {
        // I have some predicates
        bool transientError = true;
        bool fatalError = true;
        bool maxRetriesReached = true;

        // I have some actions
        Action SendToDeadletter = () => { };
        Action Retry = () => { };
        Action Complete = () => { };

        // Is there a way of building the following without using conditional statements
        // something like Specification Pattern?

        // maxRetriesReached || fatalError => SendToDeadletter()
        // transientError && !maxRetriesReached => Retry()
        // !transientError && !fatalError => Complete()

    }
puri
  • 1,829
  • 5
  • 23
  • 42
  • Even I find it okay in your example to use the conditional statements, however, you can have a look at [this](https://www.codeproject.com/Articles/670115/Specification-pattern-in-Csharp) – Yahya Nov 23 '17 at 20:09
  • 1
    A [StateMachine](https://www.hanselman.com/blog/Stateless30AStateMachineLibraryForNETCore.aspx) could solve this – Sir Rufo Nov 23 '17 at 20:12

3 Answers3

0

You can define your conditions as an enum (or a struct have more expandability):

[Flags] 
public enum WorkflowFlags
{
    SendToDeadletter, 
    Retry, 
    Continue 
} 

And then define workflow items with a method, say ShouldExecute that takes in the flags and returns true/false if the item should be ran.

public class MyWorkflowItem : IWorkflowItem
{
    public bool ShouldExecute(WorkflowFlags flags) 
    {
        return (flags & WorkflowFlags.Retry) != 0;
    } 
} 
Matan Shahar
  • 3,190
  • 2
  • 20
  • 45
0

You could probably use the Specification Pattern to code your logic There is a c# example on how to do this here

https://en.wikipedia.org/wiki/Specification_pattern

You probably found plenty of example online.

With your explanations I fail to understand the why you would use the Specification Pattern to code you logic.

At a point you are forced to use a conditional statement.

It seems like you may want something like that however.

public static void Reaction(this Func<bool> condition, Action conditionMet,Action conditionUnmet)
{
    condition() ? conditionMet() : conditionUnmet();
}

Using it the following way

Func<bool> CanDoAction = ()=> false;
Action behaviorAction = ()=> DoStuff;
Action behaviorUnableToDoAction = ()=> DoOtherStuff;

CanDoAction.Reaction(behaviorAction ,behaviorUnableToDoAction );
JM123
  • 167
  • 1
  • 12
0

Honestly, I think you're barking up the wrong tree. You can combine conditionals into a single specification to encapsulate them, or you can create different specs for different conditionals to name them, but I think other patterns are more appropriate - If I am inferring your need correctly.

You want to implement "sophisticated workflows". To me, this is saying you want to implement business use cases that have lots of branching points. For those, some form of the Strategy pattern is the best place to start.

VeteranCoder
  • 444
  • 1
  • 4
  • 12