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

namespace CustomRulesMVVM.Models
{
    class CustomRule:IRule
    {
        #region Members
        private const string COUNTRY = "COUNTRY";
        private const string CITY = "CITY";
        private const string STATE = "STATE"; 
        Country country;
        City city;
        State state;
        #endregion

        #region Properties
        public bool IsSelected { get; set; }
        public string SelectedItem { get; set; }        
        public string CodeLHS { get; set; }
        public string  Operator { get; set; }
        public string  ValueRHS { get; set; }
        public int Source { get; set; }
        public string RuleJoinOperator { get; set; }
        public int? RuleSeqNum { get; set; }       
        public string RuleDescription { get; set; }
        #endregion

        #region Public methods
        /// <summary>
        /// This function evluates each custom rule by calling method in RuleHelper
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public bool Eval(Dictionary<string, object> collection)
        {
            if (collection.ContainsKey(COUNTRY))
                country = (Country)collection[COUNTRY];
            if (collection.ContainsKey(CITY))
                city = (City)collection[CITY];
            if (collection.ContainsKey(STATE))
                state = (State)collection[STATE];            

            switch (this.Source)
            {
                case 1 :
                    this.IsSelected = RuleHelper.EvaluatePropertyValue(this,country);
                    break;
                case 2:
                    this.IsSelected = RuleHelper.EvaluatePropertyValue(this, city);
                    break;
                case 3:
                    this.IsSelected = RuleHelper.EvaluatePropertyValue(this, state);
                    break;
                default:
                    this.IsSelected = false;
                    break;
            }
            return this.IsSelected;
        }
        #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)

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 5 years of .Net experience in BFSI domain. I am an experienced developer in C#, VB.Net, Silverlight, WPF, WCF, LINQ, Entity Framework, NHibernate, ASP.Net and SQL Server.

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