@@ -8,29 +8,43 @@ namespace Belin.Sql.Reflection;
88/// Provides information about a database table.
99/// </summary>
1010/// <param name="type">The type information providing the table metadata.</param>
11- public sealed class TableInfo ( Type type ) {
11+ public sealed class TableInfo {
1212
1313 /// <summary>
1414 /// The table columns.
1515 /// </summary>
16- public IReadOnlyDictionary < string , ColumnInfo > Columns { get ; } = type
17- . GetProperties ( BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public )
18- . Where ( property => ! property . IsDefined ( typeof ( NotMappedAttribute ) ) && ( ( property . CanRead && property . CanWrite ) || property . IsDefined ( typeof ( ColumnAttribute ) ) ) )
19- . Select ( property => new ColumnInfo ( property ) )
20- . ToFrozenDictionary ( column => column . Name , StringComparer . OrdinalIgnoreCase ) ;
16+ public IReadOnlyDictionary < string , ColumnInfo > Columns { get ; }
2117
2218 /// <summary>
2319 /// The single identity column, if applicable.
2420 /// </summary>
25- public ColumnInfo ? IdentityColumn => Columns . Values . SingleOrDefault ( column => column . IsIdentity ) ?? ( Columns . TryGetValue ( "Id" , out var column ) ? column : null ) ;
21+ public ColumnInfo ? IdentityColumn { get ; }
2622
2723 /// <summary>
2824 /// The table name.
2925 /// </summary>
30- public string Name { get ; } = type . GetCustomAttribute < TableAttribute > ( ) ? . Name ?? type . Name ;
26+ public string Name { get ; }
3127
3228 /// <summary>
3329 /// The table schema.
3430 /// </summary>
35- public string ? Schema { get ; } = type . GetCustomAttribute < TableAttribute > ( ) ? . Schema ;
31+ public string ? Schema { get ; }
32+
33+ /// <summary>
34+ /// Creates new table information.
35+ /// </summary>
36+ /// <param name="type">The type information providing the table metadata.</param>
37+ public TableInfo ( Type type ) {
38+ var table = type . GetCustomAttribute < TableAttribute > ( ) ;
39+ var columns = type
40+ . GetProperties ( BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public )
41+ . Where ( property => ! property . IsDefined ( typeof ( NotMappedAttribute ) ) && ( ( property . CanRead && property . CanWrite ) || property . IsDefined ( typeof ( ColumnAttribute ) ) ) )
42+ . Select ( property => new ColumnInfo ( property ) )
43+ . ToFrozenDictionary ( column => column . Name , StringComparer . OrdinalIgnoreCase ) ;
44+
45+ Columns = columns ;
46+ IdentityColumn = columns . Values . SingleOrDefault ( column => column . IsIdentity ) ?? ( columns . TryGetValue ( "Id" , out var column ) ? column : null ) ;
47+ Name = table ? . Name ?? type . Name ;
48+ Schema = table ? . Schema ;
49+ }
3650}
0 commit comments