Packages
commanded
0.16.0-rc.0
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
lib/commanded/commands/composite_router.ex
defmodule Commanded.Commands.CompositeRouter do
@moduledoc """
Composite router allows you to combine multiple router modules into a single
router able to dispatch any registered command using its corresponding router.
## Example
defmodule ExampleCompositeRouter do
use Commanded.Commands.CompositeRouter
router BankAccountRouter
router MoneyTransferRouter
end
:ok = ExampleCompositeRouter.dispatch(%OpenAccount{account_number: "ACC123", initial_balance: 1_000})
"""
defmacro __using__(_) do
quote do
require Logger
import unquote(__MODULE__)
@registered_commands %{}
@before_compile unquote(__MODULE__)
end
end
defmacro router(router_module) do
quote location: :keep do
for command <- unquote(router_module).registered_commands() do
case Map.get(@registered_commands, command) do
nil ->
@registered_commands Map.put(@registered_commands, command, unquote(router_module))
existing_router ->
raise "duplicate registration for #{inspect command} command, registered in both #{inspect existing_router} and #{inspect unquote(router_module)}"
end
end
end
end
defmacro __before_compile__(_env) do
quote do
@doc false
def registered_commands do
Enum.map(@registered_commands, fn {command, _router} -> command end)
end
@doc false
def dispatch(command), do: dispatch(command, [])
Enum.map(@registered_commands, fn {command_module, router} ->
Module.eval_quoted(__MODULE__, quote do
@doc false
def dispatch(%unquote(command_module){} = command, opts) do
unquote(router).dispatch(command, opts)
end
end)
end)
@doc """
Return an error if an unregistered command is dispatched.
"""
def dispatch(command), do: unregistered_command(command)
def dispatch(command, opts), do: unregistered_command(command)
defp unregistered_command(command) do
Logger.error(fn -> "attempted to dispatch an unregistered command: #{inspect command}" end)
{:error, :unregistered_command}
end
end
end
end