Skip to content

Instantly share code, notes, and snippets.

@eulerfx
Last active December 13, 2015 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eulerfx/4975076 to your computer and use it in GitHub Desktop.
Save eulerfx/4975076 to your computer and use it in GitHub Desktop.
[<RequireQualifiedAccess>]
module Aggregate
type Aggregate<'TState, 'TCommand, 'TEvent> = {
zero : 'TState;
apply : 'TState -> 'TEvent -> 'TState;
exec : 'TState -> 'TCommand -> 'TEvent;
}
type Id = System.Guid
/// Creates a persistent command handler for an aggregate give load and commit functions.
let makeHandler (aggregate:Aggregate<'TState, 'TCommand, 'TEvent>) (load:Id -> 'TEvent seq, commit:Id * int -> 'TEvent -> unit) =
fun (id,version) command ->
let state = load id |> Seq.fold aggregate.apply aggregate.zero
let event = aggregate.exec state command
event |> commit (id,version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment