Click here to Skip to main content
15,896,378 members
Articles / Programming Languages / C# 4.0

Introducing Code Contracts

Rate me:
Please Sign up or sign in to vote.
4.95/5 (83 votes)
25 Aug 2010CPOL3 min read 239.2K   713   186  
Using Code Contracts to make elegant code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.ComponentModel;

namespace CodeContracts
{
    public class Customer : IPerson, INotifyPropertyChanged, INotifyPropertyChanging
    {

        #region Members
        private int _id;
        private string _name;
        private DateTime _dob;
        private int _accountId;
        private DateTime _lastUpdated;
        private event PropertyChangedEventHandler _propertyChanged;
        private event PropertyChangingEventHandler _propertyChanging;
        #endregion

        /// <summary>
        /// Get/set the account id. Note that the account contract is explicitly set.
        /// </summary>
        public int AccountId
        {
            get { return _accountId; }
            set
            {
                Contract.Requires(value > 0);
                OnChanging("AccountId");
                _accountId = value;
                OnChanged("AccountId");
            }
        }

        public DateTime LastUpdated
        {
            get
            {
                return _lastUpdated;
            }
        }

        #region IPerson Members

        /// <summary>
        /// Get/set the id. Note that the account contract is inherited from <see cref="PersonContract"/>.
        /// </summary>
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                if (_id == value) return;
                OnChanging("Id");
                _id = value;
                OnChanged("Id");
            }
        }

        /// <summary>
        /// Get/set the name. Note that the account contract is inherited from <see cref="PersonContract"/>.
        /// </summary>
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (_name == value) return;
                OnChanging("Name");
                _name = value;
                OnChanged("Name");
            }
        }

        /// <summary>
        /// Get/set the date of birth. Note that the account contract is inherited from <see cref="PersonContract"/>.
        /// </summary>
        public DateTime DOB
        {
            get
            {
                return _dob;
            }
            set
            {
                if (_dob == value) return;
                OnChanging("DOB");
                _dob = value;
                OnChanged("DOB");
            }
        }

        #endregion

        public event PropertyChangedEventHandler PropertyChanged
        {
            add { _propertyChanged += value; }
            remove { _propertyChanged -= value; }
        }

        private void OnChanged(string name)
        {
            _lastUpdated = DateTime.Now;
            PropertyChangedEventHandler handler = _propertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }

        public event PropertyChangingEventHandler PropertyChanging
        {
            add { _propertyChanging += value; }
            remove { _propertyChanging -= value; }
        }

        private void OnChanging(string name)
        {
            PropertyChangingEventHandler handler = _propertyChanging;
            if (handler != null)
                handler(this, new PropertyChangingEventArgs(name));
        }
    }
}

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
CEO
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. I live in the North East of England with 2 wonderful daughters and a wonderful wife.

I am not the Stig, but I do wish I had Lotus Tuned Suspension.

Comments and Discussions