OData集成

介绍

  在odata.orgOData定义为“一种开放协议允许使用简单或标准的方式创建和消费可查询和可交互的RESTful APIs”。你可以在ABP中使用OData。Abp.Web.Api.OData nuget包简化了它的使用。

安装

安装Nuget包

  首先需要在我们的WebApi工程中安装Abp.OData nuget包:

Install-Package Abp.Web.Api.OData

设置模块依赖

  我们的模块中需要设置AbpApiODataModule的依赖。示例:

[DependsOn(typeof(AbpWebApiODataModule))]
public class MyProjectWebApiModule : AbpModule
{
...
}

  参见模块系统了解模块依赖。

配置实体

  OData需要声明可以用做为OData资源的实体。我们应该在模块的PreInitialize方法中定义,如下所示:

[DependsOn(typeof(AbpWebApiODataModule))]
public class MyProjectWebApiModule : AbpModule
{
public override void PreInitialize()
{
var builder = Configuration.Modules.AbpWebApiOData().ODataModelBuilder; //Configure your entities here...
builder.EntitySet<Person>("Persons");
} ...
}

  这里,我们获取ODataModelBuilder引用并设置Person实体。类似的,你可以使用EntitySet添加其他的实体。参见OData文档了解更多关于builder的信息。

创建控制器

  Abp.Web.Api.OData nuget包包含AbpODataEntityController基类(扩展自标准的ODataControler)用来方便的创建你自己的控制器。创建Person实体OData终结点的实例如下:

public class PersonsController : AbpODataEntityController<Person>
{
public PersonsController(IRepository<Person> repository)
: base(repository)
{
}
}

  非常简单!AbpODataEntityController的所有方法都是virtual的。这意味着,你可以重写Get、POST、Put、Patch、Delete和其他actions并添加自己的逻辑。

配置

  Abp.Web.Api.OData使用常规配置自动调用HttpConfiguration.MapODataServiceRoute方法。如果需要,你可以设置Configuration.Modules.AbpWebApiOData().MapAction映射到自己的OData路径。

示例

  这里,有一些请求上面定义的控制器的例子。假定应用运行在http://localhost:61482。我们将展示一些基本要素。因为OData是一个标准协议,你可以轻松的在网上找到更多高级的例子。

获取实体列表

  获取所有的people。

Request

GET http://localhost:61842/odata/Persons

Response

{
"@odata.context":"http://localhost:61842/odata/$metadata#Persons","value":[
{
"Name":"Douglas Adams","IsDeleted":false,"DeleterUserId":null,"DeletionTime":null,"LastModificationTime":null,"LastModifierUserId":null,"CreationTime":"2015-11-07T20:12:39.363+03:00","CreatorUserId":null,"Id":
},{
"Name":"John Nash","IsDeleted":false,"DeleterUserId":null,"DeletionTime":null,"LastModificationTime":null,"LastModifierUserId":null,"CreationTime":"2015-11-07T20:12:39.363+03:00","CreatorUserId":null,"Id":
}
]
}

获取单个实体

  获取Id=2的person。

Request

GET http://localhost:61842/odata/Persons(2)

Response

{
"@odata.context":"http://localhost:61842/odata/$metadata#Persons/$entity","Name":"John Nash","IsDeleted":false,"DeleterUserId":null,"DeletionTime":null,"LastModificationTime":null,"LastModifierUserId":null,"CreationTime":"2015-11-07T20:12:39.363+03:00","CreatorUserId":null,"Id":
}

使用导航属性获取单个实体

  获取Id=1的person并包含他的电话号码。

Request

GET http://localhost:61842/odata/Persons(1)?$expand=Phones

Response

{
"@odata.context":"http://localhost:61842/odata/$metadata#Persons/$entity","Name":"Douglas Adams","IsDeleted":false,"DeleterUserId":null,"DeletionTime":null,"LastModificationTime":null,"LastModifierUserId":null,"CreationTime":"2015-11-07T20:12:39.363+03:00","CreatorUserId":null,"Id":,"Phones":[
{
"PersonId":,"Type":"Mobile","Number":"","CreationTime":"2015-11-07T20:12:39.363+03:00","CreatorUserId":null,"Id":
},{
"PersonId":,"Type":"Mobile","Number":"","CreationTime":"2015-11-07T20:12:39.363+03:00","CreatorUserId":null,"Id":
}
]
}

查询

  这里,一个更高级的查询包含过滤、排序并获取前两个结果。

Request

GET http://localhost:61842/odata/Persons?$filter=Name eq 'Douglas Adams'&$orderby=CreationTime&$top=2

