我目前正在执行使用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过滤器,它会被很好地处理,这一切都可以正常工作。
但现在,我想使用FindOrganizationsQuery类而不是将ODataQueryOptions类型传递给Get操作,如下:
public IQueryable<OrganizationViewModel> FindOrganizations(FindOrganizationsQuery query)
{
// query is null
}
然而,查询参数始终为空。如果ODataQueryOptions参数在另一个类中,我该如何传递oData过滤器?
FindOrganizationsQuery
,为什么需要它?为什么在FindOrganizations
返回IQueryable<OrganizationViewModel>
的同时需要内部的ODataQueryOptions<Organization>
? - AronOrganization
的查询并接收OrganizationViewModel
呢?你仍然会像这样暴露导航属性... - Aron