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.
Comments
Anonymous
December 01, 2009
Hi Kushal, This is really neat. I can see how this could be used to pre-populate properties of an activity. But I'm not clear on how this hides properties of the activity from the user. Can you please elaborate? Thanks, NotreAnonymous
December 04, 2009
@Notre: You need to use ICustomTypeDescriptor to provide properties that need to be shown by the ProeprtyGrid. Or you can write extend the property grid. Look at the Extending PropertyGrid sample that is part of .NET Fx 4 SDK. Thanks, Kushal.Anonymous
January 07, 2010
Is there anyway you can do this with a FlowNode item?Anonymous
October 27, 2010
Want to create a custom activity which can be used for flowcharting. Since Flowchart is a sealed class, We tried implementing "IActivityTemplateFactory" and returning a flowchart activity instance, however the custom properties that we have defined in our custom activity are not comming through the property window. We realised that the custom activity is just a flowchart instance. How can we get the additional properties and behaviours in the custom activity along with flowchart?Anonymous
July 19, 2013
Kushal, Nice work. Is there a sample solution in WF4. I need to customize the StateMachine itelf and the states. i.e. Rename the StateMachine to "StatusAnzeiger" or the state to "IndividuellerStatus" I also want to add a variable that is part of the statemachine by default as soon as the user adds the renamed state machine. This variable is a domain object that will provide the data access to evaluate the state for transitions. Thanks Hans