Пример #1
0
 public SqlServerDbContextOptionsProvider(SqlServerOptions sqlServerOptions, ILoggerFactory loggerFactory)
 {
     _connection       = new SqlConnection(sqlServerOptions.ConnectionString);
     _dbContextOptions = new DbContextOptionsBuilder()
                         .UseSqlServer(_connection)
                         .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
                         .UseLoggerFactory(loggerFactory)
                         .ReplaceService <IQueryCompiler, ShardingQueryCompiler>()
                         .ReplaceService <IModelCacheKeyFactory, ShardingModelCacheKeyFactory>()
                         .UseShardingSqlServerQuerySqlGenerator()
                         .Options;
 }
Пример #2
0
        public static IServiceCollection AddShardingSqlServer(this IServiceCollection services, Action <SqlServerOptions> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException($"AddScfSqlServerProvider 参数不能为空:{nameof(configure)}");
            }

            var options = new SqlServerOptions();

            configure(options);
            services.AddSingleton(options);
            services.AddShardingCore();

            services.AddScoped <IDbContextOptionsProvider, SqlServerDbContextOptionsProvider>();
            services.AddSingleton <IShardingParallelDbContextFactory, ShardingSqlServerParallelDbContextFactory>();
            if (options.HasSharding)
            {
                foreach (var shardingRoute in options.ShardingRoutes)
                {
                    var genericVirtualRoute = shardingRoute.GetInterfaces().FirstOrDefault(it => it.IsInterface && it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IVirtualRoute <>) &&
                                                                                           it.GetGenericArguments().Any());
                    if (genericVirtualRoute == null)
                    {
                        throw new ArgumentException("add sharding route type error not assignable from IVirtualRoute<>.");
                    }
                    var shardingEntity = genericVirtualRoute.GetGenericArguments()[0];
                    if (!shardingEntity.IsShardingEntity())
                    {
                        throw new ArgumentException("add sharding route type error generic arguments first not assignable from IShardingEntity.");
                    }
                    Type genericType   = typeof(IVirtualRoute <>);
                    Type interfaceType = genericType.MakeGenericType(shardingEntity);
                    services.AddSingleton(interfaceType, shardingRoute);
                }
            }
            services.AddSingleton(sp =>
            {
                var shardingCoreConfig = new ShardingCoreConfig();
                options.ShardingCoreConfigConfigure?.Invoke(sp, shardingCoreConfig);
                return(shardingCoreConfig);
            });
            services.AddSingleton <IShardingBootstrapper, ShardingBootstrapper>();
            return(services);
        }
Пример #3
0
 public ShardingSqlServerParallelDbContextFactory(IVirtualTableManager virtualTableManager, SqlServerOptions sqlServerOptions)
 {
     _virtualTableManager = virtualTableManager;
     _sqlServerOptions    = sqlServerOptions;
 }