Response

{
"@odata.context":"http://localhost:61842/odata/$metadata#Persons","value":[
{
"Name":"Douglas Adams","IsDeleted":false,"DeleterUserId":null,"DeletionTime":null,"LastModificationTime":null,"LastModifierUserId":null,"CreationTime":"2015-11-07T20:12:39.363+03:00","CreatorUserId":null,"Id":
},{
"Name":"Douglas Adams","IsDeleted":false,"DeleterUserId":null,"DeletionTime":null,"LastModificationTime":null,"LastModifierUserId":null,"CreationTime":"2016-01-12T20:29:03+02:00","CreatorUserId":null,"Id":
}
]
}

  OData支持分页,排序,过滤,投射等。参见它的文档了解更多信息。

创建新实体

  在这个例子中,我们创建一个新person。

Request

POST http://localhost:61842/odata/Persons

{
Name: "Galileo Galilei"
}

  这里,“Content-Type”头为"application/json"。

Response

{
"@odata.context": "http://localhost:61842/odata/$metadata#Persons/$entity",
"Name": "Galileo Galilei",
"IsDeleted": false,
"DeleterUserId": null,
"DeletionTime": null,
"LastModificationTime": null,
"LastModifierUserId": null,
"CreationTime": "2016-01-12T20:36:04.1628263+02:00",
"CreatorUserId": null,
"Id":
}

  如果我们再一次获取这个列表,我们就会看见这个新的person。我们也可以update或delete一个存在的实体,OData是支持的。

获取元数据

  我们可以获取实体的元数据,如在本例中所示。

Request

GET http://localhost:61842/odata/$metadata

Response

<?xml version="1.0" encoding="utf-8"?>

<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">

    <edmx:DataServices>

        <Schema Namespace="AbpODataDemo.People" xmlns="http://docs.oasis-open.org/odata/ns/edm">

            <EntityType Name="Person">

                <Key>

                    <PropertyRef Name="Id" />

                </Key>

                <Property Name="Name" Type="Edm.String" Nullable="false" />

                <Property Name="IsDeleted" Type="Edm.Boolean" Nullable="false" />

                <Property Name="DeleterUserId" Type="Edm.Int64" />

                <Property Name="DeletionTime" Type="Edm.DateTimeOffset" />

                <Property Name="LastModificationTime" Type="Edm.DateTimeOffset" />

                <Property Name="LastModifierUserId" Type="Edm.Int64" />

                <Property Name="CreationTime" Type="Edm.DateTimeOffset" Nullable="false" />

                <Property Name="CreatorUserId" Type="Edm.Int64" />

                <Property Name="Id" Type="Edm.Int32" Nullable="false" />

                <NavigationProperty Name="Phones" Type="Collection(AbpODataDemo.People.Phone)" />

            </EntityType>

            <EntityType Name="Phone">

                <Key>

                    <PropertyRef Name="Id" />

                </Key>

                <Property Name="PersonId" Type="Edm.Int32" />

                <Property Name="Type" Type="AbpODataDemo.People.PhoneType" Nullable="false" />

                <Property Name="Number" Type="Edm.String" Nullable="false" />

                <Property Name="CreationTime" Type="Edm.DateTimeOffset" Nullable="false" />

                <Property Name="CreatorUserId" Type="Edm.Int64" />

                <Property Name="Id" Type="Edm.Int32" Nullable="false" />

                <NavigationProperty Name="Person" Type="AbpODataDemo.People.Person">

                    <ReferentialConstraint Property="PersonId" ReferencedProperty="Id" />

                </NavigationProperty>

            </EntityType>

            <EnumType Name="PhoneType">

                <Member Name="Unknown" Value="0" />

                <Member Name="Mobile" Value="1" />

                <Member Name="Home" Value="2" />

                <Member Name="Office" Value="3" />

            </EnumType>

        </Schema>

        <Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">

            <EntityContainer Name="Container">

                <EntitySet Name="Persons" EntityType="AbpODataDemo.People.Person" />

            </EntityContainer>

        </Schema>

    </edmx:DataServices>

</edmx:Edmx>

  元数据用来研究这个服务。

示例工程

  你可以在这里获取示例工程的源代码:https://github.com/aspnetboilerplate/sample-odata

返回主目录

