Skip to content

Instantly share code, notes, and snippets.

@andyhite
Last active December 7, 2017 19:14
Show Gist options
  • Save andyhite/bf8b4e3681c24c3370724ad0149ac738 to your computer and use it in GitHub Desktop.
Save andyhite/bf8b4e3681c24c3370724ad0149ac738 to your computer and use it in GitHub Desktop.
# Is this preferable...
defmodule Invoice do
...
def execute(%Invoice{id: invoice_id} = invoice, %AddLineItem{} = add) do
# Generate %LineItemAdded and %InvoiceTotalUpdated using Commanded.Aggregate.Multi
# The %InvoiceTotalUpdated event would have the new total on it, calculated based on the
# existing invoice total + the new line item's total
end
def apply(%Invoice{} = invoice, %LineItemAdded{} = added) do
# Add line item to the Invoice aggregate's line_items list
end
def apply(%Invoice{} = invoice, %InvoiceTotalUpdated = updated) do
# Update the total for the Invoice aggregate
end
end
# or is this...
defmodule Invoice do
...
def execute(%Invoice{id: invoice_id} = invoice, %AddLineItem{} = add) do
# Generate %LineItemAdded, which has the new line item's data in addition
# to a invoice_total, calculated based on the existing invoice total + the new line item's total
end
def apply(%Invoice{} = invoice, %LineItemAdded{} = added) do
# Add line item to the Invoice aggregate's line_items list, AND update the total
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment