AGENT SKILL

new-mapping

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.

License 许可
MIT
最近更新
2026-11-14
Stars
★ 4
Forks
0

01 前言 Front Matter

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.

03 SKILL.md 正文

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 NSail.SourceGenerator analyzer reference in the csproj (OutputItemType=Analyzer), 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 entity with CreatedAt and UpdatedAt implements IAudited and the save writes them (an entity not yet on IAudited gets it as part of converting its handler).

  • 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.

Graph boundaries

Aggregations are the boundary of a map; compositions are its nodes. A member typed as an entity (or a CustomerId) is attached by key and never written: an UpdateInvoice cannot rewrite the InvoiceType it points at. A [Composition] is the root's own: matched by key, what the message no longer carries is deleted, a child with no key (Guid.Empty) is created with a new id. So a message carries the whole set of a composition, never a diff.

One message per act

C# has no undefined, so null cannot mean "leave it". The house answer is Detached's first one: small specific messages, one per act (RetargetTenderMethod carries one field), never a Save and never a patch type. Create and Update are separate messages with their own [MapFrom], which is also the answer to Detached's profiles: what differs between them (a constant the create sets) is the handler's step after the call. The audit stamps are not: an IAudited entity's save writes them.

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: NSG004 an organization axis mapped by name alone, NSG005 a [Primitive] of another type, NSG006 a member with no conversion, NSG007 no parameterless constructor, NSG008 a [MapFrom] that names a non-class, NSG009 a projection that cannot carry a derived type, NSG010 an open generic, NSG013 an Ignore naming a member that does not exist, NSG025 a [MapFrom] reading a nullable into a non-nullable member that is not the key (answer: make a side nullable or non-nullable, [MapIgnore] the member in both directions, or Ignore = [nameof(M.X)] on the [MapFrom] to drop it from that pairing only, so [MapTo] projections still read it). NSG011 and NSG012 are warnings, not errors: the build is green and the data is gone.

04 同仓库更多 Skills

nsail-ar
nsail-ar/nsail-stack

test-harness

Write or run tests against the NSail harnesses — which layer owes the proof (Architecture, Stack, kit WebApi.Tests on Postgres, bUnit, Apps, browser E2E), HandlerHost, the Release-build E2E host, Api.Send, culture and viewport. Use for a kit, bUnit or E2E test, deciding whether a fix earns a regression test, a flaky or instantly-failing suite, or reporting test totals.

★ 4
nsail-ar
nsail-ar/nsail-stack

new-message

Add an NSail operation end to end — a message in the Sdk ([Http] route, validation), its IHandler in WebApi, the Row/Ref/Model projection, BusinessException failures and strings; endpoint, DI and client are generated. Use for a query or command such as ListInvoices or CreatePayment, when choosing a verb (there is no Save), or on SenderNotFound.

★ 4
nsail-ar
nsail-ar/nsail-stack

new-icon

Add an icon to an NSail icon catalog (NsIcons, a kit's or app's like ProductsIcons or OpticalIcons, a Stack library's own) from a Material Symbols path. Use when a screen, menu entry, button or field needs a glyph no catalog has, instead of Icons.Material.*, or when a pasted icon renders as nothing or a dot. Covers the 960-grid viewBox transform, catalog choice and sizing.

★ 4
nsail-ar
nsail-ar/nsail-stack

doctrine

The Stack's architecture — which page to read before touching a message, handler, entity, migration, tenancy, permission, screen, Ns* component, test, React client, localization key or project layout. Use before editing code that builds on the Stack, when a CLAUDE.md routes the Stack's doctrine, or when asked how the Stack wants something done.

★ 4
nsail-ar
nsail-ar/nsail-stack

convert-to-nsload

Convert a screen's data read into an NsLoad region (OnLoad, Reload, Retry, NsPartial.Handoff for dashboard cards). Use whenever you touch a Blazor screen or card that loads in OnCreatedAsync, OnParametersSetAsync or OnInitializedAsync, or when a load failure shows as a toast, a card reads twice, or an empty state lies about a failed read.

★ 4