What Are Custom Activities? (From Microsoft Elearning)

Working with Workflow Activities
 What Are Custom Activities?

In this lesson, you have seen the wide variety of useful built-in activities that WF includes. However, because WF is a general purpose development technology, Microsoft cannot include activities to solve all eventualities. You can solve simple problems with the CodeActivity, but this is not appropriate for more complex problems, and it is difficult to reuse the code in the activity. You can solve complex problems by creating your own custom activities. Moreover, you can reuse a custom activity many times by dragging it from the Visual Studio toolbox onto the Workflow Designer. The best solutions to the following situations require a custom activity. Click each activity to view more details.

 

E-mail Communication 
Consider a workflow in which e-mails communicate between the workflow and the users. For example, a vacation approval application, in which a worker requests vacation and the system sends e-mails to managers and administrators for approval and scheduling. Many e-mails may be sent in the process, but they all require the workflow to set their To, From, Subject, and Body properties. If you use code activities to send each e-mail, you must copy and paste the code into each code activity, and alter the lines that set those properties.

Alternatively, if you create a custom activity, you can write code once that sets the To, From, Subject, and Body properties from properties of the custom activity. You can bind these properties to properties of the workflow, the input parameters, or the output of other activities, such as activities that look up e-mail addresses in a directory. You can insert new e-mail activities into the workflow by dragging your custom activity from the toolbox.

Changing Activities 
Consider a workflow that is likely to change in the near future. For example, at present, you want the workflow to look information up in a Microsoft Office Access database. However, within the next six months your data team will move that information into Microsoft Active Directory directory. If you write a CodeActivity wherever you need information from the Office Access database, you must rewrite all of those activities when the data moves to the directory.

Alternatively, if you create a custom activity to look information up in the Office Access database, you can later update the whole workflow simply by changing that activity and recompiling the workflow. You do not need to run through all of the individual code activities and repeatedly make corrections.

Creating Custom Composite Activities 
Consider a workflow in which a similar business logic appears repeatedly. For example, a human resources workflow that manages training, promotion, and skills for workers. When you build the workflow, you discover that frequently the workflow communicates with the workers’ managers by looking them up in Active Directory and sending them an e-mail. In each one of these cases, an e-mail must also be sent to the manager’s manager. As you construct the workflow you notice that three or four activities repeat in a common pattern.

In such cases you can save time by creating a custom composite activity. This is a custom activity that consists of several child activities connected to form part of a workflow. You can reuse your composite activity quickly, by dragging it onto the Workflow Designer. In this way you avoid repetitively recreating the same pattern many times.

posted @ 2006-11-20 07:41  upzone  阅读(189)  评论(0)    收藏  举报