Code Effects MVC Implementation Example

The Code Effects engine is formerly known as Web Rule

Below are the basic steps to integrate Code Effects business rules engine into an ASP.NET MVC 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 MVC demo project to see the full implementation of Code Effects in an ASP.NET MVC 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'll 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 how members of the source object are used, validated, evaluated, or rendered, 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 */ }

Now we need to add a controller to our project:

using System.Web.Mvc;
using CodeEffects.Rule.Models;
using TestLibrary.Models;
namespace TestLibrary.Controllers
	public class TestController : Controller
		public ActionResult Index()
			ViewBag.Rule = RuleModel.Create(typeof(Person));
			return View();

The Index action creates an instance of the RuleModel class, telling it that we want the Person type to be used as our source object, which is then stored in the ViewBag object. Note that if we wanted to load an existing rule into the Rule Editor, we'd use a different overload of the RuleModel.Create method, specifically the one that takes Rule XML and source object's type as parameters.

Next, we need to add Code Effects component to a view. In MVC, registration of the component consists of three statements. The first statement renders the CSS theme; you can place this statement anywhere in your markup. Code Effects component will have no styles if you set the theme value to ThemeType.None, and replace the default theme with your own.


The second statement renders the Rule Editor and all its UI elements on the page:


It's very important to give the Rule Editor a server ID. Obviously, you must use different IDs for each instance, if the view declares more than one instance. This ID plays an important role in MVC type conversion when the view posts back to the server.

Note that this code example doesn't specify any rules for the Rules and context menus. The Toolbar and reusable rules topics provide details on that subject. Also, even though this code specifies the Save, Delete and Load actions, only the code of the Save action is shown here. The MVC demo project provides implementation of all three actions.

You will have to implement your own Save, Delete, and Load UI controls, and add a form tag for them if you don't want the rule editor to display its Toolbar. To hide the Toolbar, set the value of the RuleEditor.ShowToolBar property to False.

The third statement writes all the client data that is necessary for Code Effects to function properly. This statement can be placed anywhere inside of the body tag:


All this is enough to build the project, run it, and view the Rule Editor in the browser. We just need to add the Save action to our controller to be able to save rules when the rule author presses the Save button on the Toolbar:

// Notice that the name of the only parameter
// matches the server ID set in the view
public ActionResult Save(RuleModel ruleEditor)
	// We need to "bind" the source type to the rule model
	// Add the rule model to the ViewBag object
	ViewBag.Rule = ruleEditor;
	// Make sure that the Rule Area is not empty and the current rule is valid
	if (ruleEditor.IsEmpty() || !ruleEditor.IsValid())
		return View("Index");
	// Get Rule XML
	string xml = ruleEditor.GetRuleXml();
	// Save the rule to your database
	new YourStorageService().SaveRule(ruleEditor.Id, xml);
	// Done. Load the view
	return View("Index");

As you can see, MVC performs type conversion when it deals with action parameters (with a little help from Code Effects). This conversion is based on Code Effects' ID that we set in our view. The view uses this ID to initialize the RuleEditor type. The action also uses this ID to name its single parameter, even though the parameter is of the RuleModel type. This "abnormality" is explained in some details in the Toolbar 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: