Click here to Skip to main content
15,891,372 members
Articles / Web Development / ASP.NET

Navigational Workflows Unleashed in WWF/ASP.NET 3.5

Rate me:
Please Sign up or sign in to vote.
4.97/5 (42 votes)
21 Oct 2008CPOL18 min read 225.5K   1.6K   165  
Case-study on the internals of a Navigational Workflow engine for a fictional dating website called “World Wide Dating”.
using System;
using System.Web.UI.WebControls;

namespace DateSite
{
    /// <summary>
    /// This page is the first one in the navigational workflow. It allows creating,
    /// updating, deleting, and retrieving of the navigational workflows and dating
    /// profiles.
    /// </summary>
    public partial class _default : System.Web.UI.Page
    {
        private WorkflowManager _workflowManager;

        /// <summary>
        /// Page initialization code.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            // initialize the workflow manager
            _workflowManager = new WorkflowManager(Application, Session, Request, Response);

            // populate the gridview with existing navigational workflows
            gdvExistingProfiles.DataSource = Database.GetExistingProfiles();
            gdvExistingProfiles.DataBind();
        }

        /// <summary>
        /// Button click event to create a new dating id.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            CreateNew();
        }

        /// <summary>
        /// Text changed event to create a new dating id.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtDatingId_TextChanged(object sender, EventArgs e)
        {
            CreateNew();
        }

        /// <summary>
        /// Rehydrates the selected dating profile and redirects to the page where
        /// the user left off in the navigational workflow.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gdvExistingProfiles_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            // store the dating id in session
            Master.DatingId = gdvExistingProfiles.Rows[e.NewSelectedIndex].Cells[1].Text;

            // navigate to the page where the user left off
            Guid workflowId = new Guid(gdvExistingProfiles.Rows[e.NewSelectedIndex].Cells[2].Text);
            string pageToGoTo = _workflowManager.Rehydrate(workflowId);
            _workflowManager.ManagedRedirect(pageToGoTo);
        }

        /// <summary>
        /// Deletes the selected dating profile and corresponding navigational
        /// workflow from the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gdvExistingProfiles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // delete profile record and workflow persistence record from database
            string datingId = gdvExistingProfiles.Rows[e.RowIndex].Cells[1].Text;
            Guid workflowId = new Guid(gdvExistingProfiles.Rows[e.RowIndex].Cells[2].Text);
            Database.DeleteProfile(datingId, workflowId);

            // update the gridview
            gdvExistingProfiles.DataSource = Database.GetExistingProfiles();
            gdvExistingProfiles.DataBind();

            // if the profile / navigational workflow deleted above is the one that is currently loaded 
            // in session, then clear the associated session variables and update the debug pane
            if (workflowId.Equals(_workflowManager.CurrentWorkflowInstanceId))
            {
                _workflowManager.CurrentWorkflowInstanceId = Guid.Empty;
                Master.DatingId = string.Empty;
                Master.UpdateDebuggingPane();
            }
        }

        /// <summary>
        /// Creates a new dating profile and corresponding navigational workflow in the
        /// database; then redirects to the next step in the navigational workflow.
        /// </summary>
        private void CreateNew()
        {
            // verify a valid dating id was given
            Page.Validate();
            if (!Page.IsValid)
            {
                return;
            }

            // create and start a new navigation workflow instance; 
            // WWF SqlWorkflowPersistenceService handles inserting
            // and updating of workflows into the database
            _workflowManager.StartNewWorkflow();

            // insert new profile record into the database
            Profile profile = new Profile();
            profile.DatingId = txtDatingId.Text; // the dating id the user chose
            profile.WorkflowId = _workflowManager.CurrentWorkflowInstanceId; // the workflow instance id
            Database.InsertProfile(profile); // insert record into the database

            // store the dating id in session
            Master.DatingId = txtDatingId.Text;

            // go to the next step
            string pageToGoTo = _workflowManager.Next();
            _workflowManager.ManagedRedirect(pageToGoTo);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Turing Inc.
United States United States

Comments and Discussions