它是不好的做法,有一个WCF服务的方法的输出参数? [英] Is it bad practice to have an output parameter in a method in a WCF service?

查看:238
本文介绍了它是不好的做法,有一个WCF服务的方法的输出参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找超越了通常的原因OUT参数混乱,并指出该方法是做多件事情式的争论,更多的是有什么特别不好的WCF服务的输出参数。当我现在的工作,我们在WCF服务对他们的规则,而我试图找出原因!

I'm looking for reasons beyond the usual "out parameters are confusing and indicate the method is doing more than one thing"-style arguments and more about what is specifically bad about output parameters in WCF services. Where I work now, we have a rule against them in WCF services, and I'm trying to work out why!

推荐答案

就个人而言,我用在特定的地方参数(如命名的TryParse()的方法)。所以,我有一些你讲到这里我只用它在特定的,有限的地方偏见。此外,你不能假设.Net应用程序是怎么回事,另一端要消耗这一点。因为WCF提供了一个接口耗材作为SOAP或REST Web服务(包括其他通信类型),我不能保证WCF甚至支持OUT参数,与non-.Net消费者的兼容性。

Personally, I use out parameters in specific places (such as methods named TryParse()). So, I have some of the bias you talked about where I only use it in specific, limited places. In addition, you can't assume that a .Net application is going to be consuming this on the other end. Because WCF provides an interface consumable as a SOAP or REST web service (among other communication types), I can't guarantee that WCF would even support out params for compatibility with non-.Net consumers.

除此之外,WCF服务将提供一个API来消费,和API的应提供应该与服务器的方法是如何codeD有限的知识所消耗的接口。 (不要presume的家伙写的WCF服务器是同一个人写的客户端上的另一端)。试图在一个API使用了参数似乎是一个code气味。 presumably中,应当使用一个出参数去另一个值(多个)返回给消费者。考虑使用,而不是一个报文对象。一个消息对象具体由数据的所有需要​​被从WCF服务器发送到其消费者的碎片。例如,假设您有名为TryCreateUser一个WCF服务器暴露的方法:

Beyond that, a WCF service is providing an API to a consumer, and API's should provide an interface that should be consumed with limited knowledge of how the server methods were coded. (Don't presume the guy writing the WCF server is the same guy writing the client on the other end). Attempting to use an out param on an API seems like a code smell. Presumably, one would use an out param to return another value(s) to the consumer. Consider instead using a message object. A message object is specifically composed of all the pieces of data that need to be sent from the WCF server to its consumer. For example, let's say you have a method exposed in a WCF server called TryCreateUser:

bool TryCreateUser(string name, string email, out User user){}

在这里你打算返回一个布尔表示,其中用户创建成功发生,并包含了用户,如果它成功了一个User对象。我想创建一个新的类,UserCreationMessage:

where you intend to return a bool indicating where user creation occurred successfully and a User object containing the user if it succeeded. I would create a new class, UserCreationMessage:

class UserCreationMessage {
    bool IsSuccessful;
    User user;
}

返回此消息对象​​返回给消费者,你仍然可以得到多个返回值。但是,你现在有返回一个连贯的对象,更解释到API的最终用户。

Return this message object back to the consumer, and you can still get the multiple returned values. However, you now have a coherent object being returned that's more explanatory to the end user of the API.

在最后,我有理由相信,这是不好的做法,有一个API,out参数,如WCF服务器因为程序员创建消费者对这种服务必须能够方便地看API和使用它而不通过篮球,一个出参数present跳跃。由于这个更好的设计存在,使用它。 API的要求更高编码标准,特别是在接触到终端消费者的接口。

In the end, I'd reason that it's bad practice to have an out parameter in an API, such as a WCF server because programmers creating the consumer for this service have to be able to easily look at the API and consume it without jumping through the hoops that an out param present. Since a better design for this exists, use it. API's require higher coding standards, especially in the interface exposed to the end consumer.

这篇关于它是不好的做法,有一个WCF服务的方法的输出参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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