哪种数据访问方式更适合silverlight 4?

哪种数据访问方式更适合silverlight 4?,第1张

概述If you're a regular reader of my blog, you'll probably remember my pithy blog post where I stated that "It all depends..." to the question "Which Data Access Should I Use for Silverlight 3?"  The real

If you're a regular reader of my blog,you'll probably remember my pithy blog post where I stated that "It all depends..." to the question "Which Data Access Should I Use for Silverlight 3?"  The reality is that much like the similar question I am confronted with at user groups for the past decade ("What data access should I use in my .NET app?"). The reasons for picking a strategy are wIDe and varIEd so I will not try to analyze all possible outcomes,but I think the different strategIEs need to be explained better.

The three major candIDates in Silverlight 3 are Web Services (WCF/ASMX),ADO.NET Data Services and RIA Services.  In any situation,any of these will work. But they are suited to different styles and requirements. Here's the abrIDged differences between the stacks:

Web Services: Interface-based Data Access ADO.NET Data Services: liNQ-based Data Access with change management RIA Services: Interface-based Data Access with change management

Let's dive deeper into explaining these differences and why that might matter.

Web Services

Using web services is the trIEd and true method for communicating across the firewall. This pattern is well kNown and reliable. In general this requires you specify an interface for the CRUD operations as separate operations on a web service then dutifully call them in your Silverlight code.

Reasons to Use: Any existing investment in web services can be a great reason to use them (whether in code or skillset). Also,web services are often used in cases where a project wants a very close control over the flow from the application to the database.

Reasons to AvoID: With web services you end up having to keep track of the changes yourself and kNow what services to call with updates. Any need for batching or transactional support gets cumbersome and code-intensive.

ADO.NET Data Services

ADO.NET Data Services is a simple REST-based facility for data access. It relIEs on the http stack to help define the interface. GET calls become Reads,POST becomes Updates and so on. It uses ATOM or JsON for its serialization format so it is consumable by a varIEty of clIEnts.

Underneath its covers it takes this URI based API and converts it into a LINQ call for GETs and to API calls to the provIDer for inserts,updates and deletes. That means that ADO.NET is a thin layer who's purpose is to translate the URI model to data access code.  But its better than that...

The real power of ADO.NET Data Services for Silverlight is the inclusion of the ClIEnt library. This clIEnt library allows you to issue LINQ querIEs defined in the clIEnt and executed on the server. While the LINQ Syntax is somewhat limited when compared to the server,it fulfills the 80% case and ADO.NET Data Services allows you to add operations to fill in the rest when necessary. In addition,the clIEnt library includes a powerful data context class that can monitor changes and issue changes in batches with transactional support.

Exposing your data access with ADO.NET Data Services is about exposing queryable end-points instead of defining an interface. This is what makes is unique. For example,we can query our service using a LINQ query like so:

// Silverlight Code// Create the query using liNQvar qry = (from g in ds.Games           where g.Price < 50m           orderby g.name           select g) as DataServicequery<Game>;// Execute the query// (This part is getting cleaner in ADO.NET Data Services v1.5) qry.BeginExecute(new AsyncCallback(r =>  {    games2.ItemsSource = qry.EndExecute(r).ToList();    games2.displayMemberPath = "name";  }),null);

Reasons to Use: Want a simple,secure model where the developers can define the querIEs they need in code instead of changing the interface as the needs change.  The ADO.NET Data Services' clIEnt library makes the amount of code you write on the clIEnt small as it becomes LINQ calls and working with the context class.

Reasons to AvoID: When you want tight control over the interface to your data access and do not want developers issuing LINQ querIEs directly from the clIEnt code.

RIA Services

As the new kID on the block,RIA Services has a lot to offer. RIA Services is based on the IDea of creating a data access API on the server and at the same time creating the clIEnt code in Silverlight (and other platforms in the future). Its focused on sharing code between the clIEnt and the server including valIDation logic.  In addition,while it allows you to create a set interface,it also provIDes a context object which can monitor changes on the clIEnt and batch those changes back to the server. In some ways RIA Services is like a hybrID of Web Services and ADO.NET Data Services.

Because RIA Services is based on a server-sIDe query defined in its interface,on the clIEnt we call the query by calling the call on the interface:

// Silverlight Code// The context object that tracks changesXBoxGamesContext ctx = new XBoxGamesContext();// Our RIA query,really a call to an interfacevar qry = ctx.GetGamesByGenrequery("Shooter");// Bind the datatheList.ItemsSource = ctx.Load<Game>(qry).AllEntitIEs;

While in ADO.NET Data Services you are issuing LINQ querIEs,RIA Services also allow you to add LINQ constraints to the query endpoints.  For example instead of just creating a query by calling the interface,you can add LINQ Expressions to the endpoint like so:

var riaQry = ctx.GetGamesquery()                .Where(g => g.Price < 50m)                .OrderBy(g => g.name);LoadOperation<Game> op =   ctx.Load<Game>(riaQry);

Reasons to Use: RIA Services is a good choice if you expect to develop a straightforward application minimum number of tIErs. It works best in RAPId Application development scenarios versus large architected applications. RIA Services does support a mix between the interface based from Web Servics and and LINQ based querying that is supported by ADO.NET Data Services.

Reasons to AvoID: RIA Services Leverages a lot of magic code generation to make it work and that is harder to deBUG than it should be. Integration with large enterprises is possible,its not as easy it as it should be.

"So Which Would You Suggest?"

While I paint this picture of data access in Silverlight with a broad brush,I still get the question to suggest one over the other. The fact remains that there isn't a right/wrong answer here. A lot of it depends on the environment,project and skillset of the developers. So,no...I won't tell you what I suggest because I don't kNow the requirements and environment you work in. That's why they pay you the big bucks to be a developer,right?

总结

以上是内存溢出为你收集整理的哪种数据访问方式更适合silverlight 4?全部内容,希望文章能够帮你解决哪种数据访问方式更适合silverlight 4?所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1036517.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存