1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#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
{
    /// <summary>
    ///     桥接工厂,用于创建与后台的通信通道
    /// </summary>
    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)丢失!";
 
        /// <summary>
        ///     动态配置桥接类型
        /// </summary>
        private static BridgeType _BridgeType = BridgeType.Unknow;
 
        private static AdoDirectType _AdoDirectType = AdoDirectType.LAN;
 
        /// <summary>
        ///     静态构造器
        /// </summary>
        static BridgeFactory()
        {
        }
 
        public static AdoDirectType AdoDirectType
        {
            get => _AdoDirectType;
            set => _AdoDirectType = value;
        }
 
        /// <summary>
        ///     系统当前桥接方式 (在BridgeFactory的构造器加载INI配置)
        /// </summary>
        public static BridgeType BridgeType
        {
            get => _BridgeType;
            set => _BridgeType = value;
        }
 
        /// <summary>
        ///     系统配置:db.ini
        /// </summary>
        public static IWriteSQLConfigValue DatabaseConfig { get; private set; }
 
        /// <summary>
        ///     直连模式:当前配置的数据库类型
        /// </summary>
        public static string DatabaseType { get; set; }
 
        /// <summary>
        ///     使用ADO直连模式
        /// </summary>
        /// <returns></returns>
        public static bool IsADODirect => BridgeType == BridgeType.ADODirect;
 
        /// <summary>
        ///     是否WCF连接模式
        /// </summary>
        /// <returns></returns>
        public static bool IsWCFBridge =>
            BridgeType == BridgeType.WindowsService;
 
        /// <summary>
        ///     获取SQL连接的本地配置接口。
        ///     支持4种连接配置:1.INI, 2.注册表 3.SQLExpress INI 4.Web.Config
        /// </summary>
        /// <returns></returns>
        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;
        }
 
        /// <summary>
        ///     初始化桥接功能
        /// </summary>
        /// <param name="loadType"></param>
        /// <returns></returns>
        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;
        }
 
        /// <summary>
        ///     初始化账套
        /// </summary>
        /// <param name="DBIDs">账套编号,多个编号用逗号分开。若参数为空则加载全部账套</param>
        public static void InitializeDatabase(string DBIDs)
        {
            DatabaseProvider.LoadDatabase(DBIDs);
        }
 
        /// <summary>
        ///     从配置文件获取桥接方式
        /// </summary>
        /// <returns></returns>
        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的实例
 
        /// <summary>
        ///     跟据ORM创建桥接功能
        /// </summary>
        /// <param name="ORM">ORM类</param>
        /// <returns></returns>
        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);
        }
 
        /// <summary>
        ///     根据ORM创建DAL实例
        /// </summary>
        /// <param name="ORM">ORM Model类</param>
        /// <param name="DBID">指定连接的数据库</param>
        /// <returns></returns>
        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);
        }
 
        /// <summary>
        ///     创建数据字典的数据层桥接实例
        /// </summary>
        /// <param name="tableName">数据字典表名</param>
        /// <param name="derivedClassName">派生的DAL类名,比如:dalCustomer, dalPerson</param>
        /// <returns></returns>
        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);
        }
 
        /// <summary>
        ///     创建数据字典的数据层桥接实例
        /// </summary>
        /// <param name="tableName">数据字典表名</param>
        /// <param name="derivedClassName">
        ///     派生的DAL类全名(Type.FullName),如:CSFrameworkV5.DataAccess.DAL_DataDict.dalCustomer,
        ///     CSFrameworkV5.DataAccess.DAL_DataDict.dalPerson
        /// </param>
        /// <param name="DBID">账套编号</param>
        /// <returns></returns>
        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);
        }
 
        /// <summary>
        ///     创建用户组的数据层桥接实例
        /// </summary>
        /// <returns></returns>
        public static IBridge_UserGroup CreateUserGroupBridge()
        {
            if (IsADODirect) return new dalUserGroup(Loginer.CurrentUser);
 
            if (IsWCFBridge) return new WCF_UserGroup();
 
            throw new CustomException(UNKNOW_BRIDGE_TYPE);
        }
 
        /// <summary>
        ///     创建公共数据层的桥接实例
        /// </summary>
        /// <returns></returns>
        public static IBridge_CommonData CreateCommonDataBridge()
        {
            if (IsADODirect) return new dalCommon(Loginer.CurrentUser);
 
            if (IsWCFBridge) return new WCF_CommonData();
 
            throw new CustomException(UNKNOW_BRIDGE_TYPE);
        }
 
        /// <summary>
        ///     创建权限功能的桥接实例
        /// </summary>
        /// <returns></returns>
        public static IBridge_Permission CreatePermissionBridge()
        {
            if (IsADODirect) return new dalPermission(Loginer.CurrentUser);
 
            if (IsWCFBridge) return new WCF_Permission();
 
            throw new CustomException(UNKNOW_BRIDGE_TYPE);
        }
 
        /// <summary>
        ///     创建用户管理的数据层桥接实例
        /// </summary>
        /// <returns></returns>
        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 测试后台连接
 
        /// <summary>
        ///     测试IIS承载的WebService连接,及Windows Service承载的WCF服务连接
        /// </summary>
        /// <returns></returns>
        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;
            }
        }
 
        /// <summary>
        ///     测试AdoDirect连接
        /// </summary>
        /// <returns></returns>
        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;
                }
            }
        }
 
        /// <summary>
        ///     测试连接(ADO/WCF)
        /// </summary>
        /// <param name="throwException"></param>
        /// <returns></returns>
        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
    }
}