Code Effects ASP.NET 4.6 Implementation Example

Below are the basic steps to integrate Code Effects into an ASP.NET 4.6 application. Note that this article provides code excerpts taken from the ASP.NET 4.6 demo project.

Prerequisites

  • Your project must be compiled with at least .NET Framework 4.6.2. To verify, right-click the project node in Solution Explorer, select Properties, select Application tab, and make sure that the Target Framework lists ".NET Framework 4.6.2" or a higher version.
  • Add Rule Editor to your web project by referencing its assemblies from NuGet. Keep in mind that Rule Editor requires different assemblies on different .NET web platforms. The correct NuGet package for Rule Editor on ASP.NET 4.6 and up is listed here.
  • Optionally, reference either your licensed assembly of Rule Engine, or the trial assembly from NuGet if you plan to evaluate rules in your web application. Remember that the Engine Trial license delays all calls to Evaluate() and Filter() methods for six seconds.
  • The NuGet package installs the main Rule Editor javascript file in the root of your web project as /Scripts/codeeffects.js. Add this script to the bottom of your HTML page that hosts the Rule Editor:

    <script type="text/javascript" src="/Scripts/codeeffects.js"></script>

    The latest version of that script is always available on our website as well.

  • The NuGet package also installs Code Effects CSS theme files in /Content/ folder of your project. The use of Code Effects themes is optional; please refer to this topic for details. If themes are used, reference the main codeeffects.common.css file and one of the theme files such as codeeffects.white.css from the HEAD of your /Site.master file:

    <link href="/Content/codeeffects.common.css" rel="stylesheet"/>
    <link href="/Content/codeeffects.white.css" rel="stylesheet"/>

Implementation

First, we create a .NET class called Patient that we can use as a source object. In short, the source object represents the data that must be evaluated against the rules that users create using Rule Editor:

namespace CodeEffects.Rule.Asp.Demo.Models
{
	public class Patient
	{
		public string FirstName { get; set; }
		public string LastName { get; set; }

		// the rest of the class is omitted
	}
}

Next, we need to initiate Rule Editor on the page. In order to do that, we need to prepare the client by adding a DIV container to the markup of the /Default.aspx page to host the Rule Editor. The markup is very simple:

<div id="divRuleEditor"></div>

We also need to load editor's settings from the server when the page is loaded. We are going to use ASP.NET web methods on the server and jQuery's .ajax(...) method on the client to transport data from server to the client. The ClientSettings class will serve as settings container:

namespace CodeEffects.Rule.Asp.Demo.Models
{
	public class ClientSettings
	{
		public string EditorData { get; set; }
		public string SourceData { get; set; }

		public ClientSettings() { }
	}
}

/Default.aspx.cs code-behind file declares the LoadSettings() web method (among other methods):

[WebMethod(false)]
public static ClientSettings LoadSettings()
{
	ClientSettings s = new ClientSettings();
	CodeEffects.Rule.Web.RuleEditor editor = GetEditor();

	// Load existing rules into the tool bar's rules menu
	editor.ToolBarRules = Storage.GetAllRules();

	if (editor.ToolBarRules.Count > 0)
	Storage.SortRules(editor.ToolBarRules);

	// Load only evaluation rules into context menus
	editor.ContextMenuRules = Storage.GetEvaluationRules();

	// Gets the initial settings necessary for editor's initialization on the client
	s.EditorData = editor.GetInitialSettings();

	// Gets source's fields, methods and actions as well as other source-related settings
	s.SourceData = editor.GetClientSettings();

	return s;
}

private static RuleEditor GetEditor()
{
	// Use client ID of the div container that hosts rule editor in /Default.aspx
	CodeEffects.Rule.Web.RuleEditor editor = new RuleEditor("divRuleEditor")
	{
		Mode = Common.RuleType.Execution,
		SourceType = typeof(Patient)
	};

	retur editor;
}

Our demo project also includes the main script referenced from /Default.aspx page:

<script type="text/javascript" src="/Scripts/main.js"></script>

The role of that script is to initialize Rule Editor on the page when the DOM is loaded and provide the client common rule management functionality (omitted for clarity):

var codeeffects;

$(function ()

{
	// Load initialization settings
	// The post() method is declared in the same script
	// We are calling the LoadSettings web method that returns our ClientSettings class
	post("/LoadSettings", null, function (data)
	{
		// Init the ruele editor and store its reference in a global variable
		codeeffects = $rule.init(data.EditorData);
		codeeffects.clear();

		// Set action delegates that the editor calls on rule save, load and delete
		// Those functions are declared in the same script
		codeeffects.setClientActions(loadRule, deleteRule, saveRule);

		// Load the source object
		codeeffects.loadSettings(data.SourceData);
	});
});

That's all we need in order to initialize Rule Editor on ASP.NET page. Obviously, code examples in this short article don't provide the entire Code Effects rule management process but, as was mentioned above, you can always download our demo project to see full implementation in action.

Comments: 0
Post your questions about Code Effects on Stackoverflow.com
and become a part of our growing community
Name (optional):
Comment (URLs are allowed and must start with http:// or https://; all tags will be encoded):
Remaining character count: