Problem statement
Complete the implementation of ServiceCollectionExtensions and ExportEngine so that a report is exported by the exporter that belongs to the format it was asked for, and every format the application holds can be listed.
Sample Data
An export job carries rows, and a row carries an id, a label and an amount.
Configuration ships with an Exporting section:
{
"Exporting": {
"PageSize": "A4"
}
}
Two services are registered as singletons before AddExportFormats runs:
- IPageSetupProvider.ReadPageSize answers the configured page size, and adds one to its ReadCount, which starts at 0.
- IClock.UtcNow answers the current instant.
Four exporters ship with the repository. Each one names the format it produces as Format, carries an id fixed when it is built, and shapes the same rows its own way: CsvExporter produces csv, JsonExporter produces json, XmlExporter produces xml, and PdfExporter produces pdf.
PdfExporter is sealed, and its only constructor takes two primitives, a page size and a generated-on stamp, 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 both of them in the first line of what it produces.
The other three are each handed an IRecordWriter<ExportRow>, which names the fields of a record type and reads the values out of one record of it. ReflectionRecordWriter<T> is its one implementation, and it serves every record type. Nothing hands them one yet.
Service
ServiceCollectionExtensions
Program calls AddExportFormats on the service collection while the application is being built. Implement that method so that:
- Asking for IRecordWriter<T> hands back a ReflectionRecordWriter<T> closed over that same T, whichever record type T is. That includes record types nothing here names and record types this application does not declare, so a record type added later needs no further work here. The method adds exactly one registration for it, that registration names no record type in particular, and a fresh writer is built every time one is asked for.
- All four exporters are registered against IExporter, each one under its own format key: csv reaches CsvExporter, json reaches JsonExporter, xml reaches XmlExporter and pdf reaches PdfExporter.
- Each of them is registered once for the whole life of the application, so an exporter is built once and every export in its format runs through that one instance.
- An exporter is reachable only under its own key. Asking for an IExporter without naming a key finds nothing, and asking for every IExporter at once finds nothing either.
- pdf is registered through a delegate the container calls to build it, rather than as a ready made instance or as an implementation type. Build the exporter inside that delegate, out of the two services the delegate's service provider hands it. The page size is the one the page setup provider answers, and the generated-on stamp is the date the clock reports, written as a four digit year, a hyphen, a two digit month, a hyphen and a two digit day. Neither value may be written into the code, so the same registration produces a different stamp under a different page size or a different date.
- That delegate runs the first time pdf is resolved and never again, so the page setup is read exactly once however many exports are produced afterwards.
ExportEngine
Formats answers one entry per exporter the running application holds under a format key. An entry carries that exporter's Format and its id, every such exporter is listed exactly once, and the entries run in order of their format, from A to Z, compared character by character. Exporters registered under keys AddExportFormats does not create are listed the same way.
Export is handed a format and a job, and answers an ExportOutcome holding either a document or an error:
- Treat the format as a key, take the exporter registered under it, and hand it the job. Return an outcome whose Document carries the format that was asked for, the id of that exporter, and what the exporter answered as its payload.
- Take that exporter from the registrations the application already holds rather than building one, so the exporterInstanceId on the document is the id of the registered exporter.
- The key decides, not the exporter's own Format and not a list of names written into the lookup: an exporter is reached by the key it is held under, including a key the four registrations above do not create, and by no other name.
- Match a key exactly, so CSV is not csv and Json is not json.
- A format that matches no key produces an outcome with no Document and an Error of Export format '{format}' is not supported. Supported formats are {formats}., where {format} stands for the format that was asked for and {formats} stands for the formats Formats answers, in that order, separated by a comma and a space.
POST /exports/{format} is provided. It answers 200 with the document when the outcome carries one, and 400 with the error under a message key when it does not. GET /export-formats is provided, and answers 200 with whatever Formats answered under a formats key.
Sample Requests & Responses
POST /exports/csv
Request body:
{ "rows": [ { "id": 1, "label": "Alpha", "amount": 10.50 }, { "id": 2, "label": "Beta", "amount": 4.25 } ] }
Response code: 200
{
"format": "csv",
"exporterInstanceId": "e6ade593-d25e-43a9-95b0-6d06766099e8",
"payload": "id,label,amount\n1,Alpha,10.50\n2,Beta,4.25"
}
The exporterInstanceId is the id its exporter was built with, so it is a different value from one run to the next, and the same value on every export in that format.
The same two rows through the other three formats also answer 200, each with an exporterInstanceId of its own and a payload of its own.
The pdf line below was taken with the shipped page size and a clock reading the ninth of March 2026:
json [{"id":"1","label":"Alpha","amount":"10.50"},{"id":"2","label":"Beta","amount":"4.25"}]
xml <rows><row id="1" label="Alpha" amount="10.50" /><row id="2" label="Beta" amount="4.25" /></rows>
pdf %PDF A4 2026-03-09\n1 Alpha 10.50\n2 Beta 4.25
Under a page size of LETTER and a clock reading the seventeenth of April 2031, for those same rows:
- pdf answers %PDF LETTER 2031-04-17\n1 Alpha 10.50\n2 Beta 4.25.
- The other three formats answer exactly what they answer above.
An application holding an exporter under a key of plain answers POST /exports/plain with that exporter's payload and its id, under a format of plain, whatever Format that exporter names.
GET /export-formats
Response code: 200
{
"formats": [
{ "format": "csv", "exporterInstanceId": "e6ade593-d25e-43a9-95b0-6d06766099e8" },
{ "format": "json", "exporterInstanceId": "e1b447ab-76c9-4a2c-a298-c27a858b8575" },
{ "format": "pdf", "exporterInstanceId": "0f6bea71-a9b9-49e9-9d5c-c4b19d13a3ee" },
{ "format": "xml", "exporterInstanceId": "1fca8d36-2289-4c8b-af4a-1cfd4c7debe1" }
]
}
An application holding a fifth exporter under a key of txt answers those four and that one, in the order csv, json, pdf, txt, xml.
POST /exports/yaml, with a format nothing is registered under:
Response code: 400
{
"message": "Export format 'yaml' is not supported. Supported formats are csv, json, pdf, xml."
}
CSV and Json are answered the same way, each message naming the format it was handed. The same application holding a fifth exporter under txt names csv, json, pdf, txt, xml instead.
