Last active
August 15, 2018 12:37
-
-
Save slashdotdash/01cd1d03e99b970798c8993c0a19ec59 to your computer and use it in GitHub Desktop.
Commanded multi-entity aggregates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Article do | |
defstruct [:content, comments: []] | |
# public commands | |
def execute(%Article{}, %PublishArticle{content: content}) do | |
%ArticlePublished{content: content} | |
end | |
def execute(%Article{}, %CommentOnArticle{} = comment) do | |
Comment.new() |> Comment.execute(comment) | |
end | |
# state mutators | |
def apply(%Article{}, %ArticlePublished{content: content}) do | |
%Article{content: content} | |
end | |
def apply(%Article{comments: comments} = article %ArticleCommented{} = commented) do | |
comment = Comment.new() |> Comment.apply(commented) | |
%Article{article | | |
comments: [comment | comments] | |
} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Comment do | |
defstruct [:comment] | |
def new, do: %Comment{} | |
def execute(%Comment{}, %CommentOnArticle{comment: comment}) do | |
%ArticleCommented{comment: comment} | |
end | |
def apply(%Comment{}, %ArticleCommented{comment: comment}) do | |
%Comment{comment: comment} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment