Class RowMap<T>
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
Represents a validated, read-only mapping of the properties and constructor parameters of T to the columns that feed them.
public sealed class RowMap<T> : IEnumerable<KeyValuePair<string, string?>>, IEnumerable
Type Parameters
TThe model type whose properties and constructor parameters are mapped.
- Inheritance
-
RowMap<T>
- Implements
- Inherited Members
Examples
public static RowMap<Post> PostMap
=> field ??=
[
new(nameof(Post.Id), "post_id"),
new(nameof(Post.Title), "post_title")
];
var posts = await connection
.Command("select * from posts")
.Map<Post>(PostMap)
.ToListAsync(cancellationToken);
Remarks
Use a map when the columns are named differently from the members.
Declare it once and pass it as the map argument of the query methods (Map<T>, ToChannel<T>, …).
A map is immutable and thread-safe, and caches its compiled row readers. One built at a call site is rebuilt on every call, so declare it once.
Declare it with the collection syntax, RowMap<Post> map = [new(nameof(Post.Id), "post_id")];, or with the constructor, from one pair or several; a map enumerates the pairs it was declared with.
Properties the map does not mention still map by name: a column feeds the property of that name exactly, else ignoring case, else ignoring case and underscores (q_type feeds QType).
A property with a pair is only matched by its column, exactly or ignoring case; a null column means "do not map this property".
A type with no public parameterless constructor is built through its one public constructor (a positional record, a record struct, a class with one constructor), and each parameter is fed like a property. A parameter and a public property whose names differ only in case are one member, fed once, through the constructor, and a pair names it by the property's name (exactly) or by the parameter's name (ignoring case). A parameter no column feeds, or a null column ignores, receives its declared default value, else default; a parameter a column feeds receives the column's value, under the NULL rules below.
A required property must be fed by a column in every result shape, and a constructor parameter's default never feeds it:
when none does, building the row reader throws ValueMappingException (ColumnName is the column looked for),
which happens when the first row of that shape is mapped, so a query with no rows never reports it
(the buffered routes and the Channel and Dataflow methods know the columns before any row, and report it as soon as they have them).
A constructor marked SetsRequiredMembers promises to set them: nothing is enforced then, and they map like any other property.
The pairs are validated when the map is constructed: an unknown or unsettable property or constructor parameter, a member named twice, an empty column name, a null column for a required property, or two members reading one column throw ArgumentException.
A type that cannot be mapped throws InvalidOperationException when the map is constructed or Default is read, which every query method does at the call, even when the result is empty:
- an interface, an abstract class or a delegate;
- a single value such as int or string (use
Map<T>()on a command to read one column); - a dictionary or a dynamic object (
Dictionary<string, object?>, ExpandoObject: they have no properties to set, so useAsDictionaryRows()orMap(selector)); - a type with two or more public constructors and none without parameters;
- a class with no public constructor;
- a type with a required public field (a field is never mapped, so it cannot be set), unless the constructor it is built through is marked
[SetsRequiredMembers]; - a type whose
[Column]and[NotMapped]declarations cannot all be honoured (see below).
A NULL into a member the C# type declares non-nullable is an error: a value type other than Nullable<T> (an enum counts), or a reference type with nullable annotations on (string, not string?).
The row throws ValueMappingException naming the model type and the 0-based row, with the provider's error, if it raised one, as the inner exception.
A member that takes NULL receives null: Nullable<T>, a reference type declared nullable (string?, or [AllowNull] on a property), a reference type in code with nullable annotations off, and a member declared as a type parameter.
A NullPolicyAttribute on a member or on T says what a NULL does instead, whatever the member's type,
and a NullValueAttribute on a member replaces a NULL with its value, over any policy;
a pair or a converter of this map that names a member overrides the member's own declarations (the type's policy still covers it).
A member that declares its column is a hard instruction, and a result that does not say the same throws.
The attributes are those of System.ComponentModel.DataAnnotations.Schema, read by full name:
[Column("name")]reads that column (exactly, else ignoring case; never inferred), and none in the result throws ValueMappingException;[Column(Order = n)]says columnnis the member's (its declared name, else its own name, exactly or ignoring case, never ignoring underscores), and another column there, or too few columns, throws ValueMappingException naming the member, the ordinal and what is there;[NotMapped]leaves the member out: no column feeds it, whatever its name, unless a pair names it.
These are checked once per result set, when the row reader is built, and a result set that fails is checked again the next time it comes back.
A [NotMapped] member that is also [Column], required, or set by a constructor parameter, two members that declare the same ordinal or name, and a declaration with an empty name or a negative ordinal are a type error (InvalidOperationException).
A pair that names a declared member overrides its declarations for that map:
it reads the pair's column, a null column leaves it unread, and a [NotMapped] member it names is mapped; its own Name and Order checks do not apply.
A type error above still throws whatever the map says, and the other members keep their declarations.
A NULL follows the NULL rules above, declared or not.
A column that needs a trim, a parse or a format on its way to a member has a converter:
map.Convert<long, string>(nameof(Order.Number), n => n.ToString("D6")) returns a new map (this one is unchanged)
in which the member receives what the delegate makes of its column, and every other member still maps by name.
The delegate receives the column's own value when the column is a TColumn,
else the value read the way Map<int>() reads a single value: an integer converts only within the range of TColumn, else the row throws ValueMappingException.
A converter is not called for a NULL cell, and the member follows the NULL rules above:
null for a nullable member, ValueMappingException for a non-nullable one.
A converter that must see NULL names a Nullable<T> as TColumn (int?, DateTime?): it is called with null,
and what it returns is the member's value, a non-nullable member's included.
A converter of a reference-type column (text included) is never called with null, whatever its parameter is annotated as.
A converter is not a pair: a map enumerates the pairs it was declared with and the columns Convert was given, never the delegates.
Constructors
- RowMap(IEnumerable<KeyValuePair<string, string?>>?)
Initializes a new instance of the RowMap<T> class from (property name, column name) pairs.
- RowMap(KeyValuePair<string, string?>)
Initializes a new instance of the RowMap<T> class from one (property name, column name) pair.
- RowMap(ImmutableArray<KeyValuePair<string, string?>>)
Initializes a new instance of the RowMap<T> class from (property name, column name) pairs held in an ImmutableArray<T>.
- RowMap(params ReadOnlySpan<KeyValuePair<string, string?>>)
Initializes a new instance of the RowMap<T> class from (property name, column name) pairs.
Properties
- Default
Gets the map for the model's default mapping (property name == column name), used when no map is passed.
Methods
- Convert<TColumn, TMember>(string, Func<TColumn, TMember>)
Returns a new map in which
memberreceives whatconvertmakes of its column, for a column that needs a trim, a parse or a format on its way to the member. Every other member maps as it did, and this map is unchanged.
- Convert<TColumn, TMember>(string, string, Func<TColumn, TMember>)
Returns a new map in which
memberreceives whatconvertmakes ofcolumn: a pair (new(member, column)) with a converter. Every other member maps as it did, and this map is unchanged.
- From(Delegate, params ReadOnlySpan<KeyValuePair<string, string?>>)
Creates a map that builds each
Tby callingfactory, each of its parameters fed by the column of its name (a pair renames one) under the rules of a constructor parameter.
- From(ReadOnlySpan<KeyValuePair<string, string?>>)
Returns Default when
mappingsis empty; otherwise creates a new map from them, exactly as the constructor does.