![]() |
Languages »
C# »
General
Intermediate
License: The Code Project Open License (CPOL)
Using a Rules Engine to Separate Business Rules from the ApplicationBy Jeff BramlettUsing a Rules Engine to separate Business rules from the application |
C# 1.0, C# 2.0.NET 1.1, .NET 2.0, Win2K, WinXP, ASP.NET, Visual Studio, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
I believe that a de-coupling of business logic from an application makes for more robust and maintainable code. If done correctly, the logic for the application can be maintained by "non-programming" people using their terminology and methodology. The application can continue to use the business logic as it changes over time and under changing circumstances because the application does not contain the business logic, but only uses it. The location of the business logic can then be centrally located and maintained, or customized and sent for specific reasons for specific installations.
This demo Visual Studio solution is a very simple example (kind of like using an elephant gun to kill a flea). I prepared this example to be easily understood, but have enough complexity to illustrate the capability.
The Rule Engine is freely available for all uses here (CodeProject article).
An on-line example can be seen here.
In order to use a Rules Engine for any automation, I found some requirements:
The Rules Engine used in the demo meets these requirements.
This demo is a simple invoice creation system. This invoice is for calculating varying quantities of widgets. In addition to calculating prices, the business rules must apply quantity discounts, which are different for each widget type. The weight of the total widgets is calculated and an appropriate shipper is selected and each shipper has a different rate. There is a discount applied for each customer based on the current balance that s/he has with the Widget Barn.
The order for calculating the invoice is:
The demo uses an XML file for defining rules. The rules engine does not require it but it is available and it works good. The XML rules file can be loaded from a Web site or from a local file. Both are provided for the demo application. The first thing is to open a rules file:
private void menuRulesWebFile_Click(object sender, EventArgs e)
{
JaxlabReader reader = new JaxlabReader(homeWebfile, "");
rulesEngine = reader.NewRulesEngine;
cbxCustomers.SelectedIndex = 0;
}
Once loaded, the Rules engine will execute upon changes defined in any triggers, so all I have to do is change values to initiate the changes.
rulesEngine.SetVariableValue("SilverQty", qtySilver.Text);
Or I can "run" rules at any time like this:
rulesEngine.RunRule("CalculateInvoice");
I simply set up a method to get the values I need for the invoice and write it to the textbox. The Rules Engine does everything in between.
private void MakeInvoice()
{
List<STRING> lines = new List<STRING>();
lines.Add("Invoice Demo");
lines.Add("");
lines.Add("Customer Name: " + rulesEngine.GetVariable("CustomerType").StringValue);
lines.Add("Customer Balance: " +
rulesEngine.GetVariable("CustomerBalance").DoubleValue.ToString("c"));
lines.Add("");
lines.Add("Quantity of Gold Widgets: " +
rulesEngine.GetVariable("GoldQty").StringValue +
"\t Price: " + rulesEngine.GetVariable
("GoldPrice").DoubleValue.ToString("c") +
"\t Total Price: " + rulesEngine.GetVariable
("GoldTotalPrice").DoubleValue.ToString("c") +
"\t Discount: " + rulesEngine.GetVariable("GoldQtyDiscount").StringValue);
lines.Add("Quantity of Silver Widgets: " +
rulesEngine.GetVariable("SilverQty").StringValue +
"\t Price: " + rulesEngine.GetVariable
("SilverPrice").DoubleValue.ToString("c") +
"\t Total Price: " + rulesEngine.GetVariable
("SilverTotalPrice").DoubleValue.ToString("c") +
"\t Discount: " + rulesEngine.GetVariable
("SilverQtyDiscount").StringValue);
lines.Add("Quantity of Star Widgets: " +
rulesEngine.GetVariable("StarQty").StringValue +
"\t Price: " + rulesEngine.GetVariable=
("StarPrice").DoubleValue.ToString("c") +
"\t Total Price: " + rulesEngine.GetVariable
("StarTotalPrice").DoubleValue.ToString("c") +
"\t Discount: " + rulesEngine.GetVariable("StarQtyDiscount").StringValue);
lines.Add("");
lines.Add("\t\t\t\t\t\t Subtotal: \t" +
rulesEngine.GetVariable("PriceSubTotal").DoubleValue.ToString("c"));
lines.Add("\t\t\t\t\t\t Discount: \t" +
rulesEngine.GetVariable
("CustomerTypeDiscountAmount").DoubleValue.ToString("c"));
lines.Add("\t\t\t\t\t\t Shipping: \t" +
rulesEngine.GetVariable("ShippingTotal").DoubleValue.ToString("c"));
lines.Add("\t\t\t\t\t\t Tax: \t" +
rulesEngine.GetVariable("SalesTaxTotal").DoubleValue.ToString("c"));
lines.Add("\t\t\t\t\t\t Shipper: \t" +
rulesEngine.GetVariable("ShipperName").StringValue);
lines.Add("");
lines.Add("");
lines.Add("\t\t\t\t\t\t Total: \t" +
rulesEngine.GetVariable("InvoiceTotal").DoubleValue.ToString("c"));
txtBox.Lines = lines.ToArray();
}
The event handling with the Rules engine made this easy to implement.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 29 Aug 2006 Editor: Deeksha Shenoy |
Copyright 2006 by Jeff Bramlett Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |