具有不同签名的相同方法的推荐命名约定?

发布于 2024-09-04 11:19:58 字数 187 浏览 2 评论 0原文

一直在创建一些 WCF 方法,我有一个名为 IsValidLogin 的方法...有各种版本,1 需要 2 个字符串,1 需要一个对象等。

当然,在 WCF 中,您不能重载方法,任何人都可以建议命名这些方法的最佳方法方法..

我在想IsValidLogin1,IsValidLogin2?

但我愿意接受任何建议。

been creating a few wcf methods and i have a method called IsValidLogin ... there various versions, 1 takes 2 strings, 1 takes an object etc.

Of course in WCF you can't overload methods can anyone suggest the best way to name these methods..

I was thinking of IsValidLogin1, IsValidLogin2??

But I am open to any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慕烟庭风 2024-09-11 11:19:58

当您开始向标识符添加索引号时,您通常会做错。

我见过的一种方法是将“With”和参数名称添加到名称中,即 IsValidLoginWithUsernamePassword 和 IsValidLoginWithToken (假设您的对象是某种身份验证令牌)。不过这些有点长。

我只需调用方法 IsValidUsernamePassword 和 IsValidToken。

When you start adding index numbers to your identifiers, you're usually doing it wrong.

One way I've seen is adding "With" and the parameter names to the name, i.e. IsValidLoginWithUsernamePassword, and IsValidLoginWithToken (assuming your object is some kind of authentication token). These are kind of long though.

I'd just call the methods IsValidUsernamePassword and IsValidToken.

苏大泽ㄣ 2024-09-11 11:19:58

首先,在使用 wcf 服务、传递请求并返回响应时,您应该坚持消息/契约优先的方法。这将为您省去很多麻烦。

话虽这么说,您应该创建两个方法,如下所示:

public LoginValidResponse IsLoginValid(UserObjectRequest userRequest)

并且

public LoginValidResponse IsLoginValid(UsernamePasswordRequest usernameRequest)

这些方法可能有更好的名称,但您明白了。如果您提供有关传入和返回的内容的更多信息,我可以帮助您提供更多名称。

请注意,这两个方法返回相同的响应 LoginValidResponse

将两个字符串放入 UsernamePasswordRequest 中(我假设这些字符串是用户名和密码)。将用户对象放入 UserObjectRequest 中。

您还可以在以后的方法中重用这些请求/响应,例如 GetUserInfo(UserObjectRequest request)

LoginValidResponse 将包含您的布尔值(以及您想要在响应中传回的任何其他信息)。

*注意 - 我只将方法命名为 IsLoginValid b/c 这是你的问题。除了请求/响应模式之外,我还可能将这些方法重命名为 ValidateLoginByUser 和 ValidateLoginByUsername 之类的名称(或类似的名称)。

First of all, you should stick with message/contract first methodology when working with wcf services, passing in a request and returning a response. This will save you a lot of headache down the road.

That being said, you should create two methods like so:

public LoginValidResponse IsLoginValid(UserObjectRequest userRequest)

and

public LoginValidResponse IsLoginValid(UsernamePasswordRequest usernameRequest)

There are probably better names for these, but you get the idea. If you provided more information about what you were passing in and back, I could help out with naming a bit more.

Notice these two methods return the same response LoginValidResponse.

Put your two strings in the UsernamePasswordRequest (I'm assuming the strings are username and password). Put the User Object in the UserObjectRequest.

You can also reuse these requests / responses in later methods, e.g, GetUserInfo(UserObjectRequest request).

The LoginValidResponse will contain your bool (and any other information you want to pass back in your response).

*Note - I only named the methods IsLoginValid b/c that was your question. On top of the request / response pattern, I might also rename the methods to something like ValidateLoginByUser and ValidateLoginByUsername (or something like that).

忆梦 2024-09-11 11:19:58

当然,在 WCF 中,您不能重载方法,任何人都可以建议命名这些方法的最佳方式。

您可以通过添加独特的 OperationContract 行为来重载 WCF 中的方法。 OperationContract 具有向 WSDL 架构公开 WCF 方法的 Name 属性。您的服务端 (WCF) 代码将保持干净。但是您仍然必须通过在 OperationContract 行为的 Name 属性中定义的签名来调用方法。

[OperationContract(Name="IsValidLoginWithUsernameAndPassword")]
void IsValidLogin(string username,string password);


[OperationContract(Name="IsValidLoginWithToken")]
void IsValidLogin(AuthToken token);

用法......

TestClient client = new TestClient();
string callMethod1 = client.IsValidLoginWithUsernameAndPassword("user","pass");
string callMethod2 = client.IsValidLoginWithToken(authToken);

您可以在这里阅读更多内容

http://www .codeproject.com/Tips/656042/Method-Overloading-in-WCF

Of course in WCF you can't overload methods can anyone suggest the best way to name these methods..

You can overload methods in WCF by adding unique OperationContract behaviours. OperationContract has the Name property that exposes the WCF methods to WSDL Schemas. Your service-side (WCF) code would remain clean. But you would still have to call the methods by signature you defined in the Name property of the OperationContract behaviour.

[OperationContract(Name="IsValidLoginWithUsernameAndPassword")]
void IsValidLogin(string username,string password);


[OperationContract(Name="IsValidLoginWithToken")]
void IsValidLogin(AuthToken token);

Usage......

TestClient client = new TestClient();
string callMethod1 = client.IsValidLoginWithUsernameAndPassword("user","pass");
string callMethod2 = client.IsValidLoginWithToken(authToken);

You can read more here

http://www.codeproject.com/Tips/656042/Method-Overloading-in-WCF

仅冇旳回忆 2024-09-11 11:19:58

我认为 IsValidLogin1,2 等不够清楚。通常,当您重载方法时,您不必担心名称,因为它是具有不同参数的相同名称,但是在这种情况下,您必须记住每个方法的参数,并且数字可能会令人困惑。

我可能会建议 IsValidLoginNumStr 等,也就是说,也许在方法名称中列出关键参数,以帮助提醒您所指的是哪个方法。或者如果需要密码,则使用 IsValidLoginPass 或类似的内容。我这样说是因为我喜欢长的、描述性的方法名称。如果您想使名称尽可能简短,并且可以想出一个有帮助的字母,例如 P 代表密码,O 代表对象,然后在末尾添加一个有用的字母。从长远来看,比数字更能帮助你的东西

I don't think that IsValidLogin1,2, etc. is clear enough. When you overload methods normally, you don't have to worry about names because it's the same name with different parameters, however in this case you have to remember the parameters for each method, and numbers could get confusting.

I might suggest IsValidLoginNumStr etc, which is to say, maybe list key parameters in the method name to help remind you which method you're referring to. Either that or if one takes a password, then IsValidLoginPass, or something of the like. I say this because I'm a fan of long, descriptive method names. If you want to keep the name short as possible, and you can think of a letter that would help, like P for password, or O for object, then tack on a helpful letter at the end. Something more than a number will help you in the long run

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文