首页 > 代码库 > ODATA WEB API(二)----ODATA服务与客户端

ODATA WEB API(二)----ODATA服务与客户端

一、概述

  ODATA不经可以作为WebAPI建立相应的WEBAPI控制器,还可以建立ODataControl控制器,能够通过插件建立第三方ODataClinet类库;调用和使用数据变得简单可行。

二、建立OData Web API 服务端

1、通过NuGet添加第三方Microsoft.AspNet.OData;

2、建立相应的Model:Person和Trip

using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;namespace MyMVCODataTwo.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 MyMVCODataTwo.Models{    public class Trip    {        [Key]        public String ID { get; set; }        [Required]        public String Name { get; set; }    }}

3、添加程序集合,代替数据库请求:

using MyMVCODataTwo.Models;using System.Collections.Generic;namespace MyMVCODataTwo.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 Person()                {                    ID = "003",                    Name = "Elaine",                    Description = "It has roots in a piece of classical Latin literature from 45 BC, making Lorems over 2000 years old."                }            });        }    }}

4、WebApi配置:

public static class WebApiConfig    {        public static void Register(HttpConfiguration config)        {            config.MapODataServiceRoute("odata", null, GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));            config.EnsureInitialized();        }        private static IEdmModel GetEdmModel()        {            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();            builder.Namespace = "Demos";            builder.ContainerName = "DefaultContainer";            builder.EntitySet<Person>("People");            builder.EntitySet<Trip>("Trips");            var edmModel = builder.GetEdmModel();            return edmModel;        }    }

参考博客:

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

三、建立独立的控制台客户端:

1、通过扩展工具管理,下载OData Client Code ;

2、添加控制台引用程序,添加OData Client Item;

     修改MetadataDocumentUri 地址: public const string MetadataDocumentUri = "http://localhost:8090";

3、编写应用端代码:

 static void Main(string[] args)        {            // TODO: Replace with your local URI.             string serviceUri = "http://localhost:8090/";            var container = new DefaultContainer(new Uri(serviceUri));            foreach (var p in container.People)            {                Console.WriteLine("{0} {1} {2}", p.ID, p.Name, p.Description);            }            Console.Read();        }

参看博客:

http://www.cnblogs.com/bluedoctor/p/4384659.html

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-client-app

源码下载(包括服务端、客户端、以及OData客户端封装类库)

ODATA WEB API(二)----ODATA服务与客户端