我使用的是具有以下簽名的方法:檢查參考參數值還是返回bool?
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',或者返回語句是否應該改爲返回'authenticationStatus'? – 2010-09-01 15:35:21