Click here to Skip to main content
12,359,604 members (69,367 online)
Click here to Skip to main content

Stats

42.1K views
1.1K downloads
93 bookmarked
Posted

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 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)

Share

About the Author

Renil Joy, Bangalore
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.

You may also be interested in...

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