& Lt; ABP framework & gt; OData integration, and abp framework odata Integration

Source: Internet
Author: User

<ABP framework> OData integration, and TTL framework odata Integration

Document directory

 

Content of this section:

  • Introduction
  • Install
    • Install the Nuget package
    • Set module Dependencies
    • Configure your entity
  • Create a controller
  • Example
    • Get Object List
      • Request
      • Response
    • Obtain a single object
      • Request
      • Response
    • Obtain a single object and navigation attributes
      • Request
      • Response
    • Query
      • Request
      • Response
    • Create a new object
      • Request
      • Response
    • Get metadata
      • Request
      • Response
  • Sample project

 

Introduction

OData is defined on odata.org as "an open protocol that allows you to create and use simple and standard methods of queryable and interoperable RESTful APIs ". You can use the nuget package of OData, ABP. Web. Api. OData to simplify its usage.

 

Install

Install the Nuget package

In our WebApi project, first install the nuget package of Abp. Web. api. Odata:

Install-Package Abp.Web.Api.OData

 

Set module Dependencies

Set the dependency on AbpWebApiOdataModule in our module, for example:

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

View the module system to better understand the module dependency.

 

Configure your entity

The object that OData needs to declare as its resource should be specified in the PreInitialize method of our module, as shown below:

[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 reference ODataModelBuilder and set the Person object for it. Similarly, you can use EntitySet to add other entities and view the OData document for more information.

 

Create a controller

Abp. web. api. the OData nuget package includes the AbpODataEntityController base class (which extends the standard ODataController), which makes it easier to create your own controller, the following is an example of creating an OData endpoint for the Person object:

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

This is simple. All methods of AbpODataEntityController are virtual, which means you can rewrite Get, Post, Put, Patch, Delete and other actions to add your own logic.

 

Example

Here we will list several basic examples of the controller defined in the request above. Assume that the application works inHttp: // localhost: 61842Because OData is a standard protocol, you can easily find more in-depth examples on the web page.

 

Get Object List

Obtain all persons.

 

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    }  ]}

 

Obtain a single object

Obtain 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}

 

Obtain a single object and navigation attributes

Obtain the phone number of a person with Id = 1.

 

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    }  ]}

 

Query

Here we will list a query with a slightly complex point, including filtering, sorting, and obtaining the first two 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, and projection. For more information, see its own documentation.

 

Create a new object

In the following example, we create a new person.

 

Request

POST http://localhost:61842/odata/Persons{    Name: "Galileo Galilei"}

Here, the "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 retrieve the list again, we can see the new person, and OData supports updating or deleting an existing object.

 

Get metadata

We can obtain the metadata of an object, as shown in the following 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 view service information.

 

Sample project

You can obtain the source code for this example from https://github.com/aspnetboilerplate/sample-odata.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.