I'm trying to work around a rule processor so I created the following classes (the interfaces would be straightforward to guess).
public class OrderProcessor : IOrderProcessor
{
private readonly IRuleEvaluator _ruleEvaluator;
public OrderProcessor(IRuleEvaluator ruleEvaluator)
{
_ruleEvaluator = ruleEvaluator;
}
public void Process(Command command)
{
var rules = _ruleEvaluator.GetRules(command);
foreach (var rule in rules)
{
rule.Apply(command);
}
}
}
public class RuleEvaluator : IRuleEvaluator
{
private readonly IRuleSet _ruleSet;
public RuleEvaluator(IRuleSet ruleSet)
{
_ruleSet = ruleSet;
}
public List<IRule> GetRules(Command command)
{
var ruleSet = _ruleSet.GetRules();
if (!ruleSet.Any())
{
return new List<IRule>();
}
var rules = from pair in _ruleSet.GetRules()
orderby pair.Value descending
select pair.Key;
return rules.Where(r => r.IsApplicable(command)).ToList();
}
}
public class RuleSet : IRuleSet
{
private readonly Dictionary<IRule, int> _rules = new Dictionary<IRule, int>();
public RuleSet(IDependency1 dependency1,
IDependency2 dependency2)
{
_rules.Add(new Rule1(dependency1), 1000);
_rules.Add(new Rule2(dependency2), 1);
}
public Dictionary<IRule, int> GetRules()
{
return _rules;
}
}
I was trying to abstract it in a way that would be easy to add new Rules to the RuleSet, but my issue comes where I see that if the Rules take any dependency I have to add them to RuleSet class. Not sure if this is actually a good practice as I would say it will easily bloat.