Skip to content

Instantly share code, notes, and snippets.

@perokvist
Last active August 29, 2015 14:18
Show Gist options
  • Save perokvist/7eaf14b2b643b40469ff to your computer and use it in GitHub Desktop.
Save perokvist/7eaf14b2b643b40469ff to your computer and use it in GitHub Desktop.
Commit log examples
public class Module
{
public static Func<ICommand, Task> Run(CancellationToken token, Subject<IEvent> eventSubject, Action<string> logger)
{
const string streamName = "the-stream";
var commitLog = new MessageVaultClientAdapter(new MemoryClient(), logger);
var dispatcher = new Dispatcher<ICommand, Task>();
var eventHandlers = new Dispatcher<IEvent, Task>();
var checkpointWriter = new CheckpointWriterAdapter(new MemoryCheckpointReaderWriter());
var repository = new InMemoryStateRepository<State>();
var streamProcessor = new StreamProcessor(commitLog, streamName, checkpointWriter, Serialization.Deserializer(), Console.WriteLine);
var eventLog = commitLog.ToStreamPoster(Serialization.Serializer());
dispatcher.Register<IYourAggregateCommand>(
command => ApplicationService.Execute(
command,
id => repository.GetOrCreateAsync(id),
state => new YourAggregate(state),
aggregate => aggregate.Handle(command),
eventLog.ForStream(streamName)));
eventHandlers.Register<IEvent>(@event => StateEventHandler.ForAsync(@event, (e, s) => s.When(e), repository));
streamProcessor.Run(token, @event => eventSubject.OnNext(@event));
eventSubject.Subscribe(@event => eventHandlers.Dispatch(@event).Wait(), () => { }, token);
return Mixins.WaitForEventsPublished(dispatcher.Dispatch, eventSubject, logger);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment