Code Effects ASP.NET Core 2.0 Implementation Example

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

Prerequisites

  • Your project must be compiled with at least .NET Core 2.0. To verify, right-click the project node in Solution Explorer, select Properties, select Application tab, and make sure that the Target Framework lists ".NET Core 2.0" 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 Core 2.0 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.
  • Either download Rule Editor's main script from here, or get it from /CodeEffects folder that is created by NuGet Manager when you install Code Effects packages, and save it as /wwwroot/lib/codeeffects/codeeffects.js. Then reference it from the bottom of the BODY of /Views/Shared/_Layout.cshtml view:

    <environment include="Development">
        <script src="~/lib/codeeffects/codeeffects.js"></script>
    </environment>
  • Optionally, either get the CodeEffects.Common.css file and one of the theme files from the /CodeEffects folder, or download them from here, and save them with the rest of your CSS files. Then reference them both from the HEAD of your /Site.master file:

    <environment include="Development">
        <link rel="stylesheet" href="~/css/codeeffects.common.css" />
        <link rel="stylesheet" href="~/css/codeeffects.white.css" />
    </environment>

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.Core.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. 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.Core.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
	{
		private IHostingEnvironment environment;

		public HomeController(IHostingEnvironment environment)
		{
			this = environment;
		}

		public IActionResult Index()
		{
			return View();
		}

		[HttpPost]
		public IActionResult 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);
		}

		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 site script located in /wwwroot/js/site.js and referenced from /Views/Shared/_Layout.cshtml view:

<environment include="Development">
    <script src="~/js/site.js" asp-append-version="true"></script>
</environment>
<environment exclude="Development">
    <script src="~/js/site.min.js" asp-append-version="true"></script>
</environment>

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 Core page. Obviously, code examples in this short article don't provide the entire Code Effects rule management process. However, as 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: