Rules Pattern

  • Behavioral pattern
  • Rules design pattern helps the developer to encapsulate each business rule in a separate object and decouple the definition of business rules from their processing. New rules can be added without the need to modify the rest of the application logic.
				
					namespace RulesPattern
{
    public interface IRule
    {
        bool Matches(CustomerDetailModel input);
        void Apply(CustomerDetailModel input);
    }

    public class RulesEvaluator
    {
        private IEnumerable<IRule> _rules;

        public RulesEvaluator(IEnumerable<IRule> rules)
        {
            _rules =rules;
        }
        public void Execute(CustomerDetailModel customerDetailModel)
        {
            foreach (var rule in _rules)
            {
                if (rule.Matches(customerDetailModel))
                {
                    rule.Apply(customerDetailModel);
                }
            }
        }
    }

    public class CustomerWorthiNessRule : IRule
    {
        public void Apply(CustomerDetailModel input)
        {
            input.IsCustomerWorthiness = true;
        }

        public bool Matches(CustomerDetailModel input)
        {
            //some condition check here ...true or false..
            return true;
        }
    }


    public class CustomerLoyalRule : IRule
    {
        public void Apply(CustomerDetailModel input)
        {
            input.IsCustomerLoyal = true;
        }

        public bool Matches(CustomerDetailModel input)
        {
            //some condition check here ...true or false..
            return true;
        }
    }

    public class CustomerSpecialRule : IRule
    {
        public void Apply(CustomerDetailModel input)
        {
            //some condition check then return true or false..
            input.IsCustomerSpecial = false;
        }

        public bool Matches(CustomerDetailModel input)
        {
            //some condition check here ...true or false..
            return true;
        }
    }

    public class CustomerValid : IRule
    {
        public void Apply(CustomerDetailModel input)
        {
            input.IsCustomerValid = true;
        }

        public bool Matches(CustomerDetailModel input)
        {
            //some condition check here ...true or false..
            return true;
        }
    }
}

				
			
				
					namespace RulesPattern
{
    public class CustomerDetailModel
    {
        public int Id { get; set; }
        public bool IsCustomerWorthiness { get; set; }
        public bool IsCustomerLoyal { get; set; }
        public bool IsCustomerSpecial { get; set; }
        public bool IsCustomerValid { get; set; }
    }
}

				
			
				
					using RulesPattern;

var custDetailModel = new CustomerDetailModel();
var rules = new List<IRule>()
{
    new CustomerWorthiNessRule(),
    new CustomerLoyalRule(),
    new CustomerSpecialRule(),
    new CustomerValid()
};

new RulesEvaluator(rules).Execute(custDetailModel);
Console.WriteLine($"Is Customer Credit Valid ? {custDetailModel.IsCustomerValid}");
Console.WriteLine($"Is Customer Credit Worthy ? {custDetailModel.IsCustomerWorthiness}");
Console.WriteLine($"Is Customer Credit Special ? {custDetailModel.IsCustomerSpecial}");
Console.WriteLine($"Is Customer Credit Loyal ? {custDetailModel.IsCustomerLoyal}");


				
			

Is Customer Credit Valid ? True
Is Customer Credit Worthy ? True
Is Customer Credit Special ? False
Is Customer Credit Loyal ? True

Leave a Comment