2010-09-01 209 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.. 

    } 
} 

現在,如果沒有用戶或概念的令牌,則嘗試模式可能不需要