1

I read Introduction to CQRS article and I find it very useful.

Code in project which follows this article uses InMemoryEventStorage to demonstrate EventStore and adding and retrieving events.

I'm aware that this is for demo purposes only but I will like to see on this example how can be built more productive solution using ravenDb or mssql database.

this InMemoryEventStorage implements

public interface IEventStorage
{
    IEnumerable<Event> GetEvents(Guid aggregateId);
    void Save(AggregateRoot aggregate);
    T GetMemento<T>(Guid aggregateId) where T : BaseMemento;
    void SaveMemento(BaseMemento memento);
}

so once more, how could you build ravenDb or mssql db event storage based on example given at link above?

user1765862
  • 13,635
  • 28
  • 115
  • 220

1 Answers1

1

You can use RavenDB in the normal way - just load and save items through the session:

public class RavenEventStorage : IEventStorage
{
    private IDocumentStore store;

    public RavenEventStorage(IDocumentStore store)
    {
        this.store = store;
    }

    public IEnumerable<Event> GetEvents(Guid aggregateId)
    {
        using(var session = store.OpenSession())
        {
            BaseMemento memento = session.Load<BaseMemento>(aggregateId);

            // Its null if it doesn't exist - so return an empty array
            if(memento==null)
                return new Event[0];

            return memento.Events.AsEnumerable();
        }
    }

    public void Save(AggregateRoot aggregate)
    {
        using(var session = store.OpenSession())
        {
            session.Store(aggregate);
            session.SaveChanges();
        }
    }

    public T GetMemento<T>(Guid aggregateId) where T : BaseMemento
    {
        using(var session = store.OpenSession())
        {
            return session.Load<T>(aggregateId);
        }
    }

    public void SaveMemento(BaseMemento memento)
    {
        using(var session = store.OpenSession())
        {
            session.Store(memento);
            session.SaveChanges();
        }
    }
}
Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61