Introduction

OData is defined as "An open protocol to allow the creation and consumption of queryable and interoperable RESTful APIs in a simple and standard way" in odata.org. You can use OData with ASP.NET Boilerplate.Abp.Web.Api.OData nuget package simplifies it's usage.

OData被定义为“一种允许创建和查询操作的RESTful API的一个简单的、标准的方法的开放式协议,来源odata.org。你可以使用OData ASP.NET boilerplate.abp.web.api.odata NuGet包简化了它的使用。

Setup

Install Nuget Package

We should first install Abp.Web.Api.OData nuget package to our WebApi project:

Install-Package Abp.Web.Api.OData

Set Module Dependency

We should set dependency to AbpWebApiODataModule from our module. Example:

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

See module system to understand module dependencies.

Configure Your Entities

OData requires to declare entities which can be used as OData resources. We should do this in PreInitialize method of our module, as shown below:

OData要求申明实体可作为数据资源。我们应该在分发我们的模块的方法做到这一点,如下图所示:

[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");
} ...
}

Here, we got the ODataModelBuilder reference and set the Person entity. You can use EntitySet to add other entities as similar. See OData documentation for more information on builder.

Create Controllers

Abp.Web.Api.OData nuget package includes AbpODataEntityController base class (which extends standard ODataController) to create your controllers easier. An example to create an OData endpoint for Person entity:

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

It's that easy. All methods of AbpODataEntityController is virtual. That means you can override Get, Post, Put, Patch, Delete and other actions and add your own logic.

Configuration

Abp.Web.Api.OData automatically calls HttpConfiguration.MapODataServiceRoute method with conventional configuration. If you need, you can set Configuration.Modules.AbpWebApiOData().MapAction to map OData routes yourself.

Examples

Here, some example requests to the controller defined above. Assume that the application works on http://localhost:61842. We will show some basics. Since OData is a standard protocol, you can easily find more advanced examples on the web.

Getting List of Entities

Getting all 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":1
},{
"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":2
}
]
}

Getting a Single Entity

Getting the person with Id = 2.

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":2
}

Getting a Single Entity With Navigation Properties

Getting the person with Id = 1 including his phone numbers.

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":1,"Phones":[
{
"PersonId":1,"Type":"Mobile","Number":"4242424242","CreationTime":"2015-11-07T20:12:39.363+03:00","CreatorUserId":null,"Id":1
},{
"PersonId":1,"Type":"Mobile","Number":"2424242424","CreationTime":"2015-11-07T20:12:39.363+03:00","CreatorUserId":null,"Id":2
}
]
}

Querying

Here, a more advanced query includes filtering, sorting and getting top 2 results.

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":1
},{
"Name":"Douglas Adams","IsDeleted":false,"DeleterUserId":null,"DeletionTime":null,"LastModificationTime":null,"LastModifierUserId":null,"CreationTime":"2016-01-12T20:29:03+02:00","CreatorUserId":null,"Id":3
}
]
}

OData supports paging, sorting, filtering, projections and much more. See it's own documentation for more information.

Creating a New Entity

In this example, we're creating a new person.

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

{
Name: "Galileo Galilei"
}

Here, "Content-Type" header is "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": 4
}

If we get the list again, we can see the new person. We can also update or delete an existing entity as OData supports it.

Getting MetaData

We can get metadata of entities, as shown in this example.

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>

Metadata is used to investigate the service.

Sample Project

You get source code of the sample project here: https://github.com/aspnetboilerplate/sample-odata

ABP框架系列之四十三:(OData-Integration-OData集成)的更多相关文章

  1. ABP框架系列之四十一:(Nuget-Packages-Nuget包)

    Packages ASP.NET Boilerplate is distributed on nuget. Here, a list of all official packages. Abp Cor ...

  2. ABP框架系列之四:(Repositories-仓库)

    "Mediates between the domain and data mapping layers using a collection-like interface for acce ...

  3. ABP框架系列之三十三:(Module-System-模块系统)

    Introduction ASP.NET Boilerplate provides an infrastructure to build modules and compose them to cre ...

  4. ABP框架系列之四十九:(Startup-Configuration-启动配置)

    ASP.NET Boilerplate provides an infrastructure and a model to configure it and modules on startup. A ...

  5. ABP框架系列之四十二:(Object-To-Object-Mapping-对象映射)

    Introduction It's a common to map a similar object to another object. It's also tedious and repeatin ...

  6. ABP框架系列之十三:(Authorization-授权)

    Introduction Almost all enterprise applications use authorization in some level. Authorization is us ...

  7. ABP框架系列之四十:(Notification-System-通知系统)

    Introduction Notifications are used to inform users on specific events in the system. ASP.NET Boiler ...

  8. ABP框架系列之四十四:(OWIN)

    If you are using both of ASP.NET MVC and ASP.NET Web API in your application, you need to add Abp.Ow ...

  9. ABP框架系列之四十六:(Setting-Management-设置管理)

    Introduction Every application need to store some settings and use these settings in somewhere in th ...

随机推荐

  1. python http请求及多线程应用

    目录 概述 tomorrow包准备 运行环境遇到的问题 其他尝试未果 概述 今天, 使用python3 模拟下发包, http get 请求, 然后 采用tomorrow 多线程. 代码如下: # c ...

  2. Web项目中得到访问者的真实ip

    Web项目中得到访问者的真实ip 描述:最近要实现个功能是要记录管理员登录的真实ip,但在项目中如果直接使用request.getRemoteAddr()获得ip的话,获得的可能不是真实ip,是因为使 ...

  3. 深入学习 Java 序列化

    前言 对于Java的序列化,一直只知道只需要实现Serializbale这个接口就可以了,具体内部实现一直不是很了解,正好这次在重复造RPC的轮子的时候涉及到序列化问题,就抽时间看了下 Java序列化 ...

  4. Python:笔记1_字符串处理【转载】

    [转载自:https://www.cnblogs.com/houht/p/3308634.html] 1. 判断字符串str是否为空Approach 1:如果字符串长度为0,说明字符串为空,code如 ...

  5. 代码: !AJAX

    http://www.cnblogs.com/cwp-bg/p/7668840.html ajax和jsonp使用总结 2017-10-17 var requestUrl="http://l ...

  6. JAVA 类的三大特性,封装,继承,多态 的一些发现总结

    < 一 > 封装 < 二 > 继承 1,关于父类中的私有属性和方法,子类能不能访问的问题 1.1,子类不能通过继承拥有父类的私有属性和方法 1.2,子类可以从父类继承下来的 方 ...

  7. leetcode437

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  8. TXLSReadWriteII5 单元格读写

    unit Main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  9. jQuery 新添加元素事件绑定无效

    jQuery中事件绑定,大多使用on就足够了. 但是对于新添加的元素 on 的绑定事件 会不起作用. 因为 append 中的 节点是在整个文档加载之后才添加的,页面并不会为未来的元素初始化添加点击事 ...

  10. 项目(四)DHCP服务配置

    DHCP是由Internet工作任务小组设计开发的,专门用于为TCP/IP网络中的计算机自动分配TCP/IP参数的协议. 使用DHCP可以减少管理员的工作量,避免IP地址冲突,当网络修改IP地址网段时 ...