Commanded v1.2.0 Commanded.Aggregate.Multi View Source

Use Commanded.Aggregate.Multi to generate multiple events from a single command.

This can be useful when you want to emit multiple events that depend upon the aggregate state being updated.

Example

In the example below, money is withdrawn from the bank account and the updated balance is used to check whether the account is overdrawn.

defmodule BankAccount do
  alias Commanded.Aggregate.Multi

  defstruct [:account_number, :state, balance: 0]

  def withdraw(
    %BankAccount{state: :active} = account,
    %WithdrawMoney{amount: amount})
    when is_number(amount) and amount > 0
  do
    account
    |> Multi.new()
    |> Multi.execute(&withdraw_money(&1, amount))
    |> Multi.execute(&check_balance/1)
  end

  defp withdraw_money(%BankAccount{account_number: account_number, balance: balance}, amount) do
    %MoneyWithdrawn{
      account_number: account_number,
      amount: amount,
      balance: balance - amount
    }
  end

  defp check_balance(%BankAccount{account_number: account_number, balance: balance})
    when balance < 0
  do
    %AccountOverdrawn{account_number: account_number, balance: balance}
  end
  defp check_balance(%BankAccount{}), do: []
end

Link to this section Summary

Functions

Adds a command execute function to the multi.

Reduce an enumerable by executing the function for each item.

Run the execute functions contained within the multi, returning the updated aggregate state and all created events.

Link to this section Types

Specs

t() :: %Commanded.Aggregate.Multi{aggregate: struct(), executions: [function()]}

Link to this section Functions

Link to this function

execute(multi, execute_fun)

View Source

Specs

execute(t(), function()) :: t()

Adds a command execute function to the multi.

Specs

new(aggregate :: struct()) :: t()

Create a new Commanded.Aggregate.Multi struct.

Link to this function

reduce(multi, enumerable, execute_fun)

View Source

Specs

reduce(t(), Enum.t(), function()) :: t()

Reduce an enumerable by executing the function for each item.

The aggregate apply/2 function will be called after each event returned by the execute function. This allows you to calculate values from the aggregate state based upon events produced by previous items in the enumerable, such as running totals.

Example

alias Commanded.Aggregate.Multi

aggregate
|> Multi.new()
|> Multi.reduce([1, 2, 3], fn aggregate, item ->
  %AnEvent{item: item, total: aggregate.total + item}
end)

Specs

run(t()) ::
  {aggregate :: struct(), [event :: struct()]} | {:error, reason :: any()}

Run the execute functions contained within the multi, returning the updated aggregate state and all created events.