OpenCQRS

Queries

First, create the model for the result and a query that implements the IQuery<> interface:

public class Something
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class GetSomething : IQuery<Something>
{
    public int Id { get; set; }
}

Next, create the handler:

public class GetSomethingQueryHandler : IQueryHandler<GetSomething, Something>
{
    private readonly MyDbContext _dbContext;

    public GetProductsHandler(MyDbContext dbContext)
    {
        _dbContext = dbContext;
    }
        
    public Task<Result<Something>> Handle(GetSomething query)
    {
        return _dbContext.Somethings.FirstOrDefaultAsync(s => s.Id == query.Id);
    }
}

And finally, get the result using the dispatcher:

var query = new GetSomething { Id = 123 };
var something = await _dispatcher.Get(query);

If you want your query result to be cached automatically, you can use the **CacheableQuery** abstract class for your queries:

public class GetSomethingQuery : CacheableQuery<string>;

var result = await dispatcher.Get(new GetSomethingQuery
{
    CacheKey = "my-cache-key",
    CacheTimeInSeconds = 600
});