架构B/S四 DALFactory 数据访问层 Data Access Layer

本文介绍了一种通过抽象工厂模式结合反射机制及缓存机制来动态创建不同数据层对象接口的方法。该方法利用配置文件指定命名空间路径,并通过缓存减少对象创建开销,适用于需要频繁创建数据访问对象的场景。

2008-04-10 09:45

//*******FactoryManage.cs

using System;
using System.Reflection;
using System.Configuration;
using CoalTraffic.IDAL;

namespace CoalTraffic.DALFactory
{
    /// <summary>
    /// 抽象工厂模式创建DAL。
    /// web.config 需要加入配置:(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)
    /// DataCache类在导出代码的文件夹里
    /// 可以把所有DAL类的创建放在这个DataAccess类里
    /// <appSettings>
    /// <add key="DAL" value="LiTianPing.SQLServerDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings>
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string path = System.Configuration.ConfigurationManager.AppSettings["DAL"];

        #region CreateObject

        //不使用缓存
        private static object CreateObjectNoCache(string path, string CacheKey)
        {
            try
            {
                object objType = Assembly.Load(path).CreateInstance(CacheKey);
                return objType;
            }
            catch (Exception ex)
            {
                string strErrorMessage = ex.Message.ToString();// 记录错误日志
                return null;
            }

        }
        //使用缓存
        public static object CreateObject(string path, string CacheKey)
        {
            object objType = DataCache.GetCache(CacheKey);//从缓存读取
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(path).CreateInstance(CacheKey);//反射创建
                    DataCache.SetCache(CacheKey, objType);// 写入缓存
                }
                catch (Exception ex)
                {
                    string strErrorMessage = ex.Message.ToString();// 记录错误日志
                    return null;
                }
            }
            return objType;
        }

        #endregion

        /// <summary>
        /// 创建T_User数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.IT_User CreateT_User()
        {
            string CacheKey = path + ".T_User";
            object objType = CreateObject(path, CacheKey);
            return (IT_User)objType;
        }
        /// <summary>
        /// 创建T_UserGroup数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.IT_UserGroup CreateT_UserGroup()
        {
            string CacheKey = path + ".T_UserGroup";
            object objType = CreateObject(path, CacheKey);
            return (IT_UserGroup)objType;
        }
        /// <summary>
        /// 创建T_UserInGroup数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.IT_UserInGroup CreateT_UserInGroup()
        {
            string CacheKey = path + ".T_UserInGroup";
            object objType = CreateObject(path, CacheKey);
            return (IT_UserInGroup)objType;
        }
        /// <summary>
        /// 创建T_GroupRight数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.IT_GroupRight CreateT_GroupRight()
        {
            string CacheKey = path + ".T_GroupRight";
            object objType = CreateObject(path, CacheKey);
            return (IT_GroupRight)objType;
        }
        /// <summary>
        /// 创建TDY_Country数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_Country CreateTDY_Country()
        {
            string CacheKey = path + ".TDY_Country";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_Country)objType;
        }
        /// <summary>
        /// 创建TDY_Parcel数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_Parcel CreateTDY_Parcel()
        {
            string CacheKey = path + ".TDY_Parcel";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_Parcel)objType;
        }
        /// <summary>
        /// 创建TDY_CollKind数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_CollKind CreateTDY_CollKind()
        {
            string CacheKey = path + ".TDY_CollKind";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_CollKind)objType;
        }
        /// <summary>
        /// 创建TDY_Village数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_Village CreateTDY_Village()
        {
            string CacheKey = path + ".TDY_Village";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_Village)objType;
        }
        /// <summary>
        /// 创建TDY_GFAssignUnit数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_GFAssignUnit CreateTDY_GFAssignUnit()
        {
            string CacheKey = path + ".TDY_GFAssignUnit";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_GFAssignUnit)objType;
        }
        /// <summary>
        /// 创建TDY_CollKicket数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_CollKicket CreateTDY_CollKicket()
        {
            string CacheKey = path + ".TDY_CollKicket";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_CollKicket)objType;
        }
        //<summary>
        //创建TDY_Colliery数据层接口
        //</summary>
        public static CoalTraffic.IDAL.ITDY_Colliery CreateTDY_Colliery()
        {
            string CacheKey = path + ".TDY_Colliery";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_Colliery)objType;
        }
        /// <summary>
        /// 创建TDY_CoalManageStation数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_CoalManageStation CreateTDY_CoalManageStation()
        {
            string CacheKey = path + ".TDY_CoalManageStation";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_CoalManageStation)objType;
        }
        /// <summary>
        /// 创建TDY_GFItems数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_GFItems CreateTDY_GFItems()
        {
            string CacheKey = path + ".TDY_GFItems";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_GFItems)objType;
        }
        /// <summary>
        /// 创建TDY_SendCard数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_SendCard CreateTDY_SendCard()
        {
            string CacheKey = path + ".TDY_SendCard";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_SendCard)objType;
        }
        /// <summary>
        /// 创建TDY_GFCurTotal数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_GFCurTotal CreateTDY_GFCurTotal()
        {
            string CacheKey = path + ".TDY_GFCurTotal";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_GFCurTotal)objType;
        }

        /// <summary>
        /// 创建TDY_GFDetail数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_GFDetail CreateTDY_GFDetail()
        {
            string CacheKey = path + ".TDY_GFDetail";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_GFDetail)objType;
        }

        /// <summary>
        /// 创建TDY_GFTotalHistory数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_GFTotalHistory CreateTDY_GFTotalHistory()
        {
            string CacheKey = path + ".TDY_GFTotalHistory";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_GFTotalHistory)objType;
        }
        /// <summary>
        /// 创建TDY_EmptyAgainBang数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_EmptyAgainBang CreateTDY_EmptyAgainBang()
        {
            string CacheKey = path + ".TDY_EmptyAgainBang";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_EmptyAgainBang)objType;
        }
        /// <summary>
        /// 创建TDY_EmptyBang数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_EmptyBang CreateTDY_EmptyBang()
        {
            string CacheKey = path + ".TDY_EmptyBang";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_EmptyBang)objType;
        }
        /// <summary>
        /// 创建TDY_IdentifierClip数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_IdentifierClip CreateTDY_IdentifierClip()
        {
            string CacheKey = path + ".TDY_IdentifierClip";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_IdentifierClip)objType;
        }
        /// <summary>
        /// 创建TDY_MarkedCardActive数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_MarkedCardActive CreateTDY_MarkedCardActive()
        {
            string CacheKey = path + ".TDY_MarkedCardActive";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_MarkedCardActive)objType;
        }
        /// <summary>
        /// 创建TDY_CoalAccountManage数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_CoalAccountManage CreateTDY_CoalAccountManage()
        {
            string CacheKey = path + ".TDY_CoalAccountManage";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_CoalAccountManage)objType;
        }
        /// <summary>
        /// 创建TDY_Navicert数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_Navicert CreateTDY_Navicert()
        {
            string CacheKey = path + ".TDY_Navicert";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_Navicert)objType;
        }
        /// <summary>
        /// 创建TDY_Room数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_Room CreateTDY_Room()
        {
            string CacheKey = path + ".TDY_Room";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_Room)objType;
        }
        /// <summary>
        /// 创建TDY_LogBalanceRestrictRejigger数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_LogBalanceRestrictRejigger CreateTDY_LogBalanceRestrictRejigger()
        {
            string CacheKey = path + ".TDY_LogBalanceRestrictRejigger";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_LogBalanceRestrictRejigger)objType;
        }
        /// <summary>
        /// 创建TDY_LogEditDate数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_LogEditDate CreateTDY_LogEditDate()
        {
            string CacheKey = path + ".TDY_LogEditDate";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_LogEditDate)objType;
        }
        /// <summary>
        /// 创建TDY_LogCollTransfer数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_LogCollTransfer CreateTDY_LogCollTransfer()
        {
            string CacheKey = path + ".TDY_LogCollTransfer";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_LogCollTransfer)objType;
        }
        /// <summary>
        /// 创建T_Department数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.IT_Department CreateT_Department()
        {
            string CacheKey = path + ".T_Department";
            object objType = CreateObject(path, CacheKey);
            return (IT_Department)objType;
        }

        /// <summary>
        /// 创建T_Department数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_Position CreateTDY_Position()
        {
            string CacheKey = path + ".TDY_Position";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_Position)objType;
        }

        /// <summary>
        /// 创建TDY_ShadinessInfo数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_ShadinessInfo CreateTDY_ShadinessInfo()
        {
            string CacheKey = path + ".TDY_ShadinessInfo";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_ShadinessInfo)objType;
        }

        /// <summary>
        /// 创建T_Navigation数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.IT_Navigation CreateT_Navigation()
        {
            string CacheKey = path + ".T_Navigation";
            object objType = CreateObject(path, CacheKey);
            return (IT_Navigation)objType;
        }

        /// <summary>
        /// 创建T_Navigation数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_ElecCoalMonthSet CreateDY_ElecCoalMonthSet()
        {
            string CacheKey = path + ".TDY_ElecCoalMonthSet";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_ElecCoalMonthSet)objType;
        }

        /// <summary>
        /// 创建T_Navigation数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_ElecCoalYearSet CreateTDY_ElecCoalYearSet()
        {
            string CacheKey = path + ".TDY_ElecCoalYearSet";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_ElecCoalYearSet)objType;
        }
        /// <summary>
        /// 创建TDY_CollState数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_CollState CreateTDY_CollState()
        {
            string CacheKey = path + ".TDY_CollState";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_CollState)objType;
        }
        /// <summary>
        /// 创建TDY_GelationLog数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_GelationLog CreateTDY_GelationLog()
        {
            string CacheKey = path + ".TDY_GelationLog";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_GelationLog)objType;
        }

        public static CoalTraffic.IDAL.ITDY_WeightBang CreateTDY_WeightBang()
        {
            string CacheKey = path + ".TDY_WeightBang";
            object objType = CreateObject(path, CacheKey);
            return (ITDY_WeightBang)objType;
        }

        /// <summary>
        /// 创建TDY_LogCollKicket数据层接口
        /// </summary>
        public static CoalTraffic.IDAL.ITDY_LogCollKicket CreateTDY_LogCollKicket()
        {

            string CacheKey = path + ".TDY_LogCollKicket";
            object objType = CreateObject(path, CacheKey);
            return (CoalTraffic.IDAL.ITDY_LogCollKicket)objType;
        }
    }
}

//***********DataCache.cs

using System;
using System.Web;
using System.Collections.Generic;
using System.Text;

namespace CoalTraffic.DALFactory
{
/// <summary>
/// 缓存相关的操作类
/// </summary>
public class DataCache
{
   /// <summary>
   /// 获取当前应用程序指定CacheKey的Cache值
   /// </summary>
   /// <param name="CacheKey"></param>
   /// <returns></returns>
   public static object GetCache(string CacheKey)
   {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
   }

   /// <summary>
   /// 设置当前应用程序指定CacheKey的Cache值
   /// </summary>
   /// <param name="CacheKey"></param>
   /// <param name="objObject"></param>
   public static void SetCache(string CacheKey, object objObject)
   {
    System.Web.Caching.Cache objCache = HttpRuntime.Cache;
    objCache.Insert(CacheKey, objObject);
   }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值