View Source Commanded.Commands.CompositeRouter (Commanded v1.4.6)

Composite router allows you to combine multiple router modules into a single router able to dispatch any registered command from an included child router.

One example usage is to define a router per context and then combine each context's router into a single top-level composite app router used for all command dispatching.

Example

Define a composite router module which imports the commands from each included router:

defmodule Bank.AppRouter do
  use Commanded.Commands.CompositeRouter

  router(Bank.Accounts.Router)
  router(Bank.MoneyTransfer.Router)
end

One or more routers or composite routers can be included in a Commanded.Application since it is also a composite router:

defmodule BankApp do
  use Commanded.Application

  router(Bank.AppRouter)
end

You can dispatch a command via the application which will then be routed to the associated child router:

command = %OpenAccount{account_number: "ACC123", initial_balance: 1_000}

:ok = BankApp.dispatch(command)

Or via the composite router itself, specifying the application:

:ok = Bank.AppRouter.dispatch(command, application: BankApp)

A composite router can include composite routers.

Summary

Functions

Register a Commanded.Commands.Router module within this composite router.

Functions

Link to this macro

router(router_module)

View Source (macro)

Register a Commanded.Commands.Router module within this composite router.

Will allow the composite router to dispatch any commands registered by the included router module. Multiple routers can be registered.