Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#

Who cares about Domain Rules?

Rate me:
Please Sign up or sign in to vote.
4.96/5 (88 votes)
20 Nov 2006MIT12 min read 95.8K   85   118  
With this article, I am evolving a domain problem towards the best possible solution.
using System;

namespace BusinessRules.Evolution_Five
{
    /// <summary>
    /// The Tax Band domain object symbolizes the bands found in countries tax regimes. 
    /// </summary>
    /// <remarks>
    /// <para>The tax band domain object is an immutable object and contains three properties:</para>
    /// <list type="number">
    /// <item><description><c>StartAmount</c> is the lower limit in the band.</description></item>
    /// <item><description><c>EndAmount</c> is the upper limit in the band.</description></item>
    /// <item><description><c>TaxRate</c> is the rate applied to the band.</description></item>
    /// </list>
    /// </remarks>
    public class TaxBand
    {
        #region Contants

        private const double ZeroValue = 0.0;

        #endregion

        #region Fields

        private double lowerLimitAmount;
        private double upperLimitAmount;
        private double taxRate;

        #endregion

        #region C'tors

        /// <summary>
        /// Initializes a new instance of the <see cref="TaxBand"/> class.
        /// </summary>
        /// <param name="lowerLimitAmount">The start amount.</param>
        /// <param name="upperLimitAmount">The end amount.</param>
        /// <param name="taxRate">The tax rate.</param>
        public TaxBand(double lowerLimitAmount, double upperLimitAmount, double taxRate)
        {
            this.lowerLimitAmount = lowerLimitAmount;
            this.upperLimitAmount = upperLimitAmount;
            this.taxRate = taxRate;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Calculates the band tax portion.
        /// </summary>
        /// <param name="income">The investors income.</param>
        /// <returns>
        /// Returns a double representing the portion of the tax liability of this tax band.
        /// </returns>
        public double CalculateTaxPortion(double income)
        {
            if (EqualToOrLessThan(income, LowerLimitAmount))
            {
                return ZeroValue;
            }
            else if (FallWithinTaxBand(income))
            {
                return (income - lowerLimitAmount) * taxRate;
            }
            else if (EqualToOrGreaterThan(income, UpperLimitAmount))
            {
                return (upperLimitAmount - lowerLimitAmount) * taxRate;
            }
            else
            {
                return ZeroValue;
            }
        }


        /// <summary>
        /// Determines whether the income fall within the tax band.
        /// </summary>
        /// <param name="income">The income.</param>
        /// <returns>
        /// 	<c>true</c> if the income fall within the tax band; otherwise, <c>false</c>.
        /// </returns>
        private bool FallWithinTaxBand(double income)
        {
            return EqualToOrGreaterThan(income, LowerLimitAmount) && EqualToOrLessThan(income, UpperLimitAmount);
        }

        /// <summary>
        /// Income is equal to or greater than the band value.
        /// </summary>
        /// <param name="income">The income.</param>
        /// <param name="bandValue">The band value.</param>
        /// <returns>
        /// 	<c>true</c> if the income is equal to or greater than the band value; otherwise, <c>false</c>.
        /// </returns>
        private bool EqualToOrGreaterThan(double income, double bandValue)
        {
            return income >= bandValue;
        }

        /// <summary>
        /// Income is equal to or less than the band value.
        /// </summary>
        /// <param name="income">The income.</param>
        /// <param name="bandValue">The band value.</param>
        /// <returns>
        /// 	<c>true</c> if the income is equal to or less than the band value; otherwise, <c>false</c>.
        /// </returns>
        private bool EqualToOrLessThan(double income, double bandValue)
        {
            return income <= bandValue;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the tax rate.
        /// </summary>
        /// <value>The tax rate.</value>
        public double TaxRate
        {
            get { return taxRate; }
        }

        /// <summary>
        /// Gets the lower limit amount.
        /// </summary>
        /// <value>The lower limit amount.</value>
        public double LowerLimitAmount
        {
            get { return lowerLimitAmount; }
        }

        /// <summary>
        /// Gets the upper limit amount.
        /// </summary>
        /// <value>The upper limit amount.</value>
        public double UpperLimitAmount
        {
            get { return upperLimitAmount; }
        }

        #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 MIT License


Written By
Web Developer
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions