如何为新的 Silverlight 应用程序在 WCF、REST、POX 和 RIA 服务之间进行选择
Silverlight 应用程序可以通过很多不同的方式连接回其服务器。包括
- WCF - Windows Communication Foundation
- REST(请参阅 还有)
- ADO.NET 数据服务 (或者这只是 REST?)
- POX - 普通旧 XML(例如基本 xml)
- RIA 服务
对于每项服务,请说明其用途以及您何时会使用或不会使用它。我并不是在寻找大量的细节,只是在寻找一套在它们之间进行选择的“经验法则”。
(问题是,在设计您的第一个 Silverlight 应用程序时,当您没有时间学习所有这些应用程序时,您知道要使用什么。)
如果我在这个问题中用 WPF 替换 Silverlight,它会对您的答案产生什么影响? (我假设 WPF 由于防火墙和管理策略的原因,无法直接连接到数据库。)
There a lot of different ways a Silverlight application can connect back to it’ server. Including
- WCF - Windows Communication Foundation
- REST (see also)
- ADO.NET Data Services (or is this just REST?)
- POX - Plain Old XML (E.g basic xml)
- RIA services
For each of these please say what it’s for and when you would or wouldn’t use it. I am not looking for a great level of details just a set of “rules of thumb” for choosing between them.
(The problem is when designing your first Silverlight application knowing what to use when you don’t have time to learn all of them.)
If I was to replace Silverlight with WPF in this question what effect would it have on your answers? (I am assuming with WPF that due to firewalls and admin policies a direct connect to the database is not an option.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的两(欧元)美分:
当服务可以被视为应用程序的业务层时,即当您的服务具有“智能”操作(例如“CalculateDiscountForClient”)时,WCF 似乎最适合。
当您的应用程序基本上以数据为中心并且服务只是数据库的前端时,ADO.NET 数据服务(实际上,只是一个 REST 实现)似乎很合适。也就是说,您所有的服务方法都是 GetCustomers、CreateInvoice 等类型。
RIA 服务是一项非常新的技术,我还没有尝试过,但它似乎对于创建 Silverlight 部分和服务紧密耦合的应用程序非常有用:您在服务项目中定义服务类和方法,它们会在设计时自动复制到 Silverlight 项目。此外,您还可以定义 WCF 样式的“操作”方法和 ADO.NET 数据服务样式的“数据”方法。看起来很有前途。
如果您将来有可能将客户端部分从 Silverlight 更改为任何其他技术(例如 HTML+AJAX),请使用 POX,因为它是最具互操作性的选项。
关于 WPF 的差异,我唯一能想到的是,对于数据访问,只要有可能,我都会使用直接 ADO.NET 数据连接(正确嵌入数据访问层、LINQ to SQL 等)而不是 ADO.NET数据服务,因为它更加灵活。无论如何我必须说我从来没有在 WPF 中开发过任何东西。
My two (euro) cents:
WCF seems best suited when the service can be viewed as the business layer of the application, that is, when your service has "intelligent" operations like "CalculateDiscountForClient".
ADO.NET Data Services (indeed, just a REST implementation) seems appropriate when your application is basically data-centric and the service is simply a front-end for the database. That is, all your service methods are of type GetCustomers, CreateInvoice, etc.
RIA services is a very new technology that I haven't experimented with yet, but it seems to be useful to create applications in which the Silverlight part and the service are very tightly coupled: you define your service classes and methods in the service project, and they are automatically replicated to the Silverlight project in design time. Also, you can define both WCF-style "action" methods and ADO.NET Data Services-style "data" methods. Looks promising.
Use POX if there is a chance that you change the client part from Silverlight to any other technology (for example HTML+AJAX) in the future, since it is the most interoperable option.
About differences for WPF, the only I can think of, is that for data access, whenever possible I would use direct ADO.NET data connections (properly embedded in a data access layer, LINQ to SQL or the like) instead of ADO.NET Data Services, since it is way more flexible. I must say anyway that I have never developed anything in WPF.
我们使用 RIA,这是我所知道的唯一选项之一,但我确实知道它,所以这里是我的一些想法。
RIA 尚未完成。目前正在研究中。如果您计划很快完成,并且担心必须支持可能发生很大变化的事物,那么您可能需要考虑其他选择。如果这是一个新项目,并且您将长期支持它,RIA 可能会变得更容易使用。
话虽如此,我认为 RIA 7 月预览版的工作方式以及最终版本的工作方式不会有太多变化。此外,支持水平似乎表明这将成为与 Silverlight 中的服务器对话的“方式”。
只是因为值得一提,有一些链接:
http://blogs.msdn.com/brada/布拉德·艾布拉姆斯有一个他不断更新的例子。
http://forums.silverlight.net/forums/53.aspx 这是您的位置去提问。
http://www.riaservicesblog.com/Blog/ Colin Blair 知道他的东西,而且他非常有帮助。
We use RIA, and that's the only one of the options that I know, but I do know it, so here's some of my thoughts.
RIA isn't finished yet. It is being worked on. If you are planning to be finished soon, and you're worried about having to support something that has a potential to change quite a bit, then you might want to consider other options. If this is a new project, and you're going to be supporting it for a long time, RIA will probably get easier to use.
Having said that, I kind of think that there won't be many changes in the way the July Preview of RIA works and the way that a finished version will work. Also the level of support seems to suggest that this will become "The Way" to talk to a server in Silverlight.
Just cause it's worth mentioning, have some links:
http://blogs.msdn.com/brada/ Brad Abrams has an example that he is continually updating.
http://forums.silverlight.net/forums/53.aspx this is where you go to ask questions.
http://www.riaservicesblog.com/Blog/ Colin Blair knows his stuff, and he is very helpful.
我想我不会再得痘了。如果您编写 WCF 以使服务本身独立于绑定,并且绑定是在配置文件中完成的,那么 WCF 几乎与传输和协议无关。它可以执行 SOAP、JSON、REST 或其自己形式的二进制序列化。所有这些都在绑定中。在内部,WCF 仅指定在操作和数据契约方面公开的内容(全部由类、方法和属性属性定义)。 WCF 在这方面为您提供了巨大的灵活性,2010 年还会提供更多灵活性。
从 Silverlight 方面来看,WCF 要求您编写一些管道代码。 .NET 框架具有在 Silverlight 项目中构建代理的工具,但您必须准备好异步处理所有 WCF 响应,并且代理无法捕获服务抛出的异常。
.NET RIA 服务隐藏了这一切。它在幕后使用 WCF,但这是完全隐藏的。您不必编写异步代码。您定义一次验证,主要是声明式的,它可以在服务器端和客户端工作。第 1 版将针对 Silverlight,因此您无法在其他地方使用该服务。该范围应该在以后的版本中扩大。
我对 ADO.NET 数据服务了解不够,无法进行比较。我怀疑答案取决于您是否希望将您的数据暴露给更多的 Silverlight 用途。
.NET RIA 服务看起来是我想要的方向(我自己考虑了这些问题,考虑到了大型应用程序)。对我来说,最大的问题是在服务层中实现非常大的功能集合,并且无法直接编码到数据访问层(我们必须能够在 SQL Server 或 Oracle 上运行)。
使用 WPF 而不是 Silverlight 会改变一切,具体取决于数据所在的位置。这就像 Winforms 与 ASP.NET 的老问题一样。使用 WPF,您正在构建 Windows 客户端应用程序,并且根本不需要使用任何形式的基于服务的数据接口,除非您的数据访问迫使您使用它。您仍然希望使用 MVVM、MVC 或 MVP 将数据和业务与表示代码分开。除此之外,您可以选择将数据访问视为一个层,而不是一个完全独立的层。
I think I would not go POX ever again. If you write WCF so that the service itself is independent of the binding and binding is done in configuration files, then WCF is pretty much agnostic about transport and protocol. It can do SOAP, JSON, REST, or its own form of binary serialization. All of this is in the binding. Internally, WCF only specifies what gets exposed in terms of operation and data contracts (all defined by class, method, and property attributes). WCF gives you tremendous flexibility in this regard, with more to come in 2010.
From the Silverlight side, WCF requires that you write some plumbing code. The .NET frameowrk has the tools to build the proxy in your Silverlight project, but you must be prepared to handle all WCF responses asynchronously, and the proxy cannot catch exceptions thrown by the service.
.NET RIA Services hides all this. It uses WCF under the covers, but that is completely hidden. You don't have to write asynchronous code. You define validation once, mostly declaratively, and it works both server-side and client-side. Release 1 will be targeted for Silverlight, so you don't get the versatility to use the service elsewhere. That scope is supposed to be broadened in later releases.
I don't know enough about ADO.NET Data Services to compare. I suspect the answer would depend on whether you want to expose your data to more than just Silverlight usage.
.NET RIA Services looks like the direction I'd want to go (looking at these issues myself, with a large application in mind). The big issues for me will be implementing a very large collection of functionality in the service layer, and not being able to code directly to the data access layer (we have to be able to run on either SQL Server or Oracle).
Using WPF instead of Silverlight changes everything, depending on where your data resides. It's like the old question of Winforms vs. ASP.NET. With WPF, you're building a Windows client app, and you don't need to use any form of service-based data interface at all, unless your data access forces you into it. You'll still want to separate data and business from presentation code, using MVVM, MVC, or MVP. Other than that, you have the option to treat data access as a layer, rather than a wholy independent tier.
WCF 是 Microsoft 的服务通信标准。我强烈建议任何人使用 WCF Web API(使用 WCF,但专门为 REST 定制)创建服务层,该服务将于 2012 年 4 月发布。WCF Web API 目前处于预览模式。
请记住这些经验法则:
- 你的用户界面将比你的服务层变化得更快。 RESTful 服务将在几年内出现,Silverlight 可能不会
- 您的服务会成为 API 吗?嗯...WCF REST 是正确的选择
- 您会混合使用 JavaScript 和 Silverlight 代码吗? WCF REST 将使您的生活更轻松
- 您是否有移动组件(因为 Silverlight 不能在 iOS 或 Android 上运行)...REST 是首选。
不要针对技术进行定制,而是针对整个应用程序进行定制。
WCF is Microsoft's standard for service communication. I would strongly advise anyone to create a service layer using WCF Web APIs (uses WCF, but tailored specifically for REST), which is coming out this April 2012. WCF Web APIs is currently in preview mode.
Remember these rules of thumb:
- your UI will change faster than your service layer. RESTful services will be around in several years, Silverlight probably won't
- will your services ever be APIs? Well...WCF REST is the way to go
- will you mix JavaScript and Silverlight code? WCF REST will make your life easier
- will you have a mobile component (since Silverlight won't run on iOS or android)...REST is preferred.
Don't tailor to the technology, but the app as a whole.
如果你想创建一个Silverlight应用程序并且你不关心其他客户端,那么我会选择RIA服务。使用起来非常轻松,您无需担心客户端的连接是如何建立的(即无需客户端配置)。 RIA 还为客户端上的所有实体生成类,如果需要,您甚至可以与客户端共享您自己的“服务器”代码(对于枚举或扩展方法很有用)。
备注:
If you want to create a Silverlight Application and you do not care about other clients, then I would choose RIA Services. It is quite painless to use and you do not need to worry how the connection from the client is made (i.e. no client side configuration necessary). RIA also generates classes for all your entities on the client and you can even share your own "server" code with the client if required (useful for enumerations or extension methods).
Remarks: