本文转自:http://chris.eldredge.io/blog/2014/04/24/Composite-Keys/

In our basic configuration we told the model builder that our entity has a composite key comprised of an ID and a version:

1
2
3
4
5
6
7
8
9
10
public void MapDataServiceRoutes(HttpConfiguration config)
{
var builder = new ODataConventionModelBuilder(); var entity = builder.EntitySet<ODataPackage>("Packages");
entity.EntityType.HasKey(pkg => pkg.Id);
entity.EntityType.HasKey(pkg => pkg.Version); // snip
}

This is enough for our OData feed to render edit and self links for each individual entity in a form like:

http://localhost/odata/Packages(Id='Sample',Version='1.0.0')

But if we navigate to this URL, instead of getting just this one entity by key, we get back the entire entity set.

To get the correct behavior, first we need an override on our PackagesODataController that gets an individual entity instance by key:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class PackagesODataController : ODataController
{
public IMirroringPackageRepository Repository { get; set; } public IQueryable<ODataPackage> Get()
{
return Repository.GetPackages().Select(p => p.ToODataPackage()).AsQueryable();
} public IHttpActionResult Get(
[FromODataUri] string id,
[FromODataUri] string version)
{
var package = Repository.FindPackage(id, version); return package == null
? (IHttpActionResult)NotFound()
: Ok(package.ToODataPackage());
}
}

However, out of the box WebApi OData doesn’t know how to bind composite key parameters to an action such as this, since the key is comprised of multiple values.

We can fix this by creating a new routing convention that binds the stuff inside the parenthesis to our route data map:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class CompositeKeyRoutingConvention : IODataRoutingConvention
{
private readonly EntityRoutingConvention entityRoutingConvention =
new EntityRoutingConvention(); public virtual string SelectController(
ODataPath odataPath,
HttpRequestMessage request)
{
return entityRoutingConvention
.SelectController(odataPath, request);
} public virtual string SelectAction(
ODataPath odataPath,
HttpControllerContext controllerContext,
ILookup<string, HttpActionDescriptor> actionMap)
{
var action = entityRoutingConvention
.SelectAction(odataPath, controllerContext, actionMap); if (action == null)
{
return null;
} var routeValues = controllerContext.RouteData.Values; object value;
if (!routeValues.TryGetValue(ODataRouteConstants.Key,
out value))
{
return action;
} var compoundKeyPairs = ((string)value).Split(','); if (!compoundKeyPairs.Any())
{
return null;
} var keyValues = compoundKeyPairs
.Select(kv => kv.Split('='))
.Select(kv =>
new KeyValuePair<string, object>(kv[0], kv[1])); routeValues.AddRange(keyValues); return action;
}
}

This class decorates a standard EntityRoutingConvention and splits the raw key portion of the URI into key/value pairs and adds them all to the routeValues dictionary.

Once this is done the standard action resolution kicks in and finds the correct action overload to invoke.

This routing convention was adapted from the WebApi ODataCompositeKeySampleproject.

Here we see another difference between WebApi OData and WCF Data Services. In WCF Data Services, the framework handles generating a query that selects a single instance from an IQueryable. This limits our ability to customize how finding an instance by key is done. In WebApi OData, we have to explicitly define an overload that gets an entity instance by key, giving us more control over how the query is executed.

This distinction might not matter for most projects, but in the case of NuGet.Lucene.Web, it enables a mirror-on-demand capability where a local feed can fetch a package from another server on the fly, add it to the local repository, then send it back to the client as if it was always there in the first place.

To customize this in WCF Data Services required significant back flips.

Series Index

  1. Introduction
  2. Basic WebApi OData
  3. Composite Keys
  4. Default Streams

