Share via


Task 2: Define the IOrderingService Interface 

Download sample

In this task, you define the IOrderingService interface and a class that is used to pass data from the host application to the state machine workflow when a new order is submitted.

The IOrderingService interface is used to make communication easier between the Simple Order Form host application and the OrderProcessingWorkflow.

The interface contains a single method and a defined event:

  • The ItemStatusUpdate method is used by the OrderProcessingWorkflow to send status updates back to the host application.

  • The NewOrder event is the initial event that starts the WaitingForOrder state activity. It is raised by the Simple Order Form host application whenever the user submits a new order.

NoteNote

Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start this exercise by opening the sample project and proceeding to the steps in the following section.

Defining the NewOrderEventArgs Class

First, you must define the class that is used to pass data from the host application to the state machine workflow when an order is submitted.

To define the NewOrderEventArgs class

  1. In the StateMachineWorkflow source file, create a new public class named NewOrderEventArgs that derives from the EventArgs base class.

    Make the class serializable by applying the SerializableAttribute. This class should be defined in the Microsoft.Samples.Workflow.Tutorials.StateMachineWorkflow namespace.

    <Serializable()> _
    Public Class NewOrderEventArgs : Inherits ExternalDataEventArgs
    End Class
    
    [Serializable]
    public class NewOrderEventArgs : ExternalDataEventArgs
    {
    }
    
  2. In the NewOrderEventArgs class, declare the following fields:

    Access Modifier Type Name

    Private

    System.Guid

    orderItemId

    Private

    System.String

    orderItem

    Private

    System.Int32

    orderQuantity

    Private orderItemId As Guid
    Private orderItem As String
    Private orderQuantity As Integer
    
    private Guid orderItemId;
    private string orderItem;
    private int orderQuantity;
    
  3. Create a constructor for the NewOrderEventArgs class that takes the following as parameters:

    • A System.Guid named itemId.

    • A System.String named item.

    • A System.Int32 named quantity.

    In the body of the constructor, assign each field that you created in the previous step with the associated parameter passed into the constructor.

    Public Sub New(ByVal itemId As Guid, ByVal item As String, _
        ByVal quantity As Integer)
        MyBase.New(itemId)
        Me.orderItemId = itemId
        Me.orderItem = item
        Me.orderQuantity = quantity
    End Sub
    
    public NewOrderEventArgs(Guid itemId, string item, int quantity)
        : base(itemId)
    {
        this.orderItemId = itemId;
        this.orderItem = item;
        this.orderQuantity = quantity;
    }
    
  4. In the NewOrderEventArgs class, create a public System.Guid property named ItemId.

    Create a get method for this property and return the value of the orderItemId field, and a set method that sets the orderItemId field.

    Public Property ItemId() As Guid
        Get
            Return orderItemId
        End Get
        Set(ByVal value As Guid)
            orderItemId = value
        End Set
    End Property
    
    public Guid ItemId
    {
        get { return orderItemId; }
        set { orderItemId = value; }
    }
    
  5. In the NewOrderEventArgs class, create a public System.String property named Item.

    Create a get method for this property and return the value of the orderItem field, and a set method that sets the orderItem field.

    Public Property Item() As String
        Get
            Return orderItem
        End Get
        Set(ByVal value As String)
            orderItem = value
        End Set
    End Property
    
    public string Item
    {
        get { return orderItem; }
        set { orderItem = value; }
    }
    
  6. In the NewOrderEventArgs class, create a public System.Int32 property named Quantity.

    Create a get method for this property and return the value of the orderQuantity field, and a set method that sets the orderQuantity field.

    Public Property Quantity() As Integer
        Get
            Return orderQuantity
        End Get
        Set(ByVal value As Integer)
            orderQuantity = value
        End Set
    End Property
    
    public int Quantity
    {
        get { return orderQuantity; }
        set { orderQuantity = value; }
    }
    

Creating the Interface

Next, you define the IOrderingService interface. This is used for communication between the workflow and the host application.

To define the IOrderingService interface

  1. Following the definition of the NewOrderEventArgs class, create a public interface named IOrderingService.

    Because this interface is used for communication between the host application and the OrderProcessingWorkflow, apply the ExternalDataExchangeAttribute attribute.

    <ExternalDataExchange()> _
    Public Interface IOrderingService
    End Interface
    
    [ExternalDataExchange]
    public interface IOrderingService
    {
    }
    
  2. In the IOrderingService interface, create a method named ItemStatusUpdate that takes the following as parameters:

    • A System.Guid named orderId.

    • A System.String named newStatus.

    Sub ItemStatusUpdate(ByVal orderId As Guid, ByVal newStatus As String)
    
    void ItemStatusUpdate(Guid orderId, string newStatus);
    
  3. In the IOrderingService interface, create a new event named NewOrder whose type is a generic EventHandler of the NewOrderEventArgs type.

    Event NewOrder As EventHandler(Of NewOrderEventArgs)
    
    event EventHandler<NewOrderEventArgs> NewOrder;
    

Compiling the Code

  1. Click Start, point to Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.

  2. Go to the source directory of the tutorial.

  3. At the command prompt, type MSBUILD to build the project.

In the next task, Task 3: Implement the IOrderingService Interface, you will implement this interface in the Simple Order Form host application.

See Also

Reference

ExternalDataExchangeAttribute

Concepts

Workflow and Application Communication
State Machine Workflows

Other Resources

Task 3: Implement the IOrderingService Interface
Tutorial: Create a State Machine Workflow
Communications
HostCommunication Sample
Ordering State Machine
Simple State Machine

Footer image

Send comments about this topic to Microsoft.