Using Business Rules Composer in BizTalk Server 2009 : Walkthrough

Introduction

Business Rules Composer in BizTalk Server 2009 is a great tool which enables us to separate the Business Rules from the actual process implementation.

Also, it gives the ability to implement shared BR across multiple processes, that means that the BR are shared, customizable, separately tested and modified as required.

This article is a quick walkthrough to demonstrate the usage of Business Rules Composer.  

In this sample I’m using BTS 2009, Visual Studio 2008 on Windows Server 2008

The sample about a leave request that needs to be validated against very simple business rule.

Building the sample

First, we need to build a BTS 2009 project, so create a new one in the Visual Studio.

Then, build the leave request schema as the following:

image

Build the message that will be sent to the BRE like the following:

image

Then build the business rule like that:

1. Add new Vocabulary

image

2. Name it [LeaveRequestData], and add a new definition like the following

image

Important: Remember to get the schema fully qualified name from the file properties window in Visual Studio, if you don’t but the fully qualified name you will not able to pass the parameters in Visual Studio when you try to call the rule.

image 

Fill the definition like the following

image

Repeat the above from the [RequestedDays], [LeaveType] and the [ValidationResults], but for the [ValidationResults] make it [Set] operation.

image

3. Implement the first business rule like the following:

image

Implement the orchestration as the following:

image

Remember the Business Rules calling should be like that:

image

4. Now, it’s time to test our project:

a. Add strong name key and assign it to the project

b. In the deployment tab on the project properties screen, modify the application name to [BTS2009.Articles.BRE]

c. Deploy your solution

d. Configure the project as the following:

I’ll use three file ports, one to receive the incoming message, the second for the valid requests and the last one for the invalid requests.

image 

e. Start your application

5. Implement 2 sample input message like the following:

Valid request message:

<ns0:LeaveRequest xmlns:ns0="http://BTS2009.Articles.BRE.EmployeeLeaveRequest">
  <RequestID>E0001</RequestID>
  <Name>Ahmed Shokr</Name>
  <RequestedDays>10</RequestedDays>
  <AvailableBalance>11</AvailableBalance>
  <LeaveType>Normal</LeaveType>
</ns0:LeaveRequest>

Invalid request message

<ns0:LeaveRequest xmlns:ns0="http://BTS2009.Articles.BRE.EmployeeLeaveRequest">
  <RequestID>E0001</RequestID>
  <Name>Ahmed Shokr</Name>
  <RequestedDays>12</RequestedDays>
  <AvailableBalance>11</AvailableBalance>
  <LeaveType>Normal</LeaveType>
</ns0:LeaveRequest>

6. Start your BTS application and paste the 2 sample messages to the [Intial requests] folder

You should see one message on the [Valid requests] folder and the other one should go to the [Invalid requests] folder.

7. Now, let’s modify the business rule to add a new case which is [Unpaid] vacation:

image

Deploy the new version of the business rule, restart the BTS Host Instance.

8. Update the Invalid message request message like the following:

<ns0:LeaveRequest xmlns:ns0="http://BTS2009.Articles.BRE.EmployeeLeaveRequest">
  <RequestID>E0001</RequestID>
  <Name>Ahmed Shokr</Name>
  <RequestedDays>12</RequestedDays>
  <AvailableBalance>11</AvailableBalance>
  <LeaveType>Unpaid</LeaveType>
</ns0:LeaveRequest>

However in this message the [RequestedDays] is greater than the [AvailableBalance] but the message should be moved to the [Valid requests] folder.

We can see that we applied modifications in the business rules without touching the application itself, and whatever BTS applications using this business rule, the new changes will apply without new modifications.

We can apply as much cases as we need, in the Business Rules Composer and it will apply on every application which is using the rule.

You can find the sample project plus the Policy files here

I hope that helped

Ahmed