zhaolei
9 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
#if !NET45
using Prow.Data;
using System;
 
namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// 注入数据库服务扩展类
    /// </summary>
    public static class DatabaseServiceCollectionExtensions
    {
        /// <summary>
        /// 获取 容器集合
        /// </summary>
        internal static ServiceProvider ServiceProvider { get { return _lazy.Value; } }
#nullable disable
        private static Lazy<ServiceProvider> _lazy;
#nullable restore
        /// <summary>
        /// 增加数据库适配框架到服务容器中 <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddDbAdapter(this IServiceCollection services)
        {
            // 创建服务
            services.AddSingleton(typeof(IDbContext<>), typeof(DefaultDbContext<>));
            services.AddSingleton(typeof(IDbAdapter), typeof(DefaultDbAdapter));
            _lazy = new Lazy<ServiceProvider>(() => services.BuildServiceProvider());
            return services;
        }
    }
}
#endif