Problem statement
Complete the implementation of ServiceCollectionExtensions and TenantSettingsService so that each tenant is answered with the delivery settings of its own configuration section, while the settings the application holds under no name stay as they were.
Sample Data
Configuration ships with a Tenants section holding three sections of the same shape:
{
"Tenants": {
"Default": {
"Carrier": "Standard Post",
"CutoffHour": 12,
"FreeShippingThreshold": 100.00,
"SignatureRequired": false
},
"redcliff": {
"Carrier": "Redcliff Courier",
"CutoffHour": 15,
"FreeShippingThreshold": 250.00,
"SignatureRequired": true
},
"saltmarsh": {
"Carrier": "Saltmarsh Freight",
"CutoffHour": 9,
"FreeShippingThreshold": 40.00,
"SignatureRequired": false
}
}
}
TenantSettings is the shape those sections carry: a Carrier, a CutoffHour, a FreeShippingThreshold and a SignatureRequired. An instance nothing has filled in carries a null carrier, a cutoff hour of 0, a threshold of 0 and no signature.
TenantCatalog.Names holds the tenants this application serves, redcliff then saltmarsh. Default is not one of them: Program binds Tenants:Default to the settings the application holds under no name at all, and that line does not change.
Configuration can change while the application is running, so the values in these sections are not fixed for the life of the process.
Service
ServiceCollectionExtensions
Program calls AddTenantSettings on the service collection while the application is being built. Implement it so that:
- Every tenant in TenantCatalog.Names has settings of its own, held under that tenant's own name and bound to that tenant's section of Tenants. Each tenant therefore carries all four of its own values, and none of the other tenant's.
- Each of those bindings follows its own section for as long as the application runs, so a later change to one tenant's section moves that tenant's settings and leaves every other tenant's where they were, whether one section changes or both change at once.
- The settings held under no name keep the Tenants:Default values that Program bound to them, before and after any tenant's section changes.
TenantSettingsService
Implement Read, which is handed the tenant taken from the route and answers a TenantSettingsOutcome carrying either the settings or an error:
- A name in TenantCatalog.Names is answered with the settings the running application holds under that name, rather than with a fresh reading of that tenant's configuration section. An application that adjusts one tenant's settings after they are bound answers that tenant with the adjustment, and every other tenant without it.
- Names are matched exactly, so Redcliff and SALTMARSH are not tenants, and neither is Default.
- Reading settings under a name nothing was configured under answers an instance nothing has filled in, rather than failing, so a name outside TenantCatalog.Names has to be turned away before any settings are read.
- Such a name is answered with no settings and this error: Tenant '{tenant}' is not configured. Configured tenants are {tenants}., where {tenant} stands for the name that was asked for and {tenants} stands for TenantCatalog.Names, in its own order, separated by a comma and a space.
GET /tenants/{tenant}/settings is provided. It answers 200 with the requested tenant and the four values of its settings when the outcome carries settings, and 404 with the error under a message key when it does not.
Sample Requests & Responses
GET /tenants/redcliff/settings
Response code: 200
{
"tenant": "redcliff",
"carrier": "Redcliff Courier",
"cutoffHour": 15,
"freeShippingThreshold": 250.00,
"signatureRequired": true
}
The other tenant answers 200 the same way:
- GET /tenants/saltmarsh/settings answers Saltmarsh Freight, 9, 40.00 and false.
With Tenants:redcliff changed to Redcliff Overnight, 18, 500.00 and false:
- GET /tenants/redcliff/settings answers those four values.
- GET /tenants/saltmarsh/settings still answers Saltmarsh Freight, 9, 40.00 and false.
- The settings held under no name still carry Standard Post, 12, 100.00 and false.
GET /tenants/atlas/settings
Response code: 404
{
"message": "Tenant 'atlas' is not configured. Configured tenants are redcliff, saltmarsh."
}
Redcliff, SALTMARSH and Default answer 404 the same way, each naming the name it was handed.
