#region
|
|
using System;
|
using System.Collections;
|
using System.Globalization;
|
using System.IO;
|
using System.Reflection;
|
using CSFrameworkV5.Common;
|
using CSFrameworkV5.Core;
|
using CSFrameworkV5.Core.CodeGenerator;
|
|
#endregion
|
|
namespace CSFrameworkV5.DataAccess
|
{
|
/// <summary>
|
/// 数据访问层的对象工厂
|
/// </summary>
|
public static class DALFactory
|
{
|
#region 通过.NET反射机制,加载数据字典数据访问层(dalBaseDataDict)的类型
|
|
//哈希表, 保存已加载的DAL类的引用地址
|
private static Hashtable _DalTypes = new Hashtable();
|
private static Hashtable _OrmTypes = new Hashtable();
|
|
//通过.NET反射机制,加载数据字典数据访问层(dalBaseDataDict)的类型
|
static DALFactory()
|
{
|
try
|
{
|
//应用程序根目录
|
var rootPath =
|
Path.GetDirectoryName(typeof(DALFactory).Assembly.Location);
|
|
//加载开发框架数据访问层核心库(如:CSFrameworkV5.DataAccess.dll,您的项目.DataAccess.dll)
|
LoadDalType(typeof(DALFactory).Assembly);
|
|
//加载CSFrameworkV5.DemoDAL.DLL
|
var demoPath = Path.Combine(rootPath, Globals.DEF_DEMO_DAL);
|
if (File.Exists(demoPath))
|
LoadDalType(Assembly.LoadFrom(demoPath));
|
|
//模拟加载其它扩展DAL层的DLL文件
|
//string otherPath = System.IO.Path.Combine(rootPath, "MyDataAccess.dll");
|
//if (System.IO.File.Exists(otherPath))
|
// LoadDalType(Assembly.LoadFrom(otherPath));
|
}
|
catch (Exception ex)
|
{
|
Msg.Warning("DALFactory静态类构造函数报错!" + ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 加载DAL层的所有类
|
/// </summary>
|
/// <param name="asmDAL"></param>
|
public static void LoadDalType(Assembly asmDAL)
|
{
|
try
|
{
|
//获取DataAccess程序集中所有Public类
|
var types = asmDAL.GetExportedTypes();
|
|
//枚举程序集中所有Public类
|
foreach (var T in types)
|
{
|
//注意:只查找dalBaseDataDict的子类!!!
|
// dalBaseBusiness类暂不支持DefaultORM_UpdateMode特性
|
if (false == T.IsSubclassOf(typeof(dalBaseDataDict)))
|
continue;
|
|
//查找DefaultORM_UpdateMode特性
|
var atts =
|
T.GetCustomAttributes(typeof(DefaultORM_UpdateMode),
|
false);
|
if (atts == null || atts.Length == 0) continue;
|
|
//该类有定义DefaultORM_UpdateMode特性
|
var att = atts[0] as DefaultORM_UpdateMode;
|
|
//获取ORM的表名
|
var tableName = ORM_Tools.GetTableName(att.ORM);
|
if (!_DalTypes.ContainsKey(tableName))
|
_DalTypes.Add(tableName, T);
|
|
//获取ORM的全名(Fullname)
|
var ORMTypeName = att.ORM.FullName;
|
if (!_DalTypes.ContainsKey(ORMTypeName))
|
_DalTypes.Add(ORMTypeName, T);
|
|
//将ORM模型的Type添加到哈希表
|
if (!_OrmTypes.ContainsKey(tableName))
|
_OrmTypes.Add(tableName, att.ORM);
|
|
if (!_OrmTypes.ContainsKey(ORMTypeName))
|
_OrmTypes.Add(ORMTypeName, att.ORM);
|
}
|
}
|
catch (Exception ex)
|
{
|
Msg.ShowException(ex);
|
}
|
}
|
|
/// <summary>
|
/// 根据表名获取对应的DAL层
|
/// </summary>
|
/// <param name="tableName">表名</param>
|
/// <returns></returns>
|
internal static Type GetDALTypeByTableName(string tableName)
|
{
|
if (_DalTypes.ContainsKey(tableName))
|
return _DalTypes[tableName] as Type;
|
|
return null;
|
}
|
|
/// <summary>
|
/// 根据ORM模型类全名获取对应的DAL层
|
/// </summary>
|
/// <param name="ORM_TypeName">ORM模型的全名</param>
|
/// <returns></returns>
|
internal static Type GetDALTypeByORM(string ORM_TypeName)
|
{
|
if (_DalTypes.ContainsKey(ORM_TypeName))
|
return _DalTypes[ORM_TypeName] as Type;
|
|
return null;
|
}
|
|
/// <summary>
|
/// 获取DAL类
|
/// </summary>
|
/// <param name="dalClassName">DAL类名</param>
|
/// <returns></returns>
|
internal static Type GetDALTypeByClassName(string dalClassName)
|
{
|
foreach (Type T in _DalTypes)
|
if (T.FullName.ToLower() == dalClassName.ToLower())
|
return T;
|
|
return null;
|
}
|
|
/// <summary>
|
/// 获取ORM类
|
/// </summary>
|
/// <param name="key">表名或ORM类全名(Fullname)</param>
|
/// <returns></returns>
|
internal static Type GetORMType(string key)
|
{
|
if (_OrmTypes.ContainsKey(key)) return (Type)_OrmTypes[key];
|
|
return null;
|
}
|
|
#endregion
|
|
#region DALFactory静态类的方法
|
|
/// <summary>
|
/// 跟据参数创建建数据层实例
|
/// </summary>
|
/// <param name="DBID">账套编号</param>
|
/// <param name="tableName">表名,可为空</param>
|
/// <param name="derivedClassName">DAL类的命名空间全名,可为空,如:CSFrameworkV5.DataAccess.DAL_Business.dalSO</param>
|
/// <returns></returns>
|
public static dalBaseDataDict CreateDal(Loginer loginer, string DBID,
|
string tableName)
|
{
|
var dal = CreateDalByTableName(loginer, tableName);
|
|
if (loginer.DBID != DBID && !string.IsNullOrEmpty(DBID))
|
dal.Database = DatabaseProvider.GetDatabase(DBID);
|
|
return dal;
|
}
|
|
/// <summary>
|
/// 跟据资料表名创建数据层实例,DAL类的特性定义了DefaultORM_UpdateMode特性,判断表名获取DAL
|
/// </summary>
|
/// <param name="loginer">当前登录用户</param>
|
/// <param name="tableName">资料表名,如tb_Customer</param>
|
/// <returns></returns>
|
public static dalBaseDataDict CreateDalByTableName(Loginer loginer,
|
string tableName)
|
{
|
if (string.IsNullOrEmpty(tableName))
|
throw new CustomException("没有指定表名,无法创建实例!");
|
|
var DAL_Type = GetDALTypeByTableName(tableName); //DAL类定义
|
var ORM_Type = GetORMType(tableName);
|
|
//不可删除!没有找到对应的DAL层,返回基础实例
|
if (DAL_Type == null)
|
return new dalBaseDataDict(loginer, tableName);
|
//throw new CustomException("根据表名" + tableName + "创建DAL失败!");
|
|
//创建DAL实例,对应dalBaseDataDict(Loginer loginer)构造器
|
var instance = (dalBaseDataDict)DAL_Type.Assembly.CreateInstance(
|
DAL_Type.FullName, true,
|
BindingFlags.CreateInstance, null,
|
new object[] { loginer }, CultureInfo.CurrentCulture, null);
|
|
if (instance.ORM == null) instance.ORM = ORM_Type;
|
|
return instance;
|
}
|
|
/// <summary>
|
/// 跟据ORM类全名自动创建DAL对象实例
|
/// </summary>
|
/// <param name="loginer">当前登录用户</param>
|
/// <param name="ORM_TypeName">ORM类的命名空间,如:CSFrameworkV5.DataAccess.DAL_Business.dalSO</param>
|
/// <returns></returns>
|
public static dalBaseDataDict CreateDalByORM(Loginer loginer,
|
string ORM_TypeName)
|
{
|
if (string.IsNullOrEmpty(ORM_TypeName))
|
throw new CustomException("ORM类名为空,无法创建实例!");
|
|
var ORM_Type = GetORMType(ORM_TypeName);
|
if (ORM_Type == null) return null;
|
|
var DAL_Type = GetDALTypeByORM(ORM_TypeName); //DAL类定义
|
|
//没有找到指定的数据层,预设为数据字典基类
|
if (DAL_Type == null) return new dalBaseDataDict(loginer, ORM_Type);
|
|
try
|
{
|
//创建DAL实例,对应dalBaseDataDict(Loginer loginer)构造器
|
var instance =
|
(dalBaseDataDict)DAL_Type.Assembly.CreateInstance(
|
DAL_Type.FullName, true,
|
BindingFlags.CreateInstance, null,
|
new object[] { loginer }, CultureInfo.CurrentCulture,
|
null);
|
|
if (instance.ORM == null) instance.ORM = ORM_Type;
|
|
return instance;
|
}
|
catch (Exception ex)
|
{
|
Msg.ShowException(ex);
|
return null;
|
}
|
}
|
|
/// <summary>
|
/// 跟据ORM类类型自动创建DAL对象实例
|
/// </summary>
|
/// <param name="loginer">当前登录用户</param>
|
/// <param name="ORM_Type">ORM类类型,如typeof(tb_Customer)</param>
|
/// <returns></returns>
|
public static dalBaseDataDict CreateDalByORM(Loginer loginer,
|
Type ORM_Type)
|
{
|
if (ORM_Type == null)
|
throw new CustomException("ORM类型不能为空,无法创建实例!");
|
|
var DAL = CreateDalByORM(loginer, ORM_Type.FullName);
|
if (DAL == null) DAL = new dalBaseDataDict(loginer, ORM_Type);
|
|
return DAL;
|
}
|
|
#endregion
|
}
|
}
|