GetRuleDelegate delegate

Namespace: CodeEffects.Rule.Core
Assembly: CodeEffects.Rule.dll

Defines a signature for a public method that takes a rule ID as its single parameter and returns Rule XML.

Syntax

public delegate string GetRuleDelegate(string ruleId);

Parameters

  • ruleId, System.String - ID of the rule.

Notes

If a rule references one or many reusable rules, Code Effects component receives or encounters the ID of a reusable rule and needs to get its XML while it reads the current rule. If all referenced rules are not stored in the same XML document as the original rule, GetRuleDelegate gives Code Effects the ability to get that Rule XML. This delegate is used in several features of Code Effects:

  • Rule Evaluation. All public constructors of all evaluator classes have overloads that take GetRuleDelegate as a parameter. If the delegate is not supplied, Code Effects component throws an exception if it encounters a reference to a reusable rule and cannot find it in the same XML document. For example, let's say that we have a method of the GetRuleDelegate signature (it takes one string and returns a string):

    public string LoadRuleXml(string ruleId)
    {
    	// First, check if a file with this rule ID exists in the evaluation directory
    	string file = GetFilePath(ruleId, true);
    	if(File.Exists(file)) return File.ReadAllText(file);
    	else
    	{
    		// Now check if it exists in the execution directory
    		file = GetFilePath(ruleId, false);
    		if(File.Exists(file)) return File.ReadAllText(file);
    	}
     
    	// No such rule found, return null
    	return null;
    }

    (This code sample is taken from one of our demo projects.)

    This is how we'd evaluate our rule if we knew it might reference some reusable rules:

    private void EvaluateRule()
    {
    	// Get the Rule XML
    	string ruleXml = GetMyRule();
     
    	// Create a new instance of Evaluator,
    	// passing it the rule XML and GetRuleDelegate
    	Evaluator<MySourceObject> evaluator =
    		new Evaluator<MySourceObject>(ruleXml, LoadRuleXml);
    
    	// Evaluate the rule
    	bool success = evaluator.Evaluate(mySourceObjectInstance);
    }
  • Rule Validation. Code Effects component performs automatic rule validation every time the rule author saves a rule (it's semi-automatic in MVC), but regular validation is not enough if a rule references any other rules. You need to check for circular references as well, but if all rules are not stored in the same XML file, Code Effects component must have a way to retrieve the XML of any referenced rule in order to check it, too. To enforce this check in ASP.NET, all you have to do is set the value of the RuleEditor.GetRuleDelegate property in the OnLoad handler:

    this.ruleControl.GetRuleDelegate = LoadRuleXml;

    In MVC, you need to pass the delegate to the RuleModel.IsValid() method:

    [HttpPost]
    public ActionResult Save(RuleModel ruleEditor)
    {
    	// We need to "bind" the source type to the rule model
    	ruleEditor.BindSource(typeof(Patient));
     
    	// Make sure that the Rule Area is not
    	// empty and the current rule is valid
    	if(ruleEditor.IsEmpty() ||
    		!ruleEditor.IsValid(LoadRuleXml))
    	{
    		ViewBag.Message = "The rule is empty or invalid";
    		return View("Index");
    	}
     
    	// The rest of Save action is omitted

    (This code sample is taken from one of our demo projects.)

    Note that if the delegate is not set or supplied, the check for circular references will be skipped in both cases.

  • ToString() Methods. You can get a string representation of the current rule by calling the .ToString() methods of Asp.RuleEditor or Mvc.RuleEditor. In both cases, some overloads of .ToString() take GetRuleDelegate as a parameter. The method needs this delegate in order to retrieve the proper display names if the rule references any reusable rules.

Post your support requests on Stackoverflow.com. You can also post your comments and product feedback using the form at the bottom of this page.
Comments: 0
Name (optional):
Comment (URLs are allowed and must start with http:// or https://; all tags will be encoded):
Remaining character count: