Code Effects MVC 4.5 Implementation Example
The Code Effects engine is formerly known as Web Rule
Below are the basic steps to integrate Code Effects into a classic ASP.NET MVC 4.5 application. Note that this article provides code excerpts taken from the ASP.NET MVC 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 MVC project by referencing its assemblies from NuGet. Please note that Rule Editor requires different assemblies on different .NET web platforms. The correct NuGet package for Rule Editor on ASP.NET MVC 4.5 and below 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.
- In classic ASP.NET MVC app you don't need to reference Rule Editor's main script or CSS file(s) on your views, everything that Rule Editor needs is compiled into its assemblies.
Implementation
First, let's create a .NET class called Person with three public properties that will be used as rule fields, and two public void methods that we can use as rule actions. We'll use this class as a source object for our rules. In short, the source object represents the data that must be evaluated against the rules that users create using Rule Editor. Although you can decorate your source object and its members with attributes provided by the CodeEffects.Rule.Attributes namespace to control how members of the source object are used, validated, evaluated, or rendered, in this example we will just use a plain class in order to keep things simple:
namespace TestLibrary
{
public class Person
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public void ActionOne() { }
public void ActionTwo() { }
}
}
Now we need to add a controller to our project:
using System.Web.Mvc;
using CodeEffects.Rule.Models;
using TestLibrary.Models;
namespace TestLibrary.Controllers
{
public class TestController : Controller
{
[HttpGet]
public ActionResult Index()
{
ViewBag.Rule = RuleModel.Create(typeof(Person));
return View();
}
}
}
The Index action creates an instance of the RuleModel class, telling it that we want the Person type to be used as our source object, which is then stored in the ViewBag object. Note that if we wanted to load an existing rule into the Rule Editor, we'd use a different overload of the RuleModel.Create method - specifically the one that takes Rule XML and source object's type as parameters.
Next, we need to add Rule Editor to a view. In MVC, registration of the editor consists of three statements. The first statement renders the CSS theme - you can place this statement anywhere in your markup. You can set the theme value to ThemeType.None and replace the default theme with your own.
@{
Html.CodeEffects().Styles()
.SetTheme(ThemeType.Blue)
.Render();
}
The second statement renders the Rule Editor and all its UI elements on the page:
<div>
@{
Html.CodeEffects().RuleEditor()
.Id("ruleEditor")
.SaveAction("Save", "Post")
.DeleteAction("Delete", "Post")
.LoadAction("Load", "Post")
.Mode(RuleType.Execution)
.Rule(ViewBag.Rule)
.Render();
}
</div>
It's very important to give the Rule Editor a server ID. Obviously, you must use different IDs for each instance, if the view declares more than one instance. This ID plays an important role in MVC type conversion when the view posts back to the server.
Note that this code example doesn't specify any rules for the Rules and context menus. The Toolbar and reusable rules topics provide details on that subject. Also, even though this code specifies the Save, Delete and Load actions, only the code of the Save action is shown here. The MVC demo project provides implementation of all three actions.
You will have to implement your own Save, Delete, and Load UI controls, and add a form tag for them if you don't want the rule editor to display its Toolbar. To hide the Toolbar, set the value of the RuleEditor.ShowToolBar property to False.
The third statement writes all the client data that is necessary for Rule Editor to function properly. This statement can be placed anywhere inside of the body tag:
@{
Html.CodeEffects().Scripts().Render();
}
All this is enough to build the project, run it, and view the Rule Editor in the browser. We just need to add the Save action to our controller to be able to save rules when the rule author presses the Save button on the Toolbar:
[HttpPost]
// Notice that the name of the only parameter
// matches the server ID set in the view
public ActionResult Save(RuleModel ruleEditor)
{
// We need to "bind" the source type to the rule model
ruleEditor.BindSource(typeof(Person));
// Add the rule model to the ViewBag object
ViewBag.Rule = ruleEditor;
// Make sure that the Rule Area is not empty and the current rule is valid
if (ruleEditor.IsEmpty() || !ruleEditor.IsValid())
{
return View("Index");
}
// Get Rule XML
string xml = ruleEditor.GetRuleXml();
// Save the rule to your database
new YourStorageService().SaveRule(ruleEditor.Id, xml);
// Done. Load the view
return View("Index");
}
As you can see, MVC performs type conversion when it deals with action parameters (with some assistance from Code Effects). This conversion is based on Code Effects' ID that we set in our view. The view uses this ID to initialize the RuleEditor type. The action also uses this ID to name its single parameter, even though the parameter is of the RuleModel type. This "abnormality" is explained in some details in the Toolbar topic.
Post your questions on
Stackoverflow and become a part of our growing community
Comments:
0
|
|