Commanded v1.0.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
  defstruct [
    account_number: nil,
    balance: 0,
    state: nil,
  ]

  alias Commanded.Aggregate.Multi

  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

Link to this type

t()

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

Link to this section Functions

Adds a command execute function to the multi.

Create a new Commanded.Aggregate.Multi struct.

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)
Link to this function

run(multi)

View Source
run(Commanded.Aggregate.Multi.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.