-
Notifications
You must be signed in to change notification settings - Fork 14
Writing events
You write events one at a time using the AtomEventObserver<T> class.
Events can subsequently be read using the FifoEvents<T> or LifoEvents<T> classes.
AtomEventObserver<T> supports both synchronous and asynchronous writes. Synchronous writes offer the advantage that you can treat AtomEventObserver<T> as an IObserver<T>:
IObserver<IUserEvent> obs = new AtomEventObserver<IUserEvent>(
eventStreamId, // a Guid
pageSize, // an Int32
storage, // an IAtomEventStorage object
serializer); // an IContentSerializer object
var userCreated = new UserCreated
{
UserId = eventStreamId,
UserName = "ploeh",
Password = "12345",
Email = "ploeh@fnaah.com"
};
obs.OnNext(userCreated);It's not necessary to explicitly declare obs as IObserver<IUserEvent>: you can use the var keyword as well; this example just uses explicit variable declaration in order to make it clearer what's going on.
When the call to obs.OnNext returns, the userCreated event has been written to storage.
The storage variable can be any IAtomEventStorage implementation.
The serializer variable can be any IContentSerializer implementation.
Asynchronous writes can be done using the standard Task Parallel Library (TPL) model for asynchrony:
var obs = new AtomEventObserver<IUserEvent>(
eventStreamId, // a Guid
pageSize, // an Int32
storage, // an IAtomEventStorage object
serializer); // an IContentSerializer object
var userCreated = new UserCreated
{
UserId = eventStreamId,
UserName = "ploeh",
Password = "12345",
Email = "ploeh@fnaah.com"
};
await obs.AppendAsync(userCreated);Notice that since AtomEventObserver<T> uses the standard TPL model, you can use it with async and await.
When the task returned by obs.AppendAsync completes, the userCreated event has been written to storage.
The storage variable can be any IAtomEventStorage implementation.
The serializer variable can be any IContentSerializer implementation.