[转]How to Use Web API OData to Build an OData V4 Service without Entity Framework

本文转自:http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity-framework/

 

There are quite a lot of tutorials showing how to create OData services using Web API OData, but these requires Entity Framework and a database server behind. If you want a quick try or you have your own way of implementing data sources, these tutorials may not be the best fit. In this article, we will show how to build an OData service using in-memory data as data source and with basic function.

Create the solution

Create a new solution following File -> New -> Project -> Web, then choose ASP.NET Web Application. Name it with Demo. Then in the upcoming dialogue box, choose Empty and check Web API, click OK.

Install NuGet packages

Run the following command in the Package Manager Console.

PM> Install-Package Microsoft.AspNet.OData

Add Models

In this getting-started example, we just add two model class Person.cs and Trip.cs under folder Models. Person can navigate to Trips.

using System;
using System.ComponentModel.DataAnnotations; namespace Demo.Models { public class Person { [Key] public String ID { get; set; } [Required] public String Name { get; set; } public String Description { get; set; } public List<Trip> Trips { get; set; } } }
using System;
using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace Demo.Models { public class Trip { [Key] public String ID { get; set; } [Required] public String Name { get; set; } } }

The attributes [Key] and [Required] are all from System.ComponentModel.DataAnnotations meaning the property is key and required seperately.

In-Memory data source

This tutorial uses in-memory data source, which is more flexible. Below are only one way to implement, you can definitely have your only way.

Add a folder DataSource and add a class file DemoDataSources.cs with the code below.

using Demo.Models;
using System.Collections.Generic; namespace Demo.DataSource { public class DemoDataSources { private static DemoDataSources instance = null; public static DemoDataSources Instance { get { if (instance == null) { instance = new DemoDataSources(); } return instance; } } public List<Person> People { get; set; } public List<Trip> Trips { get; set; } private DemoDataSources() { this.Reset(); this.Initialize(); } public void Reset() { this.People = new List<Person>(); this.Trips = new List<Trip>(); } public void Initialize() { this.Trips.AddRange(new List<Trip>() { new Trip() { ID = "0", Name = "Trip 0" }, new Trip() { ID = "1", Name = "Trip 1" }, new Trip() { ID = "2", Name = "Trip 2" }, new Trip() { ID = "3", Name = "Trip 3" } }); this.People.AddRange(new List<Person> { new Person() { ID = "001", Name = "Angel", Trips = new List<Trip>{Trips[0], Trips[1]} }, new Person() { ID = "002", Name = "Clyde", Description = "Contrary to popular belief, Lorem Ipsum is not simply random text.", Trips = new List<Trip>{Trips[2], Trips[3]} }, new 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值