# `Decree.Commands.Handler`
[🔗](https://github.com/sparkle-space/decree/blob/v0.1.0/lib/decree/commands/handler.ex#L1)

Defines the behaviour a command handler module must implement to support command dispatch.

## Example

An open account handler that delegates to a bank account aggregate:

    defmodule OpenAccountHandler do
      @behaviour Decree.Commands.Handler

      def handle(%BankAccount{} = aggregate, %OpenAccount{} = command) do
        %OpenAccount{account_number: account_number, initial_balance: initial_balance} = command

        BankAccount.open_account(aggregate, account_number, initial_balance)
      end
    end

# `aggregate`

```elixir
@type aggregate() :: struct()
```

# `command`

```elixir
@type command() :: struct()
```

# `domain_event`

```elixir
@type domain_event() :: struct()
```

# `reason`

```elixir
@type reason() :: any()
```

# `handle`

```elixir
@callback handle(aggregate(), command()) ::
  domain_event()
  | [domain_event()]
  | {:ok, domain_event()}
  | {:ok, [domain_event()]}
  | :ok
  | nil
  | {:error, reason()}
```

Apply the given command to the event sourced aggregate.

You must return a single domain event, a list containing the pending events,
or `nil`, `[]`, or `:ok` when no events are produced.

You should return `{:error, reason}` on failure.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
