silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1) 原文:http://www.silverlightshow.net/items/Producing-and-Consuming-OData-in-a-Silverlight-and-Windows-Phone-7-appli


Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1)


原文:http://www.silverlightshow.net/items/Producing-and-Consuming-OData-in-a-Silverlight-and-Windows-Phone-7-application.aspx


This article is Part 1 of the series “Producing and Consuming OData in a Silverlight and Windows Phone 7 application.”.

Watch the video tutorial

Download the source code | Download the slides

The Open Data Protocol (OData) is simply an open web protocol for querying and updating data. It allows for the consumer to query the datasource (usually over http) and retrieve the results in Atom,JSON or plain XML format,including pagination,ordering or filtering of the data.

In this series of articles,I am going to show you how to produce an OData Data source and consume it using Silverlight 4 and Windows Phone 7. Read the complete series of articles to have a deep understanding of OData and how you may use it in your own applications.

 

CreaTing our first OData Data source.

Most OData examples that I’ve seen are using one of the many exisTinOData Producers. The Netflix catalog is one of the most popular live OData services that is used in these types of demos. I believe that the best way to teach someone how to use a technology is by building each component starTing from File->New Project. I will begin this series by creaTing an OData producer. In this example,we will build our first OData Data source by using sql Server Compact Edition 4.0.

GetTing Setup (you will need…) 

  1. Visual studio 2010 with at leasService Pack 1.
  2. @H_188_56@microsoft sql Server Compact 4.0 @H_188_56@microsoft Visual studio 2010 SP1 Tools for sql Server Compact 4.0. @H_188_56@microsoft sql Server Compact 4.0 Tools.

Please note: All of these tools can be acquired using the Web Platform Installer 3.0 available on MSDN. 

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

 
(screenshot of the Web Platform Installer 3.0 with the sql components highlighted)

After installing the necessary components,we will begin at File->New Project inside of Visual studio 2010.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Let’s begin by clicking Web –> ASP.NET Empty Web Application and giving it a Name of “SLShowODataP1”.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Now that our project is created,let’s go ahead and right click on our Solution and add an item.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

you will be presented with the “Add New Item” screen. Go ahead and search for the keyword “compact” and SELEct sql Server Compact 4.0 Local Database. We will go ahead and give this a name of “Customers.sdf”. Please note: if you do not have the option to SELEct sql Server Compact 4.0 then you will need to re-read the section titled,“GetTing started” above.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

After you click on “Add”,you will be presented with the message located below. Go ahead and click “Yes”. This will simply add an “App_Data” folder to our ASP.NET Application.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Now that the local database is created. We will need to right click on it and SELEct Open. 

@L_772_25@

You can Now look inside of “Server Explorer” and see your “Customers.sdf” file. Go ahead and right click on Tables and SELEct “create table.”

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Name the table,“CustomerInfo” and setup the following columns. (Note: You can look at the image below or follow the column guide below)

  1. ID – Data Type as int – PriMary Key – YES (Make sure to set Identy to TruE)
  2. FirstName – Data Type as nvarchar and everything else should be default.
  3. LastName – Data Type as nvarchar and everything else should be default.
  4. Address – Data Type as nvarchar and everything else should be default.
  5. City – Data Type as nvarchar and everything else should be default.
  6. State – Data Type as nvarchar and everything else should be default.
  7. Zip – Data Type as nvarchar and everything else should be default.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Now that we have our database setup,lets go ahead and generate some sample data. Right click on CustomerInfo and SELEct “Show Table Data”.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Let’s go ahead and generate some “Fake Data”. I like to use “Fake Name Generator” to generate good sample data. You can use whatever you like. Below is a screenshot of what I used.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Let’s go ahead and create the service Now. Go BACk to your project and right click your project and SELEct “Add” –> “New Item”.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Do a search for “entity data model” and SELEct ADO.NET Entity Data Model. Give it a name of “CustomersModel.edmx”.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

The following Wizard will be shown on your screen. We are going to take the default option “Generate the model from an exisTing database” and SELEct “Next”.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

The Customers.sdf should be the default data connection on the second screen. If its not SELEct it and leave everything else alone. Go ahead and click “Next”.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

We are going to put a check in Tables to SELEct all of our tables (which should only be onE). You can leave the other options at their default SELEction. After you are finished with that click “Finish”.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

At this point,our Model has been created by Visual studio and we are ready to create the oData service.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Do a search for “data service” and SELEct WCF Data service. Give it a name of “Customerservice.svc

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

After the service is created,we will want to make a few minor tweaks to the supplied code. If you followed my tutorial then you should be able to copy/paste the following code snippet into your Customerservice.svc.cs file. If this doesn’t work then don’t worry you can always scroll to the top of this article and download the complete source code.

