Click here to Skip to main content
11,427,908 members (60,130 online)
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.Linq;

namespace CustomRulesMVVM.Models
{
    static class Converter
    {
        /// <summary>
        /// Extension method to populate Custom Rule
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static CustomRule BuildCustomRule(this SelectRules_Result entity)
        {
            CustomRule custRule = new CustomRule();
            if (null != entity)
            {
                custRule.SelectedItem = entity.ResultText;
                custRule.CodeLHS = entity.CodeLHS;
                custRule.Operator = entity.Operator;
                custRule.ValueRHS = entity.ValueRHS;
                custRule.Source = entity.Source;
                custRule.RuleJoinOperator = entity.RuleJoinOperator;
                custRule.RuleSeqNum = entity.RuleSeqNum;                
                custRule.RuleDescription = entity.RuleDescription;
            }

            return custRule;
        }

        /// <summary>
        /// Populate collection of Custom Rules 
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static CustomRules BuildCustomRules(IList<SelectRules_Result> entities)
        {
            CustomRules custRules = new CustomRules();
            if (null != entities)
            {
                foreach (SelectRules_Result item in entities)
                {
                    custRules.Add(item.BuildCustomRule());
                    custRules.SelectedItem = item.ResultText;
                    custRules.RuleGroupJoinOperator = item.RuleGroupJoinOperator;
                    custRules.RuleGroupSeqNum = item.RuleGroupSeqNum;
                }
            }
            return custRules;
        }

        /// <summary>
        /// Populate collection of Custom rule groups
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IRule BuildCustomRulesGroups(IList<SelectRules_Result> entities,string resultText)
        {
            CustomRulesGroups customRulesGroups = new CustomRulesGroups();           

            if (null != entities)
            {
                //Get distinct group ids from collection
                var list = (from r in entities                           
                            select r.RuleGroupID).Distinct();
                foreach (int groupID in list.ToList ())
                { 
                    var listRules = from rule in entities
                                where rule.RuleGroupID == groupID
                                select rule;
                    customRulesGroups.Add (Converter.BuildCustomRules(listRules.ToList()));
                    customRulesGroups.SelectedItem = resultText;
                }            
            }
            return customRulesGroups;
        }
    }
}

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.
Follow on   LinkedIn

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