I'm trying to use generics to create an abstract query request handler for aggregate roots in a domain-driven design architecture.
I've got the following classes to setup a Mediatr request.
However, I get a compiler error when using ISender.Send because it thinks my response object is a plain old object instead a QueryResult<T>.
Do you know what I'm doing wrong?
public interface IQuery<T> : IRequest<T>
where T : class
{
Guid CorrelationId { get; }
}
public abstract class BaseResult
{
protected BaseResult(ResultStatus status, Dictionary<string, List<string>>? errors)
{
this.Status = status;
this.Errors = errors ?? new Dictionary<string, List<string>>();
this.IsSuccess = status == ResultStatus.Success;
}
public bool IsSuccess { get; private set; }
public ResultStatus Status { get; private set; }
public Dictionary<string, List<string>> Errors { get; private set; }
...
}
public class QueryResult<T> : BaseResult
where T : class
{
private QueryResult(T? data, ResultStatus status, Dictionary<string, List<string>>? errors)
: base(status, errors)
{
this.Data = data;
}
public T? Data { get; }
...
}
public class AggregateRootQuery<TAggregate, TDto>
: IRequest<QueryResult<IEnumerable<TDto>>>, IQuery<IEnumerable<TDto>>
where TAggregate : class, IAggregateRoot // IAggregate root is a marker interface to only allow queries to start at the aggregate root when using my EF core DB context wrapper
where TDto : class
{
public AggregateRootQuery(Guid correlationId)
{
this.CorrelationId = correlationId;
}
public Guid CorrelationId { get; }
}
var result = await this.Mediator.Send(new AggregateRootQuery<Domain.Order, OrdrDto>(Guid.NewGuid()));
// `IsSuccess` and `Data` have compiler errors because it thinks `result` is an object and not a `QueryResult<IEnumberable<OrderDto>>`
if (!result.IsSuccess || result.Data == null)
{
// Error handing
}
I'm trying to use generics to create an abstract query request handler for aggregate roots in a domain-driven design architecture.
I've got the following classes to setup a
Mediatr
request.However, I get a compiler error when using
ISender.Send
because it thinks my response object is a plain oldobject
instead aQueryResult<T>
.Do you know what I'm doing wrong?
Usage