/// <summary>
        /// 注入EFCoreSharding
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="shardingBuilder">配置项</param>
        /// <returns></returns>
        public static IServiceCollection AddEFCoreSharding(this IServiceCollection services, Action <IShardingBuilder> shardingBuilder)
        {
            if (!services.Any(x => x.ServiceType == typeof(ILoggerFactory)))
            {
                services.AddLogging();
            }

            ShardingContainer container = new ShardingContainer(services);

            shardingBuilder(container);

            services.AddSingleton <IShardingBuilder>(container);
            services.AddSingleton <IShardingConfig>(container);
            services.AddSingleton <IDbFactory, DbFactory>();
            services.AddScoped <IShardingDbAccessor, ShardingDbAccessor>();

            return(services);
        }
示例#2
0
        /// <summary>
        /// 注入EFCoreSharding
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="shardingBuilder">配置项</param>
        /// <returns></returns>
        public static IServiceCollection AddEFCoreSharding(this IServiceCollection services, Action <IShardingBuilder> shardingBuilder = null)
        {
            services.AddOptions <EFCoreShardingOptions>();
            services.AddLogging();

            ShardingContainer container = new ShardingContainer(services);

            shardingBuilder?.Invoke(container);
            services.AddSingleton(container);
            services.AddSingleton <IShardingBuilder>(container);
            services.AddSingleton <IShardingConfig>(container);
            services.AddSingleton <DbFactory>();
            services.AddSingleton <IDbFactory, DbFactory>();
            services.AddScoped <IShardingDbAccessor, ShardingDbAccessor>();

            services.AddHostedService <EFCoreShardingBootstrapper>();

            return(services);
        }