Problem statement
Complete the implementation of ServiceCollectionExtensions to register the invoice number formatter, whose prefix is only known once the application is running.
Sample Data
Configuration ships with an Invoicing section:
{
"Invoicing": {
"RegionCode": "EU",
"SequenceSeed": 41
}
}
Three services are registered as singletons before AddInvoiceNumbering runs:
- IRegionResolver.Resolve answers the configured region code, and adds one to its ResolveCount, which starts at 0.
- ISequenceSeedProvider.Seed answers the configured sequence seed.
- IClock.UtcNow answers the current instant.
Service
InvoiceNumberFormatter
InvoiceNumberFormatter is the provided implementation of IInvoiceNumberFormatter. It is sealed, and its only constructor takes two primitives, a prefix and a seed, so a container has no way to build it from its type alone: it can only be built by code that knows what to pass.
It reports the prefix it was handed as Prefix, and NextNumber answers that prefix, a hyphen, and a running number, which is the seed the first time it is called and one higher on every call after that. A running number is written with at least four digits, padded with leading zeros while it is shorter than that.
ServiceCollectionExtensions
Implement AddInvoiceNumbering, the extension method Program already calls on the service collection:
- Register IInvoiceNumberFormatter exactly once.
- Register it through a delegate the container calls to build it, rather than as a ready made instance or as an implementation type.
- Register it for the lifetime of the application, so one formatter answers every scope, and the running number carries on from one request to the next instead of restarting.
- Build the formatter inside that delegate, out of the three services the delegate's service provider hands it. The prefix is the region code the resolver answers, then a hyphen, then the four digit year of the instant the clock reports. The seed is the one the seed provider answers. None of those three values may be written into the code, so the same registration produces a different prefix under a different region code or a different year, and a different first number under a different seed.
- The delegate runs the first time the formatter is resolved and never again, so the region is resolved exactly once however many invoice numbers are issued afterwards.
Sample Requests & Responses
GET /invoice-numbers/next
The shipped settings carry a region code of EU and a seed of 41, and the run below was taken while the clock read a date in 2026, so the prefix is EU-2026 and the first number issued is 0041.
Response code: 200
{"prefix":"EU-2026","invoiceNumber":"EU-2026-0041"}
Asking again continues the sequence, and other settings move the prefix and the first number with them:
- The second call answers an invoiceNumber of EU-2026-0042, the third EU-2026-0043, and both still report a prefix of EU-2026.
- With a region code of AP, a seed of 900 and a clock reading 2031, the first call answers a prefix of AP-2031 and an invoiceNumber of AP-2031-0900.
- With a seed of 9999, the first call answers EU-2026-9999 and the second EU-2026-10000.
