In
the previous post Workflow Application part2 we created an enhanced Hello Workflow application with a
custom hello message. In this post, we will add If/Else logic to the workflow
to display a different Hello message depending on a custom condition.
We will use the "write the test first" approach, that is, first writing
a test for the new requirement and then implementing the necessary code to make
it pass.
A
new requirement for our application has been introduced. If the name has an
odd number of letters then you want the first word of the greeting to be
"Greetings", otherwise the first word should be "Hello". As
an example, now your workflow will be the equivalent of this code:
C#
private static string SayHello(string
userName)
{
string
FirstWord = null;
if
(userName.Length % 2 == 0)
FirstWord = "Hello";
else
FirstWord = "Greetings";
return
FirstWord + ", " + userName +" from Workflow 4";
}
Visual Basic
Private
Shared Function
SayHello(ByVal userName As String) As String
Dim FirstWord
As String = Nothing
If
userName.Length Mod 2 = 0 Then
FirstWord = "Hello"
Else
FirstWord = "Greetings"
End If
Return
FirstWord & ", " &
userName & " from Workflow 4"
End Function
- First open HelloWorkFlow project from last article Workflow Application part2 .
- Create
a test to verify the new requirement. To do this, open the SayHelloFixture.cs file (C#) or SayHelloFixture.vb (VB), located under the HelloWorkflow.Test project, and add the following test.C#[TestMethod]public void ShouldReturnGreetingWithOddLengthName(){var output = WorkflowInvoker.Invoke(new SayHello() { UserName = "Odd" });string greeting = output["Greeting"].ToString();Assert.AreEqual("Greetings Odd from Workflow 4", greeting);}Visual Basic<TestMethod()>Public Sub ShouldReturnGreetingWithOddLengthName()Dim output = WorkflowInvoker.Invoke( _New SayHello() With {.UserName = "Odd"})Assert.AreEqual("Greetings Odd from Workflow 4",output("Greeting").ToString())
- Right-click
over test method and select Run Tests (ctrl + R,T) . The
test will fail because you have not modified the workflow yet to return a
different hello message in each case.ShouldReturnGreetingWithOddLengthName test failing
- Open SayHello.xaml in the workflow designer. To do this, double-click the file in Solution Explorer.
- Add
a FirstWord variable to store the
first word of the hello message, whether it is "Hello" or
"Greetings". To do this, follow these steps:a. Select the Sequence by clicking over the shape surface.b. Click the Variables button. A panel that displays the available variables for the Sequence activity appears.c. Click <Create Variable>.d. Type FirstWord in the Name box.
Adding the FirstWord variable of type String to SequenceIn Windows Workflow Foundation (WF), Variables represent the storage of data. Arguments, on the other side, represent the flow of data into and out of an activity. Variables have a scope just as they do in C# or Visual Basic. If you open the variables pane without selecting an activity you won't be able to add a variable. The activity selected in the designer will provide the scope for the variable. In this case, FirstWord belongs to the Sequence scope. - Now we need to test the UserName argument to see if it has an even or odd number of characters. To do this, drag an If activity from the Toolbox and drop it into the Sequence activity above the Assign activity.
- Set
the If activity DisplayName to Set FirstWord
value by selecting the text on the shape and editing it inline or by setting
it in the property grid (select the activity and press F4 to display it).Note: The Workflow designer allows you to give the shape a more readable name by setting its DisplayName property.
- Supply
an expression for the If condition. To
do this, double-click the If
activity to open it and type the following expression in the Condition box. This will check if the
name length is odd or even.Visual BasicUserName.Length Mod 2 = 0Note:Expressions are program statements that can be a literal string, a conditional statement, or an expression that concatenates several strings or calls a method or invokes another activity. Expressions are written using Visual Basic syntax even if the application is in C#. This means capitalization does not matter, comparison is performed using a single equals sign instead of “==”, and the Boolean operators are the words "And" and "Or" instead of the symbols "&&" and "||".
- Update the FirstWord variable value for even length names, which should be greeted with a "Hello" message. Drag an Assign activity from the Toolbox and drop it into the Then area. After that, type FirstWord in the To box (left side) and "Hello " (including the space at the end) in the Value box (right side).
- Now
update the FirstWord variable value
for odd length names, which should be greeted with a "Greetings"
message. Drag and Assign activity
from the Toolbox and drop it into the Else
area. Then type FirstWord in the To box (left side) and "Greetings " (including the
space at the end) in the Value box
(right side).The completed If activity
- Modify
the final Assign activity to customize
the hello message displayed based on the FirstWord
value. To do this, in the Assign
activity located below the If activity
replace the content of the Value box
(right side) with the following expression:Visual BasicFirstWord & UserName & " from Workflow 4"The Completed Workflow
- Press CTRL+SHIFT+B to build the solution.
- From the menu select Test / Run / All Tests In Solution or press CTRL+R,A.
- Verify
that all the tests pass.Tests pass
No comments:
Post a Comment