Skip to content

Commit 4b70325

Browse files
committed
Code optimization
1 parent d05ee06 commit 4b70325

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

src/Sql/Reflection/ColumnInfo.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public sealed class ColumnInfo(PropertyInfo property) {
2727
/// <summary>
2828
/// Value indicating whether the column value is generated by the database.
2929
/// </summary>
30-
public bool IsComputed => databaseGenerated != DatabaseGeneratedOption.None;
30+
public bool IsComputed => databaseGeneratedOption != DatabaseGeneratedOption.None;
3131

3232
/// <summary>
3333
/// Value indicating whether the column value is generated by the database when a row is inserted.
3434
/// </summary>
35-
public bool IsIdentity => databaseGenerated == DatabaseGeneratedOption.Identity;
35+
public bool IsIdentity => databaseGeneratedOption == DatabaseGeneratedOption.Identity;
3636

3737
/// <summary>
3838
/// Value indicating whether the column value is nullable.
@@ -52,7 +52,8 @@ public sealed class ColumnInfo(PropertyInfo property) {
5252
/// <summary>
5353
/// Value indicating how the database generates values for this column.
5454
/// </summary>
55-
private readonly DatabaseGeneratedOption databaseGenerated = property.GetCustomAttribute<DatabaseGeneratedAttribute>()?.DatabaseGeneratedOption ?? DatabaseGeneratedOption.None;
55+
private readonly DatabaseGeneratedOption databaseGeneratedOption =
56+
property.GetCustomAttribute<DatabaseGeneratedAttribute>()?.DatabaseGeneratedOption ?? DatabaseGeneratedOption.None;
5657

5758
/// <summary>
5859
/// The nullability information for the underlying property.

src/Sql/Reflection/TableInfo.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)