Chronik v0.1.6 Chronik.Aggregate behaviour View Source
The Chronik.Aggregate
is the base for all aggregates in Chronik.
The module that implements the Chronik.Aggregate
behaviour
module can be configured with a number of options:
shutdown_timeout
indicatesChronik
to shutdown the aggregate GenServer after a number of milliseconds. Defualt value is 15 minutes.snapshot_every
indicates that a snapshot must be done on theChronik.Store
everysnapshot_every
domain events processed. Default value is 100. This configuration is looked up in the:chronik
app under the given module.
Example
defmodule DomainEvents do
defmodule CounterCreated do
defstruct [:id]
end
defmodule CounterIncremented do
defstruct [:id, :increment]
end
end
defmodule Counter do
@behaviour Chronik.Aggregate
alias Chronik.Aggregate
alias DomainEvents.CounterCreated
alias DomainEvents.CounterIncremented
defstruct [:id, value: 0]
# Public API
def create(id), do: Aggregate.command(__MODULE__, id, {:create, id})
def increment(id, increment),
do: Aggregate.command(__MODULE__, id, {:increment, increment})
# Command handlers
def handle_command({:create, id}, nil) do
%CounterCreated{id: id}
end
def handle_command({:create, _id}, _state) do
raise "counter alredy created"
end
def handle_command({:increment, increment}, %Counter{id: id}) do
%CounterIncremented{id: id, increment: increment}
end
# Event handlers
def handle_event(%CounterCreated{id: id}, nil) do
%Counter{id: id}
end
def handle_event(%CounterIncremented{increment: i}, %Counter{} = state) do
update_in(state.value, &(&1 + i))
end
end
The application code must implement the handle_command
and handle_event
callbacks.
Link to this section Summary
Types
The state
represents the state of an aggregate
An aggregate is identified by its module and an id
Functions
The command
function is the entry point to Chronik aggregate.
It sends the cmd
request to the Aggregate identifed by module
and id
.
The timeout
is either :infinity
or a number of milliseconds (defaults
to 5000
)
Start a Chronik.Aggregate
with callbacks on module
with id id
The state(module, id)
function returns the current aggregate state
Callbacks
The handle_command
is the entry point for commands on an aggregate
The handle_event
is the transition function for the aggregate. After
command validation, the aggregate generates a number of domain events
and then the aggregate state is updated for each event with this function
Link to this section Types
The state
represents the state of an aggregate.
Is used in the to validate a command (in handle_command
) and
in handle_event
callback.
An aggregate is identified by its module and an id.
Link to this section Functions
command(module :: module, id :: Chronik.id, cmd :: term, timeout :: :infinity | non_neg_integer) :: :ok | {:error, String.t}
The command
function is the entry point to Chronik aggregate.
It sends the cmd
request to the Aggregate identifed by module
and id
.
The timeout
is either :infinity
or a number of milliseconds (defaults
to 5000
).
The results is either :ok
or {:error, reason}
in case of failure.
start_link(module :: module, id :: Chronik.id) :: {:ok, pid} | {:error, reason :: String.t}
Start a Chronik.Aggregate
with callbacks on module
with id id
.
state(module, Chronik.id) :: Chronik.Aggregate.state
The state(module, id)
function returns the current aggregate state.
This should only be used for debugging purposes.
Link to this section Callbacks
handle_command(cmd :: Chronik.command, state :: state) :: [Chronik.domain_event] | no_return
The handle_command
is the entry point for commands on an aggregate.
The command format is application dependend. Throughout Chronik
,
commands are tagged tuples where the first element is an atom
indicating the command to execute and the remaining elements are arguments
to the command. E.g: {:add_item, 13, "Elixir Book", "$15.00"}
Example
def handle_command({:add_item, id, book, price}, %Cart{}) do
%ItemsAdded{id: id, book: book, price: price}
end
This handle_command
validate the command. If the command is valid on the
given state, the function should return a list (or a single) of domain events.
If the command is invalid the handle_command
should raise an exception.
handle_event(event :: Chronik.domain_event, state :: state) :: state
The handle_event
is the transition function for the aggregate. After
command validation, the aggregate generates a number of domain events
and then the aggregate state is updated for each event with this function.
Note that this function can not fail since the domain event where generated by a valid command.