Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Custom Rule Engine in WPF - MVVM

, 11 Sep 2010
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 CityStateViewModel:BaseViewModel 
    {
        #region Members
        private const string CITY = "CITY";
        private const string STATE = "STATE";
        private City _city = new City();
        private State _state = new State();
        private RelayCommand _searchCityState;
        private string _searchCityStateResult;
        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 CityTemperature
        {
            get { return this._city.Temperature; }
            set
            {
                this._city.Temperature = value;
                base.OnProperyChanged("CityTemperature");
            }
        }
        /// <summary>
        /// Cityname entered by user
        /// </summary>
        public string StateName
        {
            get { return this._state.StateName; }
            set
            {
                this._state.StateName = value;
                base.OnProperyChanged("StateName");
            }
        }
        /// <summary>
        /// Temperatue entered by user
        /// </summary>
        public string StateTemperature
        {
            get { return this._state.Temperature; }
            set
            {
                this._state.Temperature = value;
                base.OnProperyChanged("StateTemperature");
            }
        }
        /// <summary>
        /// Result of city & state search
        /// </summary>
        public string SearchCityStateResult
        {
            get { return this._searchCityStateResult; }
            set
            {
                this._searchCityStateResult = value;
                base.OnProperyChanged("SearchCityStateResult");
            }
        }
        /// <summary>
        /// Command for searching city name and temperature OR state name and temperature
        /// </summary>
        public ICommand SearchCityState
        {
            get
            {
                if (null == this._searchCityState)
                {
                    this._searchCityState = new RelayCommand(param => this.SearchCityStateDetails());
                }
                return this._searchCityState;
            }
        }
        #endregion

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

You may also be interested in...

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.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