Click here to Skip to main content
15,896,329 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 295.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.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Northwind.Business;
using Northwind.Business.DataInterfaces;
using Northwind.Data.DTO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Reflection;

/*
 **************************************************************
 * 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.
 **************************************************************  
*/

/// <summary>
/// Summary description for Util
/// </summary>
public static class Util
{
    class Constants
    {
        public const string DAO_FACTORY_KEY = "daoFactoryType";
        public const string DEFAULT_FACTORY_TYPE = "Northwind.Data.DLinqDAOFactory";
        public const string FACTORY_ASSEMBLY = "Northwind.Data";
        public const string CACHED_PROP_PREFIX = "__$DTOProperty_";        
    }

    public static T MarshalObject<T>(FormView f) where T:new()
    {
        T dto = new T();
        Type type = dto.GetType();
        for (int i = 0; i < f.Row.Cells[0].Controls.Count; i += 2)
        {
            Control label = f.Row.Cells[0].Controls[i];
            if (label is LiteralControl)
            {
                string propName = (label as LiteralControl).Text;
                propName = Regex.Replace(propName, "([\\s:-]*(<br[ ]*/>)*)(\\w+)([\\s:-])+", "$3");
                if (!String.IsNullOrEmpty(propName))
                {
                    PropertyInfo prop = GetProperty(propName, type);
                    if (prop != null)
                    {
                        Control valueCell = f.Row.Cells[0].Controls[i + 1];
                        object value = (valueCell is TextBox)
                            ? (valueCell as TextBox).Text
                            : null;
                        prop.SetValue(dto, value, null);
                    }
                }
            }
        }
        return dto;
    }

    public static IDaoFactory CreateFactoryInstance()
    {
        string typeStr = ConfigurationManager.AppSettings[Constants.DAO_FACTORY_KEY];
        if (String.IsNullOrEmpty(typeStr)) typeStr = Constants.DEFAULT_FACTORY_TYPE;
        IDaoFactory f = null;
        try {
            Assembly a = Assembly.Load(Constants.FACTORY_ASSEMBLY);
            if (a != null) {
                Type fType = a.GetType(typeStr);
                if (fType == null) fType = a.GetType(String.Format("{0}.{1}",
                     Constants.FACTORY_ASSEMBLY, typeStr));
                f = Activator.CreateInstance(fType) as IDaoFactory;
            }
        }
        catch (Exception ex) {
            throw new Exception(String.Format("Unable to load {0} from Northwind.Data",
                      typeStr), ex);
        }
        return f;
    }

    internal static PropertyInfo GetProperty(string propName, Type type)
    {
        System.Web.Caching.Cache c = HttpRuntime.Cache;
        PropertyInfo prop = c[GetKey(propName)] as PropertyInfo;
        if (prop == null) {
            prop = type.GetProperty(propName);
            if(prop != null)    c[GetKey(propName)] = prop;
        }
        return prop;
    }

    internal static string GetKey(string propName)
    {
        return Constants.CACHED_PROP_PREFIX + propName;
    }

}

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