Click here to Skip to main content
15,867,771 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 126.9K   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 NHibernate;
using NHibernate.Expression;
using Northwind.DataAccess;
using Northwind.DataAccess.Query;
using Northwind.Domain;
using Northwind.Domain.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Northwind.DataAccess.Repository
{
    public abstract class InMemoryRepositoryBase<T, t> : IRepository<T, t> where T : DomainBase<t>
    {
        private Dictionary<t, T> inMemoryDictionary = new Dictionary<t, T>();
        protected static t lastId;

        public virtual T Load(t id)
        {
            T domainObject = null;

            if (inMemoryDictionary.TryGetValue(id, out domainObject))
            {
                domainObject.IsNew =
                domainObject.IsDirty = false;
                LoadChildren(domainObject);
                return domainObject;
            }

            throw new NotFoundException(typeof(T).Name + " not found for id " + id);
        }

        public void AddOrUpdate(ISession session, T domainObject)
        {
            if (domainObject.IsNew)
            {
                PropertyInfo[] props = domainObject.GetType().GetProperties();
                PropertyInfo pkInfo = null;
                foreach (PropertyInfo pi in props)
                {
                    if (pi.Name == "ID" && pi.DeclaringType.FullName == typeof(T).FullName)
                    {
                        pkInfo = pi;
                    }
                }
                
                foreach (Attribute att in pkInfo.GetCustomAttributes(true))
                {
                    if (att is PrimaryKeyAttribute)
                    {
                        if (((PrimaryKeyAttribute)att).Generator == PrimaryKeyType.Identity)
                        {
                            lastId = (t)GetNextID();
                            domainObject.ID = lastId;
                            break;
                        }
                    }
                }
                inMemoryDictionary.Add(domainObject.ID, domainObject);
                domainObject.IsNew = false;
                domainObject.IsDirty = false;
            }
            else
            {
                if (domainObject.IsDirty)
                {
                    inMemoryDictionary[domainObject.ID] = domainObject;
                    domainObject.IsDirty = false;
                }
            }
            AddOrUpdateChildren(session, domainObject);
        }

        public void Remove(ISession session, T domainObject)
        {
            RemoveChildren(session, domainObject);
            if (inMemoryDictionary.ContainsKey(domainObject.ID))
            {
                inMemoryDictionary.Remove(domainObject.ID);
            }
        }

        public virtual T[] FetchByExample(T example)
        {
            IList<T> result = new List<T>();
            foreach (object dictionaryItem in inMemoryDictionary.Values)
            {
                T domainObject = (T)dictionaryItem;

                bool match = true;

                //Try to match ID property
                if (example.ID != null)
                {
                    if (!domainObject.ID.Equals(example.ID))
                    {
                        match = false;
                    }
                }
                else //Try to match the other properties
                {
                    foreach (PropertyInfo pi in typeof(T).GetProperties())
                    {
                        bool canCompare = false;

                        foreach (object o in pi.GetCustomAttributes(true))
                        {
                            if (o is PrimaryKeyAttribute || o is PropertyAttribute)
                            {
                                canCompare = true;
                                break;
                            }
                        }

                        if (canCompare)
                        {
                            object exampleValue = pi.GetValue(example, null);
                            object dictionaryValue = pi.GetValue(domainObject, null);

                            if (exampleValue != null)
                            {
                                if (exampleValue != dictionaryValue)
                                {
                                    match = false;
                                    break;
                                }
                            }
                        }
                    }
                }

                if (match)
                {
                    result.Add(domainObject);
                }
            }

            T[] ret = new T[result.Count];
            int index = 0;
            foreach (T item in result)
            {
                ret[index] = item;
                index++;
            }

            return ret;
        }

        protected abstract object GetNextID();

        protected virtual void LoadChildren(T domainObject)
        {
        }

        protected virtual void AddOrUpdateChildren(ISession session, T domainModel)
        {
        }

        protected virtual void RemoveChildren(ISession session, T domainModel)
        {
        }
    }
}

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