Skip to content

Instantly share code, notes, and snippets.

@yreynhout
Created October 22, 2012 20:26
Show Gist options
  • Select an option

  • Save yreynhout/3933919 to your computer and use it in GitHub Desktop.

Select an option

Save yreynhout/3933919 to your computer and use it in GitHub Desktop.
Seek simplicity & loose coupling between aggregate root and event builder.
public interface IMessageBuilder {
object Build();
}
public interface IEventBuilder : IMessageBuilder {
IMessageBuilder IdentifiedBy(Guid id, int version);
}
public abstract class AggregateRootEntity {
//...
protected void ApplyEvent(IEventBuilder builder) {
//Explicit builder contract
var @event = builder.IdentifiedBy(_id, _version++).Build();
Play(@event);
Record(@event);
}
void Play(object @event) { _handlers[@event.GetType()](@event); }
void Record(object @event) { _changes.Add(@event); }
//...
}
public abstract class AggregateRootEntity {
//...
protected void ApplyEvent(Func<Guid, int, object> builder) {
//Func-style builder contract
var @event = builder(_id, _version++);
Play(@event);
Record(@event);
}
void Play(object @event) { _handlers[@event.GetType()](@event); }
void Record(object @event) { _changes.Add(@event); }
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment