docs
Systems
Messaging
RequestSystem
Key Components

Coordinator classes

RequestDispatcher

Public class. The single entry point of the system. It owns a RequestTracker and a RequestBus, and is the only class other systems call to send requests or deliver responses.

  • Handlers are registered per domain with Register and removed with Unregister. The dispatcher forwards these calls to the RequestBus.
  • Ask tracks the request and then sends it on the bus.
  • Reply resolves the pending request and invokes its callback. It logs a warning when no pending request matches.
  • It is held by BusRegistry, so systems normally reach it through ServiceLocator.GetBusRegistry().RequestDispatcher.

RequestTracker

Internal class. Holds requests that have been sent but not yet answered, keyed by ID.

  • Track assigns the next ID, sets it on the request, and stores the request.
  • TryResolve removes and returns the request that a response answers.
  • It is used only by RequestDispatcher.

RequestBus

Internal class that extends MessageBus<IRequest, Domain>. It routes each request by its TargetDomain.

  • Send passes the request to the base class's Route method.
  • GetRoutingKey returns the request's TargetDomain.
  • The base class stores the handler per domain, delivers the request when a handler is registered, and queues it otherwise.
  • RequestDispatcher owns it. Other systems never reference it directly.

Data classes

Request

Public sealed class. Carries a target domain, a verb, an optional payload message, and an optional response callback.

  • The Id is set by RequestTracker through SetId and cannot be set by the sender.
  • GetResponseCallback gives the dispatcher access to the callback.
  • The Params bag it inherits from Message can be filled before sending.

Response

Public class. Carries the ID of the answered request, a RequestStatus, and a reason for rejections.

  • Created through Response.Accepted or Response.Rejected, using the request's ID.
  • Passed to the sender's callback, and to RequestDispatcher.Reply.

RequestStatus

Public enum with the values Accepted and Rejected. Read from Response.GetRequestStatus.

Interfaces

IRequest

Public interface describing what a receiving system can read from a request. RequestBus uses TargetDomain as the routing key, and handlers read Verb, Payload, and Params.

IRequestHandler

Public interface with HandleRequest(IRequest req). It is part of the public contract, but RequestDispatcher.Register takes an Action<IRequest> delegate and does not reference it. A system can implement the interface and register its HandleRequest method as the delegate.

Interaction with dependency classes

  • BusRegistry. Holds the RequestDispatcher property, which is how other systems find the request system through ServiceLocator.
  • ServiceLocator. Provides the BusRegistry used by BusUtility and by MessageBus to reach the SignalBus.
  • BusUtility. Send(Request) forwards to RequestDispatcher.Ask. SendMessage(Message) routes Request instances to that method. The source has a TODO to merge this switch into a single send.
  • MessageBus. The base class of RequestBus. It stores handlers, queues messages, logs queueing, and releases queued messages in order when a handler registers.
  • SignalBus. Used by MessageBus for ready signals. RequestDispatcher does not call SetMessageQueueReadySignal, so no ready gating applies to requests through this system.
  • Message and Params. Request derives from Message, which provides TargetDomain and the Params bag. IRequest exposes both.
  • Domain. The routing key for requests, defined in the Contracts system.