Click here to Skip to main content
12,451,089 members (53,448 online)
Click here to Skip to main content

Stats

95.5K views
288 downloads
176 bookmarked
Posted

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

, 15 Nov 2007 CPOL
This article shows how you can mix together Model-View-Presenter, Domain Model, Services, ActiveRecord, and Repository patterns to create a testable application.
nunit.framework.dll
Northwind.DataAccess
Northwind.DataAccess.csproj.user
Properties
Query
Repository
Northwind.DataAccess.Test
Northwind.DataAccess.Test.csproj.user
Properties
Northwind.Domain
Dao
Model
Northwind.Domain.csproj.user
Presentation
Properties
Repository
TransferObjects
Northwind.Domain.Test
Northwind.Domain.Test.csproj.user
Properties
Northwind.Presentation
Northwind.Presentation.csproj.user
Properties
Northwind.Presentation.Test
Northwind.Presentation.Test.csproj.user
Properties
Northwind.Services
Northwind.Services.csproj.user
Properties
Northwind.Services.Test
Northwind.Services.Test.csproj.user
Properties
Northwind.TransferObjects
Model
Northwind.TransferObjects.csproj.user
Presentation
Properties
Northwind.TransferObjects.Test
Northwind.TransferObjects.Test.csproj.user
Properties
Northwind.UI
Northwind.UI.csproj.user
NorthWind.UI.suo
Properties
Northwind.UI.Controls
Northwind.UI.Controls.csproj.user
Properties
Castle.ActiveRecord.dll
Castle.Core.dll
Iesi.Collections.dll
log4net.dll
NHibernate.dll
NMock2.dll
using Northwind.Services;
using Northwind.TransferObjects.Model;
using Northwind.TransferObjects.Presentation;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Northwind.Presentation
{
    /// <summary>
    /// Implements a presenter in a Model-View-Presenter Pattern
    /// </summary>
    public class ViewCustomerPresenter
    {
        private readonly IViewCustomerView view;
        private ICustomerServices services;

        public ViewCustomerPresenter()
        {
        }

        public ViewCustomerPresenter(IViewCustomerView view) : this(view, new CustomerServices()) {}

        /// <summary>
        /// Constructs a Presenter with injection of dependencies for layers view and services
        /// </summary>
        /// <param name="view"></param>
        /// <param name="services"></param>
        public ViewCustomerPresenter(IViewCustomerView view, ICustomerServices services)
        {
            this.view = view;
            this.services = services;
        }

        private void DisplayOrders()
        {
            string customerId = SelectedCustomerId;

            CustomerTO customerTO = services.GetDetailsForCustomer(customerId);
            UpdateViewFrom(customerTO);
        }

        public void Initialize()
        {
            ChangeViewMode(ViewMode.ChooseRepository);
        }

        /// <summary>
        /// Initialize presenter by presenting customer list and setting view to master list mode
        /// </summary>
        public void ShowDatabase()
        {
            services = new CustomerServices(RepositoryMode.Database);
            services.GetCustomerList().BindTo(view.CustomerList);
            ChangeViewMode(ViewMode.MasterList);
        }

        public void ShowInMemory()
        {
            services = new CustomerServices(RepositoryMode.InMemory);
            services.GetCustomerList().BindTo(view.CustomerList);
            ChangeViewMode(ViewMode.MasterList);
        }

        private void DisplayCustomerDetails(string customerId)
        {
            if (customerId.Length > 0)
            {
                CustomerTO customerTO = services.GetDetailsForCustomer(customerId);
                UpdateViewFrom(customerTO);
            }
        }

        public void NewCustomer()
        {
            string customerId = view.RequestInputBox(CustomerMessages.INFORM_NEW_CUSTOMER_ID, CustomerMessages.CUSTOMER);
            if (customerId.Length > 0)
            {
                CustomerTO customerTO = services.NewCustomer(customerId);
                UpdateViewFrom(customerTO);
                ChangeViewMode(ViewMode.New);
            }
        }

        public void EditCustomer()
        {
            string customerID = view.Id;
            DisplayCustomerDetails(customerID);
            ChangeViewMode(ViewMode.Edit);
        }

        public void DeleteCustomer()
        {
            services.DeleteCustomer(view.Id);
            services.GetCustomerList().BindTo(view.CustomerList);
            ChangeViewMode(ViewMode.MasterList);
        }

        public void SaveCustomer()
        {
            CustomerTO customerTO = GetTOFromView();
            try
            {
                services.UpdateCustomer(customerTO);
                services.GetCustomerList().BindTo(view.CustomerList);
                ChangeViewMode(ViewMode.MasterList);
            }
            catch (Exception exc)
            {
                if (exc.InnerException == null)
                {
                    view.DisplayError(exc.Message);
                }
                else
                {
                    view.DisplayError(exc.InnerException.Message);
                }
            }
        }

        public void CancelCustomer()
        {
            ChangeViewMode(ViewMode.MasterList);
        }

        public void SearchCustomer()
        {
            services.GetCustomerList(view.NameToSearch).BindTo(view.CustomerList);
            ChangeViewMode(ViewMode.MasterList);
        }

        public void SelectedCustomerChanged()
        {
            ILookupTO selectedItem = view.CustomerList.SelectedItem;
            if (selectedItem != null)
            {
                view.Id = selectedItem.Value;
                view.EditActionButton.Enabled = true;
                view.DeleteActionButton.Enabled = true;
            }
        }

        private string SelectedCustomerId
        {
            get
            {
                string id = "";
                ILookupTO selectedItem = view.CustomerList.SelectedItem;
                
                if (selectedItem != null)
                {
                    string selectedId = selectedItem.Value;

                    if (String.IsNullOrEmpty(selectedId)) return null;

                    id = selectedId;
                }

                return id;
            }
        }

        private void UpdateViewFrom(CustomerTO customerTO)
        {
            view.Id = customerTO.ID;
            view.Company = customerTO.CompanyName;
            view.ContactName = customerTO.ContactName;
            view.ContactTitle = customerTO.ContactTitle;
            view.Address = customerTO.Address;
            view.City = customerTO.City;
            view.Region = customerTO.Region;
            view.Country = customerTO.Country;
            view.Phone = customerTO.Phone;
            view.Fax = customerTO.Fax;
            view.PostalCode = customerTO.PostalCode;

            ILookupCollection orderList = new LookupCollection(new OrderToLookupConverter().ConvertAllFrom(customerTO.Orders));
            orderList.BindTo(view.OrderList);
        }

        private CustomerTO GetTOFromView()
        {
            CustomerTO customerTO = new CustomerTO(
                                                    view.Id,
                                                    view.Company,
                                                    view.ContactName,
                                                    view.ContactTitle,
                                                    view.Address,
                                                    view.City,
                                                    view.Region,
                                                    view.PostalCode,
                                                    view.Country,
                                                    view.Phone,
                                                    view.Fax,
                                                    null
                                                   );
            return customerTO;
        }

        private void ChangeViewMode(ViewMode viewMode)
        {
            switch (viewMode)
            {
                case ViewMode.ChooseRepository:
                    view.RepositoryPanel.Visible = true;
                    view.MasterPanel.Visible = false;
                    view.DetailsPanel.Visible = false;

                    view.NewActionButton.Enabled = false;
                    view.EditActionButton.Enabled = false;
                    view.DeleteActionButton.Enabled = false;
                    view.SaveActionButton.Enabled = false;
                    view.CancelActionButton.Enabled = false;

                    break;
                case ViewMode.MasterList:
                    view.MasterPanel.Visible = true;
                    view.RepositoryPanel.Visible = false;
                    view.NewActionButton.Enabled = true;
                    if (view.CustomerList.SelectedItem == null)
                    {
                        view.EditActionButton.Enabled = false;
                        view.DeleteActionButton.Enabled = false;
                    }
                    else
                    {
                        view.EditActionButton.Enabled = true;
                        view.DeleteActionButton.Enabled = true;
                    }

                    view.DetailsPanel.Visible = false;
                    view.SaveActionButton.Enabled = false;
                    view.CancelActionButton.Enabled = false;
                    break;
                case ViewMode.New:
                case ViewMode.Edit: 
                    view.MasterPanel.Visible = false;
                    view.NewActionButton.Enabled = false;
                    view.EditActionButton.Enabled = false;
                    view.DeleteActionButton.Enabled = false;

                    view.DetailsPanel.Visible = true;
                    view.SaveActionButton.Enabled = true;
                    view.CancelActionButton.Enabled = true;
                    break;
            }
            view.Mode = viewMode;
         }

        public ICustomerServices Services
        {
            get { return this.services; }
        }

    }

    public enum ViewMode
    {
        ChooseRepository,
        MasterList,
        New,
        Edit
    }

    public class CustomerMessages
    {
        public static string INFORM_NEW_CUSTOMER_ID = "Inform new customer id";
        public static string CUSTOMER = "Inform new customer id";
    }
}

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)

Share

About the Author


You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 15 Nov 2007
Article Copyright 2007 by Marcelo Ricardo de Oliveira
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid