Path: redb/redb.Core/Data/RedbRowMapping.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Data/RedbRowMapping.cs ---- using System; using System.Collections.Generic; using System.Data; using System.Linq.Expressions; using System.Reflection; using System.Text.Json.Serialization; namespace redb.Core.Data { /// /// What the row mappers of the providers (RedbRowMapper, SqlRowMapper) know about a type, built once per type: /// which column names reach which property, and a compiled setter per property. Each provider keeps its own /// conversions; this only replaces the per-cell work around them. A page of 100 objects of 86 values each read /// 8 600 rows of 15 columns, and per cell the mapper looked the column name up again, and set the property by /// reflection: about 50 of the 98 ms of the page. /// /// /// Immutable once built (static readonly, initialised by the type initializer): safe to share between threads. /// Nothing is keyed by SQL text: the columns are bound to properties once per result set by . /// internal static class RedbRowMapping { /// /// Column name -> property, case-insensitive, the rules of the mappers as they were: for every writable public /// instance property its JsonPropertyName (or snake_case name), the same with a leading underscore, the /// property name, its lowercase form, and the lowercase form with a leading underscore. A later property /// claiming a name already taken replaces the earlier one, as the dictionary assignment did before. /// public static readonly Dictionary PropertyMap = BuildMap(); private static readonly Dictionary Slots = BuildSlots(); /// The property of each column of by ordinal, null where no property takes it. public static Slot?[] Bind(IDataRecord record) { var slots = new Slot?[record.FieldCount]; for (var i = 0; i < slots.Length; i++) if (PropertyMap.TryGetValue(record.GetName(i), out var property)) slots[i] = Slots[property]; return slots; } private static Dictionary BuildMap() { var map = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (!prop.CanWrite) continue; var jsonName = prop.GetCustomAttribute()?.Name ?? ToSnakeCase(prop.Name); map[jsonName] = prop; map["_" + jsonName] = prop; map[prop.Name] = prop; map[prop.Name.ToLowerInvariant()] = prop; map["_" + prop.Name.ToLowerInvariant()] = prop; } return map; } private static Dictionary BuildSlots() { var slots = new Dictionary(); foreach (var property in PropertyMap.Values) if (!slots.ContainsKey(property)) slots[property] = new Slot(property, CompileSetter(property)); return slots; } /// /// (obj, value) => obj.Property = (PropertyType)value, or null where that cannot be compiled (an indexer, a /// setter the compiler refuses): such a property keeps the reflection setter. /// private static Action? CompileSetter(PropertyInfo property) { var setter = property.GetSetMethod(nonPublic: true); if (setter == null || property.GetIndexParameters().Length > 0 || typeof(T).IsValueType) return null; try { var obj = Expression.Parameter(typeof(T), "obj"); var value = Expression.Parameter(typeof(object), "value"); var call = Expression.Call(obj, setter, Expression.Convert(value, property.PropertyType)); return Expression.Lambda>(call, obj, value).Compile(); } catch (Exception ex) when (ex is ArgumentException or InvalidOperationException or NotSupportedException) { return null; } } private static string ToSnakeCase(string pascalCase) { if (string.IsNullOrEmpty(pascalCase)) return pascalCase; var result = new System.Text.StringBuilder(); for (var i = 0; i < pascalCase.Length; i++) { var c = pascalCase[i]; if (char.IsUpper(c) && i > 0) result.Append('_'); result.Append(char.ToLowerInvariant(c)); } return result.ToString(); } /// One property of as a column reaches it. internal sealed class Slot { private readonly Action? _setter; internal Slot(PropertyInfo property, Action? setter) { Property = property; TargetType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; _setter = setter; } /// The property. public PropertyInfo Property { get; } /// The property type, or the underlying type of a Nullable property. public Type TargetType { get; } /// /// Sets the property. A value of exactly goes through the compiled setter; any other /// value through as before, which keeps the conversions /// reflection makes on its own (a widening of a primitive, an enum from its underlying value) and its errors. /// public void Assign(T obj, object value) { if (_setter != null && value.GetType() == TargetType) _setter(obj, value); else Property.SetValue(obj, value); } } } }