eventFlow 系列 <三> 查询所有

接着上面的例子,产生2条数据。怎么把这两条数据查询出来呢?

var commandBus = resolver.Resolve<ICommandBus>();
                var cm = new ExampleCommand(exampleId, 42, 88);
                var executionResult = await commandBus.PublishAsync(cm,CancellationToken.None).ConfigureAwait(false);var exampleId2 = ExampleId.New;
                var cm3 = new ExampleCommand(exampleId2, 423, 888);
                var executionResult3 = await commandBus.PublishAsync(cm3, CancellationToken.None).ConfigureAwait(false);

这要添加下面这2个类。查询需要在加粗的地方修改逻辑。

public class ExampleQuery : IQuery<IReadOnlyCollection<ExampleReadModel>>
    {
    }

  public class ExampleQueryHandler : IQueryHandler<ExampleQuery, IReadOnlyCollection<ExampleReadModel>>
    {
        private readonly IInMemoryReadStore<ExampleReadModel> _readStore;

        public ExampleQueryHandler( IInMemoryReadStore<ExampleReadModel> readStore)
        {
            _readStore = readStore;
        }

        public async Task<IReadOnlyCollection<ExampleReadModel>> ExecuteQueryAsync(ExampleQuery query, CancellationToken cancellationToken)
        {
        return await _readStore.FindAsync( rm => true, cancellationToken) .ConfigureAwait(false);
       } 
}

上端调用的代码:

 var exampleReadModels = await queryProcessor.ProcessAsync(new ExampleQuery(), CancellationToken.None).ConfigureAwait(false);
气功波(18037675651)
原文地址:https://www.cnblogs.com/qgbo/p/11582946.html