Click here to Skip to main content
15,897,891 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.6K   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 System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Northwind.DataAccess.Query
{
    /// <summary>
    /// This class is a variation of <see>ExampleQuery</see> and passes a name pattern to the hql query.
    /// </summary>
    public class FetchCustomerByNameQuery : ActiveRecordBaseQuery
    {
        string name = "";

        public FetchCustomerByNameQuery(string name)
            : base(typeof(ActiveRecordBase))
        {
            this.name = name;
        }

        protected override IQuery CreateQuery(ISession session)
        {
            string hqlFrom = string.Format("from {0} obj ", typeof(Northwind.Domain.Model.Customer).Name);
            string hqlWhere = "where obj.CompanyName like :name";

            //creating the query
            IQuery q = session.CreateQuery(hqlFrom + " " + hqlWhere);

            q.SetString("name", "%" + name + "%");

            return q;
        }

        protected override object InternalExecute(ISession session)
        {
            IQuery q = CreateQuery(session);
            return SupportingUtils.BuildArray(typeof(Northwind.Domain.Model.Customer), q.List());
        }
    }
}

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