Today, I had a request for a sample from one of the customers using BRE on the RFID platform. I built a simple sample for their scenario and thought it would be helpful for others as well if i post it here.

They want to add/delete rows from SQL tables in their rule actions. As far as I remember (correct me if I am wrong), the RFID interface to BRE doesnt provide any field for asserting .NET objects. You can only assert xml documents or sql connections. And to execute a query you have to have a SqlConnection object. Asserting DataConnection objects doesnt work, beacuse DataConnection doesn't expose the underlying SqlConnection object publicly. Ofcourse you can have a dirty solution in which you use reflection to get the SqlConnection object from the DataConnection object.

The solution that I built uses "FactRetrievers". I created a helper class that has a couple of helper methods for creating connections and executing queries and also implementing IFactRetriever interface. The IFactRetriever interface simply asserts an instance of itself into the ruleengine. Now the customer wanted to open the connection dynamically in rule actions. So there is a helper method

                                                 public void CreateConnection(string connectionString, IRuleSetExecutor engine)

that opens up the connection and calls engine.Halt() if the connection opening fails. This method should be called in a rule that is always true ( IF 1 =1 ) and has a prirority higher than all other rules so that it would be executed first.

I have created another helper method

                          public void ExecuteQuery(string query, string arg1, object value1, string arg2, object value2)

That can be used execute any sql queries. The arguments are and parameter name-value pairs that the query takes. Modify suit it to your needs. If BRC supported methods taking params arguments, things would have been much simpler.< P>

Attached is the code and input files that I have used for this sample. You need the northwind database for trying out this sample. Also, remember to gac the generated DatabaseHelper.dll before using the sample.

Download the sample here.