是否可以使用 mediatR 处理一个查询但使用不同的处理程序?
Is it possible to handle one query but with different handlers using mediatR?
我正在使用 ninject + mediatR。我有 class 从不同接口继承方法:
class Query : IRequest<IReadOnlyList<QueryResult>>, interfaceName1, interfaceName2
{
public string methodFromFirstInterface();
public string methodFromSecondInterface();
}
并且我想使用 mediatR 来处理此查询。我可以做这样的事情吗?
public class QueryHandler : IRequestHandler<Query, IReadOnlyList<QueryResult>>
{
//first handler
public IReadOnlyList<QueryResult> Handle(Query message)
{
//something
}
//second handler
public IReadOnlyList<QweReportModel> Handle(interfaceName1 message)
{
message.methodFromFirstInterface();
}
//third handler
public IReadOnlyList<QweReportModel> Handle(interfaceName2 message)
{
message.methodFromSecondInterface();
}
}
是否有可能以某种方式发送带有 mediatR 的查询以调用第二个或第三个处理程序,而不是第一个?
这是不可能的,请求是按具体请求类型分派给处理程序的。您必须为每个请求创建派生类型:
public class Query1 : Query, interfaceName1, IRequest<IReadOnlyList<QweReportModel>>
public class Query2 : Query, interfaceName2, IRequest<IReadOnlyList<QweReportModel>>
你的处理程序看起来像:
public class QueryHandler :
IRequestHandler<Query, IReadOnlyList<QueryResult>>,
IRequestHandler<Query1, IReadOnlyList<QweReportModel>>,
IRequestHandler<Query2, IReadOnlyList<QweReportModel>>
并发送:
mediator.Send<IReadOnlyList<QweReportModel>>(new Query1());
mediator.Send<IReadOnlyList<QweReportModel>>(new Query2());
我正在使用 ninject + mediatR。我有 class 从不同接口继承方法:
class Query : IRequest<IReadOnlyList<QueryResult>>, interfaceName1, interfaceName2
{
public string methodFromFirstInterface();
public string methodFromSecondInterface();
}
并且我想使用 mediatR 来处理此查询。我可以做这样的事情吗?
public class QueryHandler : IRequestHandler<Query, IReadOnlyList<QueryResult>>
{
//first handler
public IReadOnlyList<QueryResult> Handle(Query message)
{
//something
}
//second handler
public IReadOnlyList<QweReportModel> Handle(interfaceName1 message)
{
message.methodFromFirstInterface();
}
//third handler
public IReadOnlyList<QweReportModel> Handle(interfaceName2 message)
{
message.methodFromSecondInterface();
}
}
是否有可能以某种方式发送带有 mediatR 的查询以调用第二个或第三个处理程序,而不是第一个?
这是不可能的,请求是按具体请求类型分派给处理程序的。您必须为每个请求创建派生类型:
public class Query1 : Query, interfaceName1, IRequest<IReadOnlyList<QweReportModel>>
public class Query2 : Query, interfaceName2, IRequest<IReadOnlyList<QweReportModel>>
你的处理程序看起来像:
public class QueryHandler :
IRequestHandler<Query, IReadOnlyList<QueryResult>>,
IRequestHandler<Query1, IReadOnlyList<QweReportModel>>,
IRequestHandler<Query2, IReadOnlyList<QweReportModel>>
并发送:
mediator.Send<IReadOnlyList<QweReportModel>>(new Query1());
mediator.Send<IReadOnlyList<QweReportModel>>(new Query2());