Up
to this point the application is not very interesting. It is only working in a
console and does not receive any input arguments. Most applications that do
meaningful work will have to deal with input and output arguments. Additionally,
the application is not easily tested in its current form.
From last post Dynamic Workflows with XAML we will continue our project we will modify the SayHello activity to use arguments and prepare it to be useful
in other non-console application environments by returning a greeting rather
than writing directly to the Console with the WriteLine activity. We will be
doing this using the "write the test first" approach. First we will
create a test that fails and then add the necessary code to make it pass.
The resulting
application will be the equivalent of the following code.
C#
private static string SayHello(string
name)
{
return "Hello " + name + " from Workflow 4";
}
Visual Basic
Private
Shared Function SayHello(ByVal name As String) As String
Return "Hello " & name & " from Workflow 4"
End Function
- Open HelloWorkFlow project from last post Dynamic Workflows with XAML . or download it from this links:EzzFile ,
Uploaded ,Depositfiles - To
begin with, create a unit test for the workflow to ensure that it behaves as it
should. In Solution Explorer, right-click
the HelloWorkFlow solution and point to Add and click New Project. In the Add New Project dialog box, select the Test Project under the Test project
type in either the Visual C# or Visual Basic project Templates list and
enter HelloWorkflow.Tests as the
project Name. Leave the location by
defaultAdding a new Test Project to the solution (C#)Adding a new Test Project to the solution (Visual Basic)
- Right-click the HelloWorkflow.Tests project and click Add Reference. Using the Projects tab, add a project reference to the HelloWorkflow project. Repeat these steps, using the .NET tab instead to add a reference to the System.Activities library.
- Right-click UnitTest1.cs (C#) or UnitTest1.vb (VB), click Rename and change its name to SayHelloFixture.cs (C#) or SayHelloFixture.vb (VB). When prompted to rename the UnitTest1 class select Yes.
- Add
the following namespace directive to the SayHelloFixture.cs
(C#) file or SayHelloFixture.vb (VB):C#using System.Activities;Visual BasicImports System.Activities
- The
SayHello activity doesn’t accept any
arguments yet, but we will write the code to invoke it as though it does. This
will allow us to think about what the interface to our activity feels like
when consuming it. Don’t worry if Visual Studio warns us about the UserName property not being defined
yet. Replace the TestMethod1 with
the following code:C#[TestMethod]public void ShouldReturnGreetingWithName(){var output = WorkflowInvoker.Invoke(new SayHello(){UserName = "Test"});Assert.AreEqual("Hello Test from Workflow 4", output["Greeting"]);}Visual Basic<TestMethod()> Public Sub ShouldReturnGreetingWithName()Dim output = WorkflowInvoker.Invoke( _New SayHello() With {.UserName = "Test"})Assert.AreEqual("Hello Test from Workflow 4", output("Greeting"))End SubNotes: How do I pass arguments to an activity?We can create the activity and initialize the arguments (which are public properties) using object initialization or we can pass a Dictionary<string, object> (C#) or Dictionary(Of String, Object) (VB) of input parameters that map to the names of the input arguments of the activity.The output variable is an IDictionary<string, object> (C#) or IDictionary(Of String, Object) (VB) that contains a map of output variables using the name of the variable as the key.
- CTRL+SHIFT+B to build the
application it should fail with a compile errorUserName has not been defined yet (C#)
UserName has not been defined yet (VB) - Open SayHello.xaml in the designer.
- Open
the arguments pane by clicking on ArgumentsClick on Arguments to open the arguments pane
- Add
the UserName and Greeting arguments as shown in the
following screenshot:Declare the arguments to the activityIn Windows Workflow Foundation (WF), arguments represent the flow of data into and out of an activity. An activity has a set of arguments and they make up the signature of the activity. Each argument has a specified direction: input, output, or input/output.
- Press CTRL+SHIFT+B to build the solution, it should now build without errors.
- Open the SayHelloTestFixture.cs (C#) or SayHelloTestFixture.vb (VB) file.
- Press
CTRL+R, T to run the unit tests in
the current context. The test will run but it will fail, because the activity
is not returning anything in the Greeting out argument.ShouldReturnGreetingwithName test
- Writing text to the console with WriteLine isn’t going to make the test pass so delete the WriteLine activity by right clicking on it and selecting Delete(X).
- You
need to set the value of the Greeting
out argument. To do this, drag an Assign
activity from the Toolbox and drop it onto the designer surface.Drag an Assign activity to the design surface
- Set
the To property to Greeting.Type Greeting in the left box
- We could type the greeting expression into the design surface but since our expression is longer, use the properties window. Click the button to the right
of the Value property to open the expression editor.Click the button to the right of the Value property
- In
the expression editor, you can enter longer expressions. Set the expression to "Hello " & UserName &
" from Workflow 4"The Assign activity that sets the Greeting argument
- Press CTRL+SHIFT+B to build the solution, it should compile without errors.
- Open the Test View window. To do this, on the Test menu point to Windows and click Test View .
- In
the Test View, select the ShouldReturnGreetingWithName
test and click the Run Selection button.Selecting and running the ShouldReturnGreetingWithName test
- Verify
that the test run passes.The test passed
nice post!! man...
ReplyDelete