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);

    }

}


發佈了185 篇原創文章 · 獲贊 1 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章