Click here to Skip to main content
15,886,806 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.3K   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 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)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions