Replicator Tips and Tricks
Here are a few tips and tricks for using the ReplicatorActivity successfully. It is a powerful activity which, when approached from the correct point of view, can be relatively easy to use.
seed value - a value added to either of the ReplicatorActivity's child collections. This value is associated with a single instance of the replicator's child.
template activity - the direct child of the replicator is referred to as the template activity. At replicator startup one instance of the template activity will be run for each seed value in the child data collection.
Tips and Tricks
Use your activity properties! Oftentimes users are confused as to where best to store the seed value. It is with great dismay that the question, "Do I have to create a custom activity just to store the value?" is asked. The answer is no, you don't have to create a custom activity just for value storage in some of the most common cases.
First, if your replicator is executing in Sequence and not Parallel then you can store the variable wherever you like, even outside the scope of the replicator. Despite the fact that the object containing the variable will not be cloned once per instance since you are executing in sequence, you will always only have one copy of the template activity active at any given time.
Second, consider what you are doing with the value? Chances are that the value you are using has some significance to one or more of the activities inside the template activity. If this is the case, then store the value directly on that activity's property. If more than one activity needs to access the data, then store it on one of them and use an ActivityBind to reference it on the rest of them.
Rarely you will find that the seed value is only used within the handler for a CodeActivity or that it must undergo some serious computation before it is settable to any of the activity properties. If this is the case, then you will need to create a custom activity which will act as your template activity. This should, however, be the rare case - perhaps instead your activities should be written to accept the seed value instead of some transformed version!
When is the UntilCondition evaluated? The UntilCondition is evaluated in two places; it is evaluated once before any activities are executed and once after each instance of the template activity closes. Note that if it returns true when the replicator first starts executing then the replicator will close immediately and will not execute any children. Also, any children still executing when the UntilCondition evaluates to true will be cancelled.
How can I tell which child just closed when the UntilCondition is evaluated? Unfortunately, there is no built in mechanism for this; the UntilCondition does not accept parameters and there is no global nor ReplicatorActivity defined accessor for this kind of data. There is, however, a suitable workaround for determining the newly completed activity.
At a child's completion the replicator will first raise the ChildCompleted event and then evaluate the UntilCondition. Since these two actions occur in the same method and Windows Workflow Foundation guarantees a single thread per workflow instance (see post on Parallelism) you can be sure that no other chlidren will execute between the event handler running and the condition evaluation.
That said, some identifying data from the completed child can be stored in a well known location (variable on the root activity, property of some other activity, etc) during the ChildCompleted handler and read from the location by the UntilCondition.