Click here to Skip to main content
11,637,662 members (67,796 online)
Click here to Skip to main content
Add your own
alternative version

Custom Rule Engine in WPF - MVVM

, 11 Sep 2010 CPOL 38.4K 999 92
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.Windows.Input;
using CustomRulesMVVM.Models;

namespace CustomRulesMVVM.ViewModels
{
    class CountryViewModel:BaseViewModel
    {
        #region Members
        private const string COUNTRY = "COUNTRY";
        private Country _country = new Country();
        private RelayCommand _searchCountry;
        private string _searchCountryResult;
        Dictionary<string, object> collection = new Dictionary<string, object>();
        #endregion

        #region Properties
        /// <summary>
        /// Country name entered by user
        /// </summary>
        public string MyCountry 
        {
            get { return this._country.CountryName; }
            set
            {
                this._country.CountryName  = value;
                base.OnProperyChanged("MyCountry");                
            }
        }
        /// <summary>
        /// Search result
        /// </summary>
        public string SearchCountryResult
        {
            get { return this._searchCountryResult; }
            set
            {
                this._searchCountryResult = value;
                base.OnProperyChanged("SearchCountryResult");
            }
        }
        /// <summary>
        /// Command for button to search country
        /// </summary>
        public ICommand SearchCountry
        {
            get
            {
                if (null == this._searchCountry)
                {
                    this._searchCountry = new RelayCommand(param => this.SearchCountryDetails());
                }
                return this._searchCountry;
            }
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Searching country by firt populating the custom rules and evaluating rules by passing the Country object
        /// </summary>
        private void SearchCountryDetails()
        { 
            //Clear the result 
            this.SearchCountryResult = string.Empty;
            //Populate custom rules applicable for this screen by passing the ScreenID (here ScreenID =1)
            this.PopulateCustomRules(1);
            collection[COUNTRY] = _country;
            //Evaluate the rules for Country screen
            this.SearchCountryResult = this.EvaluateCustomRules(collection); 
        }               
        #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
Web03 | 2.8.150728.1 | Last Updated 12 Sep 2010
Article Copyright 2010 by Renil Joy, Bangalore
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid