【发布时间】:2011-04-06 21:20:32
【问题描述】:
我正在使用具有以下签名的方法:
public static bool TryAuthenticate(string userName, string password,
string domainName, out AuthenticationFailure authenticationFailure)
该方法声明:bool authenticated = false; 然后继续对用户进行身份验证。
每当authenticated 设置为true 或false,authenticationFailure 就会相应地设置为AuthenticationFailure.Failure 或AuthenticationFailure.Success。
所以基本上我可以使用 authenticationFailure 或方法的返回值来检查结果。然而,在同一方法中使用这两种方法似乎是对 DRY 的毫无意义的违反。
澄清一下,authenticationFailure 没有在该方法的其他任何地方使用,因此它看起来完全是多余的。
目前我正在这样做:
public static bool IsValidLDAPUser(string username, string password, string domain)
{
var authenticationStatus = new AuthenticationFailure();
if (ActiveDirectoryAuthenticationService.TryAuthenticate(username, password, domain, out authenticationStatus))
return true;
else return false;
}
但我可以这样做并得到一个相似的结果:
public static AuthenticationFailure IsValidLDAPUser(string username, string password, string domain)
{
var authenticationStatus = new AuthenticationFailure();
ActiveDirectoryAuthenticationService.TryAuthenticate(username, password, domain, out authenticationStatus)
return authenticationStatus;
}
- 为什么会有一个与返回值做同样事情的引用参数?
- 我应该使用哪一个来检查结果,它有什么不同吗?
- 这只是代码错误还是我没有抓住重点?
提前致谢!
【问题讨论】:
-
我认为您的第二个代码块可能包含错误。返回类型不应该是
bool而不是AuthenticationFailure还是应该将return 语句更改为返回authenticationStatus?
标签: c# .net methods pass-by-reference