Skip to content
Mark Seemann edited this page Oct 6, 2014 · 5 revisions

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.

Synchronous writes

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

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.