Click here to Skip to main content
15,886,659 members
Articles / Desktop Programming / WPF

Custom Rule Engine in WPF - MVVM

Rate me:
Please Sign up or sign in to vote.
4.98/5 (18 votes)
11 Sep 2010CPOL8 min read 75K   1.3K   95  
Custom Rule Engine approach which can evaluate complex combinations of business rules. A sample application is provided in WPF following the MVVM pattern.
using System.Collections.Generic;
using System.Collections;

namespace CustomRulesMVVM.Models
{
    class CustomRulesGroups: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; }
        #endregion

        #region Public methods
        /// <summary>
        /// Evaluate rules groups in collection of rule groups
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public bool Eval(Dictionary<string, object> collection)
        {
            return EvaluateCustomRulesGroups(collection);
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Evaluate two or more rules groups 
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        private bool EvaluateCustomRulesGroups(Dictionary<string, object> collection)
        {
            //In collection of RuleGroups evaluate between first two rules groups by calling Eval method of each rule group.
            if (null != this.Item(0).RuleGroupJoinOperator)
            {
                switch (this.Item(0).RuleGroupJoinOperator)
                {
                    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 till last rule group if more than two rules groups present 
                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).RuleGroupJoinOperator)
                        {
                            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(CustomRules item)
        {
            this.List.Add(item);
        }

        public void Remove(CustomRules item)
        {
            this.List.Remove(item);
        }
        public CustomRules Item(int index)
        {
            return this.List[index] as CustomRules;
        }
        #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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) CA-CIB
Singapore Singapore
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.

Comments and Discussions