WCF服务是否应该返回普通的旧对象,或者是你正在使用的实际类? [英] Should WCF services return plain old objects, or the actual class that you're working with?

查看:121
本文介绍了WCF服务是否应该返回普通的旧对象,或者是你正在使用的实际类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用另一家公司的WCF服务,并且返回 object 类型的对象。有没有理由不返回实际的类,并返回一个必须转换为正确形式的对象?

I am consuming a WCF service from another company, and it is returning an object of type object. Is there a reason not to return the actual class, and return an object that must be cast into the correct form?

例如,如果Web服务返回一个对象类型 OrderStatus ,为什么会返回一个普通的对象

For example, if the web service returns an object of type OrderStatus, why would you instead return a plain old object?

如果我错了,请更正我,但这将要求我手动(通过电子邮件或其他)获得 OrderStatus 类的定义,并将其放入我的解决方案。此外,我必须显式地将对象转换为 OrderStatus 对象。更不用说公司可以更改服务以返回另一个类型的对象,我的代码将停止工作。

Correct me if I'm wrong, but this will require me to get the definition of the OrderStatus class manually (via email or whatever), and put it in my solution. Plus, I will have to explicitly cast the object to an OrderStatus object. Not to mention that the company could change the service to return an object of another type, and my code will stop working.

如果服务的返回值是一个对象

If the return value of the service was an object of type OrderStatus, the class would be automatically generated for me right?

推荐答案

p>不,绝对不会返回对象! WCF应该使用DataContracts并定义请求的完整实现和每个调用的可能响应对象。

No, definitely not return "object" ! WCF should make use of DataContracts and define full implementations of the request and possibly response objects for each call.

查看关于Datacontract基础的两篇优秀文章:

See these two excellent articles on Datacontract basics:

  • WCF tutorial part 2 - Data Contracts
  • WCF Basics - Data Contracts

我不知道为什么这些服务的开发人员选择对象作为返回类型 - 对我来说没有什么意义。

I have no idea why the developers of those services picked "object" as the return type - doesn't make a whole lot of sense to me!

Marc

这篇关于WCF服务是否应该返回普通的旧对象,或者是你正在使用的实际类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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