6.6. Creating Your Own Activities

Microsoft supplies many activities out of the box, but you will want to and should create your own activities. Activities can be created in three main ways:

  • As a composition of other existing activities

  • In code

  • In pure XAML

6.6.1. Creating an Activity Composed of Other Activities

Let's create a simple activity to simulate writing a customer's ticket booking to a database:

  1. Right-click the project and add a new activity (from the Workflow section) called SaveBooking.xaml.

  2. Open SaveBooking.xaml in design view.

  3. Create an argument for the SaveBooking activity (String, In) called BookingReference.

  4. Drag a Sequence activity onto the design surface.

  5. Drag a WriteLine activity onto this activity and change its display name to "Simulate booking storage."

  6. We are not going to go to the bother of actually writing data access code, so change the Value property of the WriteLine activity to "Save booking" + BookingReference + "in database". Your workflow should look like Figure 6-12 (note the quotes).

  7. Click Save.

    Figure 6.12. SaveBooking activity
  8. Before this activity can be used, the project needs to be compiled. Compile the project.

  9. Open Workflow1.xaml, and in the toolbox (Project section), you should find that the new SaveBooking activity is now available. Drag it onto the Sequence activity just beneath the "Check availability" activity.

  10. On the properties of the SaveBooking activity, set the BookingReference argument to use the BookingReference variable defined in the main workflow.

  11. Run your workflow. You should now see the output "Save booking" and whatever you have set the booking reference variable to.

6.6.2. Creating Activities Purely in Code

Anything that can be done in the WF designer can also be done programmatically. Let's say our client is running a new promotion that every theater booking has a 1 in 100 chance of being free. We will create a new code activity to simulate this.

  1. Add a new Code activity to the project called FreeBookingPromotion.cs.

  2. Amend the code in FreeBookingPromotion.cs to the following:

    public class FreeBookingPromotion : CodeActivity
    {
        public InArgument<string> BookingReference { get; set; }
        public OutArgument<bool> FreeBooking { get; set; }
    
        protected override void Execute(CodeActivityContext context)
        {
    
            System.Random Random = new Random();
    
            if (Random.Next(1, 100) == 100)
            {
                //Customer has won free booking
                FreeBooking.Set(context, true);
            }
            else
            {
                FreeBooking.Set(context, false);
            }
        }
    }

  3. Save and build the project.

  4. Open Workflow1 and drag the FreeBookingPromotion activity from the toolbox just before the "Display output" activity.

  5. Create a new variable at Workflow1.xaml level called FreeBooking, of type Boolean.

  6. Drag a new If activity before the "Display output" activity and change the display name to "Check if booking free."

  7. Modify the activity's FreeBooking argument to use the main workflow's FreeBooking variable.

  8. Double-click the If activity.

  9. In the condition, enter freebooking=true.

  10. In the Then block, drag a WriteLine activity and change the text to "Congratulations you have won a free booking."

  11. In the Else block, drag a WriteLine activity and change the text to "Sorry try again next time."

  12. Run your workflow.

6.6.3. Pure XAML Workflows

Activities can also be created purely in XAML, which is great news for third-party vendors and code generation. You could even hold your activities' XAML definitions in a database and load them at runtime. Let's take a look at the XAML of our SaveBooking activity.

  1. In Solution Explorer, right-click SaveBooking.xaml and select "Open with."

  2. Select XML Editor, and you'll be able see the XAML that makes up this activity:

    <Activity mc:Ignorable="sap" x:Class="Chapter6.HelloWF.SaveBooking"
    xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-
    namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-
    namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-
    namespace:System;assembly=mscorlib, Version=4.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089" xmlns:s1="clr-namespace:System;assembly=mscorlib"
    xmlns:s2="clr-namespace:System;assembly=System" xmlns:s3="clr-
    namespace:System;assembly=System.Xml" xmlns:s4="clr-namespace:System;assembly=System.Core"
    xmlns:sa="clr-namespace:System.Activities;assembly=System.Activities, Version=4.0.0.0,
    Culture=neutral, PublicKeyToken=31bf3856ad364e35" xmlns:sad="clr-
    namespace:System.Activities.Debugger;assembly=System.Activities"
    xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
    xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-
    namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-
    namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-
    namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-
    namespace:System.Data;assembly=System.Data" xmlns:sd1="clr-
    namespace:System.Data;assembly=System.Data.DataSetExtensions" xmlns:sl="clr-
    namespace:System.Linq;assembly=System.Core" xmlns:st="clr-
    namespace:System.Text;assembly=mscorlib"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <x:Members>
        <x:Property Name="BookingReference" Type="InArgument(x:String)" />
      </x:Members>
      <mva:VisualBasic.Settings>Assembly references and imported namespaces serialized as XML
    namespaces</mva:VisualBasic.Settings>
      <Sequence sad:XamlDebuggerXmlReader.
    FileName="D:wwwrootookChapter6_WFChapter6.HelloWFChapter6.
    HelloWFSaveBooking.xaml"

    sap:VirtualizedContainerService.HintSize="233,200">
        <sap:WorkflowViewStateService.ViewState>
          <scg3:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg3:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
        <WriteLine DisplayName="Simulate booking storage"
    sap:VirtualizedContainerService.HintSize="211,62" Text="[&quot;Store booking &quot; +
    BookingReference + &quot;in database&quot;]" />
      </Sequence>
    </Activity>

Yikes, probably not the sort of thing you want to be typing yourself.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.47.166