Click here to Skip to main content
15,892,927 members
Articles / Database Development / SQL Server

How design patterns can help you in developing unit testing-enabled applications

Rate me:
Please Sign up or sign in to vote.
4.61/5 (34 votes)
15 Nov 2007CPOL15 min read 127.5K   323   177  
This article shows how you can mix together Model-View-Presenter, Domain Model, Services, ActiveRecord, and Repository patterns to create a testable application.
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using Castle.ActiveRecord.Queries;
using NHibernate;
using NHibernate.Expression;
using Northwind.Domain;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Northwind.Domain.Model
{
    /// <summary>
    /// This domain model class is mapped to the Customers table of Northwind database for Castle Project ActiveRecorrd framework.
    /// Besides, it is also mapped to the CustomerTO transfer class.
    /// </summary>
    [ActiveRecord("Customers")]
    [MappedTO("Northwind.TransferObjects.Model.CustomerTO")]
    public class Customer : DomainBase<string>
    {
        string customerID;
        string address;
        string city;
        string companyName;
        string contactName;
        string contactTitle;
        string country;
        string fax;
        string phone;
        string postalCode;
        string region;
        IList orders = new List<object>();

        public Customer()
        {
        }

        public override bool Equals(object obj)
        {
            if (obj is Customer)
            {
                bool ret = false;
                Customer objComp = (Customer)obj;
                ret =
                (objComp.ID == this.ID);
                return ret;
            }
            else
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        [PrimaryKey(Column = "CustomerID", Generator = PrimaryKeyType.Assigned)]
        public override string ID
        {
            get { return customerID; }
            set {
                    if (customerID != value)
                    {
                        IsDirty = true;
                    }
                    customerID = value;
                }
        }

        [Property()]
        public string Address
        {
            get { return address; }
            set
            {
                if (address != value)
                {
                    IsDirty = true;
                }
                address = value;
            }
        }

        [Property()]
        public string City
        {
            get { return city; }
            set
            {
                if (city != value)
                {
                    IsDirty = true;
                }
                city = value;
            }
        }

        [Property()]
        public string CompanyName
        {
            get { return companyName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new EmptyValueException("CompanyName");

                if (companyName != value)
                {
                    IsDirty = true;
                }
                companyName = value;
            }
        }

        [Property()]
        public string ContactName
        {
            get { return contactName; }
            set
            {
                if (contactName != value)
                {
                    IsDirty = true;
                }
                contactName = value;
            }
        }

        [Property()]
        public string ContactTitle
        {
            get { return contactTitle; }
            set
            {
                if (contactTitle != value)
                {
                    IsDirty = true;
                }
                contactTitle = value;
            }
        }

        [Property()]
        public string Country
        {
            get { return country; }
            set {
                if (country != value)
                {
                    IsDirty = true;
                }
                country = value; 
                }
        }

        [Property()]
        public string Fax
        {
            get { return fax; }
            set
            {
                if (fax != value)
                {
                    IsDirty = true;
                }
                fax = value;
            }
        }

        [Property()]
        public string Phone
        {
            get { return phone; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new EmptyValueException("Phone");
                if (phone != value)
                {
                    IsDirty = true;
                }
                phone = value;
            }
        }

        [Property()]
        public string PostalCode
        {
            get { return postalCode; }
            set
            {
                if (postalCode != value)
                {
                    IsDirty = true;
                }
                postalCode = value;
            }
        }

        [Property()]
        public string Region
        {
            get { return region; }
            set
            {
                if (region != value)
                {
                    IsDirty = true;
                }
                region = value;
            }
        }

        public IList Orders
        {
            get { return orders; }
            set {
                if (orders != value)
                {
                    IsDirty = true;
                }
                orders = value; 
            }
        }
    }
}

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
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions