Migrate generated identifiers in Cratis Arc
To make server-owned identifiers controllable in C# Cratis Arc command specs, move generation from inside
Handle() to a [GeneratedValue] parameter and pin that parameter by name. The identifier stays out of
the client’s request, while each scenario can choose the value it needs to assert.
The APIs, analyzer diagnostics, and migration commands below are C#-specific. Other backends may express server-owned values differently; check their capability references before applying this migration.
Use this guide when a handler creates an identifier inline or still depends on the removed
IIdentitySource API. If you are replacing queued identities, also follow
replace identity-source queues with named pins.
This guide assumes an Arc command and an identifier that implements IGeneratable<TSelf> from
Cratis.Fundamentals 7.23.0 or later. See
declaring a server-owned identifier
for the CartId declaration used below. The before and after code is the StartCartWithInlineGeneration and
StartCart pair in the
Generated Values Shop sample.
See the diagnostic
Section titled “See the diagnostic”Find handlers that create server-owned identifiers inside Handle(). This command is a small example:
using Cratis.Arc.Commands.ModelBound;
namespace Shop.Carts.Starting;
[Command]public record StartCartWithInlineGeneration{ public CartId Handle() => CartId.New();}ARC0028 identifies the inline call. Its default severity is Info; the shop sample reports it as a warning:
Shop/Carts/Starting/StartCartWithInlineGeneration.cs(22,31): warning ARC0028: Move inline generation of 'CartId' to a [GeneratedValue] Handle parameter so scenarios can pin itApply the quick fix
Section titled “Apply the quick fix”In your editor, choose Move to a [GeneratedValue] parameter on the diagnostic. For a supported single expression it adds the parameter and replaces the call:
using Cratis.Arc.Commands.ModelBound;
namespace Shop.Carts.Starting;
[Command]public record StartCart{ public CartId Handle([GeneratedValue("cart")] CartId cartId) => cartId;}The fix derives a parameter name. Give the generated value an explicit name such as cart, as shown
above, so scenario pins survive a C# parameter rename. Arc now calls CartId.New(), validates the value
and supplies it to Handle(). The value stays outside the command’s request properties.
Update any direct callers of the handler yourself; the fix does not change them. Keep the factory on the identifier type so the same generation strategy applies wherever Arc resolves the parameter.
The fix recognizes X.New(), GenerateValue.*(...) and Guid.NewGuid() converted or wrapped into a
concept that implements a generation contract. A raw primitive cannot become a generated parameter. It does
not move generation inside loops, branches or nested functions, and it does not carry over nonconstant
strategy arguments. Migrate those by hand: moving a per-iteration or conditional allocation into a Handle
parameter changes when and how many values are created. Review the concept’s factory too, because it must
own the strategy the handler used before.
Keep identifiers the client supplies, such as an existing cart to update, on command properties. They are not generated values.
Move time-dependent strategies to the context contract
Section titled “Move time-dependent strategies to the context contract”If your handler read the clock to build the value, such as a time-ordered order identifier, do not
carry that read into New(). Implement IGeneratableInContext<TSelf> with New(GenerationContext)
instead, as the shop’s OrderId does with GenerateValue.TypeId("order", context). Follow
context-dependent generation
for the full declaration. Arc passes the command’s ReceivedAt and the mapped tenant.
If both interfaces remain on the type, Arc uses only the context form and
ARC0027 warns about the plain one. Manually constructed
contexts need a received time; a default time fails rather than using UtcNow.
Update your specs
Section titled “Update your specs”After moving generation to a parameter, update scenarios that execute the command. A pin supplies a
typed value under the name in [GeneratedValue]; without one, a scenario rejects generation by default.
Set the pin before the first Execute, then assert that the handler returns it:
using Shop.Carts.Starting;
namespace Shop.Specs.Carts.Starting.for_StartCart;
public class when_starting_a_cart : Specification{ readonly CommandScenario<StartCart> _scenario = new(); readonly CartId _cartId = new(Guid.Parse("aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee")); CommandResult _result = null!;
void Establish() => _scenario.PinGeneratedValue("cart", _cartId); async Task Because() => _result = await _scenario.Execute(new StartCart()); void Destroy() => _scenario.Dispose();
[Fact] void should_succeed() => _result.ShouldBeSuccessful(); [Fact] void should_return_the_pinned_cart_id() => ((CommandResult<CartId>)_result).Response.ShouldEqual(_cartId);}Apply the same changes to the rest of your specs:
- An unpinned generated parameter fails the command with a
GeneratedValueNotPinnedoutcome. Pin it, or callAllowGeneration()when the exact value is irrelevant. - Pins and generation policy must be set before the first
Execute. - For a context-dependent concept, use
ClockandTenantas in fix the clock and tenant, and configureClockbefore the firstExecuteorValidate. - A fixed clock does not make a random suffix deterministic. Keep pinning the whole value when an assertion depends on it.
Replace identity-source queues with named pins
Section titled “Replace identity-source queues with named pins”Arc v22.65.3 removed IIdentitySource and CommandScenario.Generate(Guid[]). Remove the identity-source
dependency and its calls from your handler and use a generated parameter. If your identifier does not yet
implement a generation contract, add IGeneratable<TSelf> and its static New() strategy, or use
IGeneratableInContext<TSelf> when the strategy needs the command’s time or tenant.
Replace queued raw Guids in specs with typed, named pins. With AllocateCartId and CartId from the
declaration linked above, this fragment goes in an async spec that imports Cratis.Arc.Commands,
Cratis.Arc.Testing.Commands and Cratis.Specifications:
using var scenario = new CommandScenario<AllocateCartId>();var cartId = CartId.New();scenario.PinGeneratedValue("cart", cartId);var result = await scenario.Execute(new AllocateCartId());result.ShouldBeSuccessful();scenario.GeneratedValues.Last<CartId>("cart").ShouldEqual(cartId);A pin identifies the command type and the generated name, instead of relying on the order in which
handlers consume a queue. It supplies the same value on every execution of that command in the scenario.
Use separate scenarios when executions need different values, or AllowGeneration() when their exact values
do not matter.
Check concept rules on generated values
Section titled “Check concept rules on generated values”Generated values, including scenario pins, now fail with GeneratedValueRejected if any concept rule
fails, including Warning and Information rules. Previously, those severities allowed execution. Review
your generation strategies and pins so they satisfy every concept rule; a rejected value is a server
defect, not a request validation failure. Validation of caller-supplied input is unchanged.
Check the migration
Section titled “Check the migration”Before treating the migration as complete, check the affected commands and scenarios:
- The new identifier is a
[GeneratedValue]Handle parameter, not a client-supplied command property. - Direct handler callers pass the new parameter, and no handler still depends on
IIdentitySource. - Each scenario pins values before execution or explicitly calls
AllowGeneration(). Assertions that depend on an exact value still use a pin, even with a fixed clock. - Pinned and normally generated values satisfy every concept rule. Investigate
GeneratedValueRejectedin the factory or pin rather than treating it as invalid client input.
Next steps
Section titled “Next steps”- Pin generated values in command specs covers multiple parameters, nested commands and reading all captures.
- Generated values explains resolution, validation and when not to use
[GeneratedValue].