Code Effects ASP.NET Implementation Example
Below are the basic steps to integrate Code Effects into an ASP.NET application. Note that this article provides code excerpts taken from the ASP.NET 7.0 demo project.
Prerequisites
- The current version of the Code Effects engine 5.x can be referenced in projects that target either the old .NET Core 2.0 - 3.1 or the latest .NET 6.0 and up platforms. To verify your current platform target, right-click the project node in Solution Explorer of VS, select Properties, and check the value of the Target Framework.
- 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 - 3.1 and ASP.NET 6.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.
-
Download Rule Editor's main script from here and save it with the rest of your scripts, for example as /lib/codeeffects/editor.js. Then reference it from the bottom of the BODY of /Views/Shared/_Layout.cshtml view:
<environment include="Development">
<script src="~/lib/codeeffects/editor.js"></script>
</environment>
-
Optionally, download the CodeEffects.Common.css file and one of the theme CSS files 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; }
}
}
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();
return Json(s);
}
private RuleEditor GetRuleEditor()
{
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.
Post your questions on
Stackoverflow and become a part of our growing community
Comments:
0
|
|