Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify aggregate root requirements #31

Closed
slashdotdash opened this issue Nov 17, 2016 · 0 comments
Closed

Simplify aggregate root requirements #31

slashdotdash opened this issue Nov 17, 2016 · 0 comments
Assignees

Comments

@slashdotdash
Copy link
Member

slashdotdash commented Nov 17, 2016

Remove dependency on eventsourced library and allow Commanded to use any Elixir module that follows these conventions.

Command functions accept the aggregate root state and the command. They return the events that have resulted. This may be zero (nil or []), one, or more.

defmodule Account do
  defstruct [account_number: nil, balance: 0]

  defmodule Deposit, do: defstruct [:account_number, :amount]
  defmodule MoneyDeposited, do: defstruct [:account_number, :amount]
  
  # command function
  def execute(%Account{}, %Deposit{amount: amount}) when amount > 0 do
    %MoneyDeposited{amount: amount}
  end

  # state mutator
  def apply(%Account{balance: balance} = account, %MoneyDeposited{amount: amount}) do
    %Account{account | balance: balance + amount}
  end
end

Tracking raised events, aggregate version, and calling the apply state mutator will be handled by theCommanded.Aggregates.Aggregate process.

The expected execute and apply functions could be specified as a behaviour (e.g. Commanded.Aggregates.AggregateRoot).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant