I am designing a query builder, where the user can just add conditions or groups, and each group can, in turn, have conditions in it. I came up wit the below class design. Can this design be improved?
public enum RuleType
{
Group,
Condition
}
public enum Operator
{
And,
Or
}
public enum ConditonValues
{
GreaterThan = 1,
EqualTo = 2,
LessThan = 3
}
public abstract class RuleParent
{
public Guid Id { get; set; }
public int Order { get; set; }
public RuleType RuleType { get; set; }
}
public class RuleSet : RuleParent
{
public ConditonValues Condition { get; set; }
public string Field { get; set; }
public object Data { get; set; }
}
public class MyGroup : RuleParent
{
public Operator Operator { get; set; }
public List<RuleParent> Rules { get; set; }
public MyGroup()
{
Rules = new List<RuleParent>();
}
}
Example usage:
var groupM = new MyGroup();
groupM.Rules.AddRange(new List<RuleParent>
{
new RuleSet() { Condition = ConditonValues.EqualTo, Field="name", Data ="reza" , RuleType = RuleType.Condition },
new MyGroup() {
Rules = new List<RuleParent>()
{
new RuleSet() { Condition = ConditonValues.EqualTo, Field="category", Data ="Tools" , RuleType = RuleType.Condition },
new RuleSet() { Condition = ConditonValues.GreaterThan, Field="price", Data ="100" , RuleType = RuleType.Condition }
}
, RuleType = RuleType.Group
}
});