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;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using CustomRulesMVVM.Models;

namespace CustomRulesMVVM.ViewModels
{
    class BaseViewModel : INotifyPropertyChanged
    {
        #region Properties
        public IList<IRule> CustomRules { get; set; }
        #endregion

        #region Public Methods
        /// <summary>
        /// Populating all custom rules for a given screen by calling the stored procedure SelectRules.
        /// </summary>
        /// <param name="screenID"></param>
        protected void PopulateCustomRules(Int16 screenID)
        {
            IList<IRule> customRules = new List<IRule>();
            IList<SelectRules_Result> rulesCollection = new List<SelectRules_Result>();

            //Populate custom rules by calling stored procedure for a screenID
            using (CustomRuleEntities context = new CustomRuleEntities())
            {
                var rules = context.SelectRules(screenID);

                foreach (var item in rules)
                {
                    rulesCollection.Add(item);
                }
            }

            //Get distinct Result set from collection(a collection having same result text will evaluate to a single result)
            var listResult = (from res in rulesCollection
                              select res.ResultText).Distinct();
            //For each Result Text populate custom rules groups collection
            foreach (string resText in listResult.ToList())
            {
                var listRules = from rule in rulesCollection
                                where rule.ResultText == resText
                                select rule;
                customRules.Add(Converter.BuildCustomRulesGroups(listRules.ToList(), resText));
            }
            this.CustomRules = customRules;
        }
        /// <summary>
        /// Evaluating Custom Rules for each customrules group
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        protected string EvaluateCustomRules(Dictionary<string, object> collection)
        {
            string result = string.Empty;

            foreach (IRule rule in this.CustomRules)
            {
                CustomRulesGroups ruleGroups = rule as CustomRulesGroups;
                if (rule.Eval(collection))
                    result = ruleGroups.SelectedItem;
            }
            return result;
        }

        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnProperyChanged(string propertyName)
        {
            if (null != this.PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #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 | Mobile
Web02 | 2.8.141015.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