Storing Rules

As we mentioned in the Code Effects Basics topic, Code Effects component is not a system or environment - it's a component. As such, it doesn't manage your rules. It helps rule authors to create, update, or delete business rules, and provides developers with a way to evaluate them.

Code Effects component generates business rules in XML format, called Rule XML. Its schema for the latest version 4.1.x.x is located here.

Because it's an XML document, a rule can be stored pretty much anywhere. The most common way (in the .NET world, at least) is to store them in a SqlServer database. SqlServer 2005 and up provides an XML data type that is perfect for XML-based business rules.

If you intend to store rules as XML files in a file system, you have two options:

  • You can simply store each rule as a separate XML file, the way you received it from Code Effects:

    <?xml version="1.0" encoding="utf-8"?>
    <codeeffects
    		xmlns="http://CodeEffects.com/schemas/rule/41"
    		xmlns:ui="http://CodeEffects.com/schemas/ui/4">
    	<rule
    		id="f480a6ce-deb0-4622-b7a3-b87fcca316ff"
    		webrule="3.0.0.151"
    		utc="2012-08-04T13:26:10.3429"
    		type="CodeEffects.Rule.Site.Common.Soap.Objects.Patient,
    			CodeEffects.Rule.Site, Version=2.0.0.0, Culture=neutral,
    			PublicKeyToken=null"
    		eval="false">
    			<name>Sample Rule</name>
    			<definition>
    				<!-- Definition nodes are omitted -->
    			</definition>
    			<format>
    				<!-- Formatting nodes are omitted -->
    			</format>
    	</rule>
    </codeeffects>
  • You can also store multiple rules in a single XML file. Such file is known as ruleset:

    <?xml version="1.0" encoding="utf-8"?>
    <codeeffects
    		xmlns="http://CodeEffects.com/schemas/rule/41"
    		xmlns:ui="http://CodeEffects.com/schemas/ui/4">
    	<rule
    		id="f480a6ce-deb0-4622-b7a3-b87fcca316ff"
    		webrule="3.0.0.151"
    		utc="2012-08-04T13:26:10.3429"
    		type="CodeEffects.Rule.Site.Common.Soap.Objects.Patient,
    			CodeEffects.Rule.Site, Version=2.0.0.0, Culture=neutral,
    			PublicKeyToken=null"
    		eval="false">
    			<name>Sample Rule 1</name>
    			<definition>
    				<!-- Definition nodes are omitted -->
    			</definition>
    			<format>
    				<!-- Formatting nodes are omitted -->
    			</format>
    	</rule>
    	<!-- There can be any number of rules in a single ruleset -->
    	<rule
    		id="0D1484FB-1757-4855-871C-90539A3569EB"
    		webrule="3.0.0.151"
    		utc="2012-08-05T16:23:26.0945"
    		type="CodeEffects.Rule.Site.Common.Soap.Objects.Patient,
    			CodeEffects.Rule.Site, Version=2.0.0.0, Culture=neutral,
    			PublicKeyToken=null"
    		eval="false">
    		<name>Sample Rule 2</name>
    		<definition>
    			<!-- Definition nodes are omitted -->
    		</definition>
    		<format>
    			<!-- Formatting nodes are omitted -->
    		</format>
    	</rule>
    </codeeffects>

    Its schema is the same as for a single Rule XML. The only difference is that its root <codeeffects> node can contains multiple <rule> nodes (business rules).

    All public constructors of all evaluator classes have a string XML parameter called rulesetXml. This parameter can take either the entire ruleset, or a single Rule XML document, or even an outer XML of a single <rule> node (this is convenient if you loop through the rules in a ruleset.)

    It's easy to create an empty ruleset:

    System.Xml.XmlDocument ruleset =
    		CodeEffects.Rule.Common.Xml.GetEmptyRuleDocument();

    You can add rules to its DocumentElement node and save its OuterXml as an XML file.

When a rule author creates a new business rule on the client using the Rule Editor, and clicks the Save button on the Toolbar, Code Effects component simply tells the server: "The user wants to create or update a business rule. Here is that rule as an XML string, and here is some additional information about it, such as its auto-generated ID, a list of optional reusable rules that it may reference, as well as some other useful data." At this point, all the server has to do is store the received Rule XML in your rule storage, whether it’s a database, a local file system, or something as complicated as network storage or remote web services.

To receive a rule in ASP.NET, the server needs to handle the SaveRule event of the RuleEditor class. This event passes an instance of the SaveEventArgs class that contains the rule's data. Download and run the ASP.NET demo project to see an implementation of such an event handler.

In MVC, the server needs to declare an action that receives an instance of the RuleModel class as a parameter, which contains everything about that rule. Again, our demo project has all the details.

The previous two paragraphs are true only if you run Code Effects component with the Toolbar enabled. If you hide it by setting RuleEditor.ShowToolBar to False, then you must handle all this logic yourself, getting the Rule XML from Code Effects by calling its GetRuleXml() method. Our data filtering demo projects use this approach.

Things become even easier (or more difficult, depending on your level of client scripting) if you integrate Code Effects with your client-side application, and run it as a native object, either in MVC or ASP.NET.

Post your support requests on Stackoverflow.com. 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: