@@ -32,7 +32,21 @@ public DbContextManager(IConfigManager configManager, ILoggerFactory loggerFacto
3232 _loggerFactory = loggerFactory ;
3333 _configManager = configManager ;
3434
35- _possibleModels = ReflectionTool . GetPublicClasses ( typeof ( DbContext ) )
35+ _possibleModels = GetPossibleModels ( ) ;
36+ _configuredModels = GetConfiguredModels ( ) ;
37+
38+ foreach ( var wrapper in _configuredModels )
39+ {
40+ InitializeConfigurator ( wrapper ) ;
41+ }
42+ }
43+
44+ /// <summary>
45+ /// This method uses reflection to find all possible models in the current AppDomain
46+ /// </summary>
47+ private static PossibleModelWrapper [ ] GetPossibleModels ( )
48+ {
49+ var possibleModels = ReflectionTool . GetPublicClasses ( typeof ( DbContext ) )
3650 . Where ( type => type != typeof ( DbContext ) && typeof ( DbContext ) . IsAssignableFrom ( type ) &&
3751 ! type . IsAbstract &&
3852 type . GetCustomAttributes < DatabaseTypeSpecificDbContextAttribute > ( ) . Any ( ) )
@@ -41,65 +55,72 @@ public DbContextManager(IConfigManager configManager, ILoggerFactory loggerFacto
4155 var dbTypeAttributes = type . GetCustomAttributes < DatabaseTypeSpecificDbContextAttribute > ( ) ! ;
4256 return dbTypeAttributes . Select ( attr => new
4357 {
44- DbContextType = type ,
45- BaseDbContextType = attr . BaseDbContextType ?? type ,
46- ModelConfiguratorType = attr . ModelConfiguratorType
58+ DbContextType = type , BaseDbContextType = attr . BaseDbContextType ?? type , ModelConfiguratorType = attr . ModelConfiguratorType
4759 } ) ;
4860 } ) . GroupBy ( pc => pc . BaseDbContextType ) . Select ( g =>
4961 {
5062 var modelConfiguratorMap = g . ToDictionary ( x => x . ModelConfiguratorType , x => x . DbContextType ) ;
5163 return new PossibleModelWrapper { DbContext = g . Key , ModelConfiguratorMap = modelConfiguratorMap } ;
5264 } ) . ToArray ( ) ;
5365
54- _configuredModels = _possibleModels
55- . Select ( possibleModel =>
66+ return possibleModels ;
67+ }
68+
69+ /// <summary>
70+ /// This method loads all configured models with support of the <see cref="IConfigManager"/>
71+ /// It matches the configured models with the possible models and creates them
72+ /// </summary>
73+ private ConfiguredModelWrapper [ ] GetConfiguredModels ( )
74+ {
75+ var configuredModels = new List < ConfiguredModelWrapper > ( ) ;
76+ foreach ( var possibleModel in _possibleModels )
77+ {
78+ var config = _configManager . GetConfiguration < DatabaseConfig < DatabaseConnectionSettings > > ( ConfigFilename ( possibleModel . DbContext ) ) ;
79+ Type configuratorType = null ;
80+ Type specificDbContextType = null ;
81+ if ( ! string . IsNullOrEmpty ( config . ConfiguratorTypename ) )
5682 {
57- var config = configManager . GetConfiguration < DatabaseConfig < DatabaseConnectionSettings > > ( ConfigFilename ( possibleModel . DbContext ) ) ;
58- Type configuratorType = null ;
59- Type specificDbContextType = null ;
60- if ( ! string . IsNullOrEmpty ( config . ConfiguratorTypename ) )
83+ var configuredConfiguratorType = Type . GetType ( config . ConfiguratorTypename ) ;
84+ if ( configuredConfiguratorType != null &&
85+ possibleModel . ModelConfiguratorMap . TryGetValue ( configuredConfiguratorType , out specificDbContextType ) )
6186 {
62- var configuredConfiguratorType = Type . GetType ( config . ConfiguratorTypename ) ;
63- if ( configuredConfiguratorType != null && possibleModel . ModelConfiguratorMap . TryGetValue ( configuredConfiguratorType , out specificDbContextType ) )
64- {
65- configuratorType = configuredConfiguratorType ;
66- }
87+ configuratorType = configuredConfiguratorType ;
6788 }
68- else
89+ }
90+ else
91+ {
92+ var defaultMatch = possibleModel . ModelConfiguratorMap . FirstOrDefault ( ) ;
93+ if ( ! defaultMatch . Equals ( default ( KeyValuePair < Type , Type > ) ) )
6994 {
70- var firstOrDefault = possibleModel . ModelConfiguratorMap . FirstOrDefault ( ) ;
71- if ( ! firstOrDefault . Equals ( default ( KeyValuePair < Type , Type > ) ) )
72- {
73- configuratorType = firstOrDefault . Key ;
74- specificDbContextType = firstOrDefault . Value ;
75- }
95+ configuratorType = defaultMatch . Key ;
96+ specificDbContextType = defaultMatch . Value ;
7697 }
98+ }
7799
78- if ( configuratorType == null || specificDbContextType == null )
79- throw new InvalidOperationException ( $ "No valid configurator found for DbContext '{ possibleModel . DbContext . FullName } '") ;
100+ if ( configuratorType == null || specificDbContextType == null )
101+ throw new InvalidOperationException ( $ "No valid configurator found for DbContext '{ possibleModel . DbContext . FullName } '") ;
80102
81- var configType = configuratorType . BaseType . GenericTypeArguments . First ( ) ;
103+ var configType = configuratorType . BaseType . GenericTypeArguments . First ( ) ;
82104
83- var typedConfig = ( IDatabaseConfig ) configManager . GetConfiguration ( configType ,
84- ConfigFilename ( possibleModel . DbContext ) , true ) ;
105+ var typedConfig = ( IDatabaseConfig ) _configManager . GetConfiguration ( configType ,
106+ ConfigFilename ( possibleModel . DbContext ) , true ) ;
85107
86- // If database is empty, fill with TargetModel name
87- if ( string . IsNullOrWhiteSpace ( typedConfig . ConnectionSettings . Database ) )
88- typedConfig . ConnectionSettings . Database = possibleModel . DbContext . Name ;
89-
90- return new ConfiguredModelWrapper
91- {
92- BaseDbContextType = possibleModel . DbContext ,
93- SpecificDbContextType = specificDbContextType ,
94- DatabaseConfig = typedConfig ,
95- Configurator = ( IModelConfigurator ) Activator . CreateInstance ( configuratorType )
96- } ;
97- } ) . ToArray ( ) ;
108+ // If database is empty, fill with TargetModel name
109+ if ( string . IsNullOrWhiteSpace ( typedConfig . ConnectionSettings . Database ) )
110+ {
111+ typedConfig . ConnectionSettings . Database = possibleModel . DbContext . Name ;
112+ }
98113
99- foreach ( var wrapper in _configuredModels )
100- {
101- InitializeConfigurator ( wrapper ) ;
114+ configuredModels . Add ( new ConfiguredModelWrapper
115+ {
116+ BaseDbContextType = possibleModel . DbContext ,
117+ SpecificDbContextType = specificDbContextType ,
118+ DatabaseConfig = typedConfig ,
119+ Configurator = ( IModelConfigurator ) Activator . CreateInstance ( configuratorType )
120+ } ) ;
102121 }
122+
123+ return configuredModels . ToArray ( ) ;
103124 }
104125
105126 /// <inheritdoc />
0 commit comments