Which C# design pattern would suit writing custom workflow in asp.net


Question

Trying to find some examples on the intertubes. I am thinking of state or strategy pattern but if anyone has any war stories, examples or resources that they could point me to it would be appreciated.

I don't/can't use windows workflow.

My example is that i have a complex wizard that changes the state of the process based on what user is doing and by who the user is.

For example:

  • Cancelled
  • Requested by user
  • Request by manager
  • Confirmed
  • Refereed
  • Administrator Received
  • Administrator Confirmed
  • Administrator Cancelled

Cheers John

1
2
2/27/2009 10:24:21 AM

Accepted Answer

How about the State pattern (wikipedia link)?

public abstract class State
{
    /// <summary>
    /// Holds the current state we're in.
    /// </summary>
    public State CurrentState
    { 
        get; 
        set; 
    }

    public virtual string Cancelled(State context)
    {
        return "";
    }

    public virtual string RequestedByUser(State context)
    {
        return "";
    }

    public virtual string RequestedByManager(State context)
    {
        return "";
    }
}

public class CancelledState : State
{
    public override string Cancelled(State context)
    {
        context.CurrentState = new SittingState();
        return "Cancelled.";
    }

    public override string RequestedByUser(State context)
    {
        context.CurrentState = new RequestedByUserState();
        return "Requested by User.";
    }

    public override string RequestedByManager(State context)
    {
        return "You can't do this before it's been requested by the User";
    }
}

// (RequestedByUserState and RequestedByManagerState classes have been cut out)

As you can see, the pattern does fit exactly though.

Chain of Responsibility might be also be relevant if there's security concerns. If the wikipedia article makes no sense then this book has a good examples of both. Another is the Command pattern for wizards. None of them fit perfectly but they give you some good ideas to start with.

6
6/13/2012 3:32:43 PM

"Chain of Responsibility" is more or less similar to the work flow style. That is an awesome pattern to work with with immense flexibility options. I would strongly suggest you try that.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon