#region using System; using System.IO; using System.Windows.Forms; using CSFramework.DB; using CSFrameworkV5.Common; using CSFrameworkV5.Core; using CSFrameworkV5.DataAccess; using CSFrameworkV5.Interfaces; using CSFrameworkV5.WebRef; using CSFrameworkV5.WebRef.CommonService; using CSFrameworkV5.WebRef.SystemModule; #endregion namespace CSFrameworkV5.Business { /// /// 桥接工厂,用于创建与后台的通信通道 /// public class BridgeFactory { public const string UNKNOW_BRIDGE_TYPE = "没有指定桥接类型(Bridge Type),创建数据层桥接实例失败!"; public const string TEST_BRIDGE_FAILED = "测试桥接功能失败,无法建立与后台数据层的连接!"; public const string LOST_DAL_FILE = "数据访问层模块文件(DataAccess.dll)丢失!"; /// /// 动态配置桥接类型 /// private static BridgeType _BridgeType = BridgeType.Unknow; private static AdoDirectType _AdoDirectType = AdoDirectType.LAN; /// /// 静态构造器 /// static BridgeFactory() { } public static AdoDirectType AdoDirectType { get => _AdoDirectType; set => _AdoDirectType = value; } /// /// 系统当前桥接方式 (在BridgeFactory的构造器加载INI配置) /// public static BridgeType BridgeType { get => _BridgeType; set => _BridgeType = value; } /// /// 系统配置:db.ini /// public static IWriteSQLConfigValue DatabaseConfig { get; private set; } /// /// 直连模式:当前配置的数据库类型 /// public static string DatabaseType { get; set; } /// /// 使用ADO直连模式 /// /// public static bool IsADODirect => BridgeType == BridgeType.ADODirect; /// /// 是否WCF连接模式 /// /// public static bool IsWCFBridge => BridgeType == BridgeType.WindowsService; /// /// 获取SQL连接的本地配置接口。 /// 支持4种连接配置:1.INI, 2.注册表 3.SQLExpress INI 4.Web.Config /// /// public static IWriteSQLConfigValue GetConfigInterface() { //取注册表配置 //IWriteSQLConfigValue cfgRegistry = new RegisterWriter(SqlConfiguration.REG_SQL_CFG); //取web.config连接配置 //IWriteSQLConfigValue cfgWebConfig = new WebConfigCfg("", ""); //取本地INI文件,SQL标准连接 IWriteSQLConfigValue cfgINI = new IniFileWriter(Application.StartupPath + Globals.DEF_DB_CFG); return cfgINI; } /// /// 初始化桥接功能 /// /// /// public static bool InitializeBridge(bool loadType = true) { var connected = false; try { //从配置文件获取桥接方式 if (loadType) LoadBridgeType(); //ADODirect方式,从INI文件读配置信息 if (BridgeType.ADODirect == BridgeType) { //读取SQL连接配置信息 var cfgSystem = GetConfigInterface(); //加载账套数据库信息 DatabaseProvider.LoadSystemDatabase(cfgSystem); //读取系统数据库名称 Globals.DEF_SYSTEM_DBName = cfgSystem.DatabaseName; //测试AdoDirect连接 connected = TestADOConnection(cfgSystem); DatabaseType = cfgSystem.DatabaseType; DatabaseConfig = cfgSystem; DatabaseProvider.IsAdoDirect = BridgeType == BridgeType.ADODirect; DatabaseProvider.IsLocalConnection = AdoDirectType == AdoDirectType.LAN; } else if (BridgeType.WindowsService == BridgeType) { //测试Windows Service连接 connected = TestWCFConnection(); } } catch (Exception ex) { Msg.ShowException(ex); } //测试桥接是否成功 if (false == connected) Msg.Warning(TEST_BRIDGE_FAILED + "\r\n\r\nBridgeType:" + BridgeType.ToStringEx()); return connected; } /// /// 初始化账套 /// /// 账套编号,多个编号用逗号分开。若参数为空则加载全部账套 public static void InitializeDatabase(string DBIDs) { DatabaseProvider.LoadDatabase(DBIDs); } /// /// 从配置文件获取桥接方式 /// /// private static void LoadBridgeType() { _BridgeType = BridgeType.ADODirect; var iniFile = Application.StartupPath + Globals.DEF_DB_CFG; //有配置文件 if (File.Exists(iniFile)) { var ini = new IniFile(iniFile); var bridgeType = ini.IniReadValue("BridgeType", "BridgeType", BridgeType.ADODirect.ToStringEx()); if (Enum.IsDefined(typeof(BridgeType), bridgeType)) _BridgeType = (BridgeType)Enum.Parse(typeof(BridgeType), bridgeType); var IPType = ini.IniReadValue("BridgeType", "IPType", AdoDirectType.LAN.ToStringEx()); if (Enum.IsDefined(typeof(AdoDirectType), IPType)) _AdoDirectType = (AdoDirectType)Enum.Parse(typeof(AdoDirectType), IPType); } else { throw new Exception("db.ini文件丢失!"); } } #region 创建IBridge_XXX的实例 /// /// 跟据ORM创建桥接功能 /// /// ORM类 /// public static IBridge_DataDict CreateDataDictBridge(Type ORM) { //直连模式,返回数据访问层实例 if (IsADODirect) { if (ORM == null) throw new CustomException("未指定ORM模型!"); //跟据ORM类全名自动创建对应的DAL对象实例 var dal = DALFactory.CreateDalByORM(Loginer.CurrentUser, ORM); if (dal != null) return dal; //根据Model定义查找DAL return new dalBaseDataDict(Loginer.CurrentUser, ORM); } //WCF服务模式,返回WCF服务代理实例 if (IsWCFBridge) return new WCF_DataDict(ORM); throw new CustomException(UNKNOW_BRIDGE_TYPE); } /// /// 根据ORM创建DAL实例 /// /// ORM Model类 /// 指定连接的数据库 /// public static IBridge_DataDict CreateDataDictBridge(Type ORM, string DBID) { if (IsADODirect) { var dal = DALFactory.CreateDalByORM(Loginer.CurrentUser, ORM); if (Loginer.CurrentUser.DBID != DBID) dal.Database = DatabaseProvider.GetDatabase(DBID); return dal; } if (IsWCFBridge) return new WCF_DataDict(ORM); throw new CustomException(UNKNOW_BRIDGE_TYPE); } /// /// 创建数据字典的数据层桥接实例 /// /// 数据字典表名 /// 派生的DAL类名,比如:dalCustomer, dalPerson /// public static IBridge_DataDict CreateDataDictBridge(string tableName) { if (IsADODirect) return DALFactory.CreateDal(Loginer.CurrentUser, Loginer.CurrentUser.DBID, tableName); if (IsWCFBridge) return new WCF_DataDict(tableName); throw new CustomException(UNKNOW_BRIDGE_TYPE); } /// /// 创建数据字典的数据层桥接实例 /// /// 数据字典表名 /// /// 派生的DAL类全名(Type.FullName),如:CSFrameworkV5.DataAccess.DAL_DataDict.dalCustomer, /// CSFrameworkV5.DataAccess.DAL_DataDict.dalPerson /// /// 账套编号 /// public static IBridge_DataDict CreateDataDictBridge(string tableName, string DBID) { if (IsADODirect) return DALFactory.CreateDal(Loginer.CurrentUser, DBID, tableName); if (IsWCFBridge) return new WCF_DataDict(tableName, DBID); throw new CustomException(UNKNOW_BRIDGE_TYPE); } /// /// 创建用户组的数据层桥接实例 /// /// public static IBridge_UserGroup CreateUserGroupBridge() { if (IsADODirect) return new dalUserGroup(Loginer.CurrentUser); if (IsWCFBridge) return new WCF_UserGroup(); throw new CustomException(UNKNOW_BRIDGE_TYPE); } /// /// 创建公共数据层的桥接实例 /// /// public static IBridge_CommonData CreateCommonDataBridge() { if (IsADODirect) return new dalCommon(Loginer.CurrentUser); if (IsWCFBridge) return new WCF_CommonData(); throw new CustomException(UNKNOW_BRIDGE_TYPE); } /// /// 创建权限功能的桥接实例 /// /// public static IBridge_Permission CreatePermissionBridge() { if (IsADODirect) return new dalPermission(Loginer.CurrentUser); if (IsWCFBridge) return new WCF_Permission(); throw new CustomException(UNKNOW_BRIDGE_TYPE); } /// /// 创建用户管理的数据层桥接实例 /// /// public static IBridge_User CreateUserBridge() { if (IsADODirect) return new dalUser(Loginer.CurrentUser); if (IsWCFBridge) return new WCF_User(); throw new CustomException(UNKNOW_BRIDGE_TYPE); } #endregion #region 测试后台连接 /// /// 测试IIS承载的WebService连接,及Windows Service承载的WCF服务连接 /// /// private static bool TestWCFConnection() { try { //return new WCF_CommonData().TestConnection(); var result = new WCF_CommonData().TestConnection(); var dt = new WCF_DataDict().GetServerTime(); return dt > DateTime.Parse("1900-01-01"); } catch (Exception ex) { return false; } } /// /// 测试AdoDirect连接 /// /// private static bool TestADOConnection(IWriteSQLConfigValue config) { // 数据访问层的模块文件 var DalFile = Application.StartupPath + @"\" + Globals.DEF_DAL_DLL; if (!File.Exists(DalFile)) throw new CustomException(LOST_DAL_FILE + "\r\n\r\nFile:" + DalFile); //测试SQL连接 var dbType = (DatabaseType)Enum.Parse(typeof(DatabaseType), config.DatabaseType); var factory = DatabaseFactory.GetADOFactory(dbType, config.ConnectionString); using (var conn = factory.CreateConnection()) { try { conn.Close(); conn.Open(); conn.Dispose(); return true; } catch { return false; } } } /// /// 测试连接(ADO/WCF) /// /// /// public static bool TestConnection(bool throwException = false) { var conn = false; if (IsADODirect) conn = TestADOConnection(GetConfigInterface()); if (IsWCFBridge) conn = TestWCFConnection(); if (conn = false && throwException) throw new CustomException( $"测试连接模式{_BridgeType.ToStringEx()}失败!"); return conn; } #endregion } }