StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 1 · Dependency Injection

Notification Dispatch Center

easy50%1 of 2 solved

Problem statement

Use everything from this module in one app: send a message through a collection of channels, count every delivery in a shared counter, and tag each request with its own id.

Requirements

The channels, the formatter, the counter, the request context and the DispatchController are already provided. Your task has two parts: register the services in NotificationDispatchApi/Services/ServiceRegistration.cs, and write the constructor and the loop of NotificationDispatcher in NotificationDispatchApi/Services/NotificationDispatcher.cs.

ServiceRegistration

AddApplicationServices registers:

  • INotificationChannel with EmailChannel, SmsChannel and PushChannel, in that order, using AddScoped
  • IDeliveryCounter with DeliveryCounter, using AddSingleton, so the totals keep growing across requests
  • IRequestContext with RequestContext, using AddScoped, so each request has one RequestId that everything in it shares
  • INotificationDispatcher with NotificationDispatcher, IMessageFormatter with MessageFormatter, and IMessagePrefixProvider with MessagePrefixProvider, using AddScoped. Each channel receives the formatter, and the formatter receives the prefix provider, through their constructors

NotificationDispatcher

  • The constructor receives IEnumerable<INotificationChannel>, IDeliveryCounter and IRequestContext and keeps them in the three fields. The dispatcher uses only these and never creates them with new
  • Dispatch goes through channels in order. For each channel, it:
    • calls Send(message) once
    • sets the RequestId of the returned delivery to the request context's RequestId
    • calls Record on the counter with the channel's Name
    • adds the delivery to deliveries

The blank-message check and the return are already written.

DispatchController (provided)

  • POST /dispatch answers 200 with the dispatch result, plus controllerRequestId: the controller's own RequestId, which matches requestId. A missing or blank message answers 400
  • GET /deliveries answers 200 with the total and the count for each channel
  • GET /context answers 200 with the current RequestId
Sample Requests & Responses

Your ids will be different.

POST /dispatch

Request body:

{ "message": "server restarted" }

Response (200 OK):

{
  "requestId": "556f5e40-3060-43b8-99e6-87846552dc08",
  "controllerRequestId": "556f5e40-3060-43b8-99e6-87846552dc08",
  "message": "server restarted",
  "channelCount": 3,
  "deliveries": [
    { "channel": "Email", "message": "server restarted", "formattedMessage": "[dispatch] server restarted", "requestId": "556f5e40-3060-43b8-99e6-87846552dc08" },
    { "channel": "Sms", "message": "server restarted", "formattedMessage": "[dispatch] server restarted", "requestId": "556f5e40-3060-43b8-99e6-87846552dc08" },
    { "channel": "Push", "message": "server restarted", "formattedMessage": "[dispatch] server restarted", "requestId": "556f5e40-3060-43b8-99e6-87846552dc08" }
  ]
}

A second POST /dispatch gets a new requestId, for example 22644990-e9df-4e4b-b448-83e2f6fc276f.

GET /deliveries (after the two dispatches above)

Response (200 OK):

{
  "total": 6,
  "perChannel": [
    { "channel": "Email", "count": 2 },
    { "channel": "Push", "count": 2 },
    { "channel": "Sms", "count": 2 }
  ]
}

POST /dispatch with { "message": " " }

Response (400 Bad Request):

{ "error": "message is required" }

More in Dependency Injection

See the module
Request Tracking Service LifetimesThis problem covers choosing how long each service should live, so a counter shared by the whole application keeps counting while a value that belongs to one request starts fresh every time.33%3 of 9 solvedeasyLayered Discount Pricing APIThis problem covers applying a chain of discount rules to a price one after another, with the rules coming from the container as a set rather than being listed by hand, and the order they were registered in deciding the order they apply.100%2 of 2 solvedeasyOrder Service Constructor GraphThis problem covers letting the container build a whole chain of collaborating objects, so a service and everything beneath it arrives ready to use instead of being constructed by hand at the top.100%1 of 1 solvedeasy

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.