API 设计:公开 XML 或对象 #2 [英] API Design: Expose XML or Objects #2

查看:22
本文介绍了API 设计:公开 XML 或对象 #2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了这个问题:Expose XML or对象 - 感谢大家的回应.

I recently asked this question: Expose XML or Objects - thanks all for the responses.

有一点需要澄清.

  • API 将始终被远程访问(即作为服务),最有可能通过网络服务或 WCF.

我同意理论上将对象作为输入/输出公开的强类型 API 是正确的方式.但是,我觉得对于公开 XML 仍有争论.在我看来,使用 XML 的原因是:

I agree that in theory a strongly typed API exposing objects as the inputs/outputs is the right way to go. However, I feel that there is still an argument to be made for exposing XML. As I see it the reasons for using XML are:

  1. 业务规则可由业务分析师在 Schematron 中编写.
  2. 该接口是弱类型的,但只要调用它,就可以根据数据和业务规则验证数据.
  3. 服务的实现会更简单.无需创建域对象模型.
  4. 已经定义了 XML 模式(我们有一个模式的数据字典).
  5. 使用网络服务技术意味着基于 XML 的 API 不需要随着新汽车类型"的添加而改变,例如

  1. The business rules can be written by business analysts in Schematron.
  2. The interface is weakly typed, but as soon as it is called the data can be validated against data and business rules.
  3. The service's implementation will be simpler. There won't be any need to create an domain object model.
  4. The XML schema is already defined (we have a data dictionary of schema).
  5. Using web services technology means that an XML based API will not need to change as new car 'types' are added, e.g.

void AddNewCar( string newCarXml )    
string[] GetCars( /* some query conditions */ )

如果我们使用基于对象的 API,那么添加新类型将需要一个新的查询方法来定义可以返回的可能派生类型(参见 扩展网络服务).像这样更新 Web 服务将需要重建和重新部署此服务和所有现有客户端.

If we used an object based API then adding a new type would require a new query method defining the possible derived types that could be returned (see extending web services). Updating the web service like this would require this services and all existing clients to be rebuilt and redeployed.

基于对象的 API 给我们带来了什么?强类型声明式接口.它不提供比 XML 更多的抽象(XML 本身就是一个抽象).基于对象的 API 的成本是多少?它花费了一整套域对象,这些域对象需要业务规则和数据验证.

What does an object based API give us? A strongly typed declarative interface. It does not provide any more abstraction than XML (XML is itself an abstraction). What does the object based API cost? It costs an entire set of domain objects which will need business rules and data validation.

那么,我的问题是什么?给我一个不可战胜的、无可争辩的理由,为什么我应该和对象一起去.

So, what is my question? Give me an un-defeatble, unarguable reason why I should go with objects.

推荐答案

  • 对象可以表现得更好(这里考虑二进制序列化).
  • 对象可以有更强大的简单类型验证.
  • 对象允许您将验证和业务规则更接近数据结构定义.
  • 对象本质上允许您编写更简单的业务规则和验证,因为其中大部分内容都嵌入在对象定义本身中.
  • 对象也可以定义行为.
  • .Net 使通过序列化将对象转换为 Xml 并再次返回变得简单,为对象提供与 xml 相同的大部分好处.
  • 这篇关于API 设计:公开 XML 或对象 #2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