我目前正在使用 oData 过滤器请求执行 Web API,如下所示:
public IQueryable<OrganizationViewModel> Get(ODataQueryOptions<Organization> oDataQuery)
{
var query = new FindOrganizationsQuery(oDataQuery);
var result =_findOrganizationsQueryHandler.Execute(query);
return result.Organizations.Select(o => new OrganizationViewModel { Id = o.PublicId, Name = o.Name });
}
处理程序看起来像:
public FindOrganizationsQueryResult Execute(FindOrganizationsQuery request)
{
var organizations = request.ODataQuery.ApplyTo(_mgpQueryContext.Organizations).Cast<Organization>();
return new FindOrganizationsQueryResult(organizations);
}
查询类如下所示:
public class FindOrganizationsQuery
{
public FindOrganizationsQuery(ODataQueryOptions<Organization> oDataQuery)
{
ODataQuery = oDataQuery;
}
public ODataQueryOptions<Organization> ODataQuery { get; set; }
}
因此,如果我通过请求传递一个 oData 过滤器,它会得到很好的处理,这一切都很好。
但是现在,我不想将类型 ODataQueryOptions 传递给 Get 操作,而是拥有 FindOrganizationsQuery 类,例如:
public IQueryable<OrganizationViewModel> FindOrganizations(FindOrganizationsQuery query)
{
// query is null
}
但是,查询参数始终为空。如果 ODataQueryOptions 参数在另一个类中,如何传递 oData 过滤器?