Assign gapless invoice numbers in Arc
A shared numbering source lets Cratis Arc assign invoice numbers without gaps or duplicates. With read protection On, read the assigned count, then append the assignment and the invoice event in one guarded batch. A competing assignment rejects the batch without consuming a number.
| Rule | Invoice numbers have no gaps and no duplicates across all invoices |
| Mechanism | A numbering event source ("invoice-numbers") with a projection of the next number, read with IDecisionReads directly in Handle() with a reasoned ARCCHR0040 suppression linked to the tracking feature. The invoice carries the number |
| Alternatives | [Unique] on the number: no duplicates, but gaps are possible |
| Not | A declared Concurrency without an event source id: it is not anchored to a read |
using Cratis.Arc.Chronicle.ReadModels;using Cratis.Arc.Commands.ModelBound;using Cratis.Arc.Validation;using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.Projections.ModelBound;using Cratis.Chronicle.ReadModels;using Cratis.Monads;
[EventType]public record InvoiceNumberAssigned(string Invoice);
public record NextInvoiceNumber( EventSourceId Id, [Increment<InvoiceNumberAssigned>] int Assigned);
[EventType]public record InvoiceIssued(int Number);
public record InvoiceId(string Value) : EventSourceId(Value);
[Command]public record IssueInvoice(InvoiceId Invoice){ public static readonly EventSourceId Numbering = "invoice-numbers";
#pragma warning disable ARCCHR0040 // Cross-source key; https://github.com/Cratis/Arc/issues/3620 tracks acquisition gaps. public async Task<IEnumerable<EventForEventSourceId>> Handle(IDecisionReads reads) { var read = await reads.Get<NextInvoiceNumber>(Numbering.Value); var number = (read.Instance?.Assigned ?? 0) + 1; return [ new(Numbering, new InvoiceNumberAssigned(Invoice.Value)), new(Invoice, new InvoiceIssued(number)) ]; }#pragma warning restore ARCCHR0040}guard invoice-numbers: types=[InvoiceNumberAssigned], after <tail before the read>NextInvoiceNumber counts InvoiceNumberAssigned events on "invoice-numbers". The command adds one to that count and returns two events in the same atomic batch: the numbering source records which invoice received the assignment, and InvoiceIssued records the chosen number on the invoice.
If another assignment commits between the read and this commit, neither event is written. Submit the command again to read the updated count.
What holds
Section titled “What holds”- No two invoices get the same number, and a number is not skipped, when every issue uses this guarded read-and-append path.
- A failed issue writes nothing, so no number is consumed.
What does not hold
Section titled “What does not hold”- A nested command that reads the numbering source after the outer command already staged
InvoiceNumberAssignedis refused withReadOfSourceWithStagedEvents: the read cannot see the staged event. Decide once in the outer command and pass the value down. - All invoices serialize on the numbering source. That is the cost of a gapless sequence. If gaps are acceptable, use
[Unique]on the number.