2010-09-01 205 views
4

我使用的是具有以下签名的方法:检查参考参数值还是返回bool?

public static bool TryAuthenticate(string userName, string password, 
    string domainName, out AuthenticationFailure authenticationFailure) 

的方法声明:bool authenticated = false;然后继续对用户进行认证。

每当authenticated设置为true或false时,authenticationFailure设置为AuthenticationFailure.FailureAuthenticationFailure.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; 
} 
  • 为什么你有一个参考参数和返回值一样吗?
  • 我应该使用哪一个来检查结果,它有什么区别?
  • 这只是一个错误的代码的情况下,还是我错过了这一点?

在此先感谢!

+0

我想你第二个代码块可能包含一个错误。返回类型不应该是'bool'而不是'AuthenticationFailure',或者返回语句是否应该改为返回'authenticationStatus'? – 2010-09-01 15:35:21

回答

4

通常有更多的错误代码,而不仅仅是成功或失败。也许这种方法的设计者会为所有不同的失败类型添加更多的枚举。

有时,也有不止一种成功类型 - 例如, HTTP在200和300块中有很多返回码,这些都会以某种方式被认为是成功的。所以bool总体上告诉你它是否成功,并且枚举提供更准确的信息。

有很多方法可以做到这一点,但这种方法并不常见,但如果他们打算添加更多的代码,则不会反对DRY。

另一种方法是将其封装到Result类中,该类具有enum和IsSuccess属性。你甚至可以提供一个转换到布尔,使其易于在if语句中使用。

+0

好点 - 我刚才看了一下关于TryAuthenticate的可扩展性,结果证明AuthenticationFailure有各种其他值,比如AccountDisabled,PasswordExpired,InvalidWorkstation等......所以我会继续检查bool,但也许使用身份验证失败时,身份验证无法提供有关其失败原因的一些反馈。 – Nobody 2010-09-01 15:44:47

2

如果某个函数只返回一个值,则应优先使用常规函数返回值,而不是outref参数。

我会说这是连名为IsXyz方法,更重要的(重点是在),并以这种方式命名的方法应该返回bool

3

首先,你的枚举变量名似乎有点扭曲。 AuthenticationFailure.Success没有多大意义!应该是AuthenticationResult什么的。另外,因为在你的方法中你只关心认证是否成功,你应该返回bool。你在想干,但也想到KISS!

您仍然可以使用您可能添加的其他错误代码的枚举,但只要查看一下方法的返回值就会告诉您它是否成功。如果你想找到它不成功的细节,那么使用作为'out'参数传递的枚举。

+0

是的,我注意到枚举的命名不是非常深思熟虑。幸运的是,制作这些代码并不是我做的,我不保留它 - 它是公司范围广泛的公共图书馆之一。 – Nobody 2010-09-01 15:48:22

1

参考参数不易于使用和理解与返回值比较。但是,有些情况下使用ref参数可能会有好处。对于这种情况,如果我们假设分配AuthenticationFailure是耗时的,那么使用ref参数是合适的,因为在认证成功的情况下不需要分配。

无论如何,在这种情况下,我更喜欢使用AuthenticationResult返回类型,因为应该没有性能影响。

1

恕我直言,我不认为这是布尔与裁判的事。对我来说,看起来是错误的事情正在ref中返回。

我希望看到下面的方法

public static bool TryAuthenticate(string userName, 
       string password, 
       string domainName, 
       out User user) 

public static User Authenticate(string userName, 
       string password,      
       string domainName) 

一个用于当你不关心为什么,一个是你怎么做。例如

这使得调用代码做身份验证,然后做一些事情没有赶上

User u; 
if (TryAuthenticate(user,pass,domain,ref u)) 
    //do somthing 
else 
    return; 

或者,如果他们需要额外的信息,然后使用一个catch

例如

User u; 
try 
{ 
    u = Authenticate(user,pass,domain); 
    //do somthing 
} 
catch(AuthenticationError ae) 
{ 
    switch (ae.Failure) 
    { 
     case AuthenticationFailure.AccountLocked: 
      //dosomthing A 
      break; 
     case AuthenticationFailure.BadPassword: 
      //dosomthing B 
      break; 
     case AuthenticationFailure.InvalidUser: 
      //dosomthing c 
      break; 
     etc.. 

    } 
} 

现在,如果没有用户或概念的令牌,则尝试模式可能不需要