public interface IQueryHandler<in TQuery, out TResult>
where TQuery : IQuery<TResult>
{
TResult Handle(TQuery query);
}
这是一个查询处理程序接口(interface),需要一个参数来执行查询。
public class PlaceByIdHandler : IQueryHandler<PlaceById, PlaceModel>
{
...........
public PlaceModel Execute(PlaceById query)
{
return repository.Query<PlaceModel>("select * from places where id="+ query.id);
}
}
但是有些查询不需要参数。例如获取所有地点:
public class PlaceAllHandler : IQueryHandler<PlaceAll, PlaceModel>
{
..........
public PlaceModel Execute(PlaceAll query)
{
return return repository.Query<PlaceModel>("select * from places");
}
}
但是现在 PlaceAll 是一个没有成员的类。
public class PlaceAll{}
这是真正的方法吗?有什么命令吗?
最佳答案
But some queries does not need parameter.
查询的其他参数/属性是可选的。
您的查询PlaceAll
(我称之为 AllPlaces
)包含所有信息。如果查询处理程序不需要任何附加参数 Handle
查询则不需要指定它们。
我希望您使用以下 IQuery<TResult>
接口(interface):
public interface IQuery<TResult>
{ }
如您所见,它也没有任何参数。 QueryHandler
仅取决于查询的类型(TQuery
或 IQuery<TResult>
)。
关于c# - 没有参数的 CQRS 查询处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28857135/