zhaolei
8 days ago 4a2e5b9a21940f11757be37d99f0944e240e908b
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
#if !NET45
using Microsoft.Extensions.Configuration;
using System.Linq;
#else
using System.Configuration;
#endif
using System;
using System.Reflection;
using System.Collections.Concurrent;
 
namespace Prow.Data
{
    /// <summary>
    /// IDbContext 默认实现类
    /// </summary>
    internal class DefaultDbContext<T> : IDbContext<T> where T : class
    {
        private static readonly ConcurrentDictionary<string, T?> _contextPool = new ConcurrentDictionary<string, T?>();
 
#if !NET45
        private readonly IConfiguration _configuration;
 
        /// <summary>
        /// 默认构造函数
        /// </summary>
        /// <param name="configuration">IConfiguration 实例</param>
        public DefaultDbContext(IConfiguration configuration) => _configuration = configuration;
 
        private static DbAdapterItem CreateAdapterItemFromConfig(IConfiguration configuration)
        {
            var option = configuration.GetSection("DB").GetChildren().Select(config => new DbAdapterItem()
            {
                Widget = config["Widget"],
                Enabled = bool.TryParse(config["Enabled"], out var en) ? en : false
            });
            return option.FirstOrDefault(i => i.Enabled);
        }
#else
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public DefaultDbContext()
        {
 
        }
#endif
 
        private T? CreateInstance(string? assemblyName)
        {
            T? ret;
            if (!string.IsNullOrEmpty(assemblyName))
            {
                var assembly = Assembly.Load(assemblyName);
                var type = assembly.GetType($"{assemblyName}.{typeof(T).Name}", true, true);
                ret = type == null ? null : Activator.CreateInstance(type) as T;
            }
            else
            {
                ret = Activator.CreateInstance<T>();
            }
            return ret;
        }
 
        /// <summary>
        /// 创建 T 实例方法
        /// </summary>
        /// <returns>T 实例</returns>
        public T? Create()
        {
#if !NET45
            var widget = CreateAdapterItemFromConfig(_configuration);
            var t = _contextPool.GetOrAdd(widget?.Widget ?? "default", key => CreateInstance(widget?.Widget));
#else
            var widget = ConfigurationManager.AppSettings["DbWidget"];
            var t = CreateInstance(widget);
#endif
            return t;
        }
 
        /// <summary>
        ///
        /// </summary>
        private class DbAdapterItem
        {
            /// <summary>
            /// 获得/设置 是否启用
            /// </summary>
            public bool Enabled { get; set; }
 
            /// <summary>
            /// 获得/设置 插件描述 动态链接库名称
            /// </summary>
            public string? Widget { get; set; }
        }
    }
}