@@ -15,21 +15,35 @@ public static class SchemaHelper
15
15
//private static readonly ConcurrentDictionary<string, List<StoreSchema>> _databaseSchemasCache = new();
16
16
//private static bool _schemasScanned = false;
17
17
//private static readonly object _lock = new();
18
- internal static readonly ConcurrentDictionary < string , Type > _schemaCache = new ( ) ;
18
+ internal static readonly ConcurrentDictionary < Type , IMagicTableBase ? > _schemaCache = new ( ) ;
19
19
20
20
internal static void EnsureSchemaIsCached ( Type type )
21
21
{
22
- string typeKey = type . FullName ! ;
23
-
24
- _schemaCache . GetOrAdd ( typeKey , _ =>
22
+ _schemaCache . GetOrAdd ( type , _ =>
25
23
{
26
- if ( ! typeof ( IMagicTableBase ) . IsAssignableFrom ( type ) )
27
- throw new InvalidOperationException ( $ "Type { type . Name } does not implement IMagicTableBase." ) ;
28
-
29
- return type ; // Cache the type itself
24
+ // Check if the type implements IMagicTableBase
25
+ return typeof ( IMagicTableBase ) . IsAssignableFrom ( type )
26
+ ? ( Activator . CreateInstance ( type ) as IMagicTableBase ) // Store the instance if valid
27
+ : null ; // Store null if it doesn’t implement IMagicTableBase
30
28
} ) ;
31
29
}
32
30
31
+
32
+ public static bool ImplementsIMagicTable ( Type type )
33
+ {
34
+ return type . GetInterfaces ( ) . Any ( i => i . IsGenericType
35
+ && i . GetGenericTypeDefinition ( ) == typeof ( IMagicTable < > ) ) ;
36
+ }
37
+
38
+ public static List < Type > ? GetAllMagicTables ( )
39
+ {
40
+ var assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
41
+ return assemblies
42
+ . SelectMany ( a => a . GetTypes ( ) )
43
+ . Where ( t => t . IsClass && ! t . IsAbstract && SchemaHelper . ImplementsIMagicTable ( t ) )
44
+ . ToList ( ) ;
45
+ }
46
+
33
47
private static readonly ConcurrentDictionary < Type , string > _tableNameCache = new ( ) ;
34
48
private static readonly ConcurrentDictionary < Type , string > _databaseNameCache = new ( ) ;
35
49
@@ -143,16 +157,24 @@ public static StoreSchema GetStoreSchema(Type type)
143
157
PropertyMappingCache . EnsureTypeIsCached ( type ) ;
144
158
EnsureSchemaIsCached ( type ) ;
145
159
146
- if ( ! _schemaCache . TryGetValue ( type . FullName ! , out var cachedType ) )
147
- throw new InvalidOperationException ( $ "Type { type . Name } is not cached and does not implement IMagicTableBase.") ;
160
+ // Retrieve the cached entry
161
+ if ( ! _schemaCache . TryGetValue ( type , out var instance ) )
162
+ {
163
+ throw new InvalidOperationException ( $ "Type { type . Name } is not cached.") ;
164
+ }
148
165
149
- var instance = Activator . CreateInstance ( cachedType ) as IMagicTableBase ;
166
+ // Ensure the type actually implements IMagicTableBase
150
167
if ( instance == null )
151
- throw new InvalidOperationException ( $ "Failed to create an instance of { cachedType . Name } .") ;
168
+ {
169
+ throw new InvalidOperationException ( $ "Type { type . Name } does not implement IMagicTableBase and cannot be used as a Magic Table.") ;
170
+ }
152
171
172
+ // Retrieve table name
153
173
var tableName = instance . GetTableName ( ) ;
154
174
if ( string . IsNullOrWhiteSpace ( tableName ) )
155
- throw new InvalidOperationException ( $ "Type { cachedType . Name } returned an invalid table name.") ;
175
+ {
176
+ throw new InvalidOperationException ( $ "Type { type . Name } returned an invalid table name.") ;
177
+ }
156
178
157
179
var schema = new StoreSchema
158
180
{
@@ -165,7 +187,9 @@ public static StoreSchema GetStoreSchema(Type type)
165
187
. FirstOrDefault ( prop => PropertyMappingCache . GetPropertyEntry ( prop , type ) . PrimaryKey ) ;
166
188
167
189
if ( primaryKeyProperty == null )
190
+ {
168
191
throw new InvalidOperationException ( $ "The entity { type . Name } does not have a primary key attribute.") ;
192
+ }
169
193
170
194
schema . PrimaryKey = PropertyMappingCache . GetJsPropertyName ( primaryKeyProperty , type ) ;
171
195
@@ -183,5 +207,6 @@ public static StoreSchema GetStoreSchema(Type type)
183
207
184
208
return schema ;
185
209
}
210
+
186
211
}
187
212
}
0 commit comments