Write or convert a handler so the generated entity mapper copies the data — [MapFrom] a message onto an entity graph, [MapTo] a Row/Model/Ref projection, EntityMapper.Map/Upsert/Project. Use when a handler would assign entity.X = message.X by hand, build a new Entity from a message, merge a child collection by key, or project with .Select; when adding a Create/Update/List/Get/Lookup to a kit; or when a build says NSG004 to NSG013 or NSG025.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of New Mapping?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/nsail-ar-new-mapping)
---
name: new-mapping
description: Write or convert a handler so the generated entity mapper copies the data — [MapFrom] a message onto an entity graph, [MapTo] a Row/Model/Ref projection, EntityMapper.Map/Upsert/Project. Use when a handler would assign entity.X = message.X by hand, build a new Entity from a message, merge a child collection by key, or project with .Select; when adding a Create/Update/List/Get/Lookup to a kit; or when a build says NSG004 to NSG013 or NSG025.
---
# New mapping
A handler's copy work is declared on the entity and generated; the handler keeps the
rules. Read `${CLAUDE_PLUGIN_ROOT}/doctrine/data.md`, *Mapping* (the graph rules, one table of calls)
before you declare anything. The mapper is Detached Mapper's graph semantics generated at
compile time: no reflection, nothing saved until the handler's `SaveChanges`.
## The three pieces
1. **The holder, once per Data project.** `Mappings.cs` with
`[Generated(Mappers.Entities)] public static partial void Add{Kit}Mappings(this IServiceCollection services);`,
the generator's analyzer reference in the csproj (the `NSail.Messaging.SourceGenerator`
package with `PrivateAssets="all"`),
and in the project's `Persistence.cs`: `services.Add{Kit}Mappings();` plus
`services.TryAddScoped<EntityMapper>();`. Precedent: `NSail.Accounting.Data`.
2. **The declaration, on the entity.** The entity names the message, never the reverse (the
Sdk that holds the message cannot see the Data types):
- `[MapFrom(typeof(CreateX))]` and `[MapFrom(typeof(UpdateX))]` — writes
- `[MapTo(typeof(XRow))]`, `[MapTo(typeof(XModel))]`, `[MapTo(typeof(XRef))]` — reads
- `[Composition]` on a collection the root owns, `[Parent]` on the child's way back,
`[MapFrom(typeof(XChildModel))]` on the child entity
- `[MapIgnore]` on a member the handler writes by rule; `[MapFrom(typeof(M), "OrganizationId")]`
on the member to say it out loud (NSG004 refuses it by name alone)
- **where the names differ, the entity says so**: `[MapFrom(typeof(M), "Title")]` on the member
written from `M.Title`, `[MapTo(typeof(T), "Heading")]` on the member read as `T.Heading`.
A message member no entity member writes is NSG011, a row member no entity member fills is
NSG012 — both warnings for data lost in silence, and a drop that is on purpose is declared
on the entity's own declaration: `[MapFrom(typeof(M), Ignore = [nameof(M.X)])]`,
`[MapTo(typeof(T), Ignore = [nameof(T.X)])]`
3. **The call, in the handler.**
| Handler act | Call |
|---|---|
| `Create` with a caller-supplied `Id` | `await _mapper.Upsert<CreateX, X>(message, ct)` — `Map` on an `Id` that names no row is `NotFound` |
| `Update` | guards first, then `await _mapper.Map<UpdateX, X>(message, ct)` — a missing row is `NotFound` for free |
| `List`/`Get`/`Lookup` | `_mapper.Project<X, XRow>(query...)` — an expression tree, the store selects only the row's columns |
| a document whose rows cannot be ordered | `Import` |
## What stays in the handler
The mapper copies; it does not decide. Keep, in this order: **guards** (`Ensure*`, uniqueness,
exists), then **the call**, then **what the mapper does not write** — the constants a Create
sets (`IsEnabled = true`, `IsSystem = false`), anything `[MapIgnore]`d — then `SaveChanges`. Never
the stamps: an `IAudited` entity's save writes them (data.md, *Mapping*).
- A guard that needs the row's old state (`channel.IsEnabled` before the merge) loads it first;
the mapper's load returns the same tracked instance and merges onto it.
- A claimed parent the message carries only to be checked (`FromOrganizationId`, `InsurerId`)
is `[MapIgnore]`d: it is verified against the row, never written.
- Side effects that need the old value (`previousPartyId` for a publish) capture it before the call.
The graph rules — aggregations are the boundary of a map and compositions its nodes (a message
carries the **whole** set, never a diff), one message per act (no `Save`, no patch type, no
profiles) — are data.md, *Mapping*.
## What not to convert
Leave hand-written what is a rule and not a copy: credentials and sealed fields (`User`, the
settings handlers), priced or numbered documents (a sale, a voucher, a work order), state
machines and ledgers, writes that split or snapshot rows, orchestration across kits, and any
projection with a subquery, aggregate or merged source (`Select` that computes). A flat
`Select(e => new XRow { Id = e.Id, Name = e.Name })` is a conversion; a `Select` with a
correlated count is not.
## Prove it with a unit test
A mapping is a copy, so a unit test over `HandlerHost` proves it — no browser. For each
converted handler: create-then-get-then-update round trip; for a composition, the set rules
(kept row, new row gets an id, dropped row gone, stamps kept); for an `IVersioned` entity, a
stale version refused — an edit of the compositions alone included, since the root's token
guards its parts (data.md, *Mapping*) — and a zero version not held; a missing row is `NotFound`. Precedents in
`test/Kits/Accounting/NSail.Accounting.WebApi.Tests`: `CurrencyCrudTests`,
`TenderMethodModalitiesTests`, `SalesChannelConcurrencyTests`. A build error is the generator
speaking: each `NSG` code and its message is `NSail.SourceGenerator/Generation/GeneratorDiagnostics.cs`,
and the answers to NSG004, NSG011–NSG013 and NSG025 are data.md, *Mapping*. `NSG011` and `NSG012` are
warnings, not errors: the build is green and the data is gone.