views:

1417

answers:

3

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  A: 

By the sounds of it on the surface, I would say the Unit of Work pattern could be suited. A good example of this using NHibernate:

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx

And a reference for a great book:

Patterns of Enterprise Application Architecture By Martin Fowler et al.

EDIT: From your updated comment I would also say Optimistic Concurrency would be a vital component.

REA_ANDREW
Unit of work?? Not at all
Chev
+1  A: 

Design Pattern's are not specific to any language (C# design pattern?).

I suggest you should focus on building a framework for your workflow implementation, by using different patterns, instead of just thinking about an ideal pattern that may suit all your needs.

Recently read an article in Javaworld about using Dispatcher pattern to implement the workflow.

See http://www.javaworld.com/javaworld/jw-10-2001/jw-1019-dispatcher.html

You might also consider evaluating alternate, existing open source workflow frameworks in C#, like NetBPM http://www.netbpm.org/

amazedsaint
+3  A: 

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.

Chris S