public async Task <TodoItem> FindTodoItem(Guid id)
 {
     using (ReadModelDbContext db = _dbContextFactory.Invoke())
     {
         return(await db
                .TodoItems
                .AsNoTracking()
                .Where(e => e.Id == id)
                .SingleOrDefaultAsync());
     }
 }
 public async Task <IEnumerable <TodoItem> > GetAllTodoItems()
 {
     using (ReadModelDbContext db = _dbContextFactory.Invoke())
     {
         return(await db
                .TodoItems
                .AsNoTracking()
                .OrderByDescending(e => e.SequenceId)
                .ToListAsync());
     }
 }
        public async Task Handle(
            Envelope <TodoItemDeleted> envelope,
            CancellationToken cancellationToken)
        {
            TodoItemDeleted domainEvent = envelope.Message;

            using (ReadModelDbContext db = _dbContextFactory.Invoke())
            {
                TodoItem todoItem = await db
                                    .TodoItems
                                    .Where(e => e.Id == domainEvent.SourceId)
                                    .SingleAsync(cancellationToken);

                db.TodoItems.Remove(todoItem);

                await db.SaveChangesAsync(cancellationToken);
            }
        }
        public async Task Handle(
            Envelope <TodoItemCreated> envelope,
            CancellationToken cancellationToken)
        {
            TodoItemCreated domainEvent = envelope.Message;

            using (ReadModelDbContext db = _dbContextFactory.Invoke())
            {
                var todoItem = new TodoItem
                {
                    Id          = domainEvent.SourceId,
                    CreatedAt   = domainEvent.RaisedAt,
                    Description = domainEvent.Description
                };

                db.TodoItems.Add(todoItem);

                await db.SaveChangesAsync(cancellationToken);
            }
        }