Odata 1-1 entry: implement a simple odata Service

Source: Internet
Author: User

1. What is odata: odata is a Web protocol for querying and updating data. Odata uses Web technologies such as HTTP, Atom publish protocol (atompub), and JSON to provide different applications.Program, Service and storage Information Access

 

2. odata advantages (my opinion)

1) A common cross-language protocol,

2) based on. net implementation can be very convenient to implement some functions (such as using lambda expressions on the client, using odata to transmit requests to the server, and finally the server returns a result set, note what operations to filter and sort are implemented on the server. I personally think this function is encapsulated thoroughly and the implementation is also cool)

 

BelowCodeIs the simplest implementation of odata.

 

Code

  [Dataservicekey (  "  ID  " )]  //  Primary Key  
Public Class Userinformation
{
Public Int Id { Get ; Set ;}
Public String Username { Get ; Set ;}
Public Int Age { Get ; Set ;}
}
Public Partial Class Datacontext
{
Public Iqueryable < Userinformation > Userinformations
{
Get
{
List < Userinformation > Users = New List < Userinformation > ()
{
New Userinformation () {ID = 1 , Username = " Test 1 " , Age = 30 },
New Userinformation () {ID = 2 , Username = " Test 2 " , Age = 30 },
New Userinformation () {ID = 3 , Username = " Test 3 " , Age = 20 },
};
Return Users. asqueryable < Userinformation > ();
}
}
}
Public Class Wcfdataservice2: dataservice < Datacontext >
{
Public Static Void Initializeservice (idataserviceconfiguration
Config)
{
Config. setentitysetaccessrule ( " * " , Entitysetrights. All ); // Define Access Permissions
}
}

To implement odata for multiple objects, you only need to add different attributes to datacontext.

This kind of code is similar to the use of EF in the client (it is implemented using odata internally)

 

Access the service directly when running, as shown below:

The server mainly tells the client what he has implemented.

Code

    <?  XML version = "1.0" encoding = "UTF-8" standalone = "yes"  ?>   
< Service XML: Base = "Http: // localhost: 12002/wcfdataservice2.svc /" Xmlns: Atom = "Http://www.w3.org/2005/Atom" Xmlns: app = "Http://www.w3.org/2007/app" Xmlns = "Http://www.w3.org/2007/app" >
< Workspace >
< Atom: Title > Default </ Atom: Title >
< Collection Href = "Userinformations" >
< Atom: Title > Userinformations </ Atom: Title >
</ Collection >
</ Workspace >
</ Service >

 

 

 

 

The following is an example of client usage:

1. Reference Web Services --#

2. Write the following code ~~

 

Code

 Datacontext servercontext  =     New  Datacontext (  New  Uri (  "  Http: // localhost: 12002/wcfdataservice2.svc/  "  ));
VaR users = Servercontext. userinformations. Where (P => P. ID > 0 ). Orderby (P => P. Age). Skip ( 1 ). Take ( 2 );
Foreach (VAR user In Users)
{
Console. writeline ( " {0}-{1} {2} " , User. ID, user. username, user. Age );
}

 

The code is very streamlined, and it is very convenient to use high-performance and convenient lambda expressions on the client.

If you design a server program, odata is a good choice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.