1919using ShardingCore . Core . VirtualRoutes . TableRoutes ;
2020using ShardingCore . Core . VirtualRoutes . TableRoutes . RouteTails . Abstractions ;
2121using ShardingCore . Core . VirtualTables ;
22+ using ShardingCore . DynamicDataSources ;
2223using ShardingCore . Exceptions ;
2324using ShardingCore . Extensions ;
2425using ShardingCore . Jobs ;
@@ -60,6 +61,7 @@ public class ShardingDbContextBootstrapper<TShardingDbContext> : IShardingDbCont
6061 private readonly IEntityMetadataManager < TShardingDbContext > _entityMetadataManager ;
6162 private readonly IShardingTableCreator < TShardingDbContext > _tableCreator ;
6263 private readonly IParallelTableManager < TShardingDbContext > _parallelTableManager ;
64+ private readonly IDefaultDataSourceInitializer < TShardingDbContext > _dataSourceInitializer ;
6365 private readonly ILogger < ShardingDbContextBootstrapper < TShardingDbContext > > _logger ;
6466
6567 public ShardingDbContextBootstrapper ( IShardingConfigOption shardingConfigOption )
@@ -71,6 +73,7 @@ public ShardingDbContextBootstrapper(IShardingConfigOption shardingConfigOption)
7173 _tableCreator = ShardingContainer . GetService < IShardingTableCreator < TShardingDbContext > > ( ) ;
7274 _virtualDataSource = ShardingContainer . GetService < IVirtualDataSource < TShardingDbContext > > ( ) ;
7375 _parallelTableManager = ShardingContainer . GetService < IParallelTableManager < TShardingDbContext > > ( ) ;
76+ _dataSourceInitializer = ShardingContainer . GetService < IDefaultDataSourceInitializer < TShardingDbContext > > ( ) ;
7477 _logger = ShardingContainer . GetService < ILogger < ShardingDbContextBootstrapper < TShardingDbContext > > > ( ) ;
7578 }
7679 /// <summary>
@@ -143,130 +146,10 @@ private void InitializeConfigure()
143146 var dataSources = _shardingConfigOption . GetDataSources ( ) ;
144147 foreach ( var dataSourceKv in dataSources )
145148 {
146- using ( var serviceScope = ShardingContainer . ServiceProvider . CreateScope ( ) )
147- {
148- var dataSourceName = dataSourceKv . Key ;
149- var connectionString = dataSourceKv . Value ;
150- _virtualDataSource . AddPhysicDataSource ( new DefaultPhysicDataSource ( dataSourceName , connectionString , false ) ) ;
151- using var context =
152- ( DbContext ) serviceScope . ServiceProvider . GetService ( _shardingConfigOption . ShardingDbContextType ) ;
153- if ( _shardingConfigOption . EnsureCreatedWithOutShardingTable )
154- EnsureCreated ( context , dataSourceName ) ;
155- foreach ( var entity in context . Model . GetEntityTypes ( ) )
156- {
157- var entityType = entity . ClrType ;
158-
159- if ( _entityMetadataManager . IsShardingTable ( entityType ) )
160- {
161- var virtualTable = _virtualTableManager . GetVirtualTable ( entityType ) ;
162- //创建表
163- CreateDataTable ( dataSourceName , virtualTable ) ;
164- }
165- else
166- {
167- if ( _shardingConfigOption . NeedCreateTable ( entityType ) )
168- {
169- _tableCreator . CreateTable ( dataSourceName , entityType , string . Empty ) ;
170- }
171- }
172- }
173- }
149+ var dataSourceName = dataSourceKv . Key ;
150+ var connectionString = dataSourceKv . Value ;
151+ _dataSourceInitializer . InitConfigure ( dataSourceName , connectionString ) ;
174152 }
175153 }
176- private void CreateDataTable ( string dataSourceName , IVirtualTable virtualTable )
177- {
178- var entityMetadata = virtualTable . EntityMetadata ;
179- foreach ( var tail in virtualTable . GetVirtualRoute ( ) . GetAllTails ( ) )
180- {
181- if ( NeedCreateTable ( entityMetadata ) )
182- {
183- try
184- {
185-
186- //添加物理表
187- virtualTable . AddPhysicTable ( new DefaultPhysicTable ( virtualTable , tail ) ) ;
188- _tableCreator . CreateTable ( dataSourceName , entityMetadata . EntityType , tail ) ;
189- }
190- catch ( Exception e )
191- {
192- if ( ! _shardingConfigOption . IgnoreCreateTableError . GetValueOrDefault ( ) )
193- {
194- _logger . LogWarning ( e ,
195- $ "table :{ virtualTable . GetVirtualTableName ( ) } { entityMetadata . TableSeparator } { tail } will created.") ;
196- }
197- }
198- }
199- else
200- {
201- //添加物理表
202- virtualTable . AddPhysicTable ( new DefaultPhysicTable ( virtualTable , tail ) ) ;
203- }
204-
205- }
206- }
207- private bool NeedCreateTable ( EntityMetadata entityMetadata )
208- {
209- if ( entityMetadata . AutoCreateTable . HasValue )
210- {
211- if ( entityMetadata . AutoCreateTable . Value )
212- return entityMetadata . AutoCreateTable . Value ;
213- else
214- {
215- if ( entityMetadata . AutoCreateDataSourceTable . HasValue )
216- return entityMetadata . AutoCreateDataSourceTable . Value ;
217- }
218- }
219- if ( entityMetadata . AutoCreateDataSourceTable . HasValue )
220- {
221- if ( entityMetadata . AutoCreateDataSourceTable . Value )
222- return entityMetadata . AutoCreateDataSourceTable . Value ;
223- else
224- {
225- if ( entityMetadata . AutoCreateTable . HasValue )
226- return entityMetadata . AutoCreateTable . Value ;
227- }
228- }
229-
230- return _shardingConfigOption . CreateShardingTableOnStart . GetValueOrDefault ( ) ;
231- }
232- private void EnsureCreated ( DbContext context , string dataSourceName )
233- {
234- if ( context is IShardingDbContext shardingDbContext )
235- {
236- var dbContext = shardingDbContext . GetDbContext ( dataSourceName , false , _routeTailFactory . Create ( string . Empty ) ) ;
237-
238- var isDefault = _virtualDataSource . IsDefault ( dataSourceName ) ;
239-
240- var modelCacheSyncObject = dbContext . GetModelCacheSyncObject ( ) ;
241-
242- var acquire = Monitor . TryEnter ( modelCacheSyncObject , TimeSpan . FromSeconds ( 3 ) ) ;
243- if ( ! acquire )
244- {
245- throw new ShardingCoreException ( "cant get modelCacheSyncObject lock" ) ;
246- }
247-
248- try
249- {
250- if ( isDefault )
251- {
252- dbContext . RemoveDbContextRelationModelThatIsShardingTable ( ) ;
253- }
254- else
255- {
256- dbContext . RemoveDbContextAllRelationModelThatIsNoSharding ( ) ;
257- }
258- dbContext . Database . EnsureCreated ( ) ;
259- dbContext . RemoveModelCache ( ) ;
260- }
261- finally
262- {
263- Monitor . Exit ( modelCacheSyncObject ) ;
264- }
265- }
266- }
267-
268-
269-
270-
271154 }
272155}
0 commit comments