Commanded v0.19.1 Commanded.Commands.Router behaviour View Source

Command routing macro to allow configuration of each command to its command handler.

Example

Define a router module which uses Commanded.Commands.Router and configures available commands to dispatch:

defmodule BankRouter do
  use Commanded.Commands.Router

  dispatch OpenAccount,
    to: OpenAccountHandler,
    aggregate: BankAccount,
    identity: :account_number
end

Once configured, you can dispatch a command using the module:

:ok = BankRouter.dispatch(%OpenAccount{
  account_number: "ACC123",
  initial_balance: 1_000
})

The to option determines which module receives the command being dispatched. This command handler module must implement a handle/2 function. It receives the aggregate's state and the command to execute. Usually the command handler module will forward the command to the aggregate.

Dispatch command directly to an aggregate

You can route a command directly to an aggregate, without requiring an intermediate command handler.

Example

defmodule BankRouter do
  use Commanded.Commands.Router

  dispatch OpenAccount, to: BankAccount, identity: :account_number
end

The aggregate must implement an execute/2 function that receives the aggregate's state and the command being executed.

Define aggregate identity

You can define the identity field for an aggregate once using the identify macro. The configured identity will be used for all commands registered to the aggregate, unless overridden by a command registration.

Example

defmodule BankRouter do
  use Commanded.Commands.Router

  identify BankAccount,
    by: :account_number,
    prefix: "bank-account-"

  dispatch OpenAccount, to: BankAccount
end

An optional identity prefix can be used to distinguish between different aggregates that would otherwise share the same identity. As an example you might have a User and a UserPreferences aggregate that you wish to share the same identity. In this scenario you should specify a prefix for each aggregate (e.g. "user-" and "user-preference-").

The prefix is used as the stream identity when appending and reading the aggregate's events: "". It can be a string or a zero arity function returning a string.

Consistency

You can choose the consistency guarantee when dispatching a command. The available options are:

  • :eventual (default) - don't block command dispatch while waiting for event handlers

    :ok = BankRouter.dispatch(command) :ok = BankRouter.dispatch(command, consistency: :eventual)

  • :strong - block command dispatch until all strongly consistent event handlers and process managers have successfully processed all events created by the command.

    Use this when you have event handlers that update read models you need to query immediately after dispatching the command.

    :ok = BankRouter.dispatch(command, consistency: :strong)

  • Provide an explicit list of event handler and process manager modules (or their configured names), containing only those handlers you'd like to wait for. No other handlers will be awaited on, regardless of their own configured consistency setting.

    :ok = BankRouter.dispatch(command, consistency: [ExampleHandler, AnotherHandler])
    :ok = BankRouter.dispatch(command, consistency: ["ExampleHandler", "AnotherHandler"])

    Note you cannot opt-in to strong consistency for a handler that has been configured as eventually consistent.

Aggregate version

You can optionally choose to include the aggregate's version as part of the dispatch result by setting include_aggregate_version true.

{:ok, aggregate_version} = BankRouter.dispatch(command, include_aggregate_version: true)

This is useful when you need to wait for an event handler (e.g. a read model projection) to be up-to-date before continuing or querying its data.

Execution results

You can also choose to include the execution result as part of the dispatch result by setting include_execution_result true.

{:ok, execution_result} = Router.dispatch(command, include_execution_result: true)

Or by setting include_execution_result in your application config file:

# config/config.exs
config :commanded, include_execution_result: true

Use this if you need to get information from the events produced by the aggregate but you don't want to wait for the events to be projected.

Metadata

You can associate metadata with all events created by the command.

Supply a map containing key/value pairs comprising the metadata:

:ok = BankRouter.dispatch(command, metadata: %{"ip_address" => "127.0.0.1"})

Link to this section Summary

Functions

Configure the command, or list of commands, to be dispatched to the corresponding handler for a given aggregate.

Define an aggregate's identity

Include the given middleware module to be called before and after success or failure of each command dispatch

Link to this section Functions

Link to this macro

dispatch(command_module_or_modules, opts)

View Source (macro)

Configure the command, or list of commands, to be dispatched to the corresponding handler for a given aggregate.

Link to this macro

identify(aggregate_module, opts)

View Source (macro)

Define an aggregate's identity

You can define the identity field for an aggregate using the identify macro. The configured identity will be used for all commands registered to the aggregate, unless overridden by a command registration.

Example

defmodule BankRouter do
  use Commanded.Commands.Router

  identify BankAccount,
    by: :account_number,
    prefix: "bank-account-"
end
Link to this macro

middleware(middleware_module)

View Source (macro)

Include the given middleware module to be called before and after success or failure of each command dispatch

The middleware module must implement the Commanded.Middleware behaviour.

Middleware modules are executed in the order they are defined.

Example

defmodule BankingRouter do
  use Commanded.Commands.Router

  middleware CommandLogger
  middleware MyCommandValidator
  middleware AuthorizeCommand

  dispatch [OpenAccount,DepositMoney] to: BankAccount, identity: :account_number
end

Link to this section Callbacks

Link to this callback

dispatch(struct, keyword)

View Source
dispatch(struct(), keyword()) ::
  :ok | {:ok, non_neg_integer()} | {:ok, struct()} | {:error, term()}