Code Effects ASP.NET MVC 4.6 Implementation Example

Below are the basic steps to integrate Code Effects into an ASP.NET MVC 4.6 web application. Note that this article provides code excerpts taken from the ASP.NET MVC 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 MVC 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 /Views/Shared/_Layout.cshtml view:

    <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 same /Views/Shared/_Layout.cshtml view:

    <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.Mvc.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 in the view. To do that, we need to prepare the client by adding a DIV container to the markup of the /Views/Home/Index.cshtml view 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 view is loaded. The ClientSettings class will serve as settings container:

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

		public ClientSettings() { }
	}
}

Our HomeController declares an action method LoadSettings:

namespace CodeEffects.Rule.Mvc.Demo.Controllers
{
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			// Create a new rule model and store it in the bag
			// The view passes it to the RuleEditor
			ViewBag.Rule = Rule.Models.RuleModel.Create(typeof(Patient));

			return View();
		}

		[HttpPost]
		public ActionResult LoadSettings()
		{
			ClientSettings s = new ClientSettings();
			RuleEditor editor = GetRuleEditor();

			s.EditorData = editor.GetInitialSettings();
			s.SourceData = editor.GetClientSettings();

			// Send the settings back to the client
			return Json(s, JsonRequestBehavior.DenyGet);
		}

		private RuleEditor GetRuleEditor()
		{
			// Use client ID of the DIV container
			Code.Rule.Web.RuleEditor editor = new RuleEditor("divRuleEditor")
			{
				Mode = Common.RuleType.Execution,
				SourceType = typeof(Patient),
				Rule = Rule.Models.RuleModel.Create(typeof(Patient))
			};
			return editor;
		}
	}
}

Our demo project also includes the main script referenced from /Views/Shared/_Layout.cshtml view:

<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 action 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 MVC 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: