Sunday, October 21, 2012

Refactoring Workflows

We will refactor the solution created in last post Hello Workflow . Even though we have a working application, we might want to make a few improvements. In this post, as Workflow1 is not a very descriptive name, you will change it to SayHello.
  1. To change the name of the workflow defined in Workflow1.xaml, Open it with the workflow designer and click on an empty area the designer surface. This will allow us to change the properties of the workflow.
    Click on an empty area of the designer surface so you can set the name of the workflow
    Click on an empty area of the designer surface so you can set the name of the workflow
    1. In the Properties window, set the Name property of the Workflow to HelloWorkflow.SayHello (C#) or SayHello (VB).
      Set the Name property of the workflow (C#)
      Set the Name property of the workflow (C#)
      Set the Name property of the workflow (VB)
      Set the Name property of the workflow (VB)
      1. While it is not required, it is a good idea for the name of the .xaml file to match the name of the activity contained in it. In Solution Explorer, right-click Workflow1.xaml, select Rename and type SayHello.xaml.
        Watch Out
        When you rename C# or Visual Basic files, Visual Studio asks you if you want refactoring to change the name of the class also. It does not do this when renaming XAML files, so if you want to change the name of the workflow also you need to do this manually. 
      2. Press CTRL+SHIFT+B to build the solution. It will fail to compile because you changed the name of the workflow.
        The workflow fails to compile (C#)
        The workflow fails to compile (C#)
          The workflow fails to compile (VB)
          The workflow fails to compile (VB)
          1. Note : Why does the application fail to compile when I change the workflow name? 
            Your workflow is actually a class declared in XAML that inherits from System.Activities.Activity. The name of the workflow is the name of the class that must be created to run the workflow. When you set the Name property, you changed the name of the class.
          2.  To fix the build break, open Program.cs (C#) or Module1.vb (VB) and modify it to use the new SayHello class name. To do this:
            a.       Locate the WorkflowInvoker.Invoke method call.
            b.       Modify it to use SayHello instead of Workflow1 as shown in the following code (shown in bold).
            C#
            static void Main(string[] args)
            {
              WorkflowInvoker.Invoke(new SayHello());
            }

            Visual Basic
            Shared Sub Main()
              WorkflowInvoker.Invoke(New SayHello())
            End Sub
            Note: Why does Visual Studio warn me that the type SayHello is not defined?
            If you change Main() before compiling the workflow with the new SayHello name, you may notice a warning about the type SayHello not being defined. The reason for this is that Visual Studio is not aware of types declared in XAML until you build the solution.




          3. Press CTRL+SHIFT+B to build the solution. It should now build without errors.
          4. Press CTRL+F5 to run it. You should see the same message as before “Hello Workflow 4"
            Hello Workflow 4 message
            Hello Workflow 4 message
            1. If you want to see the XAML, right click on the SayHello.xaml file and select View Code. Visual Studio will warn you that the file is already open in the designer
              File already opened in the designer warning
              File already opened in the designer warning

              Just click Yes to close the designer and you will see the workflow definition in XAML.
              XAML
              <Activity mc:Ignorable="sap"
                   x:Class="SayHello"
                   xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
                   ...
               <WriteLine ... Text="Hello Workflow 4" />
              </Activity>


            1 comment:

            Automatic Traffic Exchange

            YallaTech Facebook page