简体   繁体   English

服务合同设计(运营签名)

[英]Service contract design (operations signature)

I'm designing some services and I would like to get some feedback about the conventions I'm using. 我正在设计一些服务,我想获得一些有关所使用约定的反馈。

For all operations, I always define a 'Context' object and a 'Result' one, because of the following advantages: 对于所有操作,由于以下优点,我总是定义一个“上下文”对象和一个“结果”对象:

  • extensibility: I can add parameters to the context or objects to the result without changing the interface 可扩展性:我可以在不更改接口的情况下将参数添加到上下文中或将对象添加到结果中
  • compactness: I only have a single object in the definition, even if I need many parameters 紧凑性:即使我需要很多参数,定义中也只有一个对象

Example: 例:

[OperationContract]    
DoSomethingResult DoSomething(DoSomethingContext context)

Anyway, I'm not really sure that this is the best way to do it because of the following reasons: 无论如何,由于以下原因,我不太确定这是最好的方法:

  • overhead: I always wrap the response properties into an object. 开销:我总是将响应属性包装到一个对象中。 Sometimes, the Result object has no properties 有时,Result对象没有属性
  • versioning: WCF has built-in versioning for contracts, and maybe it could be better to use a different version to inform about the difference 版本控制:WCF具有合同的内置版本控制,也许最好使用其他版本来告知差异

In fact I use the same technique with normal methods too, so it would be important for me to get some feedback, advices, critics and so on and so forth. 实际上,我也使用普通方法使用相同的技术,因此获得一些反馈,建议,批评等等对我来说很重要。

Thank you 谢谢

I think that's a perfectly legitimate way to write your contracts. 我认为这是编写合同的完全合法的方法。 I've worked on a number of projects with these sort of contracts and it is has been a pleasure - very easy during development (just add a property to the object and you're done), a straightforward and clear pattern that applies to all services, and allows for things like a single validation method for all operations. 我从事过具有此类合同的多个项目,这是一种乐趣,在开发过程中非常容易(只需向对象添加属性即可完成),一种适用于所有项目的简单明了的模式服务,并为所有操作提供诸如单一验证方法之类的功能。

In response to your concerns: 针对您的担忧:

  • I don't think the overhead of creating an empty object is at all significant. 我认为创建一个空对象的开销根本不重要。 Don't worry about this unless it becomes an issue. 除非这成为问题,否则不要担心。
  • If the Result object has no properties (ie you aren't returning anything) then simply return void. 如果Result对象没有属性(即您不返回任何内容),则只需返回void。 You aren't gaining anything by returning an empty object. 返回空对象不会获得任何收益。
  • You can (and probably should) version these objects as you version your contracts. 您可以(可能应该)在对合同进行版本控制时对这些对象进行版本控制。 What you are doing in no way precludes you from versioning your objects. 您所做的任何事情都不会阻止您对对象进行版本控制。

Please note that versioning objects does not mean changing them to DoSomethingResult_v1 , DoSomethingResult_v2 as I've seen before. 请注意,版本控制对象并不意味着像我之前看到的那样将它们更改为DoSomethingResult_v1DoSomethingResult_v2 You should version with namespaces; 您应该使用名称空间进行版本控制; it makes things clearer and cleaner. 它使事情变得更加清晰和清洁。 Just put a version in the XML namespaces in both the operation contract and data member attributes. 只需在操作约定和数据成员属性的XML名称空间中放置一个版本。

I don't think there are any performance concerns here, and the code looks easy to work with from the code-owners perspective. 我认为这里没有任何性能问题,从代码所有者的角度来看,代码看起来很容易使用。

My big concern is that it isn't at all clear from the consumers perspective how your service works. 我最担心的是,从消费者的角度来看,您的服务如何运作尚不清楚。 They would have to rely on separate documentation or error messages. 他们将不得不依赖单独的文档或错误消息。

It would be much easier for someone unfamiliar with your code (ie just downloaded the WSDL) to consume your service if the parameters that it required were declared. 如果声明了所需的参数,那么对您的代码不熟悉的人(即刚刚下载WSDL的人)使用您的服务会容易得多。 You also get a good degree of validation out of the box. 您还可以立即获得很好的验证。

To illustrate: 为了显示:

[OperationContract]     
DoSomethingResult DoSomething(DoSomethingContext context) 

vs VS

[OperationContract]   
[FaultContract(typeof(CustomerNotFoundFault))]   
Customer GetCustomer(UInt32 customerId) 

This point is mostly relevant to the design of APIs. 这一点与API的设计有关。 Where this isn't so relevant, is where you are both the author and the consumer of the service. 在这不太相关的地方,您既是服务的作者又是服务的使用者。

I totally support Kirk Broadhurst's suggestion of using namespaces for versioning. 我完全支持Kirk Broadhurst关于使用名称空间进行版本控制的建议。 I use that and it works well. 我使用它,并且效果很好。

EDIT: on a second reading, I think I misread your post. 编辑:在二读时,我认为我误读了您的帖子。 I was assuming here that your parameter and return value objects were some generic object that you use across all services. 我在这里假设参数和返回值对象是在所有服务中使用的一些通用对象。 If indeed they are specific to each service, then that's a great approach which I've used successfully on many occasions. 如果确实是针对每种服务的,那么这是我在很多场合成功使用过的一种很棒的方法。 You'll do well with it. 您会做得很好。

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

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