workflowworkflow-foundation-4workflow-foundationworkflow-foundation-4.5

Set a key/id to a Workflow


New to Workflow Foundation, so it may be a basic question for many of you.

I have a workflow designer (client) /server application. In the designer I can upload workflows to the server , which stores them in the DB. The designer can request to download a workflow from the server. Once downloaded , we can modify it in the designer and upload it again. To be able to do that I need to add some sort of ID/Key to the workflow.

What's the best way to do that?

I was thinking about adding a property to the ActivityBuilder, but doing that adds it to the argument list, which doesn't seem right...

Any help would be much appreciated


Hi @Will, I gave it a go attaching the property to Activities but I can not get it working. I'm not using any models like WorkflowRecord, it's just a basic desginer.

I use _workflowDesigner.Save(path); to save it or _workflowDesigner.Load(path); to load it.

I create and add the attached property

attachProp = new AttachedProperty<int?>()
    {
        Name = "Key",
        IsBrowsable = true,
        Getter = (modelItem) => 5,
        Setter = ((modelItem, keyValue) => modelItem.Properties["Key"].SetValue(keyValue)),
        OwnerType = typeof(Activity)
    };

    _workflowDesigner.Context.Services.GetService<AttachedPropertiesService>().AddProperty(attachProp);

If I try to access the attached property, it throws me an exception (doesn't contain a definition for key)

dynamic mainActivity = ((_workflowDesigner.Context.Services.GetService<ModelService>().Root.GetCurrentValue() as ActivityBuilder).Implementation as Activity);
int? testValue = mainActivity.Key; //Exception, Activity' does not contain a definition for 'Key'

I read through this [post] (http://blogs.msdn.com/b/kushals/archive/2010/01/04/base-designer-with-additional-adornments.aspx) to add the attach property to the activity


Solution

  • Attached properties, via the AttachablePropertyServices Class.

    public class WorkflowRecord
    {
        static AttachableMemberIdentifier Id = 
            new AttachableMemberIdentifier(typeof(Guid), "Id");
    
        public static object GetCommentText(object instance)
        {
            object viewState;
            AttachablePropertyServices.TryGetProperty(instance, Id, out viewState);
            return viewState;
        }
    
        public static void SetCommentText(object instance, object value)
        {
            AttachablePropertyServices.SetProperty(instance, Id, value);
        }
    }
    

    You can use this to get or set your database Id on an instance of the workflow, and it will be serialized/deserialized to/from xaml.

    <Activity
        xmlns:me="clr-namespace:Herp;assembly=derp"
        me:WorkflowRecord.Id="This is a guid lol"
        x:HideAdditionalAttributesBecauseThisIsAnExample="true" />
    

    For more info, read this blog post on msdn