[转]Composite Keys With WebApi OData的更多相关文章

  1. [转]Web API OData V4 Keys, Composite Keys and Functions Part 11

    本文转自:https://damienbod.com/2014/09/12/web-api-odata-v4-keys-composite-keys-and-functions-part-11/ We ...

  2. OData – the best way to REST–实例讲解ASP.NET WebAPI OData (V4) Service & Client

    一.概念介绍 1.1,什么是OData? 还是看OData官网的简单说明: An open protocol to allow the creation and consumption of quer ...

  3. AspNet WebApi OData 学习

    OData介绍:是一个查询和更新数据的Web协议.OData应用了web技术如HTTP.Atom发布协议(AtomPub)和JSON等来提供对不同应用程序,服务 和存储的信息访问.除了提供一些基本的操 ...

  4. AspNet.WebAPI.OData.ODataPQ

    AspNet.WebAPI.OData.ODataPQ实现WebAPI的分页查询服务 AspNet.WebAPI.OData.ODataPQ实现WebAPI的分页查询服务-(个人拙笔) AspNet. ...

  5. AspNet.WebAPI.OData.ODataPQ实现WebAPI的分页查询服务-(个人拙笔)

    AspNet.WebAPI.OData.ODataPQ 这是针对 Asp.net WebAPI OData 协议下,查询分页.或者是说 本人在使用Asp.Net webAPI 做服务接口时写的一个分页 ...

  6. 主攻ASP.NET MVC4.0之重生:Asp.Net MVC WebApi OData

    1.新建MVC项目,安装OData Install-Package Microsoft.AspNet.WebApi.OData -Version 4.0.0 2.新建WebAPI Controller ...

  7. AspNet.WebAPI.OData.ODataPQ实现WebAPI的分页查询服务-(个人拙笔)(转)

    出处:http://www.bubuko.com/infodetail-827612.html AspNet.WebAPI.OData.ODataPQ 这是针对 Asp.net WebAPI ODat ...

  8. [转]OData – the best way to REST–实例讲解ASP.NET WebAPI OData (V4) Service & Client

    本文转自:http://www.cnblogs.com/bluedoctor/p/4384659.html 一.概念介绍 1.1,什么是OData? 还是看OData官网的简单说明: An open ...

  9. webAPi OData的使用

    一.OData介绍 开放数据协议(Open Data Protocol,缩写OData)是一种描述如何创建和访问Restful服务的OASIS标准. 二.OData 在asp.net mvc中的用法 ...

随机推荐

  1. Mounting VMDK files in Linux

    1.用 loop 方式挂载 vmdk 文件 losetup /dev/loop0 docker_pull-flat.vmdk 2.查看分区 [root@localhost]# parted /dev/ ...

  2. collections模块—— Counter

    ounter目的是用来跟踪值出现的次数.它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value.计数值可以是任意的Interger(包括0和负数).Counter类和 ...

  3. “全栈2019”Java第九十七章:在方法中访问局部内部类成员详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  4. Python 用队列实现多线程并发

    # Python queue队列,实现并发,在网站多线程推荐最后也一个例子,比这货简单,但是不够规范 # encoding: utf-8 __author__ = 'yeayee.com' # 由本站 ...

  5. C语言中printf与i++,C++中的cout

    一,printf与i++ 1,C语言中的printf是自右向左输出,. 2,而i++与++i不同的 i++首先取得i的值,下一行时候i = i + 1: ++i,首先i = i + 1,再取得i的值. ...

  6. jquery中的$()详解

    一.jQuery的三种$() $号是jQuery“类”的一个别称,$()构造了一个jQuery对象.所以,“$()”可以叫做jQuery的构造函数. 1.$()可以是$(expresion),即css ...

  7. 机器学习笔记(五) K-近邻算法

    K-近邻算法 (一)定义:如果一个样本在特征空间中的k个最相似的样本中的大多数属于某一个类别,则该样本也属于这个类别. (二)相似的样本,特征之间的值应该是相近的,使用k-近邻算法需要做标准化处理.否 ...

  8. 半年的iOS代码生活

    半年的iOS代码生活 在高考大军中拼杀过,也在大学校园中荒芜过,曾经低迷消沉,也常满怀壮志…… 但是最多的还是被称为小伙子以及自称为iOS工程师!博主就是这种喜闻乐见的这类人,实习一年后在2015年的 ...

  9. #Go# 常用类型转换

    #string 2 int int, err := strconv.Atoi(string) #string 2 int64 int64, err := strconv.ParseInt(string ...

  10. scp 一次拷贝多个文件

    用正则表达式去匹配即可, scp  *.tar  root@11.11.11.12:/root/ 拷贝当前目录下的所有tar类型的文件到服务器上