Click here to Skip to main content
15,893,381 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 NHibernate;
using Northwind.DataAccess;
using Northwind.DataAccess.Repository;
using Northwind.Domain;
using Northwind.Domain.Model;
using Northwind.TransferObjects.Model;
using Northwind.TransferObjects.Presentation;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Northwind.Services
{
    /// <summary>
    /// Implements a Service Pattern for customer management
    /// </summary>
    public class CustomerServices : ICustomerServices
    {
        IRepository<Customer, string> customerRepository;
        ISessionFactory sessionFactory;

        public CustomerServices()
        {
            DataAccessHelper.Initialize();
            sessionFactory = DataAccessHelper.SessionFactory;
        }

        public CustomerServices(RepositoryMode repositoryMode) : this(repositoryMode, DataAccessHelper.SessionFactory){}

        public CustomerServices(RepositoryMode repositoryMode, ISessionFactory sessionFactory)
        {
            DataAccessHelper.Initialize();
            switch (repositoryMode)
            {
                case RepositoryMode.Database:
                    customerRepository = new CustomerRepository();
                    break;
                case RepositoryMode.InMemory:
                    customerRepository = new InMemoryCustomerRepository();
                    break;
            }
            this.sessionFactory = sessionFactory;
        }

        public CustomerServices(IRepository<Customer,string>  customerRepository, ISessionFactory sessionFactory)
        {
            DataAccessHelper.Initialize();
            this.customerRepository = customerRepository;
            this.sessionFactory = sessionFactory;
        }

        public CustomerTO GetDetailsForCustomer(string id)
        {
            Customer customer = customerRepository.Load(id);
            return (CustomerTO)TOHelper<Customer>.GetTO(typeof(CustomerTO), customer);
        }

        public void UpdateCustomer(CustomerTO customerTO)
        {
            try
            {
                Customer customer = customerRepository.Load(customerTO.ID);

                Order o = new Order();
                o.CustomerID = customer.ID;
                o.EmployeeID = 3;
                o.ShipVia = 1;
                customer.Orders.Add(o);

                TOHelper<Customer>.UpdateDomainFromTO(customerTO, customer);

                using (ISession session = sessionFactory.OpenSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        customerRepository.AddOrUpdate(session, customer);
                        transaction.Commit();
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        public CustomerTO NewCustomer(string customerId)
        {
            Customer c = new Customer();
            c.ID = customerId;
            //c.Address = "";
            //c.City = "";
            c.CompanyName = "[COMPANY NAME]";
            //c.ContactName = "";
            //c.ContactTitle = "";
            //c.Country = "";
            //c.Fax = "";
            c.Phone = "[PHONE NUMBER]";
            //c.PostalCode = "";
            //c.Region = "";
            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    customerRepository.AddOrUpdate(session, c);
                    transaction.Commit();
                }
            }
            return (CustomerTO)TOHelper<Customer>.GetTO(typeof(CustomerTO), c);
        }

        public void DeleteCustomer(string customerId)
        {
            Customer c = new Customer();
            c.ID = customerId;
            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    customerRepository.Remove(session, c);
                    transaction.Commit();
                }
            }
        }

        public ILookupCollection GetCustomerList()
        {
            Customer c = new Customer();
            Customer[] customers = customerRepository.FetchByExample(c);
            List<object> toList = (List<object>)TOHelper<Customer>.GetTO(typeof(CustomerTO), customers);
            return new LookupCollection(new CustomerToLookupConverter().ConvertAllFrom(toList));
        }

        public ILookupCollection GetCustomerList(string name)
        {
            Customer[] customers = ((CustomerRepository)customerRepository).FetchByName(name);
            List<object> toList = (List<object>)TOHelper<Customer>.GetTO(typeof(CustomerTO), customers);
            return new LookupCollection(new CustomerToLookupConverter().ConvertAllFrom(toList));
        }
    }

    public enum RepositoryMode
    {
        Database,
        InMemory
    }
}

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