Posts

....
Technical Blog for .NET Developers ©

Sunday, November 4, 2012

WWF Workflow Activities

In this sample, we are going to develop a workflow with a custom code activity, this activity will read an xml file and return a list of Customers to be updated / inserted in our database

The first step is add a Sequence Activity in the designer. This activity enables you to construct a list of other activities, and when executed it will start with the first child activity and execute each child in turn



We have defined a class named Customer, with attributes corresponding to data, and three methods: one to check if exists in the database, one to update, and one to insert

Now we add a Code Activity to read our xml, and return a List of Customers. We call it ReadCustomers.cs



We also add a Code Activity named UpdateCustomer, and another one named InsertCustomer

The code for ReadCustomers activity is as follows

     
public class ReadCustomers : CodeActivity<List<Customer>>
{
    protected override List<Customer> Execute(CodeActivityContext context)
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(@"c:\customers.xml");

        XmlNodeList customers = xDoc.GetElementsByTagName("Customer");

        List<Customer> listCustomers = new List<Customer>();
        foreach (XmlElement customer in customers)
        {
            listCustomers.Add(new Customer()
            {
                CustomerId = new Guid(customer.GetElementsByTagName("Id")[0].InnerText),
                FirstName = customer.GetElementsByTagName("Firstname")[0].InnerText,
                LastName = customer.GetElementsByTagName("Lastname")[0].InnerText,
                Telephone = customer.GetElementsByTagName("Telephone")[0].InnerText,
                Email = customer.GetElementsByTagName("Email")[0].InnerText
            });
        }

        return listCustomers;
    }
}


The next is the code for update
     
public sealed class UpdateCustomer : CodeActivity
{
    public InArgument<Customer> Customer { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        Customer customer = context.GetValue(this.Customer);
        customer.Update();
        Console.WriteLine("Customer " + customer.FirstName + " has been updated");
    }
}


And for insert

     
public sealed class InsertCustomer : CodeActivity
{
    public InArgument<Customer> Customer { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        Customer customer = context.GetValue(this.Customer);
        customer.Insert();
        Console.WriteLine("Customer " + customer.FirstName + " has been inserted");
    }
}


Now we have defined our Code Activities, we will proceed to complete our workflow diagram

First we drag the ReadCustomers activity into our sequence



Now we have to declare a variable with Sequence scope to take the return value of the activity. We select List type, and as element for the List we select our custom Customer type



At this point, we have implemented our activity to retrieve the list of Customers from the xml, so now we add the next elements to the diagram

- Foreach activity
- If activity
- Our custom activities in each branch of the if activity




The Foreach activity will iterates each Customer in the List, and the If activity will determines the Code activity to execute

With a correct execution, this is the result



<METHOD SOFTWARE © 2012>