140 likes | 255 Views
Custom Activities. Developing Custom Activities. Why? Unit of encapsulation & re-use Model custom control flow Implement custom business logic Save to file system Process images Expand vocabulary of activities with domain specific activities . Activity Class Hierarchy. Approaches:
E N D
Developing Custom Activities • Why? • Unit of encapsulation & re-use • Model custom control flow • Implement custom business logic • Save to file system • Process images • Expand vocabulary of activities with domain specific activities
Activity Class Hierarchy • Approaches: • Code-based Atomic Activities – CodeActivity, Async & Native • Code-based Composite Activities - NativeActivity • XAML-based Composite Activities - Activity
XAML-based Composite Activities MyActivity <p:Activity x:Class=MyActivity” …> <p:Sequence …> <my:DoWork1 ...> <my:DoWork2 ...> <my:DoWork3 ...> </p:Sequence> </p:Activity> DoWork1 DoWork2 DoWork3
Code-Based Atomic Activities public InArgument<string> TextToLog {get;set;} public class SimpleLogActivity: CodeActivity{ protected override void Execute(CodeActivityExecutionContext context) { //activity logic goes herecontext.Track(new UserTrackingRecord(“Activity says: “ + TextToLog.Get(context))); } …} Derive from CodeActivity Override Execute method Use the Code Activity Execution Context
Code-based Composite Activities public sealed class MyNativeActivity : NativeActivity { protected override void Execute(NativeActivityContext c) { } } Native Activity Use Similar to Code Activity Context enables more advanced scenarios
Variables: Using Arguments & Variables in Custom Activities Result Arguments get data in or out of a custom activity Variables provide the means for a composite custom activity to share data with its children DoWork WorkToDo Result OutArgument InArgument DoWork WorkToDo Result
Creating a Simple Custom Activity Implementing the ArithmeticAdd Activity
Using ADO.NET for CRUD from Custom Activities • Inherit from CodeActivity • Override Execute method • Use DataReader, Command or DataAdapter CRUD Activity: CodeActivity In Arguments Out Arguments If using Integrated Security, ensure AppPool Identity has DB access • Execute • Get Connection String (web.config) • Open Connection • Perform CRUD ops • Write values to Output Arguments • Close Connection
Database Activity Pack • Set of custom Activities • Perform CRUD using ADO.NET • Enable using SQL or Stored Procedure for command • Support mapping returned record collection to collection variable in workflow • Pull connection string from config Define complex mapping as activity sequence Easily process one record at a time Provides Out Argument as List<T> Returns # of Rows Affected
DB Activities with Transactions Creates or Joins Ambient Transaction Transaction Scope Isolation Level Join ambient transaction • No persistence within transaction • No explicit rollback or commit activities OOB • NativeActivityContext can get current transaction (if needed) • TransactedReceiveScope activity supports flow-in of distributed transactions in workflow services Timeout Create Activity Exceptions thrown cause implicit rollback Update Activity Delete Activity Completion of Transaction Scope implicitly commits transaction & persists workflow
Custom Activity Design Time Experience Activity Designers WPF Based Validate individual properties or entire activity tree Validation Expression support Customize presentation & design time behavior Category Editors allow “batch” editing of multiple properties with single complex UI Custom Inline, Pop-Up & Dialog Property Value Editors Property Grid Extensibility
Lab 4 • Custom Activities