简体   繁体   English

WCF数据合同中是否包含WCF操作合同? 为什么?

[英]Can a WCF data contract contain a WCF operation contract inside it? Why?

I have a data contract say User. 我有一个数据合同说用户。 It is serializable and goes across the wire. 它是可序列化的,并且可以跨越线路。 I want an Operation Contract SaveUser(). 我想要一个Operation Contract SaveUser()。 I can keep SaveUser(User user) in my service contract as an operation contract. 我可以将SaveUser(用户用户)作为操作合同保存在我的服务合同中。 But can I keep it inside my data contract itself as its own behavior? 但是我可以将它作为自己的行为保存在我的数据合同中吗?

Save() should ideally save itself. 理想情况下Save()应该保存自己。 So as per OO principles, every data contract should know how to save itself and the details should be abstracted from the outer world. 因此,根据OO原则,每个数据合同都应该知道如何保存自己,并且细节应该从外部世界中抽象出来。

Is this possible in WCF? 这可能在WCF中吗?

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. 虽然我同意你的OO原则和封装,但WCF处理SO(面向服务)原则。 Think of this in terms of a CD Player and CD's. 根据CD播放器和CD来考虑这一点。 The CD Player is the Service. CD播放器就是服务。 The CD is the Data Contract. CD是数据合同。 OO principles would call for the CD to have a Play method in order to be able to play itself. OO原则要求CD具有Play方法以便能够自己发挥作用。 But, there is a lot more to playing a CD than knowing it's data. 但是,播放CD要比知道它的数据要多得多。 There is the electronics, the interface to the output jacks, etc. These are all provided by the CD Player...the Service. 有电子设备,输出插孔的接口等。这些都是由CD播放器......服务提供的。 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). 这就是为什么您的服务合同具有Play方法,并接受CD作为数据合同告诉它播放什么(而不是如何播放)。

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. 最糟糕的情况是,您将拥有34个服务合同,每个服务合同平均有6种方法。 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. 方面1:您的服务设计。 Instead of 34 Service Contracts, you should group the 34 classes into a grouping that makes sense, and create 1 Service Contract per group. 您应该将34个类分组为有意义的分组,而不是34个服务合同,并为每个组创建1个服务合同。 Eg, you may end up with an InventoryManagement Service, and a SalesOrderProcessing service and a BackOfficeOperations Service. 例如,您最终可能会获得InventoryManagement服务,SalesOrderProcessing服务和BackOfficeOperations服务。 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. 方面2:客户端发生了什么。 I mentioned that you must consider whether each class's methods MUST be WCF Service Operations. 我提到你必须考虑每个类的方法是否必须是WCF服务操作。 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. 问题变成了如何通过服务将它们传递给客户端,这里有两种选择:a)在客户端实例化一个实例,并从服务操作返回的DataContract中填充它的属性。 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). b)直接从服务操作返回对象,就像在CSLA这样的框架中完成(我认为DevForce遵循类似的方法通过WCF服务返回业务类)。 HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM