A few years ago I was responsible for selecting the WCSF (Web Client Software Factory) as the architecture for a website for one of my clients; at the time it was 'the' MVC based framework being touted most heavily by Microsoft.
Although it eventually turned out to be a mixed experience over the life of the project (particularly the Guidance Toolkit aspect), the feature that caught my eye when choosing it was the use of WF state machines as an underlying navigation management framework. This seemed particularly appropriate for the project as the previous version of the website had suffered badly from poorly implemented and unmaintainable navigation (the site presented multistage applications for financial products with complex dependencies between data and the order and display of pages).
Anyway, to paraphrase the 15th century French, 'WCSF is dead. Long live Asp.Net MVC!' So with this in mind, I thought I would see how easy/useful it would be to re-implement the use of WF as the basis of a generic 'wizard' MVC controller - separating the responsibility for all navigation to an underlying workflow.
At first I planned to use a standard state machine workflow wrapped in an MVC Controller class in the same way the WCSF had worked - but then I remembered Matt Winkler's Pageflow sample. This was a specialised workflow type designed explicitly to model navigation graphs and released informally through his blog back in mid 2007 (sadly too late for my first project).
So, after downloading and re-familiarising myself with this, I set about coding a generic MVC Controller class that would support the standard multi-step actions we all expect in wizards (Start/Resume/Previous/Next/Abort) by delegating the required view selection logic to an instance of a Pageflow workflow. To make this vaguely realistic, I decided that anything I wrote had to be able to reproduce 'Workflow1' sample included with the download of the Pageflow bits (a 6 page (with two data driven steps) helpdesk request example). I also restricted myself to referencing the Pageflow DLL (Microsoft.Samples.Workflow.UI.dll) without recompilation.
Here is what I came up with:
MvcPageflow.csproj
This class library is the crux of the solution and where all the Pageflow to MVC logic resides, in particular:
- The WorkflowWizardController class contains the majority of the code. It is a generic class (parameterised by a Pageflow derived type) that defines the actions described above and handles all the necessary communication with the Pageflow instance.
- The WizardControllerViewData (internal) class. This is passed to all views (which are defined as ViewPage<Workflow1Controller.WizardControllerViewData> to allow them to enable/disable various navigation options and to receive the output of the previous Pageflow transition.
- The HttpCookieWizardContextManager class which is the default implementation for handling the correlation of client requests and Pageflow instances (see below).
- EnhancedBookmark class which derives from the original Bookmark class by adding IsStartingActivity/IsTerminatingActivity properties necessary for the population of similar properties passed to the view.
- EnhancedNavigationManager again derived from the equivalent NavigationManager class adding extra methods for working out the IsStartingActivity/IsTerminatingActivity info for each stage.
Note: The two final classes are certainly not the most elegant implementation and are only necessary as a result of my self imposed restriction to maintain binary compatibility with the original Microsoft.Samples.Workflow.UI.dll.
MvcSampleWeb.csproj
A standard MVC web project with:
-
A Workflow1Controller (derived from WorkflowWizardController with a few logic specific methods overridden).
-
6 simple views (copied in layout from the Asp.Net pages of the original sample) and with Previous/Next/Abort links enabled based on the ViewData passed to them.
-
An edited Index view with links to Start/Resume a Workflow1Pageflow process and some explanatory text.
MvcSamplewebPageflow.csproj
A a standard workflow class library containing:
- Workflow1.xoml - the unaltered Pageflow (modelling a helpdesk request) from Matt's original Asp.Net sample.
Note: I actually started off with Workflow1.xoml in the same folder as the Workflow1Controller class in the MVC web project, but the WF designer continually crashed until I placed this in a separate assembly.
MvcSampleWebTests.csproj
This is a standard VSTest project that contains a few rudimentary tests which exercise the Workflow1Controller, checking all the views are transitioned to in the correct order and that the Resume and Abort actions work/throw appropriately.
Note: The the 'fake' context/request/response test helper classes are extracted as-is from here.
In Summary:
So exactly how hard was this, well not very hard at all. The only really interesting/difficult decisions were:
Instance Correlation: As we all know, the browser/server interaction is essentially stateless, yet state machine workflow's are fundamentally stateful so how do we reassociate a client's request for a relative action such as 'Next' with the correct Pageflow instance (which maintains the knowledge of which state Next is to be applied to). Well, the original Asp.Net example used cookies and so do I. Although for you IoC fans out there, this is factored into an interface (IWizardContextManager) and made a Controller constructor dependency for easy substitution.
Pageflow/View Communication: The original sample had a reaonably tight coupling between the Pageflow and the pages (as is the way with traditional Asp.Net). One of my goals was to write a reusable base Controller which could be derived from and used to model more than just trivial Next,Next,Next processes. If, like the Workflow1 example, these required branching logic within the Next transitions based on user entered data there needed to be an easy way to pass this into/and out of the pageflow. The solution I came up for output data was simple, make it a property on the ViewData class passed to the views. For the input data, which would vary from view to view, I added a virtual GetWorkflowInputData() method that can be overriden to provide any data into the workflow at each action. This can be seen in action in the Workflow1 controller which simply returns the appropriate request form fields depending on the view; these are then used internally by the transitions on the EnterAlias and EnterProfile Pageflow InteractionActivities to decide which activity (and so view) comes next.
Future Areas for Improvement:
WorkflowRuntime: WF requires Pageflows (as with all workflows) to run in a single instance of the runtime configured with the appropriate services. Purely for ease of coding the download currently stores this in a static field of the WorkflowWizardController - this would be better placed in the Application object. The configuration of this runtime is also currently hardcoded (see download instructions) and should be more configurable.
Download:
If you are interested in getting this to work on your machine you will need to:
- Download and install (to default location) Asp.Net MVC Preview 3 from here.
- Download and install (to default location) the Pageflow sample from here.
- Download and extract the source to this post (and open in VS2008) from here: MvcPageflow.zip (65.67 kb)
You will also need to have setup a persistence service SQL database (this page has a paragraph on how to do this) and update the connection string in WorkflowWizardController static constructor (I know this should be read from the web.config).