Click here to Skip to main content
12,698,179 members (21,598 online)
Click here to Skip to main content


93 bookmarked

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

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

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


        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnProperyChanged(string propertyName)
            if (null != this.PropertyChanged)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

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.


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) 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
Web01 | 2.8.170118.1 | Last Updated 12 Sep 2010
Article Copyright 2010 by Renil Joy, Bangalore
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid