ABP框架系列之四十三:(OData-Integration-OData集成)
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集成)的更多相关文章
- ABP框架系列之四十一:(Nuget-Packages-Nuget包)
Packages ASP.NET Boilerplate is distributed on nuget. Here, a list of all official packages. Abp Cor ...
- ABP框架系列之四:(Repositories-仓库)
"Mediates between the domain and data mapping layers using a collection-like interface for acce ...
- ABP框架系列之三十三:(Module-System-模块系统)
Introduction ASP.NET Boilerplate provides an infrastructure to build modules and compose them to cre ...
- ABP框架系列之四十九:(Startup-Configuration-启动配置)
ASP.NET Boilerplate provides an infrastructure and a model to configure it and modules on startup. A ...
- ABP框架系列之四十二:(Object-To-Object-Mapping-对象映射)
Introduction It's a common to map a similar object to another object. It's also tedious and repeatin ...
- ABP框架系列之十三:(Authorization-授权)
Introduction Almost all enterprise applications use authorization in some level. Authorization is us ...
- ABP框架系列之四十:(Notification-System-通知系统)
Introduction Notifications are used to inform users on specific events in the system. ASP.NET Boiler ...
- 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 ...
- ABP框架系列之四十六:(Setting-Management-设置管理)
Introduction Every application need to store some settings and use these settings in somewhere in th ...
随机推荐
- crossdomain.xml配置不当的利用和解决办法
00x1: 今天在无聊的日站中发现了一个flash小站,点进crossdomain.xml一看,震惊 本屌看到这个*就发觉事情不对 百度一下,这是一个老洞,配置不当能引起各种问题就算能远程加载恶意的s ...
- 转的,具体 https://www.cnblogs.com/icyJ/p/FreeShare.html
网络资源下载网址 屏幕录像.图像处理,汉化和破解版本很新.国内破解汉化:大眼仔旭 http://www.dayanzai.me/ 开发工具,系统,数据库 http://msdn.itellyou.cn ...
- C#进阶系列——AOP
一.AOP概念(转自) 老规矩,还是先看官方解释:AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程 ...
- 【转】修改mysql数据库的用户名和密码
修改mysql数据库的用户名和密码 更改密码 mysql -u root -p Enter password:*** mysql>use mysql; 选择数据库 Database change ...
- leetcode1026
public class Solution { Stack<int> S = new Stack<int>(); ; public void Trace(TreeNode ro ...
- 最近面试 Java 后端的感受!
来源:cnblogs.com/JavaArchitect/p/10011253.html 上周,密集面试了若干位Java后端候选人,工作经验在3到5年间.我的标准其实不复杂: 第一能干活,第二Java ...
- spring的ioc与aop原理
ioc(反向控制) 原理: 在编码阶段,既没有实例化对象,也没有设置依赖关系,而把它交给Spring,由Spring在运行阶段实例化.组装对象.这种做法颠覆了传统的写代码实例化.组装对象.然后一 ...
- 《Android Studio开发实战 从零基础到App上线》资源下载和内容勘误
转载于:https://blog.csdn.net/aqi00/article/details/73065392 资源下载 下面是<Android Studio开发实战 从零基础到App上线&g ...
- jquery 找同胞系列siblings() ,next() ,nextAll(), nextUntil(), prev(), prevAll(), prevUntil()
注:expr是指可选的参数,包含用于匹配元素的选择器表达式. .siblings(expr) ---查找所有兄弟(包括哥哥和弟弟) .next(expr) ---查找紧挨着的弟弟 .nextAll(e ...
- 用 Python + itchat 写一个爬虫脚本每天定时给女朋友发微信暖心话
https://github.com/sfyc23/EverydayWechat.git