ningshuxia
2023-02-06 1fc0b4ed96d4c4b1ff7142926239998de32b2ada
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.DAL.Factory
{
    /// <summary>
    /// 数据访问基类
    /// </summary>
    public class DataAccessBase
    { 
        /// <summary>
        /// 缓存
        /// </summary>
        private static Dictionary<string, object> _dict = new Dictionary<string, object>();
        private static readonly object _locker = new object();
 
 
        /// <summary>
        /// 创建DAL
        /// </summary> 
        public static I CreateDAL<I>(string dllName, string nameSpace, bool isSaveCache = true)
        {
            if (true)
            {
 
            }
            object obj = null;
            var type = typeof(I);
            var cacheKey = string.Format("{0}.{1}", nameSpace, type.Name.Remove(0, 1));
            if (_dict.ContainsKey(cacheKey))
                obj = _dict[cacheKey];
 
            if (obj == null)
            {
                lock (_locker)
                {
                    if (obj == null)
                    {
                        try
                        {
                            //注意要主入口程序和DAL的程序都是any cpu或都是x86
                            obj = Assembly.Load(dllName).CreateInstance(cacheKey);
                            if (obj == null)
                                throw new Exception("DAL对象创建失败");
                            if (isSaveCache && obj != null)
                                _dict[cacheKey] = obj;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
 
            }
            return (I)obj;
        }
 
        
    }
}