Click here to Skip to main content
15,883,772 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.2K   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 Northwind.DataAccess.Query;
using Northwind.Domain.Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace Northwind.DataAccess.Repository
{
    public class InMemoryCustomerRepository : InMemoryRepositoryBase<Customer, string>, IInMemoryCustomerRepository
    {
        protected static string lastId;
        protected override object GetNextID()
        {
            return null;
        }

        public Customer[] FetchByName(string name)
        {
            FetchCustomerByNameQuery fbn = new FetchCustomerByNameQuery(name);
            Customer[] domainObjects = (Customer[])ActiveRecordBase.ExecuteQuery(fbn);
            return domainObjects;
        }

        public Customer[] FetchByNameRange(string lowerBoundName, string upperBoundName)
        {
            FetchCustomerByNameRangeQuery fbnr = new FetchCustomerByNameRangeQuery(lowerBoundName, upperBoundName);
            Customer[] domainObjects = (Customer[])ActiveRecordBase.ExecuteQuery(fbnr);
            return domainObjects;
        }

        protected override void LoadChildren(Customer customer)
        {
            OrderRepository orderRepository = new OrderRepository();
            Order orderExample = new Order();
            orderExample.CustomerID = customer.ID;
            Order[] orders = orderRepository.FetchByExample(orderExample);
            foreach (Order order in orders)
            {
                order.IsNew = false;
                order.IsDirty = false;
                customer.Orders.Add(order);
            }
        }

        protected override void AddOrUpdateChildren(ISession session, Customer customer)
        {
            InMemoryOrderRepository inMemoryOrderRepository = new InMemoryOrderRepository();
            foreach (Order order in customer.Orders)
            {
                inMemoryOrderRepository.AddOrUpdate(session, order);
            }
        }

        protected override void RemoveChildren(ISession session, Customer customer)
        {
            InMemoryOrderRepository inMemoryOrderRepository = new InMemoryOrderRepository();
            LoadChildren(customer);
            foreach (Order order in customer.Orders)
            {
                inMemoryOrderRepository.Remove(session, order);
            }
        }
    }
}

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