|
using System.Collections.Generic;
using System.Collections;
namespace CustomRulesMVVM.Models
{
class CustomRules : CollectionBase, IRule
{
#region Members
private const string AND = "AND";
private const string OR = "OR";
#endregion
#region Properties
public bool IsSelected { get; set; }
public string SelectedItem { get; set; }
public string RuleGroupJoinOperator { get; set; }
public int? RuleGroupSeqNum { get; set; }
#endregion
#region Public methods
/// <summary>
/// Evalue rules in Rule collection
/// </summary>
/// <param name="collection"></param>
/// <returns></returns>
public bool Eval(Dictionary<string, object> collection)
{
return EvaluateCustomRules(collection);
}
#endregion
#region Private Methods
/// <summary>
/// Evaluate Two or More rules joined by AND/OR operator
/// </summary>
/// <param name="collection"></param>
/// <returns></returns>
private bool EvaluateCustomRules(Dictionary<string, object> collection)
{
//In Rule collection evaluate between first two rules by calling Eval method of each rule.
if (null != this.Item(0).RuleJoinOperator)
{
switch (this.Item(0).RuleJoinOperator)
{
case AND:
if (this.Item(0).Eval(collection) && this.Item(1).Eval(collection))
this.IsSelected = true;
break;
case OR:
if (this.Item(0).Eval(collection) || this.Item(1).Eval(collection))
this.IsSelected = true;
break;
default:
this.IsSelected = false;
break;
}
//Evaluate if more than two rules present till last rule
if (this.Count > 2)
{
for (int i = 1; i < this.Count - 2; i++) //Loop till count-2 as last rule will not have RuleJoinOperator
{
switch (this.Item(i).RuleJoinOperator)
{
case AND:
if (this.IsSelected && this.Item(i).Eval(collection))
this.IsSelected = true;
else
this.IsSelected = false;
break;
case OR:
if (this.IsSelected || this.Item(i).Eval(collection))
this.IsSelected = true;
else
this.IsSelected = false;
break;
default:
this.IsSelected = false;
break;
}
}
}
}
else
{
this.IsSelected = this.Item(0).Eval(collection);
}
return this.IsSelected;
}
#endregion
#region CollectionBase methods
public void Add(CustomRule item)
{
this.List.Add(item);
}
public void Remove(CustomRule item)
{
this.List.Remove(item);
}
public CustomRule Item(int index)
{
return this.List[index] as CustomRule;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am a .Net developer, currently working in Singapore worked wiith Societe Generale Global Solution Centre, Bangalore and was previously with Cognizant.I have more than 8 years of .Net experience in BFSI domain. I am an experienced developer in C#, VB.Net, Silverlight, WPF, WCF, LINQ, Entity Framework, SSIS, NHibernate, ASP.Net and SQL Server.