From e101f84bc065c4198777a155d2d7ed04bba69f1a Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:23:08 -0800 Subject: [PATCH] List why a Map has no generated reader, with the fix, in one table The Generated readers section of the mapping page now has one table of the three outcomes of a Map call: the reader is generated; the runtime map is chosen at build time (one row per reason); the runtime map is used for a result set or a type (when). Each row says what to change to get a generated reader, or that there is no fix. The table is built from the generator's code, and a note under it says which rows still apply to a model whose members all declare their column order. The generator's README keeps one pointer to that table in place of its own lists of declines and requirements, so the list has one home. Its notes on known differences and the generator's mechanism stay. Reviews: swarm of lenses on the truth of each row against the generator code and on the form of the table, applied; a last read of the final changes against the code and the anchors. Co-Authored-By: Claude Sonnet 5.5 --- Source/Core/README.md | 2 +- Source/Generator/README.md | 28 ++++-------------- docsite/articles/mapping.md | 59 ++++++++++++++++++++++++++++++------- 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/Source/Core/README.md b/Source/Core/README.md index 81019146..6018c592 100644 --- a/Source/Core/README.md +++ b/Source/Core/README.md @@ -58,7 +58,7 @@ Post[] posts = await connectionFactory ## Generated row readers -This package carries a source generator. With C# 12 or later and the .NET 9.0.300 SDK or Visual Studio 17.14, or newer, a plain `Map()` on a command, a reader or a `DbQuery` gets its row reader written at build time, with nothing to add. A generated reader reads each member with the provider's getter for its type, without the runtime map's conversions, and a model that declares every column's ordinal is read by position, unchecked. To switch it off, set `false` in the project. See [Generated readers](https://open-net-libraries.github.io/Open.Database.Extensions/articles/mapping.html#generated-readers). +This package carries a source generator: a plain `Map()` on a command, a reader or a `DbQuery` gets its row reader written at build time. What it needs, what it changes, when the runtime map reads the rows instead, and how to switch it off are in [Generated readers](https://open-net-libraries.github.io/Open.Database.Extensions/articles/mapping.html#generated-readers). ## Where to go next diff --git a/Source/Generator/README.md b/Source/Generator/README.md index 548a7277..7e413ee6 100644 --- a/Source/Generator/README.md +++ b/Source/Generator/README.md @@ -11,8 +11,8 @@ overload `Map(RowMap?)`, with one map per model type made by Core's hidden en only; not for direct use). `TParams` may be a private type or a type parameter of the caller; `T` may not be. - **Binding.** Core's read loop calls `Bind` once per result set, after the first row is read and before it is mapped (not for a result set with no row to map). Except for a trusted model (below), `Bind` matches the reader's column count and names against a small - cache of layouts (or resolves a new one) and returns that layout's row reader, or `null` for a layout it does not serve: the runtime map then serves - that result set exactly as the default map would. A next result set is bound again, at its own ordinals. `Bind` is thread-safe. The match costs + cache of layouts (or resolves a new one) and returns that layout's row reader. A null answer means the runtime map reads that result set. + A next result set is bound again, at its own ordinals. `Bind` is thread-safe. The match costs about a nanosecond per column, once per result set; a trusted model skips it. - **Each member is read by its own type**, as a hand-written reader reads it: a typed getter (`GetInt32`, `GetString` and so on), an enum its underlying type's getter, `byte[]`, `object`, interfaces and `dynamic` a cast of `GetValue`, and any other type (`DateOnly`, `TimeSpan`, `uint` @@ -48,19 +48,17 @@ only; not for direct use). `TParams` may be a private type or a type parameter o - **A model that declares every column's ordinal is trusted.** When every mapped member has `[Column(Order = n)]`, the generated reader reads those positions and looks at nothing else in the result: no name or count, as a hand-written reader reads. Where another column the getter accepts sits at a position (a swapped or inserted column), it gives that value with no error; a missing column fails the read. The runtime map checks the - declarations. A reader that is not a `DbDataReader`, `object[]` rows and a converted map use the runtime map, which checks. Pinned by tests. + declarations. Pinned by tests. - **A NULL into a non-nullable member** reports the exception of the getter or the cast that read it, where the runtime map may report `SqlNullValueException` with its NULL message: for `byte[]` and an interface `DBNull` does not implement, the generated reader's plain cast, as a hand-written reader casts, reports `InvalidCastException`; a class reads with `GetFieldValue`, which reports the provider's own error, and a provider type with a null value of its own (`SqlInt32` and so on) takes it. Same row, same model. Agreeing would cost the generated hot path a NULL check, or cost the runtime map its clear NULL message. Pinned by tests. -- **NULL policies are not generated.** A model with `NullPolicy` or `NullValue` is declined to the runtime map (below). ## Use A project that references Core, directly or through another Open.Database.Extensions package, has it; there is nothing to add. -`false` switches it off for the project it is set in. It needs C# 12 or later and a compiler of Roslyn 4.14 or -later (.NET SDK 9.0.300, Visual Studio 17.14); an older compiler does not load it. +What it needs, and how to switch it off, is in [Generated readers](https://open-net-libraries.github.io/Open.Database.Extensions/articles/mapping.html#generated-readers). In this repository, a project uses it as an analyzer: @@ -72,23 +70,7 @@ In this repository, a project uses it as an analyzer: ## Declines to the runtime -Not intercepted (the call runs the runtime map): scalars, tuples, anonymous, abstract, open generic or inaccessible types, a model with more than one public constructor -and no parameterless one, overridden or hidden properties, a required member fed by a constructor parameter, a required field or a required property without a public setter (unless the constructor sets the required members), -a `ref struct` property that can be set or a `ref struct` constructor parameter, a constructor, setter, property or type marked `[Obsolete(…, true)]`, -a file-local or extern-alias type, a constructor with an `OverloadResolutionPriority`, a `DataTable` receiver, a call written `x?.Map()`, and a call inside a generic method (`Map()` with an open `T`). -A `dynamic` member is read as an `object` member. A model with a `char` member is not intercepted. A model that declares a NULL policy is not intercepted either, so the policy is never silently ignored: `NullPolicy` on the type or a base type, `NullPolicy` -or `NullValue` on a property or on a constructor or positional record parameter. -Only `Map()` call sites are intercepted, so the output model of `ExecuteOutputAsync()` (output parameters read into a model, one record per call) always uses the runtime map. - -A partial model (it or a base type declared `partial`) is checked once, when first read, against the built type: if another source generator added -constructors or settable properties that this one could not see, the runtime map reads that model. The check uses reflection on the built type, -with no trim or native AOT annotations. It runs in the built program, so it cannot -help the build: where another generator gives a constructor to a partial model that declares none, or adds a `required` property, the generated file -does not compile (CS1729, CS7036 or CS9035). Declare the constructor in your part of the model, or switch the generator off. Attributes another -generator puts on a partial member are not seen either. - -Per result set, handed to the runtime map (never for a trusted model, above): declared columns that do not match (the runtime then throws its own exception), -and a layout that leaves an init-only or required member without a column. Such a result set reports a failed row as the runtime map does, with its row. +Every case the runtime map reads instead of a generated reader, and what to change to get one, is listed in [Generated readers](https://open-net-libraries.github.io/Open.Database.Extensions/articles/mapping.html#generated-readers). ## Tests diff --git a/docsite/articles/mapping.md b/docsite/articles/mapping.md index b4fbc53a..64cdbb6e 100644 --- a/docsite/articles/mapping.md +++ b/docsite/articles/mapping.md @@ -200,7 +200,7 @@ The constructor also takes the pairs as arguments, with no brackets: `Map(new Ro ## Decorated models -A model can say where its columns are, with the attributes of `System.ComponentModel.DataAnnotations.Schema`. A declaration is a hard instruction: when the result does not say the same, reading it throws, so a query that changed shape fails loudly instead of mapping the wrong column. A model that declares nothing is read by name, as above, and so is every undeclared member of a model that does. One case differs: a [generated reader](#generated-readers) for a model that declares every ordinal trusts the declarations and does not check them. +A model can say where its columns are, with the attributes of `System.ComponentModel.DataAnnotations.Schema`. A declaration is a hard instruction: when the result does not say the same, reading it throws, so a query that changed shape fails loudly instead of mapping the wrong column. A model that declares nothing is read by name, as above, and so is every undeclared member of a model that does. One case differs: a [generated reader](#differences-from-the-runtime-map) for a model that declares every ordinal does not check them. [!code-csharp[](../samples/Models.cs#decorated_model)] @@ -219,15 +219,44 @@ The attributes are read by their full name, so a model's own copy of them counts ## Generated readers -The Core package carries a source generator. With C# 12 or later and the .NET 9.0.300 SDK or Visual Studio 17.14, or newer, a plain `Map()` on a command, a reader or a `DbQuery` gets its row reader written at build time. The call and its terminals are the same, and so are the results except as below. Where the generator cannot describe a model or a result exactly, and with an older compiler, the runtime map described on this page reads the rows. A `Map` given a map or a selector is not generated. +The Core package carries a source generator. A plain `Map()` on a command, a reader or a `DbQuery` gets its row reader written at build time. The call and its terminals are the same, and so are the results except as below. -Unless the model declares every column's ordinal, a generated reader matches the columns by name once per result set; it then reads each member with the provider's getter for the member's type, as a hand-written reader does. Three differences follow: - -- **No conversions.** Only what the provider's getter converts is converted: elsewhere the read fails (`ValueMappingException`). What a provider does with a column of another kind (convert, coerce or throw), and the value it then gives, is the provider's: on SQL Server, for one, an `int` member over a `bigint` column fails. The [generator's README](https://github.com/Open-NET-Libraries/Open.Database.Extensions/blob/master/Source/Generator/README.md#known-differences-from-the-runtime-map) lists the conversions the runtime map makes that a generated reader does not. Make the member the column's type, store an enum or a `Guid` as text only for a `string` member, or switch the generator off. -- A model whose mapped members **all** declare `[Column(Order = n)]` is trusted. Its reader takes those positions and looks at nothing else in the result: no declared name or column count is checked. -- A NULL in a non-nullable member is reported with the provider's or the cast's own exception inside the `ValueMappingException`, where the runtime map may report `SqlNullValueException`. +A `Map()` either gets a generated reader or is read by the runtime map described on this page. The runtime map is chosen at build time for the cases marked "At build", once for a model type for "Per type", and for each result set, when its rows arrive, for "Per result set". The last column says what to change to get a generated reader. -The name match is the one thing a generated reader does that a hand-written reader, which assumes its columns' positions, does not. It costs about a nanosecond per column, once per result set, not per row. A model whose members all declare `[Column(Order = n)]` skips it, and its reader then does only what a hand-written reader does. +| Outcome | Case | To get a generated reader | +|---|---|---| +| Generated | `Map()` with no argument on a command, a reader, a `DbQuery`, a `DbQuery`, a `DbQueryCall` (values added) or a `DbQueryUsing` (bound with `Using`), none of the cases below applying | Nothing to change | +| At build | A compiler older than .NET SDK 9.0.300 or Visual Studio 17.14 | Use that SDK or Visual Studio, or newer | +| At build | C# language version below 12 | Use C# 12 or later | +| At build | A project that does not list `Open.Database.Extensions.Generated` in `InterceptorsNamespaces` | Import the package's build targets, or add the namespace to `InterceptorsNamespaces` | +| At build | `OdxGenerateMaps` set to false | Remove the setting | +| At build | `Map` given a map or a selector | No fix | +| At build | `CoreExtensions.Map(x)`, the static form | Write `x.Map()` | +| At build | `x?.Map()` | Check `x` for null first, then call `x.Map()` | +| At build | `Map()` inside a generic method, with `T` open | Call it with a closed type | +| At build | An array as the model | Wrap it in a class or record | +| At build | A `DataTable` receiver | No fix | +| At build | `ExecuteOutputAsync()`: not a `Map()` call | No fix: always the runtime map | +| At build | A single value as the model (a number, `bool`, `string`, `Guid`, a date or time type, an enum, a nullable value type) | No fix: not a model | +| At build | A tuple, an anonymous type, a `ref struct`, a static class, an interface, a delegate or an abstract type as the model | Use a closed class, struct or record | +| At build | A model that is a dictionary (`IDictionary`, `IReadOnlyDictionary`) or a dynamic object (`IDynamicMetaObjectProvider`) | No fix | +| At build | A model, or the type of a member, that generated code cannot name: not accessible to the project, file-local, behind an extern alias, or `[Obsolete(..., true)]` (also the constructor the generator uses, a property or a setter) | Make it accessible to the project (not file-local, not behind an extern alias), or drop the `[Obsolete(..., true)]` | +| At build | A class with no public constructor; or several public constructors and none without parameters | Give it one public constructor, or a parameterless one | +| At build | The constructor the generator uses has a `ref`, `out`, `in` or pointer parameter, or a parameter of a `ref struct` type | Use ordinary parameters | +| At build | A constructor with `[OverloadResolutionPriority]` | Remove the attribute | +| At build | A `required` field; a `required` property without a public setter; or a `required` property a constructor parameter also feeds | Put `[SetsRequiredMembers]` on the constructor the generator reads (the public parameterless one, else the only public one), or drop `required` | +| At build | An overridden, hidden or ref-returning public property | Declare each public property once, without `override` or `new` | +| At build | A public property of a pointer type, or a settable one of a `ref struct` type | Use an ordinary type; a settable property may carry `[NotMapped]` instead, unless it is a pointer | +| At build | A `char` or `char?` member, also with `[NotMapped]` | Use `string` | +| At build | A `NullPolicy` on the model, a base type, a property or a constructor parameter, or a `NullValue` on a property or a constructor parameter | Drop it, or accept the runtime map | +| At build | A column or ordinal declared wrongly: a blank `[Column]` name, a negative `Order`, or two members with one ordinal or one column name | Correct the declaration | +| At build | `[NotMapped]` on a member that cannot be skipped: a `required` settable property, a property beside `[Column]`, or a property a constructor parameter feeds | Remove `[NotMapped]` | +| Per type | A `partial` model that another source generator added constructors or settable properties to: the generated reader declines once per type | Declare the constructors and properties in your own part of the model | +| Per result set | The declared columns or ordinals do not match the result, or a `required` member has no column | Return the declared columns | +| Per result set | An init-only member has no column in the result | Select a column for it | +| Per result set | The reader is not a `DbDataReader` | Read through a `DbDataReader` | + +The "Per type" check reads the built type by reflection when the program runs, with no trim or native AOT annotations, so it cannot help the build: where another generator gives a constructor to a `partial` model that declares none, or adds a `required` property, the generated file does not compile (CS1729, CS7036 or CS9035). Declare the constructor in your part of the model, or switch the generator off. Attributes another generator puts on a `partial` member are not seen either. To switch the generator off for a project (the setting applies to the project it is in: a library built with the generator keeps its generated readers): @@ -237,9 +266,19 @@ To switch the generator off for a project (the setting applies to the project it ``` -The [generator's README](https://github.com/Open-NET-Libraries/Open.Database.Extensions/blob/master/Source/Generator/README.md) lists what is left to the runtime map. +### Differences from the runtime map + +Unless the model declares every column's ordinal, a generated reader matches the columns by name once per result set; it then reads each member with the provider's getter for the member's type, as a hand-written reader does. Three differences follow: + +- **No conversions.** Only what the provider's getter converts is converted: elsewhere the read fails (`ValueMappingException`). What a provider does with a column of another kind (convert, coerce or throw), and the value it then gives, is the provider's: on SQL Server, for one, an `int` member over a `bigint` column fails. The [generator's README](https://github.com/Open-NET-Libraries/Open.Database.Extensions/blob/master/Source/Generator/README.md#known-differences-from-the-runtime-map) lists the conversions the runtime map makes that a generated reader does not. Make the member the column's type, store an enum or a `Guid` as text only for a `string` member, or switch the generator off. +- A model whose mapped members **all** declare `[Column(Order = n)]` is trusted. Its reader takes those positions and looks at nothing else in the result: no declared name or column count is checked, and it skips the name match, so its reader does only what a hand-written reader does. Of the cases in the table, the "At build" ones, the "Per type" one and the reader that is not a `DbDataReader` still apply to it; the "Per result set" column checks do not. +- A NULL in a non-nullable member is reported with the provider's or the cast's own exception inside the `ValueMappingException`, where the runtime map may report `SqlNullValueException`. + +The name match is the one thing a generated reader does that a hand-written reader, which assumes its columns' positions, does not. It costs about a nanosecond per column, once per result set, not per row. + +### Output parameters -Output parameters read into a model (`ExecuteOutputAsync()`, see [Output parameters](expressive-commands.md#output-parameters)) follow the rules of this page: each member is matched to a parameter and the model is built as from a row, always by the runtime map, never a generated reader. Two things apply only there: a `decimal` member needs `[Column(TypeName = "decimal(p,s)")]` for the precision and scale it is sent with, a `DateTime` member is sent as `DbType.DateTime2`, a `DateOnly` as `DbType.Date` and a `TimeOnly` as `DbType.Time`. +Output parameters read into a model (`ExecuteOutputAsync()`, see [Output parameters](expressive-commands.md#output-parameters)) follow the rules of this page: each member is matched to a parameter and the model is built as from a row, always by the runtime map. Two things apply only there: a `decimal` member needs `[Column(TypeName = "decimal(p,s)")]` for the precision and scale it is sent with, a `DateTime` member is sent as `DbType.DateTime2`, a `DateOnly` as `DbType.Date` and a `TimeOnly` as `DbType.Time`. ## Types and NULL