private async Task BuildEventStore(IServiceCollection services)
        {
            //Create EventStore Connection
            var eventStoreConnection = EventStoreConnection.Create(
                Configuration["EventStore:ConnectionString"],
                ConnectionSettings.Create()
                .KeepReconnecting()
                .EnableVerboseLogging()
                .SetHeartbeatInterval(TimeSpan.FromMilliseconds(5 * 1000))
                .UseDebugLogger(),
                Environment.ApplicationName
                );

            eventStoreConnection.Connected += (sender, args)
                                              => Console.WriteLine($"Connection to {args.RemoteEndPoint} event store established.");

            eventStoreConnection.ErrorOccurred += (sender, args)
                                                  => Console.WriteLine($"Connection error : {args.Exception}");

            await eventStoreConnection.ConnectAsync();


            var serializer = new JsonNetSerializer();

            var eventMapper = new EventTypeMapper()
                              .Map <Domain.Events.V1.ReviewCreated>("reviewCreated")
                              .Map <Domain.Events.V1.CaptionAndContentChanged>("reviewUpdated")
                              .Map <Domain.Events.V1.ReviewPublished>("reviewPublished")
                              .Map <Domain.Events.V1.ReviewApproved>("reviewApproved");

            //Dont forget to add ReviewSnapshot event to eventmapper!
            //.Map<Domain.ReviewSnapshot>("reviewSnapshot");


            var aggregateStore = new GesAggrigateStore(
                eventStoreConnection,
                serializer,
                eventMapper,
                (type, id) => $"{type.Name}-{id}",
                null);

            services.AddSingleton(new ApplicationService(aggregateStore));

            IAsyncDocumentSession GetSession() => BuildRevenDb().OpenAsyncSession();

            await ProjectionManager.With
            .Connection(eventStoreConnection)
            .CheckpointStore(new RavenDbChecklpointStore(GetSession))
            .Serializer(serializer)
            .TypeMapper(eventMapper)
            .SetProjections(new Projection[]
            {
                new ActiveReviews(GetSession),
                new ReviewsByOwner(GetSession)
            })
            .StartAll();
        }
        public async Task can_save_aggregate()
        {
            var aggregate = new Reviews.Domain.Review();

            aggregate.Apple(AutoFixture.Create <Domain.Events.V1.ReviewCreated>());
            aggregate.Apple(AutoFixture.Create <Domain.Events.V1.ReviewApproved>());

            var sut = new GesAggrigateStore(Connection, Serializer, EventTypeMapper, (a, b) => $"{a}-{b}", null);

            var result = await sut.Save(aggregate);

            result.NextExceptedVersion.Should().Be(1);
        }
        public async Task can_load_aggregate()
        {
            var aggregate = new Reviews.Domain.Review();

            aggregate.Apple(AutoFixture.Build <Domain.Events.V1.ReviewCreated>().With(e => e.Id, AggregateId).Create());
            aggregate.Apple(AutoFixture.Build <Domain.Events.V1.ReviewApproved>().With(e => e.Id, AggregateId).Create());


            var sut = new GesAggrigateStore(Connection, Serializer, EventTypeMapper, (a, b) => $"{a}-{b}", null);

            var saveResult = await sut.Save(aggregate);

            var result = await sut.Load <Domain.Review>(AggregateId.ToString());

            result.Id.Should().Be(AggregateId);
        }
        private async Task BuildEventStore(IServiceCollection services)
        {
            //Create EventStore Connection
            var gesConnection = EventStoreConnection.Create(
                Configuration["EventStore:ConnectionString"],
                ConnectionSettings.Create()
                .KeepReconnecting()
                .EnableVerboseLogging()
                .SetHeartbeatInterval(TimeSpan.FromMilliseconds(5 * 1000))
                .UseDebugLogger(),
                Environment.ApplicationName
                );

            gesConnection.Connected += (sender, args)
                                       => Console.WriteLine($"Connection to {args.RemoteEndPoint} event store established.");

            gesConnection.ErrorOccurred += (sender, args)
                                           => Console.WriteLine($"Connection error : {args.Exception}");

            await gesConnection.ConnectAsync();


            var serializer = new JsonNetSerializer();

            var eventMapper = new EventTypeMapper()
                              .Map <Domain.Events.V1.ReviewCreated>("reviewCreated");

            //Events will be registered to evetTypeMappers
            //.Map<Domain.Events.V1.CaptionAndContentChanged>("reviewUpdated")
            //.Map<Domain.Events.V1.ReviewPublished>("reviewPublished")
            //.Map<Domain.Events.V1.ReviewApproved>("reviewApproved");

            var aggregateStore = new GesAggrigateStore(
                gesConnection,
                serializer,
                eventMapper,
                (type, id) => $"{type.Name}-{id}",
                null);

            services.AddSingleton(new ApplicationService(aggregateStore));
        }