@H_609_197@ 1: public class Customerservice : Dataservice<CustomersEntities>
 2:     {
 3:         // This method is called only once to initialize service-wide policies.
 4:         static void Initializeservice(DataserviceConfiguration config)
 5:         {
 6:             config.SetEntitySetAccessRule(@H_450_229@"CustomerInfoes",EntitySetrights.AllRead);
 7:             config.DataserviceBehavior.MaxProtocolVersion = DataserviceProtocolVersion.V2;
 8:         }
 9:     }

On line 1,you will see that the Dataservice is the main entry point for an ASP.NET ADO service. it is expecTing a generic that was actually created by Entity Framework called CustomersEntities. This code was generated by Visual studio and all we have to do is plug it in.

We did not make any modifications to line 4,but on line 6,we added “CustomerInfoes” which was also created by Entity Framework. If you remember from an earlier step,we SELEcted the option to “pluralize” generated object names.This is the reason this field is named “CustomerInfoes”.  We also set the Entity rights to full read access. The protocol version on line 7 was set to V2 as this relates to the AtomPub protocol.

Go ahead and right-click the service named,“Customerservice.svc” and SELEct “View in Browser”

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

 

Depending on what browser you are using,you should see the following:

(in my case I am using Internet Explorer 8)

URL – “http://localhost:6844/Customerservice.svc/” – Note the localhost:6844 will be different depending on the port number Visual studio generated for your project. 

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

At this point you have “produced” an OData Data service. Let’s examine the URI:

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

This is called a service Root URI – it simply identifies the root of the oData service.

We can Now navigate one Level up and start to view our “CustomerInfoes” collection. Note: You may have to “View source” to view the raw xml.

As you can see from the image below,we are seeing our first entry in our collection which is “MIchael Crump”

URL - http://localhost:6844/Customerservice.svc/CustomerInfoes

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

We can Now take this same collection and navigate to an individual entry by doing the following:

URL - http://localhost:6844/Customerservice.svc/CustomerInfoes(2)/

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

As you can see we just SELEcted our second item from the Collection.

Let’s go ahead and create a filter. We only want to show items that the state equal “AL”.

URL - http://localhost:6844/Customerservice.svc/CustomerInfoes?$filter=State eq 'AL'

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Let’s take this another step forWARD and add multiple parameters. Let’s filter by the ID greater than 3 and order the data by FirstName.

URL - http://localhost:6844/Customerservice.svc/CustomerInfoes?$filter=ID gt 3&$orderby=FirstName

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Let’s examine the URI Now:

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

To recap:

  1. The service root URI identifies the root of an OData service.
  2. The resource path section of a URI identifies the resource to bE interacted with (such as Customers,a single Customer,orders related to Customers in London,and so forth).
  3. System Query Options are query String parameters a client may specify to control the amount and order of the data that an OData service returns for the resource identified by the URI.

DeFinitions provided by the official OData URI Conventions documentation.

As you can see there is a lot of possibilities of querying the data within the Web Browser. If you want to look at a complete list then visit the OData site to learn more. 

Let’s go ahead and take a look at querying the data using a free tool called “LinqPad”.To get started go ahead and download linqpad at http://linqpad.net/. Once you have it installed,we will need to keep our Visual studio project running and Add an connection to the OData service.

you can do this by clicking “Add Connection” –> WCF Data services (OData) –> Next.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

You are going to want to add in your URL to your service on the screen below.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

You can leave the username/password fields blank. Now hit OK to conTinue.

service. So make sure under databases your OData service is SELEcted. Now type:

from g in CustomerInfoes 
select g

You should see the results listed below:

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Let’s try one more in order to demonstrate a where and orderby clause.

Now type:

(from g in CustomerInfoes 
where g.ID > 2 
orderby g.FirstName 
SELEct g)

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

Conclusion

you would produce an OData Data source and learn some basic sorTing and filtering using the web browser and LinqPad. In the next part of the series,I am going to show you how to consume this data in a Silverlight Application and in Part 3 a Windows Phone 7 Application. Again,thanks for reading and please come BACk for the final two parts.

自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))

@H_919_40@michael Crump is an MCPD that has been involved with computers in one way or another for as long as he can remember,but started professionally in 2002. After spending years working as a systems administrator/tech support analyst,Michael branched out and started developing internal utilities that automated repetitive tasks and freed up full-time employees. From there,he was offered a job working at McKesson corporation and has been working with some form of .NET and VB/C# since 2003.

He shares his findings in his personal blog: http://michaelcrump.net and he also tweets at:@mbcrump

大佬总结

以上是大佬教程为你收集整理的自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))全部内容,希望文章能够帮你解决自定义odata数据源(Producing and Consuming OData in a Silverlight and Windows Phone 7 application (Part 1))所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。