WCF数据合同中是否可以包含WCF操作合同?为什么? [英] Can a WCF data contract contain a WCF operation contract inside it? Why?

查看:80
本文介绍了WCF数据合同中是否可以包含WCF操作合同?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据合同,说用户.它是可序列化的,可以在线访问.我想要一个操作合同SaveUser().我可以将SaveUser(User user)保留在服务合同中作为操作合同.但是我可以将它作为自己的行为保留在我的数据契约中吗?

I have a data contract say User. It is serializable and goes across the wire. I want an Operation Contract SaveUser(). I can keep SaveUser(User user) in my service contract as an operation contract. But can I keep it inside my data contract itself as its own behavior?

Save()应该理想地保存自身.因此,按照面向对象的原则,每个数据契约都应该知道如何保存自身,并且应该从外部世界中提取细节.

Save() should ideally save itself. So as per OO principles, every data contract should know how to save itself and the details should be abstracted from the outer world.

在WCF中有可能吗?

推荐答案

我会说不,这是正确的. 尽管我在OO原则和封装方面与您达成共识,但WCF还是在处理SO(面向服务)原则. 从CD播放器和CD的角度考虑这一点. CD播放器就是服务. CD是数据合同. OO原则要求CD具有Play方法,以便能够播放自身.但是,播放CD不仅要了解其数据,还需要做更多的工作.有电子设备,到输出插孔的接口等.这些都由CD播放器提供...服务. 因此,这就是您的服务合同具有Play方法,并接受CD作为数据合同的原因,告诉CD要播放什么(而不是如何播放).

I would say no, and rightly so. Though I agree with you on the OO principles and encapsulation, WCF deals with SO (Service Oriented) principles. Think of this in terms of a CD Player and CD's. The CD Player is the Service. The CD is the Data Contract. OO principles would call for the CD to have a Play method in order to be able to play itself. But, there is a lot more to playing a CD than knowing it's data. There is the electronics, the interface to the output jacks, etc. These are all provided by the CD Player...the Service. So that is why your Service Contract has the Play method, and accepts the CD as a Data Contract telling it WHAT to play (and not HOW to play it).

不能(希望)没有.最坏的情况是您将拥有34个服务合同,每个合同平均有6种方法.仅当您确定每个类上的每个方法务必作为服务操作发生时,情况才如此.您需要考虑两个方面.方面1:您的服务设计.您应该将34个类别分组为一个有意义的分组,而不是34个服务合同,并为每个组创建1个服务合同.例如,您可能最终得到一个库存管理服务,一个SalesOrderProcessing服务和一个BackOfficeOperations服务.这些服务中的每一个都包含与分组到其域中的类别范围相关的服务操作(和数据合同). 方面2:客户端上发生了什么.我提到过,您必须考虑每个类的方法是否必须为WCF服务操作.当然,在客户端上会有丰富的完全封装的业务类.在不需要将其操作作为服务操作执行的情况下,这些操作将在客户端域中执行其逻辑.问题变成了如何通过服务将它们传递给客户端,这里有两种选择:a)在客户端上实例化一个实例,并通过服务操作返回的DataContract填充其属性. b)直接从服务操作返回对象,就像在CSLA之类的框架中所做的那样(我认为DevForce遵循通过WCF服务返回业务类的类似方法). HTH

EDIT after the question in your comment: No certainly (hopefully) not. The worst case is that you will have 34 Service Contracts, each with on average 6 methods. And this is only the case if you are sure that every one of the methods on each class MUST happen as a service operation. There are 2 aspects you need to consider. Aspect 1: The design of your services. Instead of 34 Service Contracts, you should group the 34 classes into a grouping that makes sense, and create 1 Service Contract per group. Eg, you may end up with an InventoryManagement Service, and a SalesOrderProcessing service and a BackOfficeOperations Service. Each of these services contain the service operations (and Data Contracts) relevant to the range of classes grouped into their domain. Aspect 2: What is happening on the client. I mentioned that you must consider whether each class's methods MUST be WCF Service Operations. There certainly is a case for having rich fully encapsulated business classes on the client. And where their operations don't need to execute as service operations, these operations execute their logic in the client domain. The question becomes how to get them to the client via a service, and here you have two alternatives: a) instantiate an instance on the client, and populate it's properties from the DataContract returned by a service operation. b) return the object directly from a service operation, as is done in a framework like CSLA (and I think DevForce follows a similar approach for returning business classes via a WCF Service). HTH

这篇关于WCF数据合同中是否可以包含WCF操作合同?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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