我怎样才能手动网络阿比执行微风过滤器? [英] How can I manually execute Breeze filters in Web Api?
问题描述
我想用一些外部服务器端逻辑对查询结果修改属性。要做到这一点,我需要申请微风查询选项,修改结果集,并返回它。
I want to use some external server-side logic to modify properties on the results of a query. To do this, I'll need to apply the Breeze query options, modify the result set, and return it.
我知道基本上我怎么可以申请 OdataQueryOptions
来我的查询,但我不想错过所有的事情,BreezeJS确实该Web阿比的OData的没有按'吨。例如,我想保持 inlineCount
。
I know essentially how I can apply OdataQueryOptions
to my query, but I don't want to miss out on all the things that BreezeJS does that Web Api's OData doesn't. For example, I want to keep inlineCount
.
我怎样才能做到这一点?是否有某种方式挂接到微风的查询,修改code?
How can I do this? Is there some way to hook into Breeze's query modifying code?
在情况下它的事项,我使用实体框架6和Web阿比2。
In case it matters, I'm using Entity Framework 6 and Web Api 2.
推荐答案
好吧,我不知道是否有更好的方法(因为这似乎是一个大量的工作,对什么似乎像这将是一个常见的情况) ,但这里是我如何解决这一点。
Ok, I'm not sure if there's a better way (because this seems like a lot of work for what seems like it would be a common use case), but here's how I solved this.
我从 QueryHelper
类继承修改 PostExecuteQuery
方法来执行委托。
I inherited from the QueryHelper
class to modify the PostExecuteQuery
method to execute a delegate.
public class ExtendedQueryHelper : QueryHelper
{
public ExtendedQueryHelperOptions Options { get; set; }
public ExtendedQueryHelper(ODataQuerySettings querySettings) : base(querySettings)
{}
public override IEnumerable PostExecuteQuery(IEnumerable queryResult)
{
if (Options != null && Options.PostExecuteQueryHandler != null)
{
return Options.PostExecuteQueryHandler(queryResult);
}
return base.PostExecuteQuery(queryResult);
}
}
该委托是一类被称为定义的 ExtendedQueryHelperOptions
public class ExtendedQueryHelperOptions
{
private const string EXTENDED_QUERY_HELPER_OPTIONS_KEY = "EXTENDED_QUERY_HELPER_OPTIONS_KEY";
public delegate IEnumerable PostExecuteQueryDelegate(IEnumerable queryResult);
public PostExecuteQueryDelegate PostExecuteQueryHandler { get; set; }
public void InjectIntoRequest(HttpRequestMessage request)
{
request.Properties.Add(EXTENDED_QUERY_HELPER_OPTIONS_KEY, this);
}
public static ExtendedQueryHelperOptions GetFromRequest(HttpRequestMessage request)
{
object options;
request.Properties.TryGetValue(EXTENDED_QUERY_HELPER_OPTIONS_KEY, out options);
return (ExtendedQueryHelperOptions)options;
}
}
为了设置这些选项,我不得不从 BreezeQueryableAttribute
继承并注入这些选项时, QueryHelper
是正在创建:
In order to set these options, I had to inherit from BreezeQueryableAttribute
and inject these options when the QueryHelper
is being created:
public class ExtendedBreezeQueryableAttribute : BreezeQueryableAttribute
{
protected HttpRequestMessage Request { get; set; }
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
Request = actionContext.Request;
base.OnActionExecuting(actionContext);
}
protected override QueryHelper NewQueryHelper()
{
var queryHelper = new ExtendedQueryHelper(GetODataQuerySettings());
queryHelper.Options = ExtendedQueryHelperOptions.GetFromRequest(Request);
return queryHelper;
}
}
现在我可以注入code要在筛选的结果是这样运行的:
Now I can inject code to be run on the filtered results like this:
[BreezeController]
public class BreezeController : BaseController
{
//...
[HttpGet]
[ExtendedBreezeQueryable]
public IQueryable<Foo> Foos()
{
var options = new ExtendedQueryHelperOptions
{
PostExecuteQueryHandler = delegate(IEnumerable results) {
// This code will be run after the querying has been
// applied by Breeze
var foos = results.Cast<Foo>().ToList();
foreach (var foo in foos)
{
foo.ComputedProperty = ComputeSomething();
}
return foos;
}
};
// Inject these options into the Request, so the ExtendedBreezeQueryableAttribute
// can get to them later
options.InjectIntoRequest(Request);
return Db.Foos;
}
}
这篇关于我怎样才能手动网络阿比执行微风过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!