IActivityTemplateFactory

As part of workflow authoring, it is always the case where the customer says, I like your out of box activity, however it has too many arguments/properties. I don't want to ask my user to set all these properties. And hence, now I would need to write a custom activity which has some of these properties and arguments already configured and maybe hidden as well from the user.

We faced the same dilemma as we were working on our Messaging activities. We wanted the user to know about the Receive and SendReply activities. However we knew that giving them separately would be not an easy configuration task for every user. To solve this problem and the customer asks we talked above, we came up with IActivityTemplateFactory.

In a nutshell, it is like pre-configuring your existing out of box activities as and when they are dropped on the the designer surface. And to check a simple example of it, look at ReceiveAndSendReply activity under the Messaging category in VS toolbox. If you lookup, we do not have any activity called ReceiveAndSendReply in the System.Activities assembly. It is the IActivityTemplate that is being termed as ReceiveAndSendReply.

The interface IActivityTemplateFactory is something like:

 public interface IActivityTemplateFactory
  {
      Activity Create(DependencyObject target);
  }

Now as part of the implementation, in the Create method, you can create the instances of the objects/activities you want to be dropped on the workflow designer along with their pre-configured properties/arguments.

Internally, as part of drag drop from toolbox, generally an Activator.CreateInstance(type of activity) happens. However if we recognize the type is derived form IActivityTemplateFactory, then we do all the ‘typeobject’.Create.

Example:

 public sealed class MyDelayActivity : IActivityTemplateFactory
    {
        public Activity Create(DependencyObject target)
        {
            return new System.Activities.Statements.Delay
            {
                DisplayName = "DelayActivityTemplate",
                Duration = new TimeSpan(0,0,10)

            };
        }
    }

Add this activity to the toolbox, drag drop it on the workflow, immediately you would see the Delay activity with its properties/arguments configured with ‘DisplayName’ as “DelayActivityTemplate” and the ‘Duration’ set to 10 seconds.

Thanks,

Kushal.