Click here to Skip to main content
15,893,722 members
Articles / Desktop Programming / WPF

Custom Rule Engine in WPF - MVVM

Rate me:
Please Sign up or sign in to vote.
4.98/5 (18 votes)
11 Sep 2010CPOL8 min read 75.3K   1.3K   95  
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 CityViewModel:BaseViewModel 
    {
        #region Members
        private const string CITY = "CITY";
        private City _city = new City();
        private RelayCommand _searchCity;
        private string _searchCityResult;
        Dictionary<string, object> collection = new Dictionary<string, object>();
        #endregion

        #region Properties
        /// <summary>
        /// Cityname entered by user
        /// </summary>
        public string CityName
        {
            get { return this._city.CityName; }
            set
            {
                this._city.CityName = value;
                base.OnProperyChanged("CityName");
            }
        }
        /// <summary>
        /// Temperatue entered by user
        /// </summary>
        public string Temperature
        {
            get { return this._city.Temperature; }
            set
            {
                this._city.Temperature = value;
                base.OnProperyChanged("Temperature");
            }
        }
        /// <summary>
        /// Result of city search
        /// </summary>
        public string SearchCityResult
        {
            get { return this._searchCityResult; }
            set
            {
                this._searchCityResult = value;
                base.OnProperyChanged("SearchCityResult");
            }
        }
        /// <summary>
        /// Command for searching city name and temperature
        /// </summary>
        public ICommand SearchCity
        {
            get
            {
                if (null == this._searchCity)
                {
                    this._searchCity = new RelayCommand(param => this.SearchCityDetails());
                }
                return this._searchCity;
            }
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Searching city details by populating custom rules for this screen and evaluating by passing City object
        /// </summary>
        private void SearchCityDetails()
        {
            //Clear the result 
            this.SearchCityResult = string.Empty;
            //Populate custom rules applicable for this screen by passing the ScreenID (here ScreenID = 2)
            this.PopulateCustomRules(2);
            this.collection[CITY] = _city;
            //Evaluate the rules for City screen            
            this.SearchCityResult = 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)


Written By
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.

Comments and Discussions