Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Custom Rule Engine in WPF - MVVM

, 11 Sep 2010 CPOL
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 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.

License

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

Share

About the Author

Renil Joy, Bangalore
Software Developer (Senior) Societe Generale
India India
I am a .Net developer, currently working with 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.
Follow on   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 12 Sep 2010
Article Copyright 2010 by Renil Joy, Bangalore
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid