Microsoft Press Windows Azure Step by Step Note

WCF Data Services and OData

Open DataProtocol (OData) is an emerging protocol for querying and updating data across

the web. Ituses existing technologies such as HTTP, JSON (JavaScript Object Notation), and

AtomPublishing Protocol (AtomPub) to provide abstract access to a variety ofservices,

stores,applications, and data.

 

Exposing data using OData protocol

WCF DataService (providers : Entity Data Model)

1.Use theAdd New Item wizard to add a WCF Data Service

2.publicclass EstatesManagement : DataService<Entities>

{...

publicstatic void InitializeService(DataServiceConfiguration config)

{

// TODO: setrules to indicate which entity sets and service

operationsare visible, updatable, etc.

// Examples:

config.SetEntitySetAccessRule("*",EntitySetRights.All);

config.SetServiceOperationAccessRule("*",

ServiceOperationRights.All);

config.DataServiceBehavior.MaxProtocolVersion=

DataServiceProtocolVersion.V2;

}

 

http://msmvps.com/blogs/siva/archive/2010/11/04/exposing-data-via-odata.aspx

With WCFData Services (DS for short), providing data using OData is simple and easytoo. Out of the box, DS can pull data in two ways and expose it as one or more feeds:

ADO.NETEntity Framework - DS integrates with ADO.NET Entity Framework (EF) well and assuch it can provide an OData layer on top of an existing EF model and exposedata from the underlying relational database. In this way, each entity in themodel may be given a unique URI by DS and exposed as a feed (in OData terms,Entity Set - collection of business entities). The advantage with this is that DS itself gives the plumbing for CRUD operations that you may want to performon the entities exposed.

Plain .NETclasses - Lets you expose plain .NET classes as WCF Data Services. For exampleyou can expose your plain-old-C#-class (POCO) libraries as OData feeds simplyusing DS. Behind the scenes, DS uses .NET reflection to detect IQueryablecollections on a target class and exposes them as feeds. The advantage is thatyou have better control over the actual CRUD operations when invoked via HTTP.The downsides are many:

Obviously,you have to write extra code to expose your object collection as IQueryable.

DS supportsonly Select operation by default for this type of data source and Atomformatted output. If you want to provide Update, Delete and Insert operationsas well on the feeds then you should implement IUpdatable interface.

OnlyAtom-formatted results are supported. If you need JSON-formatted response, youhave to again write more code.

 

[DataServiceKey("LawyerID")]

public classLawyerInfo

{

    public int LawyerID {get; set;}

    public string Name {get; set;}

    public string OfficeCode {get; set;}

    public AddressInfo Address {get; set;}

}

 

[DataServiceKey("AddressID")]

public classAddressInfo

{

    public int AddressID {get; set;}

    public string AddressLine {get; set;}

    public string City {get; set;}

    public string State {get; set;}

}

public classLawyerDataProvider

{

    private static List<LawyerInfo> _ds;

    public LawyerDataProvider ()

    {

        _ds = new List<LawyerInfo> ();

        _ds.Add (new LawyerInfo ()

        {

            LawyerID = 89,

            Name = "James Horner",

            OfficeCode = "NY921",

            Address = new AddressInfo ()

            {

                AddressID = 8,

                AddressLine = "100 ParkAve",

                City = "New York",

                State = "NY"

            }

        });

        _ds.Add (new LawyerInfo ()

        {

            LawyerID = 61,

            Name = "Mathew Perry",

            OfficeCode = "IL017",

            Address = new AddressInfo ()

            {

                AddressID = 15,

                AddressLine = "625 WestMadison",

                City = "Chicago",

                State = "IL"

            }

        });

        _ds.Add (new LawyerInfo ()

        {

            LawyerID = 17,

            Name = "Samatha Mathis",

            OfficeCode = "NJJC",

            Address = new AddressInfo ()

            {

                AddressID = 35,

                AddressLine = "JournalSquare",

                City = "Jersey City",

                State = "NJ"

            }

        });

    }

 

    public IQueryable<LawyerInfo> Lawyers

    {

        get {return_ds.AsQueryable<LawyerInfo>();}

    }

}

 

public classLawyerDs : DataService<LawyerDataProvider>

{

    public static voidInitializeService(DataServiceConfiguration config)

    {

        config.DataServiceBehavior.MaxProtocolVersion= DataServiceProtocolVersion.V2;

        config.SetEntitySetAccessRule("Lawyers", EntitySetRights.All);

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值