ABP官方文档翻译 5.3 OData集成的更多相关文章

  1. ABP官方文档翻译 7.2 Hangfire集成

    Hangfire集成 介绍 ASP.NET Core集成 ASP.NET MVC 5.x集成 面板授权 介绍 Hangfire是一个综合的后台job管理器.你可以 把它集成到ABP,用来取代默认的后台 ...

  2. ABP官方文档翻译 9.3 NHibernate集成

    NHibernate集成 Nuget包 配置 实体映射 仓储 默认实现 自定义仓储 应用程序特定基础仓储类 ABP可以使用任何ORM框架,它内置集成NHibernate.此文档将讲解ABP如何使用NH ...

  3. ABP官方文档翻译 9.1 EntityFramework集成

    EntityFramework集成 Nuget包 DbContext 仓储 默认仓储 自定义仓储 应用特定的基础仓储类 自定义仓储示例 仓储最佳实践 事务管理 数据存储 ABP可以使用ORM框架,它内 ...

  4. ABP官方文档翻译 8.2 SignalR集成

    SignalR集成 介绍 安装 服务器端 客户端 建立连接 內建特征 通知 在线客户端 PascalCase与CamelCase对比 你的SignalR代码 介绍 ABP中的Abp.Web.Signa ...

  5. ABP官方文档翻译 7.3 Quartz集成

    Quartz集成 介绍 安装 创建Jobs 计划安排Jobs 更多 介绍 Quartz是一个全功能的.开源的job计划安排系统,可以用在小的apps也可以用于大型的企业系统.Abp.Quartz包简化 ...

  6. ABP官方文档翻译 5.4 SwaggerUI集成

    SwaggerUI集成 介绍 ASP.NET Core 安装Nuget包 配置 测试 ASP.NET 5.x 安装Nuget包 配置 测试 介绍 在它的网站上:“...使用Swagger可用的API, ...

  7. ABP官方文档翻译 1.6 OWIN集成

    OWIN集成 安装 使用 如果在应用程序里既使用ASP.NET MVC也使用ASP.NET Web API,需要在工程里安装Abp.Owin包. 安装 添加Abp.Owin包到主工程里(一般是web工 ...

  8. ABP官方文档翻译 10.1 ABP Nuget包

    ABP Nuget包 Packages Abp Abp.AspNetCore Abp.Web.Common Abp.Web Abp.Web.Mvc Abp.Web.Api Abp.Web.Api.OD ...

  9. ABP官方文档翻译 0.0 ABP官方文档翻译目录

    一直想学习ABP,但囿于工作比较忙,没有合适的契机,当然最重要的还是自己懒.不知不觉从毕业到参加工作七年了,没留下点儿什么,总感觉很遗憾,所以今天终于卯足劲鼓起勇气开始写博客.有些事能做的很好,但要跟 ...

随机推荐

  1. bzoj:1687;poj 2434:[Usaco2005 Open]Navigating the City 城市交通

    Description A dip in the milk market has forced the cows to move to the city. The only employment av ...

  2. MIT公开课:算法导论 笔记(一)

    课程链接:http://open.163.com/special/opencourse/algorithms.html 第一课:算法分析基础 1.介绍插入排序与归并排序,计算并比较最坏运行时间 2.算 ...

  3. SpringMVC框架学习笔记(4)——结果跳转方式

    1.设置ModelAndView对象.根据View和视图解析器跳转到指定页面(视图解析器前缀+viewname+视图解析器后缀) @Override public ModelAndView handl ...

  4. jquery实现上下滑动选择

    $('.rightShow').on('mousewheel', function(ev) { var dir = ev.originalEvent.wheelDelta if(dir == 120) ...

  5. Python 之 基础知识(三)

    一.函数 def 函数名(): 函数封装的代码 ... def是英文define缩写 别的Python文件可以引入 调用 定义时 和其他代码包括注释保留两个空行 pycharm 调试时 F8 Step ...

  6. Logback日志配置的简单使用

    Logback介绍 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access ...

  7. 逢三退一(boolean数组的使用)

    package com.hanqi.count; // 逢三退一 输出留到最后值的索引; public class Count1 { //主方法 public static void main(Str ...

  8. JAVA:创建类和对象

    package duixiang; public class duixiang { /* * 类的实例化:创建对象 */ public static void main(String[] args) ...

  9. 什么是A记录  域名

    A (Address) 记录是用来指定主机名(或域名)对应的IP地址记录.用户可以将该域名下的网站服务器指向到自己的web server,FTP server等上面.同时也可以设置域名的子域名. 通俗 ...

  10. Apache日志分析_shell命令行

    说明: 1.我的日志预先设定好按日生成文件:"CustomLog "|/opt/apache/bin/rotatelogs /opt/apache/logs/www.website ...