locked
bind WF params to activity / default value for activity property RRS feed

  • Question

  • I created a simple activity which stores some data in a database. The activity contains a dependency property which is initialized in the constructor of the activity.

    public class InsertBusinessProcessActivity : System.Workflow.ComponentModel.Activity
    {
      public
    static readonly DependencyProperty BusinessProcessProperty = DependencyProperty.Register("BusinessProcess", typeof(BusinessProcess), typeof(InsertBusinessProcessActivity));

      public BusinessProcess BusinessProcess
      {
        get
        {
          return ((BusinessProcess)(this.GetValue(InsertBusinessProcessActivity
    .BusinessProcessProperty)));
        }
        set
        {
          this.SetValue(InsertBusinessProcessActivity.BusinessProcessProperty, value
    );
        }
      }

      //constructor
      public
    InsertBusinessProcessActivity()
      {
        BusinessProcess = new BusinessProcess
    ();
      }

      protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
      {
        //some actions
      }

    }

    I use this activity in a workflow by binding workflow parameters to the activity property described above.

    public sealed partial class InsertBusinessProcessWorkflow1 : SequentialWorkflowActivity
    {
      public
    InsertBusinessProcessWorkflow1()
      {
        InitializeComponent();
      }

      public
    static DependencyProperty BusinessProcessProperty = DependencyProperty.Register("BusinessProcess", typeof(Schleupen.CS.SY.WF.BusinessProcess), typeof(Schleupen.CS.SY.InsertBusinessProcessWorkflow1));

      public
    BusinessProcess BusinessProcess
      {
       
    get
        {
          return ((Schleupen.CS.SY.WF.BusinessProcess)(base.GetValue(Schleupen.CS.SY.InsertBusinessProcessWorkflow1
    .BusinessProcessProperty)));
        }
       
    set
       
    {
          base.SetValue(Schleupen.CS.SY.InsertBusinessProcessWorkflow1.BusinessProcessProperty, value
    );
        }
      }
    }

    public sealed partial class InsertBusinessProcessWorkflow1
    {
      private void
    InitializeComponent()
      {
        this.CanModifyActivities = true
    ;

        System.Workflow.ComponentModel.ActivityBind activitybind1 = new System.Workflow.ComponentModel.ActivityBind();
        this.insertBusinessProcessActivity1 = new Schleupen.CS.SY.WF.Activities.InsertBusinessProcessActivity
    ();

        activitybind1.Name = "InsertBusinessProcessWorkflow1"
    ;
        activitybind1.Path = "BusinessProcess"
    ;

        this.insertBusinessProcessActivity1.Name = "insertBusinessProcessActivity1"
    ;
        this.insertBusinessProcessActivity1.SetBinding(Schleupen.CS.SY.WF.Activities.InsertBusinessProcessActivity.BusinessProcessProperty, ((System.Workflow.ComponentModel.ActivityBind
    )(activitybind1)));

        this.Activities.Add(this
    .insertBusinessProcessActivity1);
        this.Name = "InsertBusinessProcessWorkflow1"
    ;
        this.CanModifyActivities = false
    ;
    }

      private Schleupen.CS.SY.WF.Activities.InsertBusinessProcessActivity
    insertBusinessProcessActivity1;
    }

    Problem:
    By initializing the workflow with no input parameters, the activity property  BusinessProcess becomes "null". Before the method call this.insertBusinessProcessActivity1.SetBinding(...) in InitializeComponent() the activity property has the value which has been assigned in the activity constructor. After the binding assignment the activity property is "null".

    Can someone help me assigning "default" values to activity properties which will be preserved after binding assignments? A pointer or code sample would be greatly appreciated.

    thx, gabr

    Friday, March 17, 2006 8:48 AM

Answers

  • Since the activity is going to get the value from the workflow, I would suggest that in the workflow property get, if the call to get the value for the property fails, you return a new instance of your businessprocess class.  This way, you ensure that you get the default value.  What you would probably do is something like this pseudocode in your get:

    object val = base.GetValue(dependencyProp);

    if(val != null)

     return (businessprocess)val;

    else

    base.SetValue(dependencyProp, new businessprocess());

    return (businessProcess)base.getvalue(dependencyProp);

     

    This ensures that your workflow always has a valid input, even if the host does not pass one in. 

     

    Matt

    Friday, March 17, 2006 3:35 PM

All replies

  • Since the activity is going to get the value from the workflow, I would suggest that in the workflow property get, if the call to get the value for the property fails, you return a new instance of your businessprocess class.  This way, you ensure that you get the default value.  What you would probably do is something like this pseudocode in your get:

    object val = base.GetValue(dependencyProp);

    if(val != null)

     return (businessprocess)val;

    else

    base.SetValue(dependencyProp, new businessprocess());

    return (businessProcess)base.getvalue(dependencyProp);

     

    This ensures that your workflow always has a valid input, even if the host does not pass one in. 

     

    Matt

    Friday, March 17, 2006 3:35 PM
  • Thank you for the quick answer. You are right, this ensures that my workflow has always a valid input. But the described workflow is only a sample. The activities should be developed independently of the workflow. This means I write some activities and someone else reuses the activities to create workflows. That's why the activity itself should provide a valid BusinessProcess-object if the workflow has no input.

    I would be grateful for any pointers or ideas. Thanks,
    gabr

    Friday, March 17, 2006 7:48 PM