Can I use class members for data or do I need to use Variable? RRS feed

  • Question

  • I have a class derived from CodeActivity that creates some objects in its constructor which will be used by Execute. These objects are exposed as properties so I can fake them for unit tests.

    In a code review a colleague suggested that I might need to make all of those objects Variable<T> "for them to play nicely in WF (with all the threading etc. going on)", but wasn't sure on that.

    Do I need to make these Variable<T>? If not, when would I want to use Variable<T> in a CodeActivity?

    Tuesday, September 14, 2010 7:47 PM


  • In a CodeActivity, you can use regular .NET properties that are initialized in the constructor in the Execute override with no problems.

    You would use Variable<T> if your activity was an Activity or NativeActivity derived class with child activities and you wanted to be able to capture the output of one activity and use it in another, or have some value operated on by multiple child activities. Also, when a workflow is persisted, any Variables that are in scope for currently executing activities are persisted (this would be the current activity, and any parent activities including the root workflow activity, and whichever the current child activity is - if this isn't clear I can try to provide a diagram) as part of the workflow state (and variables that are oart of activities that are no longer executing are not persisted).

    With CodeActivity, you would use InArguments and OutArguments to flow data into and out of the activity, and since all the work is done in the single burst during the Execute override (and there is no persistence then and no child activities of a CodeActivity) you don't need to use Variables with a CodeActivity.

    Does that clear it up for you? If there are specifics of your scenario that I didn't cover, please reply back and I can add more detail.


    Steve Danielson [Microsoft]
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm


    • Marked as answer by RickKrAtWork Tuesday, September 14, 2010 8:33 PM
    Tuesday, September 14, 2010 8:26 PM