网页API的OData,$ inlinecount和测试 [英] Web API, OData, $inlinecount and testing

查看:180
本文介绍了网页API的OData,$ inlinecount和测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我previously有看起来像这样的网络API控制器:

I previously had a Web API controller that looked like this:

    public IQueryable<ApiDesignOverview> GetList(
        string brandIds = "", 
        string categoryIds = "", 
        string query = "",
        string categoryOp = "or")

我听说的OData的NuGet包现在支持$ inlinecount OData的参数,所以我试图使用说明从<一个添加href=\"http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options\" rel=\"nofollow\">http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options - 我不希望有使用OData的批发因为这将意味着大量的应用程序重新architecturing的,所以我去了 PageResult&LT; T&GT; 选项

I heard that the OData NuGet package now supports the $inlinecount OData parameter, so I tried to add it using the instructions from http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options - I don't want to have to use OData wholesale as that would entail a large amount of re-architecturing of the app, so I went for the PageResult<T> option.

所以,现在我的控制器看起来是这样的:

So now my controller looks like this:

    public PageResult<ApiDesignOverview> GetList(
        ODataQueryOptions<ApiDesignOverview> options,
        string brandIds = "", 
        string categoryIds = "", 
        string query = "",
        string categoryOp = "or")

我的问题现在就是:

My problems are now:


  • 如何模拟一个单元测试ODataQueryOptions?

  • 如果他们不能被嘲笑,我怎么创建?我需要一个 ODataQueryContext 来构造一个,这需要一个 Microsoft.Data.Edm.IEdmModel ,这就需要...什么?我找不到任何文档这一点。

  • How do I mock a ODataQueryOptions for unit testing?
  • If they can't be mocked, how do I create one? I need a ODataQueryContext to construct one, which requires a Microsoft.Data.Edm.IEdmModel, which requires... what? I can't find any documentation for this.

真的,它会更好,如果我可以从控制器签名删除ODataQueryOptions像以前一样。这可能吗?

Really, it would be better if I could remove the ODataQueryOptions from the controller signature like before. Is this possible?

推荐答案

如果您preFER返回IQueryable的,但想为$ inlinecount支持,仍然可以通过modyifying QueryableAttribute做到这一点。

If you prefer returning IQueryable and yet want support for $inlinecount, it is still possible to do that by modyifying QueryableAttribute.

public class InlineCountQueryableAttribute : QueryableAttribute
{
    private static MethodInfo _createPageResult =
        typeof(InlineCountQueryableAttribute)
        .GetMethods(BindingFlags.Static | BindingFlags.NonPublic)
        .Single(m => m.Name == "CreatePageResult");

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        base.OnActionExecuted(actionExecutedContext);

        HttpRequestMessage request = actionExecutedContext.Request;
        HttpResponseMessage response = actionExecutedContext.Response;

        IQueryable result;
        if (response.IsSuccessStatusCode
            && response.TryGetContentValue<IQueryable>(out result))
        {
            long? inlineCount = request.GetInlineCount();
            if (inlineCount != null)
            {
                actionExecutedContext.Response = _createPageResult.MakeGenericMethod(result.ElementType).Invoke(
                    null, new object[] { request, request.GetInlineCount(), request.GetNextPageLink(), result }) as HttpResponseMessage;
            }
        }
    }

    internal static HttpResponseMessage CreatePageResult<T>(HttpRequestMessage request, long? count, Uri nextpageLink, IEnumerable<T> results)
    {
        return request.CreateResponse(HttpStatusCode.OK, new PageResult<T>(results, nextpageLink, count));
    }
}

请注意,我使用反射创建PageResult。您可以返回,而不是自己的喜好,可以通过使用格式进行格式化的对象。使用结果和计数的匿名对象将工作太多,如果你是使用JSON格式。

Notice, that I am using reflection to create PageResult. You can instead return an object of your liking that can be formatted by the formatter that you use. An anonymous object with results and count will work too if you are using the Json formatter.

这篇关于网页API的OData,$ inlinecount和测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