ABP框架 - OData 集成

文档目录

 

本节内容:

 

简介

OData在odata.org上的定义是:“一个开放的协议,允许创建和使用可查询、可互操作的RESTful api的简单的标准方式”。你可以在ABP里使用OData,Abp.Web.Api.OData的nuget包简化了它的使用方式。

 

安装

安装Nuget包

在我们的WebApi项目里,先安装Abp.Web.api.Odata的nuget包:

Install-Package Abp.Web.Api.OData

 

设置模块依赖

在我们的模块上设置对AbpWebApiOdataModule的依赖,例如:

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

 查看模块系统更好地理解模块依赖。

 

配置你的实体

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文档获取更多信息。

 

创建控制器

Abp.Web.Api.OData的nuget包包括了AbpODataEntityController基类(它扩展了标准的ODataController),用它可更容易地创建你自己的控制器,如下是一个为Person实体创建一个OData端点的例子:

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

这很简单,AbpODataEntityController的所有方法都是virtual,也就是说你可以重写Get、Post、Put、Patch、Delete和其它Action来添加自己的逻辑。

 

示例

这里我们列几个请求上面定义的控制器的基本的例子,假设应用工作在http://localhost:61842上,因为OData是一个标准的协议,你可以很容易地在网页上找到更深入的例子。

 

获取实体列表

获取所有person。

 

请求

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

 

响应

{
  "@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
    }
  ]
}

 

获取单个实体

获取Id=2的person。

 

请求

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

 

响应

{
  "@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
}

 

获取单个实体及导航属性

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

 

请求

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

  

响应

{
  "@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
    }
  ]
}

 

查询

这里列举一个稍微复杂点的查询,包含过滤,排序和获取最前面2条结果。

 

请求

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

 

响应 

{
  "@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支持分页,排序,过滤,投射等更多,请查阅它自己的文档

 

创建一个新实体

接下来的例子,我们创建一个新person。

 

请求

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

{
    Name: "Galileo Galilei"
}

此处,“Content-Type"头是”application/json“。

 

响应

{
  "@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
}

如果我们再次获取列表,我们可以看到这个新person,也OData支持更新或删除一个已经存在的实体。

 

获取元数据

我们可以获取实体的元数据,如接下来的例子所示。

 

请求

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

 

响应

<?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上获取这个示例项目的源代码。

posted @ 2016-10-30 19:40 kid1412 阅读(...) 评论(...) 编辑 收藏