Web Rule ASP.NET Implementation Example

The following are the basic integration steps of Web Rule into ASP.NET application. Note that this is intentionally simplified example. Web Rule includes much richer functionality documented throughout this section of the website. Download our ASP.NET demo project to see the full implementation of Web Rule in ASP.NET application.

First, let's create a .NET class Person with three public properties and two public void methods that we could use as rule actions. We'll use this class as a source object of our rules. In short, source object represents the data that must be evaluated against the rules that users create using Web Rule. Although you can decorate your source object with attributes provided by the CodeEffects.Rule.Attributes namespace to control how members of the source object are used, validated, evaluated or rendered, here we just use the plain class in order to keep things simple:

using CodeEffects.Rule.Attributes;

namespace TestLibrary
{
	public class Person
	{
		public string FirstName { get; set; }
		public string MiddleName { get; set; }
		public string LastName { get; set; }

		public void ActionOne() { /* some action logic goes here */ }

		public void ActionTwo() { /* some other action logic goes here */ }
	}
}

Assuming that we have referenced CodeEffects.Rule.dll in our project, we need to register Web Rule control on a web page and assign the Person class as its source object:

<!-- Only Web Rule-related markup is shown -->

<%@ Register assembly="CodeEffects.Rule"
	namespace="CodeEffects.Rule.Asp" tagprefix="rule" %>

<rule:RuleEditor ID="ruleEditor" runat="server"
	Mode="Execution"
	ShowToolBar="false"
	SourceAssembly="TestLibrary"
	SourceType="TestLibrary.Person" />

<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="Go" />

Setting the Mode property to Execution allows us to use the Rule Editor to create both types of rules - execution and evaluation. The default value of the Mode is Evaluate which allows creating of only evaluation type rules. Setting the ShowToolBar to False hides the Toolbar and expects us to provide our own mechanism to save and load our rules. The button Go takes care of that.

These simple steps are all we need to be able to run the page and produce our first business rule on the screen:

if ( FirstName is equal to "Paul" or LastName starts with "D" ) and MiddleName contains "J" then ActionOne else ActionTwo

The next step is to store the rule that we just created in a database in order to make it available later. We can do this in our code-behind when the Go button is clicked:

using System;

namespace TestWebApplication
{
	public partial class WebForm1 : System.Web.UI.Page
	{
		protected void Go(object sender, EventArgs e)
		{
			// Save the rule to database
			if (!ruleEditor.IsEmpty && ruleEditor.IsValid)
			{
				string ruleXml = ruleEditor.GetRuleXml();
				YourDataModel.SaveRuleXml(ruleXml);
			}
		}
	}
}

In real life the instances of Person class would probably come from some external source to be evaluated against our rule by some .NET process somewhere on our corporate network. But here we just create a small program that manually instantiates two instances, fills them up with some test values and passes them to the Evaluator class for final evaluation against our rule:

using CodeEffects.Rule.Core;
using TestLibrary;

namespace TestProgramm
{
	public class Executor
	{
		static void Main(object[] args)
		{
			// Load the rule from database
			string ruleXml = YourDataModel.GetRuleXml();

			// Create first instance of the source object
			Person person = new Person();
			person.FirstName = "John";
			person.MiddleName = "James";
			person.LastName = "Doe";

			// Evaluate the rule against the first instance
			Evaluator<Person> evaluator = new Evaluator<Person>(ruleXml);
			evaluator.Evaluate(person);

			// Second instance
			person = new Person();
			person.FirstName = "John";
			person.MiddleName = "James";
			person.LastName = "Smith";

			// Evaluate the rule against the second instance.
			// We can reuse the existing evaluator because
			// both evaluations use the same rule.
			evaluator.Evaluate(person);
		}
	}
}

It's easy to see that the ActionOne will be called at the end of first evaluation and the ActionTwo at the end of the second evaluation. Evaluator makes those calls by invoking action methods. Read more about action methods, external actions, in-rule methods and other cool features of Web Rule in the source object topic.

Code Effects Software ©  · Web Scheduler · CodeEffects.com
Current version 2.0.1.4 · Built on March 27, 2012
Terms and Conditions · Privacy Policy