向 WCF 的 DataContract 对象添加方法 [英] Adding methods to DataContract objects for WCF

查看:24
本文介绍了向 WCF 的 DataContract 对象添加方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WCF 中的 DataContracts 只是 DTO 吗?我正在阅读有关 WCF 的内容,并且有一些想法.如果某些 DataContract 对象可以具有方法,那么客户端可以在发送或检索回服务之前或之后对它们执行基本操作,那就太好了.

Are DataContracts in WCF nothing more than DTOs? I was reading up about WCF and just had a couple of thoughts. It would be nice if some of the DataContract objects could have methods on them so that the client could do basic things with them before or after sending or retrieving back to the service.

对我来说这似乎不可能或不合逻辑.我可能是错的,我每天都在学习新事物.那么接下来最好的事情是将 DataContracts 视为 DTO,并为将从 DTO 创建真实对象的客户端提供库.包含方法的对象.

To me this just doesn't seem possible or logical. I could be wrong, I learn new things everyday. So would the next best thing be to treat DataContracts as DTOs and provide libraries for the clients that would create real objects from the DTOs. Objects that would contain methods.

任何指导将不胜感激.

推荐答案

不确定我是否正确理解您的答案,如果我错了,请纠正我.

Not sure if I correctly understood your answer, so correct me if I'm wrong.

您可以使用 DataContracts 类创建一个类库,并在客户端和服务器之间共享该库.通过这种方式,标记为 [DataContract] 的类将具有方法(行为)和 [DataMember] 字段/属性(状态).

You can create a class library with your DataContracts classes and share the library between the client and server. In this way class marked [DataContract] will have methods (behavior) and [DataMember] fields/properties (state).

当您通过 WCF 在客户端和服务器之间传递此类对象时,状态将被持久化,但由于类库是共享的,您将在双方都有方法.

When you will pass such objects between client and server via WCF state will be persisted, but since class library is shared you will have methods on both sides.

这篇关于向 WCF 的 DataContract 对象添加方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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