Code Effects ASP.NET Implementation Example

The Code Effects engine is formerly known as Web Rule

Below are the basic steps to integrate Code Effects into an ASP.NET application. Note that this is an intentionally simplified example. Code Effects component includes much richer functionality documented throughout this section of the website. Download our ASP.NET demo project to see the full implementation of Code Effects in an ASP.NET application.

First, let's create a .NET class called Person with three public properties that will be used as rule fields, and two public void methods that we can use as rule actions. We are going to use this class as a source object for our rules. In short, the source object represents the data that must be evaluated against the rules that users create using Code Effects. Although you can decorate your source object and its members with attributes provided by the CodeEffects.Rule.Attributes namespace to control their use, validation, evaluation, or rendering, in this example we will just use a 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 the Code Effects control on a web page, and assign the Person class as its source object:

<!-- Only Code Effects-related markup is shown -->

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

<rule:RuleEditor ID="ruleEditor" runat="server"
	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 creation of only evaluation type rules. Setting ShowToolBar to False hides the Toolbar, so we must provide our own mechanism to save and load our rules. The Go button takes care of that.

The above code is all we need to be able to run the page and produce our first business rule on the screen:

	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();

In real life, the instances of Person class would probably come from some external source, and would be evaluated against our rule by some .NET process somewhere on our corporate network. But here we will just create a small program that manually instantiates two instances of Person, fills them with some test values, and passes them to the Evaluator<TSource> 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 the 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);

			// 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.

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

Post your support requests on You can also post your comments and product feedback using the form at the bottom of this page.
Comments: 0
Name (optional):
Comment (URLs are allowed and must start with http:// or https://; all tags will be encoded):
Remaining character count: