FlowControllerSystem Architecture
Design patterns in use
- Service Locator:
FlowControllergets theCommandBusthroughServiceLocator.GetBusRegistry().CommandBus. The lookup happens on each access to the privatecommandBusproperty, not once at construction. - Base class with abstract hook (template-method style): The base class provides
Init, which performs the registration step. Subclasses supply the domain identity (Domain) and the handler (HandleCommand). Subclasses can overrideInit, but they must callbase.Init(). - Routing by key: Registration is keyed by
Domain. TheCommandBusroutes each command to the single handler registered forICommand.TargetDomain. The flow controller does not check the domain itself.
Connections to other systems
- InitializationManager and InitializationOrchestrator: Subclasses are created in the
InitializationManagerRegisterSystemsmethod, usually throughSafeRegister. BecauseFlowControllerimplementsIInitializableand is not an event receiver, it is placed in the sync initializables list. The orchestrator callsIniton it duringStart, after the event receivers have run. - ServiceLocator:
InitializationManagercallsServiceLocator.InitializeinAwake, beforeRegisterSystems.Initruns later, so the bus registry is available whenCommandBus.Registeris called. - CommandBus:
InitcallsRegister(Domain, HandleCommand)on the bus. Commands sent withSendare routed by theirTargetDomain. Per theCommandBusdocumentation, commands are queued until a handler is registered and ready. - ICommand:
HandleCommandreceives anICommand. The controller uses itsVerbandParamsto decide what to do. - Domain enum: The
Domainvalue identifies each controller. Each value in theDomainenum names one system, so each subclass should return a unique value.
Key concepts and terminology
- Domain: The system that owns a group of commands. It is the routing key for both registration and dispatch.
- Command: An
ICommandaddressed to a domain. It carries a verb and params. - Flow controller: A subclass of
FlowControllerthat owns one domain and handles its commands. - Registration: The
Initstep that connects a controller'sHandleCommandto its domain on the command bus.
Event and data flow
ServiceLocatoris initialized with the bus registry duringInitializationManager.Awake.RegisterSystemscreates the flow controller instances.- During
Start, the orchestrator callsIniton each controller. Each controller registers itsHandleCommandwith the command bus for itsDomain. - A sender creates an
ICommandand sends it on the command bus. - The command bus uses
TargetDomainto find the registered handler and callsHandleCommandon the matching controller. HandleCommandprocesses the command. The subclass decides how to act onVerbandParams.
Thread safety
Init runs on the main thread, during the synchronous part of initialization. FlowController has no thread-safety measures of its own, so send commands from the main thread.