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.