using HH.AMS.Ex.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace HH.AMS.Ex.Factroy { /// /// 抽象的工厂模式,提供创建一个数据层 /// 利用反射机制返回程序集 /// public sealed class DataAccess { private static DataType _DataType; public static DataType DataAccessType { get { if (AssemblyPath == "HH.AMS.Ex.MSSQLServices") _DataType = DataType.MsSQL; else if (AssemblyPath == "HH.AMS.Ex.MySQLServices") _DataType = DataType.MySQL; else if (AssemblyPath == "HH.AMS.Ex.OracleServices") _DataType = DataType.Oracle; else if (AssemblyPath == "HH.AMS.Ex.NpgsqlServices") _DataType = DataType.Npgsql; return _DataType; } set { _DataType = value; } } private static readonly string AssemblyPath = System.Configuration.ConfigurationManager.AppSettings["DbProvider"]; private static readonly string CalculateAssemblyPath = System.Configuration.ConfigurationManager.AppSettings["Calculate"]; public DataAccess() { } #region CreateObject /// /// 不使用缓存 /// /// 路径 /// 缓存关键字 /// object private static object CreateObjectNoCache(string path, string CacheKey) { try { object objType = Assembly.Load(path).CreateInstance(CacheKey); return objType; } catch (System.Exception ex) { throw new Exception(ex.Message); } } /// /// 使用缓存 /// /// 路径 /// 缓存关键字 /// object private static object CreateObject(string path, string CacheKey) { object objType = ObjectCache.GetCache(CacheKey); if (objType == null) { try { objType = Assembly.Load(path).CreateInstance(CacheKey); ObjectCache.SetCache(CacheKey, objType);// 写入缓存 } catch (System.Exception ex) { throw new Exception(ex.Message); } } return objType; } #endregion public static ITN_AM_RES_INF_LOGInterface CreateTN_AM_RES_INF_LOGInstance() { string ClassNamespace = AssemblyPath + ".ITN_AM_RES_INF_LOGServices"; object objType = CreateObject(AssemblyPath, ClassNamespace); return (ITN_AM_RES_INF_LOGInterface)objType; } public static TN_AM_REQ_INF_LOGInterface CreateTN_AM_REQ_INF_LOGInstance() { string ClassNamespace = AssemblyPath + ".TN_AM_REQ_INF_LOGServices"; object objType = CreateObject(AssemblyPath, ClassNamespace); return (TN_AM_REQ_INF_LOGInterface)objType; } public static IDispatch CreateDispatch() { string ClassNamespace = CalculateAssemblyPath + ".AMSInterfaceCalculate"; object objType = CreateObject(CalculateAssemblyPath, ClassNamespace); return (IDispatch)objType; } /// /// Autobom系统 /// /// public static IAutoBomInterface CreateAutobomSystem() { string ClassNamespace = AssemblyPath + ".AutoBomServices"; object objType = CreateObject(AssemblyPath, ClassNamespace); return (IAutoBomInterface)objType; } } }