实施的OData用的WebAPI和映射模型 [英] Implementing OData with WebApi and Mapped Models

查看:134
本文介绍了实施的OData用的WebAPI和映射模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在的WebAPI实现的OData。我使用存储库模式和EF5(在后端),这仍然是我发现的所有例子是一致的。这里是东西去靠不住的。我试图躲在正在控制器使用AutoMapper映射模型EF生成的类。我所看到的例子似乎回到一切散发出来的回购

I am trying to implement OData in WebApi. I am using the repository pattern and EF5 (in the backend) which is still consistent with all the examples I have found. Here is where thing go wonky. I am trying to hide the EF generated classes behind models that are being mapped using AutoMapper in the controller. The examples I have seen seem to return whatever comes out of the repo

我不希望应用的OData参数(以已映射的结果)的控制器,但在仓库中,以preserve从延迟执行的价值。我可以通过ODataCriteria到库,但是当我尝试APPY,我得到一个错误,因为它似乎/结果输入到的IQueryable&LT的选项;从presentation层不IQueryable的&LT型号>; EF_Class>。

I don't want to apply the OData parameters (to the results that have been mapped) in the controller but in the repository to preserve the value from delayed execution. I can pass the ODataCriteria into the repository, but when I try to Appy, I get an error because it seems the options/results are typed to the IQueryable< Model > from the presentation layer not IQueryable< EF_Class >.

我看到别人躲避这个在另一篇文章,但是,它是后一小部分,它似乎并没有帮助。

I saw someone else eluded to this in another post, however, it was a minor part of the post and it didn't seem to help.

有没有其他人处理这件事?我真的不希望暴露的EF类。呵呵,我是第一次使用DB。

Has anyone else dealt with this? I really don't want to expose the EF classes. Oh, I am using DB first.

在此先感谢...

推荐答案

下面是一些code演示您的要求。

Here's some code that demonstrates your requirement.

要达到你需要确保该查询已执行的结果(使用了ToList())。最有效的方法,这样做是为了添加分页(奖金),并返回一个 PageResult&LT;方式&gt; 对象

To achieve the result you need to ensure that the query has been executed (using ToList()). The most efficient way to do this is to add paging (bonus) and return a PageResult<> object.

public PageResult<WebPoco> Get(ODataQueryOptions<WebPoco> queryOptions)
{
    var data2 = DatabaseData();

    //Create a set of ODataQueryOptions for the internal class
    ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
    modelBuilder.EntitySet<DatabasePoco>("DatabasePoco"); 
    var context = new ODataQueryContext(
         modelBuilder.GetEdmModel(), typeof(DatabasePoco));
    var newOptions = new ODataQueryOptions<DatabasePoco>(context, Request);

    var t = new ODataValidationSettings() { MaxTop = 25 };
    var s = new ODataQuerySettings() { PageSize = 25 };
    newOptions.Validate(t);
    IEnumerable<DatabasePoco> results =
        (IEnumerable<DatabasePoco>)newOptions.ApplyTo(data2, s);

    int skip = newOptions.Skip == null ? 0 : newOptions.Skip.Value;
    int take = newOptions.Top == null ? 25 : newOptions.Top.Value;

    List<DatabasePoco> internalResults = results.Skip(skip).Take(take).ToList();

    // map from DatabasePoco to WebPoco here:
    List<WebPoco> webResults; 

    PageResult<WebPoco> page =
        new PageResult<WebPoco>(
            webResults, Request.GetNextPageLink(), Request.GetInlineCount());

    return page;
}

这里的using语句

Here's the using statements

using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Query;

测试类

public class WebPoco
{
    public int id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
}

public class DatabasePoco
{
    public int id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
}

和一些数据,用于测试

private IQueryable<DatabasePoco> DatabaseData()
{
    return (
        new DatabasePoco[] { 
            new DatabasePoco() { id = 1, name = "one", type = "a" },
            new DatabasePoco() { id = 2, name = "two", type = "b" },
            new DatabasePoco() { id = 3, name = "three", type = "c" },
            new DatabasePoco() { id = 4, name = "four", type = "d" },
            new DatabasePoco() { id = 5, name = "five", type = "e" },
            new DatabasePoco() { id = 6, name = "six", type = "f" },
            new DatabasePoco() { id = 7, name = "seven", type = "g" },
            new DatabasePoco() { id = 8, name = "eight", type = "h" },
            new DatabasePoco() { id = 9, name = "nine", type = "i" }
        })
        .AsQueryable();
}

这篇关于实施的OData用的WebAPI和映射模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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