Kế thừa từ Activity<TResult>

Một phần của tài liệu Tìm hiểu mô hình và công nghệ wf 4.0 (Trang 38 - 40)

Activity<TResult>. Ở đây chúng ta sẽ xây dựng một custom activity là AppendString thực hiện việc kết nối 2 chuỗi lại với nhau.

AppendString activity cần đưa vào 2 string để thực hiện việc ghép nối và trả về string đã ghép nối. Để thực hiện cho phép đưa vào custom activity, chúng ta cần 2 InArgument và 2 argument này là bắt buộc.

1. var inArguments = new Dictionary<string, object>(); 2. inArguments.Add("OrderInfo", myOrder);

3.

4. var outArguments = WorkflowInvoker.Invoke(new TotalAmountWF(), inArguments); 5.

6. var totalAmount = (decimal)outArguments["TotalAmount"];

7. Console.WriteLine("Workflow returns ${0} for total amount.", totalAmount);

1. public sealed class AppendString : Activity<string> 2. { 3. public AppendString() 4. { 5. 6. } 7. }

1. public sealed class AppendString : Activity<string> 2. {

3. [RequiredArgument]

4. public InArgument<string> TheFirstStr { get; set; } 5.

6. [RequiredArgument]

Đối với các custom activities thừa kế từ Activity<TResullt>, để định nghĩa phần logic code của activity, chúng ta sẽ thông qua property Implementation, thông qua property này, chúng ta sẽ định nghĩa 1 delegate để thực hiện logic code cho activity.

Sau khi đã định nghĩa xong custom activity AppendString, chúng ta tiến hành kiểm tra kết quả thu được thông qua method sau:

Và kết quả thu được như sau: 1. public AppendString() 2. {

3. // Define the implementation for AppendString activity 4. this.Implementation = () => new Assign<string>()

5. {

6. Value = new InArgument<string>(context => TheFirstStr.Get(context) 7. + TheSecondStr.Get(context)),

8. To = new OutArgument<string>(context => this.Result.Get(context)) 9. };

10. }

1. static void AppendStringDemo() 2. {

3. // Initialize AppendString activity

4. AppendString appendString = new AppendString() 5. {

6. TheFirstStr = "Hello ",

7. TheSecondStr = "custom activiy in WF4."

8. }; 9.

10. // Using WorkflowInvorker to invoke appendString activity 11. // appendString is a generic-activity Activity<string>, 12. // so Invoke method will return a string

13. var outArgument = WorkflowInvoker.Invoke(appendString); 14.

15. Console.WriteLine("Result: {0}", outArgument); 16. Console.WriteLine("Press Enter to exit ..."); 17. Console.ReadLine();

Chú ý: Đối với phương thức Invoke trong class WorkflowInvoker, nếu chúng ta invoke 1 activity được thừa kế từ Activity<TResult> thì kết quả trả về từ phương thức WorkflowInvoker.Invoke sẽ có cùng kiểu dữ liệu là TResult mà chúng ta thừa kế từ Activity<TResult>.

Chú ý: Như trong những phần trước đã nhắc đến, để có thể lấy được giá trị hay dữ liệu của các Arguments, chúng ta phải thông qua phương thức Get và Workflow context. Tuy nhiên, với việc sử dụng lamda expression thì công việc này trở nên rất đơn giản. Cụ thể trong ví dụ trên, để có thể lấy ra dữ liệu của TheFirstStr chúng ta sử dụng expression là context => TheFirstStr.Get(context). (adsbygoogle = window.adsbygoogle || []).push({});

Kế thừa từ CodeActivity<TResult>

Một phần của tài liệu Tìm hiểu mô hình và công nghệ wf 4.0 (Trang 38 - 40)