使用WebApi和映射模型实现OData

Len*_*nny 9 c# entity-framework odata asp.net-web-api

我正在尝试在WebApi中实现OData.我正在使用存储库模式和EF5(在后端),这仍然与我找到的所有示例一致.这是事情变得不稳定的地方.我试图隐藏EF生成的类隐藏在控制器中使用AutoMapper映射的模型后面.我看到的例子似乎都归还了回购中的任何内容

我不想在控制器中但在存储库中应用OData参数(已映射的结果)以保留延迟执行的值.我可以将ODataCriteria传递到存储库中,但是当我尝试Appy时,我得到一个错误,因为看起来选项/结果是从表示层而不是IQueryable <EF_Class>键入IQueryable <Model>.

我在另一篇文章中看到其他人没有注意到这一点,但是,它只是帖子的一小部分,似乎没有帮助.

还有其他人处理过这件事吗?我真的不想暴露EF类.哦,我先使用DB.

提前致谢...

quj*_*jck 1

这是一些演示您的要求的代码。

要获得结果,您需要确保查询已执行(使用ToList())。最有效的方法是添加分页(奖励)并返回一个PageResult<>对象。

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;
}
Run Code Online (Sandbox Code Playgroud)

这是使用语句

using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Query;
Run Code Online (Sandbox Code Playgroud)

测试班

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; }
}
Run Code Online (Sandbox Code Playgroud)

以及一些测试数据

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();
}
Run Code Online (Sandbox Code Playgroud)