I put together a demonstration for one of our clients demonstrating the Business Rule Engine.
The scenario was:
We have a form that has the person with an loan amount, and based on the state that they live in, a certain interest rate needs to be charged.
The entire purpose of the Business Rule Engine is to abstract the business logic from a developer. So in theory, I could put each of the states and their subsequent rate in the BRE. I wanted to abstract it even further. I wanted to have the data in a db that the business would be able to modify without even looking at the rule.
I created at table to store the rates
I created this schema so I can create the underlying class:
I opened the visual studio command prompt and created the .cs file from the .xsd by exectuting xsd.exe RateFacts.xsd /classes
I added a new method to the given class
I also created a Fact Creator (for testing purposes)
But the real purpose of this article is to show how to have long term database facts.
I have found the documentation on this rather sparse.
In the BRC, I have created two database facts, notice that they are Data table entries, this means that I am need to pass a Typed Data Table for this to work
I have created a couple of other Vocabulary items:
Here are the list of all of the Vocabulary Items
Trick: Hopefully this caught your attention
When you pass a data table, and you determine the value in the IF pane, it keeps that row of the table in memory for use in the THEN pane.
Here is the rule, I highlighted the DB Rate and DB State vocabulary items in the rule
Here is the form:
The code to actually call the Apply Rate policy is:
Notice that we don’t ever set the database. This is where the documentation seems to drop off.
I created a FactRetriever method that returns a Typed database table to the BRE (in the namespace as the Record class).
Notice, this is a long term fact. It only populates the Typed Table if it is null.
Now I need to make sure this logic gets called by anything that calls the policy, notice in the properties of the Apply Loan policy, the Fact Retriever points to the DBFactRetiever class (yes I had to gac and restart the BRC before I could see it)
When I run application, this is what I get:
And as long as I don’t restart the console application, it won’t re-query the db.