Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 15 Nov 2007
This article shows how you can mix together Model-View-Presenter, Domain Model, Services, ActiveRecord, and Repository patterns to create a testable application.
Northwind.zip
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
Settings.settings
Northwind.UI.Controls
Northwind.UI.Controls.csproj.user
Properties
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)

About the Author

Marcelo Ricardo de Oliveira
Software Developer
Brazil Brazil
Marcelo Ricardo de Oliveira is a senior software developer who lives with his lovely wife Luciana and his little buddy and stepson Kauê in Guarulhos, Brazil, is co-founder of the Brazilian TV Guide TV Map and currently works for ILang Educação.
 
He is often working with serious, enterprise projects, although in spare time he's trying to write fun Code Project articles involving WPF, Silverlight, XNA, HTML5 canvas, Windows Phone app development, game development and music.
 
Published Windows Phone apps:
 
 
Awards:
 
CodeProject MVP 2012
CodeProject MVP 2011
 
Best Web Dev article of March 2013
Best Web Dev article of August 2012
Best Web Dev article of May 2012
Best Mobile article of January 2012
Best Mobile article of December 2011
Best Mobile article of October 2011
Best Web Dev article of September 2011
Best Web Dev article of August 2011
HTML5 / CSS3 Competition - Second Prize
Best ASP.NET article of June 2011
Best ASP.NET article of May 2011
Best ASP.NET article of April 2011
Best C# article of November 2010
Best overall article of November 2010
Best C# article of October 2010
Best C# article of September 2010
Best overall article of September 2010
Best overall article of February 2010
Best C# article of November 2009

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 15 Nov 2007
Article Copyright 2007 by Marcelo Ricardo de Oliveira
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid