Esempio n. 1
0
        private static void LoadDatabaseSets()
        {
            var databaseSets = ConfigurationSection.DatabaseSets;

            if (databaseSets == null)
            {
                throw new DalException("Missing DatabaseSets.");
            }
            //一个DataBaseSet可以配置多个 例如主从 或者 分片
            DatabaseSets = new Dictionary <String, DatabaseSetWrapper>();

            foreach (DatabaseSetElement databaseSet in databaseSets)
            {
                if (!DatabaseProviders.ContainsKey(databaseSet.Provider))
                {
                    throw new DalException("DatabaseProvider doesn't match.");
                }
                IDatabaseProvider provider = DatabaseProviders[databaseSet.Provider];

                //build set wrapper 同一个DataBaseSet的Provider必须一致
                var databaseSetWrapper = new DatabaseSetWrapper
                {
                    Name = databaseSet.Name,
                    EnableReadWriteSpliding = false,//默认关闭读写分离
                    ProviderType            = DatabaseProviderTypeFactory.GetProviderType(provider.ProviderType),
                };

                foreach (DatabaseElement database in databaseSet.Databases)
                {
                    databaseSetWrapper.DatabaseWrappers.Add(new DatabaseWrapper
                    {
                        Name             = database.Name,
                        ConnectionString = database.ConnectionString,
                        DatabaseType     = database.DatabaseType,
                        DatabaseProvider = provider,
                        Database         = new Database(databaseSet.Name, database.Name, database.ConnectionString, provider)
                        {
                            DatabaseRWType = database.DatabaseType
                        },
                    });

                    if (database.DatabaseType == DatabaseType.Slave && !databaseSetWrapper.EnableReadWriteSpliding)
                    {
                        databaseSetWrapper.EnableReadWriteSpliding = true;
                    }
                }

                DatabaseSets.Add(databaseSet.Name, databaseSetWrapper);
            }
        }
Esempio n. 2
0
        private static void LoadDatabaseSets()
        {
#if !NETSTANDARD
            var databaseSets = ConfigurationSection.DatabaseSets;
            if (databaseSets == null)
            {
                throw new DalException("Missing DatabaseSets.");
            }
            //一个DataBaseSet可以配置多个 例如主从 或者 分片
            DatabaseSets = new Dictionary <String, DatabaseSetWrapper>();

            foreach (DatabaseSetElement databaseSet in databaseSets)
            {
                if (!DatabaseProviders.ContainsKey(databaseSet.Provider))
                {
                    throw new DalException("DatabaseProvider doesn't match.");
                }
                IDatabaseProvider provider = DatabaseProviders[databaseSet.Provider];

                //build set wrapper 同一个DataBaseSet的Provider必须一致
                var databaseSetWrapper = new DatabaseSetWrapper
                {
                    Name = databaseSet.Name,
                    EnableReadWriteSpliding = false,//默认关闭读写分离
                    ProviderType            = DatabaseProviderTypeFactory.GetProviderType(provider.ProviderType),
                    ShardingStrategy        = ShardingStrategyFactory.Instance.GetShardingStrategy(databaseSet)
                };

                foreach (DatabaseElement database in databaseSet.Databases)
                {
                    String shard      = database.Sharding ?? String.Empty;
                    Int32  ratio      = 0;
                    Int32  ratioStart = 0;
                    Int32  ratioEnd   = 0;
                    if (shard.Length > 0)
                    {
                        if (database.DatabaseType == DatabaseType.Slave)
                        {
                            ratioStart = ratio;
                            ratio     += database.Ratio;
                            ratioEnd   = ratio;
                        }

                        databaseSetWrapper.AllShards.Add(shard);
                        if (!databaseSetWrapper.TotalRatios.ContainsKey(shard))
                        {
                            databaseSetWrapper.TotalRatios.Add(shard, ratio);
                        }
                    }

                    databaseSetWrapper.DatabaseWrappers.Add(new DatabaseWrapper
                    {
                        Name             = database.Name,
                        ConnectionString = database.ConnectionString,
                        DatabaseType     = database.DatabaseType,
                        DatabaseProvider = provider,
                        Database         = new Database(databaseSet.Name, database.Name, database.ConnectionString, provider)
                        {
                            DatabaseRWType = database.DatabaseType
                        },
                        Sharding   = shard,
                        RatioStart = ratioStart,
                        RatioEnd   = ratioEnd
                    });

                    if (database.DatabaseType == DatabaseType.Slave && !databaseSetWrapper.EnableReadWriteSpliding)
                    {
                        databaseSetWrapper.EnableReadWriteSpliding = true;
                    }
                }

                DatabaseSets.Add(databaseSet.Name, databaseSetWrapper);
            }
#else
            if (DataConnection.DefaultSettings == null || DataConnection.DefaultSettings.ConnectionStrings == null || !DataConnection.DefaultSettings.ConnectionStrings.Any())
            {
                throw new DalException("Missing DataConnection.ConnectionStrings.");
            }
            //一个DataBaseSet可以配置多个 例如主从 或者 分片
            DatabaseSets = new Dictionary <String, DatabaseSetWrapper>();
            foreach (var databaseSet in DataConnection.DefaultSettings.ConnectionStrings)
            {
                if (!DatabaseProviders.ContainsKey(databaseSet.ProviderName))
                {
                    throw new DalException("DatabaseProvider doesn't match.");
                }
                IDatabaseProvider provider = DatabaseProviders[databaseSet.ProviderName];

                //build set wrapper 同一个DataBaseSet的Provider必须一致
                var databaseSetWrapper = new DatabaseSetWrapper
                {
                    Name = databaseSet.Name,
                    EnableReadWriteSpliding = false,//默认关闭读写分离
                    ProviderType            = DatabaseProviderTypeFactory.GetProviderType(provider.ProviderType),
                };

                foreach (var database in databaseSet.ConnectionItemList)
                {
                    databaseSetWrapper.DatabaseWrappers.Add(new DatabaseWrapper
                    {
                        Name             = database.Name,
                        ConnectionString = database.ConnectionString,
                        DatabaseType     = database.DatabaseType,
                        DatabaseProvider = provider,
                        Database         = new Database(databaseSet.Name, database.Name, database.ConnectionString, provider)
                        {
                            DatabaseRWType = database.DatabaseType
                        },
                    });

                    if (database.DatabaseType == DatabaseType.Slave && !databaseSetWrapper.EnableReadWriteSpliding)
                    {
                        databaseSetWrapper.EnableReadWriteSpliding = true;
                    }
                }

                DatabaseSets.Add(databaseSet.Name, databaseSetWrapper);
            }
#endif
        }
