Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / WPF

Layers Pattern in Practice

Rate me:
Please Sign up or sign in to vote.
4.96/5 (59 votes)
23 Apr 2010CPOL25 min read 152.3K   8.1K   187  
Layers Pattern via a WPF project.
using System;
using System.ComponentModel;

namespace BillsEntityLib
{
    /// <summary>
    /// Bill entity object
    /// </summary>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/billschema.xsd")]
    public partial class Bill: INotifyPropertyChanged
    {

        private string nameField;

        private System.DateTime dueDateField;

        private decimal amountField;

        private System.DateTime addedOnField;

        private string statusField;

        private string idField;

        [NonSerialized]
        private BillStatus billStatusField; //not serialized //

        #region Constructors
        /// <summary>
        /// Parameterless constructor
        /// </summary>
        public Bill() { }
        /// <summary>
        /// Bill's constructor
        /// </summary>
        /// <param name="name">Bill's name</param>
        /// <param name="dueDate">Duedate</param>
        /// <param name="amount">Amount</param>
        /// <param name="addedOn">Added On</param>
        /// <param name="status">Status</param>
        /// <param name="id">Bill's Id</param>
        public Bill(string name, DateTime dueDate, decimal amount, DateTime addedOn, string status, string id)
            : this()
        {
            nameField = name;
            dueDateField = dueDate;
            amountField = amount;
            addedOnField = addedOn;
            statusField = status;
            billStatusField = BillHelper.Convert(status);
            idField = id;
        }
        /// <summary>
        /// Bill's constructor
        /// </summary>
        /// <param name="name">Bill's name</param>
        /// <param name="dueDate">Duedate</param>
        /// <param name="amount">Amount</param>
        /// <param name="addedOn">Added On</param>
        /// <param name="status">Status</param>
        /// <param name="id">Bill's Id</param>
        public Bill(string name, DateTime dueDate, decimal amount, DateTime addedOn, BillStatus status, Guid id)
            : this(name, dueDate, amount, addedOn, status.ToString(), id.ToString())
        { }
        /// <summary>
        /// Bill's constructor
        /// </summary>
        /// <param name="name">Bill's name</param>
        /// <param name="dueDate">Duedate</param>
        /// <param name="amount">Amount</param>
        /// <param name="addedOn">Added On</param>
        /// <param name="status">Status</param>
        /// <param name="id">Bill's Id</param>
        public Bill(string name, DateTime dueDate, decimal amount, DateTime addedOn, string status, Guid id)
            : this(name, dueDate, amount, addedOn, status, id.ToString())
        { }

        #endregion

        #region Properties
        /// <summary>
        /// Bill's name
        /// </summary>
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
                this.OnPropertyChanged(new BillPropertyChangeEventArgs(this.idField, "Name"));
            }
        }

        /// <summary>
        /// Due date
        /// </summary>
        public System.DateTime DueDate
        {
            get
            {
                return this.dueDateField;
            }
            set
            {
                this.dueDateField = value;
                this.OnPropertyChanged(new BillPropertyChangeEventArgs(this.idField, "DueDate"));
            }
        }

        /// <summary>
        /// Amount of money to pay
        /// </summary>
        public decimal Amount
        {
            get
            {
                return this.amountField;
            }
            set
            {
                this.amountField = value;
                this.OnPropertyChanged(new BillPropertyChangeEventArgs(this.idField, "Amount"));
            }
        }

        /// <summary>
        /// Added on
        /// </summary>
        public System.DateTime AddedOn
        {
            get
            {
                return this.addedOnField;
            }
            set
            {
                this.addedOnField = value;
                this.OnPropertyChanged(new BillPropertyChangeEventArgs(this.idField, "AddedOn"));
            }
        }

        /// <summary>
        /// Bill's status
        /// </summary>
        public string Status
        {
            get
            {
                return this.statusField;
            }
            set
            {
                this.billStatusField = BillHelper.Convert(value);
                this.statusField = value;
                this.OnPropertyChanged(new BillPropertyChangeEventArgs(this.idField, "Status"));
            }
        }
        /// <summary>
        /// Bill's status
        /// </summary>
        public BillStatus BillStatus
        {
            get 
            {
                return this.billStatusField;
            }
            set 
            {
                this.statusField = value.ToString();
                this.billStatusField = value;
                this.OnPropertyChanged(new BillPropertyChangeEventArgs(this.idField, "Status"));
            }
        }

        /// <summary>
        /// Bill's ID
        /// </summary>
        /// <remarks>Represets a XML attribute in the serialized xml document</remarks>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string ID
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
                this.OnPropertyChanged(new BillPropertyChangeEventArgs(this.idField, "ID"));
            }
        }
        #endregion

        #region Object ovverides
        /// <summary>
        /// Converts bill into string representation
        /// </summary>
        /// <returns>String representation of the bill</returns>
        public override string ToString()
        {
            return "Name=" + nameField + "\n"
                + "DueDate=" + dueDateField.ToString() + "\n"
                + "Amount=" + amountField.ToString() + "\n"
                + "AddedOn=" + addedOnField.ToString() + "\n"
                + "Status=" + statusField.ToString() + "\n"
                + "ID=" + idField + "\n";
        }
        #endregion

        #region INotifyPropertyChanged Members
        /// <summary>
        /// Property Changed Event Handler
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Invoke OnPropertyChanged every time a property of an object is changed
        /// </summary>
        /// <param name="args">BillPropertyEventArgs - event arguments</param>
        protected virtual void OnPropertyChanged(BillPropertyChangeEventArgs args)
        { 
            //save delegate into a temporary field for thread-safety
            PropertyChangedEventHandler temp = this.PropertyChanged;
            if (temp != null)
            {
                temp.Invoke(this, args);    //invoke all subscribers
            }
        }
        #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
Moldova (Republic of) Moldova (Republic of)
Interested in computer science, math, research, and everything that relates to innovation. Fan of agnostic programming, don't mind developing under any platform/framework if it explores interesting topics. In search of a better programming paradigm.

Comments and Discussions