Lev Gorodinski
gorodinski.com
@eulerfx
SaaSOvation / Collaboration
Forum Aggregate
public class Forum : EventSourcedRootEntity
{
public void AssignModerator(Moderator moderator)
{
AssertionConcern.AssertArgumentNotNull(moderator, "The moderator must be provided.");
AssertOpen();
Apply(new ForumModeratorChanged(this.tenantId, this.forumId, moderator, this.exclusiveOwner));
}
void AssertOpen()
{
if (this.closed)
throw new InvalidOperationException("Forum is closed.");
}
void When(ForumModeratorChanged e)
{
this.moderator = e.Moderator;
}
}
source
public abstract class EventSourcedRootEntity : EntityWithCompositeId
{
readonly List<IDomainEvent> mutatingEvents;
readonly int unmutatedVersion;
protected void Apply(IDomainEvent e)
{
this.mutatingEvents.Add(e);
When(e);
}
void When(IDomainEvent e)
{
// dynamic dispatch
(this as dynamic).When(e);
}
}
type State = {
moderator : Collaborator;
description : NonEmptyString;
closed : bool; }
type Command =
| AssignModerator of Collaborator
| ChangeDescription of NonEmptyString
type Event =
| ModeratorChanged of Id * Collaborator * ExclusiveOwner
| DescriptionChanged of Id * NonEmptyString * ExclusiveOwner
let apply state = function
| ModeratorChanged (_,moderator,_) ->
{ state with moderator = moderator }
| DescriptionChanged (_,desc,_) ->
{ state with description = desc }
let exec state =
let assertOpen = validator (fun _ -> state.closed <> true)
["Forum must be open."] state
function
| AssignModerator moderator -> assertOpen <?> [ModeratorChanged(...)]
| ChangeDescription desc -> assertOpen <?> [DescriptionChanged(...)]
source
want to learn event sourcing? f(state, event) => state
— gregyoung (@gregyoung) March 17, 2013
let apply state = function
| ModeratorChanged (_,moderator,_) -> { state with moderator = moderator }
| DescriptionChanged (_,desc,_) -> { state with description = desc }
Capture all changes to an application state as a sequence of events.Martin Fowler
Capture allchanges to an application statebehaviors as a sequence of events.
Replay all past events to bring aggregate into current state.
type Aggregate<'TState, 'TCommand, 'TEvent> = {
zero : 'TState;
apply : 'TState -> 'TEvent -> 'TState;
exec : 'TState -> 'TCommand -> 'TEvent; }
let events = load (typeof<'TEvent>,id)
let state = events |> Seq.fold aggregate.apply aggregate.zero
source
let load (typ,id) =
let slice = conn.ReadStreamEventsForward(id,1,Int32.Max,false)
slice.Events
|> Seq.map (fun e -> decode(typ,e.Event.EventType,e.Event.Data))
source
let commit (id,expectedVersion) e =
let eventType,bytes = serialize e
let meta = [||] : byte array
let data = EventData(Guid.NewGuid(),eventType,true,bytes,meta)
if version = 0 then
conn.CreateStream(id, Guid.NewGuid(), true, metaData)
conn.AppendToStream(id,expectedVersion,data)
source
fromCategory('Forum').foreachStream().when({
$init: function() {
return { description: null, moderator: null };
},
"ModeratorAssigned": function (s, e) {
s.moderator = e.body.moderator;
var streamId = "ReadModel-" +
e.streamId.replace("Forum-", "");
var eventType = e.eventType + "_ReadModel";
emit(streamId, eventType, s);
},
"DescriptionChanged": function (s, e) {
s.description = e.body.description;
var streamId = "ReadModel-" +
e.streamId.replace("Forum-", "");
var eventType = e.eventType + "_ReadModel";
emit(streamId, eventType, s);
}
});