StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 9 · Options and Feature Management

Options Change Notifications

medium—No data yet

Problem statement

Complete the implementation of ShippingRateCache so that the rate card it holds follows the shipping settings as they change while the application runs, and so that it reports how many times it has been refreshed.

Sample Data

Program binds the Shipping configuration section to a ShippingOptions, which carries a Carrier, a BaseRate, a PerKilogramRate and an ExpressEnabled. Configuration ships with:

{
  "Shipping": {
    "Carrier": "  Northwind Freight  ",
    "BaseRate": 4.50,
    "PerKilogramRate": 1.20,
    "ExpressEnabled": true
  }
}

The application normalizes the carrier before the settings are handed out: it is trimmed, and a blank one arrives as Unassigned. So the shipping settings in force at startup carry a Carrier of Northwind Freight, not the padded spelling above.

Any of those settings can change while the application is running, and configuration is reloaded when they do.

IRateCardBuilder.Build turns one ShippingOptions into a RateCard, and adds one to its BuildCount, which starts at 0. A card carries the settings' carrier as Carrier, their ExpressEnabled as ExpressAvailable, and three Bands, one for 1, 5 and 20 kilograms, each priced at the base rate plus the per kilogram rate times its weight. The builder also keeps the settings object it was last handed as LastSettings. One builder serves the whole application, and Program registers it.

Service

ShippingRateCache

One ShippingRateCache serves the whole application, and Program already registers it that way. Implement it so that:

  • It builds a card through the registered IRateCardBuilder once, when it is created, and holds on to it. Reading Current hands back the card it is holding, so reading it any number of times builds nothing and, until the next change, answers the same card object every time.
  • RefreshCount starts at 0 and counts the refreshes since the cache was created.
  • When the settings change, the cache rebuilds its card from the settings then in force and adds one to RefreshCount. The rebuild happens as the change is reported rather than the next time Current is read, so BuildCount has already moved before anything reads the card again, and the card the cache holds is a different object from the one it held before.
  • Every reload of configuration counts as one refresh and rebuilds the card, whether the shipping values moved or not. A change to a configuration key the shipping settings do not use, and a change that republishes the values already in force, each add one.
  • A change reaches the cache without a restart and without a new scope: the one ShippingRateCache that answered the old card answers the new one, and every scope reads the same refresh count.
  • Take the settings from the ones the application registers, rather than reading the Shipping section yourself and rather than making your own copy of them. The carrier then arrives normalized as described above, and after a change the builder's LastSettings is the settings object the application is handing out at that moment rather than a copy of it.

GET /shipping/rate-card is provided. It answers 200 with the card the cache is holding and that cache's RefreshCount.

Sample Requests & Responses

GET /shipping/rate-card, on a freshly started application

Response code: 200

{
  "carrier": "Northwind Freight",
  "expressAvailable": true,
  "bands": [
    { "kilograms": 1, "price": 5.70 },
    { "kilograms": 5, "price": 10.50 },
    { "kilograms": 20, "price": 28.50 }
  ],
  "refreshCount": 0
}

Carrying on in that same running application, the same endpoint answers each change with no restart:

  • Changing the shipping settings to a Carrier of Contoso Cargo, a BaseRate of 8.00, a PerKilogramRate of 0.75 and ExpressEnabled off answers an expressAvailable of false, bands of 8.75, 11.75 and 23.00, and a refreshCount of 1.
  • Changing a configuration key the shipping settings do not use answers those same Contoso Cargo values, with a refreshCount of 2.
  • Changing the settings again, to Fabrikam Lines at a BaseRate of 2.25 and a PerKilogramRate of 3.10 with express on, answers bands of 5.35, 17.75 and 64.25 and a refreshCount of 3.

On a freshly started application, changing only the PerKilogramRate to 2.00 leaves the carrier, the base rate and the express flag where configuration put them, so the bands read 6.50, 14.50 and 44.50 under a refreshCount of 1.

More in Options and Feature Management

See the module
Reloadable Feature Settings100%1 of 1 solvedmediumNamed Tenant OptionsThis problem covers giving each tenant its own set of delivery settings, read from that tenant's own part of the configuration, so that one tenant's values never replace another's and the settings the application starts with are left alone.—No data yetmediumFeature Flag ServiceThis problem covers serving feature flags that stay the same for the length of one request, keeping a long lived reader up to date as those flags change, and stopping the application from starting when a flag set carries a value it is not allowed to carry.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.