Click here to Skip to main content
15,886,724 members
Articles / Web Development / ASP.NET

Enterprise Application Architecture with LINQ to SQL

Rate me:
Please Sign up or sign in to vote.
4.73/5 (44 votes)
17 Oct 200722 min read 294.1K   4.1K   277  
A discussion about architectural patterns for using LINQ to SQL in enterprise applications, along with performance implications
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using Northwind.Service;
using Northwind.Data.DTO;
using Northwind.Business.DataInterfaces;
using Northwind.Presentation.ViewInterfaces;

/*
 **************************************************************
 * Author: Rohit Gadagkar (rohit.gadagkar@gmail.com)
 * Copyright © 2007 Rohit Gadagkar
 * License:
 * Free without any restrictions whatsoever 
 *
 * Created: 9/1/2007
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 **************************************************************  
*/

namespace Northwind.Presentation
{
    /// <summary>
    /// This presenter is associated with IViewCustomerListing and
    /// implements the functionality for a READ only view of customers.
    /// </summary>
    /// <remarks>
    /// There are two overloads for the constructor. The first overload 
    /// internally creates a default instance of the service. The second overload 
    /// may be used in unit tests by injecting mocked objects as dependencies.
    /// </remarks>
    public class ListCustomersPresenter : BasePresenter<ListCustomersPresenter, Customer>
    {
        protected ICustomerService service;

        public ListCustomersPresenter(IViewCustomerListing view, IDaoFactory daoFactory)
            : base((IView<ListCustomersPresenter,Customer>) view, daoFactory) 
        {
            service = new CustomerService(base.DaoFactory);
        }

        public ListCustomersPresenter(IViewCustomerListing view, 
            IDaoFactory daoFactory, ICustomerService service)
            : base((IView<ListCustomersPresenter, Customer>)view, daoFactory)
        {
            this.service = service;
        }

        //  Override initialize view to display total customer count and pages
        public override void InitializeView()
        {
            base.InitializeView();
            View.TotalCustomers = service.GetCustomersCount();
        }

        public void ShowCurrentPage()
        {
            IEnumerable<Customer> ds = null;
            if (String.IsNullOrEmpty(View.SortPropertyName))
                ds = service.GetCustomersByPage(StartRowIndex, View.PageSize);
            else
            {
                if (String.IsNullOrEmpty(View.PrevSortPropertyName) ||
                    View.SortPropertyName != View.PrevSortPropertyName)
                {
                    View.SortAscending = true;
                    View.PrevSortPropertyName = View.SortPropertyName;
                }
                else if(!View.PageIndexChanged) 
                    View.SortAscending = !View.SortAscending;
                string sortParam = String.Concat(
                    View.SortPropertyName,
                    View.SortAscending ? "" : " DESC");
                ds = service.GetCustomersByPage(StartRowIndex, View.PageSize,
                    sortParam);
            }
            View.DisplayData(ds);
            View.TotalCustomers = service.GetCustomersCount();
        }

        /*
         * Override base implementation in case the presenter needs strongly typed access
         * to the specific view
         */
        protected new IViewCustomerListing View
        {
            get { return (IViewCustomerListing)base.View; }
            set { base.View = (IView<ListCustomersPresenter, Customer>)value; }
        }

        protected int StartRowIndex
        {
            get {
                return View.CurrentPageIndex * View.PageSize;
            }
        }

        protected override IEnumerable<Customer> GetInitialViewData()
        {
            if (View.IsPagingEnabled)            
                return service.GetCustomersByPage(StartRowIndex, View.PageSize);            
            else return service.GetAllCustomers();
        }
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am a tech lead working for Cap Gemini. Although I primarily work with the Microsoft technology stack (including .NET and legacy technologies) I like to keep myself informed about developments in the Java world.

Comments and Discussions