Chain, Command and Observer

Chain, Command and Observer An architecture diagram generated by Archify. POST /orders · SO-4417 · Architecture component POST /orders SO-4417 RateLimiter · 10 a minute for this ip · Chain: whichever guard trips ends it · 429 RateLimiter 10 a minute for this ip 429 Authentication · a bearer token, and whose · Chain: whichever guard trips ends it · 401 Authentication a bearer token, and whose 401 OrderValidation · the basket has lines · Chain: whichever guard trips ends it · 400 OrderValidation the basket has lines 400 PlaceOrderHandler · the end of the line · Chain: whichever guard trips ends it · 201 PlaceOrderHandler the end of the line 201 OrderPlaced · a multicast delegate · Observer: handed to everyone at once OrderPlaced a multicast delegate EmailReceipt · sends the confirmation · Observer: handed to everyone at once EmailReceipt sends the confirmation UpdateStockLevels · decrements the reserved count · Observer: handed to everyone at once UpdateStockLevels decrements the reserved count RefreshDashboard · invalidates the cached totals · Observer: handed to everyone at once RefreshDashboard invalidates the cached totals Outbox · SendOrderConfirmation, pending · Command: the work becomes a row Outbox SendOrderConfirmation, pending Worker · nowhere near the request thread · Command: the work becomes a row Worker nowhere near the request thread Email provider · the confirmation actually leaving · Architecture component Email provider the confirmation actually leaving one request raise OrderPlaced first same transaction claims the row at least once on the caller's thread Chain: whichever guard trips ends it Observer: handed to everyone at once Command: the work becomes a row Legend Caller Your handler Durable row Guard Event Third party

Chain of Responsibility

  • • Registration order is pipeline order, and that is the whole behaviour
  • • RequestLogging sits ahead of RateLimiter, checks nothing and passes on
  • • A tripped guard returns its own status and the handlers after it never run

Command

  • • The row is written in the same transaction as the order
  • • The caller already has its 201 and will never hear about the email
  • • A worker can do the work and die before deleting the row, so delivery is at least once and the handler has to be safe to run twice

Observer

  • • A plain event is a multicast delegate: raising it walks the list in order, on the calling thread
  • • The first subscriber to throw ends the walk, so RefreshDashboard is never called and the exception surfaces at the raise site
  • • GetInvocationList with a try/catch per handler fixes that, and costs you a failure nobody is ever told about