Esempio n. 3
0
        private static void LoadDatabaseSets()
        {
            var databaseSets = ConfigurationSection.DatabaseSets;

            if (databaseSets == null)
            {
                throw new DalException("Missing DatabaseSets.");
            }
            DatabaseSets = new Dictionary <String, DatabaseSetWrapper>();

            foreach (DatabaseSetElement databaseSet in databaseSets)
            {
                if (!DatabaseProviders.ContainsKey(databaseSet.Provider))
                {
                    throw new DalException("DatabaseProvider doesn't match.");
                }
                IDatabaseProvider provider = DatabaseProviders[databaseSet.Provider];

                //build set wrapper
                var databaseSetWrapper = new DatabaseSetWrapper
                {
                    Name = databaseSet.Name,
                    EnableReadWriteSpliding = false,
                    ProviderType            = DatabaseProviderTypeFactory.GetProviderType(provider.ProviderType),
                    ShardingStrategy        = ShardingStrategyFactory.Instance.GetShardingStrategy(databaseSet)
                };

                foreach (DatabaseElement database in databaseSet.Databases)
                {
                    String shard      = database.Sharding ?? String.Empty;
                    Int32  ratio      = 0;
                    Int32  ratioStart = 0;
                    Int32  ratioEnd   = 0;
                    if (shard.Length > 0)
                    {
                        if (database.DatabaseType == DatabaseType.Slave)
                        {
                            ratioStart = ratio;
                            ratio     += database.Ratio;
                            ratioEnd   = ratio;
                        }

                        databaseSetWrapper.AllShards.Add(shard);
                        if (!databaseSetWrapper.TotalRatios.ContainsKey(shard))
                        {
                            databaseSetWrapper.TotalRatios.Add(shard, ratio);
                        }
                    }

                    databaseSetWrapper.DatabaseWrappers.Add(new DatabaseWrapper
                    {
                        Name             = database.Name,
                        ConnectionString = database.ConnectionString,
                        DatabaseType     = database.DatabaseType,
                        DatabaseProvider = provider,
                        Database         = new Database(databaseSet.Name, database.Name, database.ConnectionString, provider)
                        {
                            DatabaseRWType = database.DatabaseType
                        },
                        Sharding   = shard,
                        RatioStart = ratioStart,
                        RatioEnd   = ratioEnd
                    });

                    if (database.DatabaseType == DatabaseType.Slave && !databaseSetWrapper.EnableReadWriteSpliding)
                    {
                        databaseSetWrapper.EnableReadWriteSpliding = true;
                    }
                }

                DatabaseSets.Add(databaseSet.Name, databaseSetWrapper);
            }
        }