How to read events with Eventflow?

63 Views Asked by At

I work on a .NET 6 application which uses Eventflow. I would like to read my events.

Events are in the table EventEntity

table-evententity

Do you have any idea ? Can we create a ReadModel for this table ? Is there any method to get data from this table ?

1

There are 1 best solutions below

0
On BEST ANSWER

I've found the solution, here is my code :

public class EventStoreService : IEventStoreService
{
        private readonly ILogger logger;
        private readonly IEventPersistence eventPersistence;
        private readonly IDbContextProvider<CartAppDbContext> _contextProvider;

        public EventStoreService(ILogger<EventStoreService> aLogger, IDbContextProvider<CartAppDbContext> contextProvider, IEventPersistence eventPersistence)
        {
            this.logger = aLogger;
            this.eventPersistence = eventPersistence;
            this._contextProvider = contextProvider;
        }

        public async Task<AllCommittedEventsPage> GetLastEventsAsync(int pageSize)
        {
            try
            {
                using ( var context = _contextProvider.CreateContext() )
                {
                    var entities = await context
                        .Set<EventEntity>()
                        .OrderByDescending(e => e.GlobalSequenceNumber)
                        .Take(pageSize)
                        .ToListAsync(CancellationToken.None)
                        .ConfigureAwait(false);

                    var nextPosition = entities.Any()
                        ? entities.Max(e => e.GlobalSequenceNumber) + 1
                        : 0;

                    return new AllCommittedEventsPage(new GlobalPosition(nextPosition.ToString()), entities);
                }
            }
            catch ( Exception ex )
            {
                this.logger.LogError("Récupération des évènements impossible : ", ex.Message);
                throw;
            }
        }

}