I am using a method that has the following signature:
public static bool TryAuthenticate(string userName, string password,
string domainName, out AuthenticationFailure authenticationFailure)
The method declares: bool authenticated = false;
then goes on to authenticate the user.
Whenever authenticated
is set to true or false, authenticationFailure
is set to AuthenticationFailure.Failure
or AuthenticationFailure.Success
correspondingly.
So basically I can use either authenticationFailure or the return value of the method to check the result. However it seems to be a pointless violation of DRY to have these two approaches in the same method.
Just to clarify, authenticationFailure is not used anywhere else in the method so it appears to be totally redundant.
At the moment I'm doing this:
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;
}
But I could do this and get a similar result:
public static AuthenticationFailure IsValidLDAPUser(string username, string password, string domain)
{
var authenticationStatus = new AuthenticationFailure();
ActiveDirectoryAuthenticationService.TryAuthenticate(username, password, domain, out authenticationStatus)
return authenticationStatus;
}
Thanks in advance!
Often there are more error codes than just success or failure. Perhaps the designer of this method is going to add more enumerations for all the different failure types.
Sometimes, there is also more than one success type -- e.g. HTTP has a lot of return codes in the 200 and 300 block that would all be considered success in some way. So the bool tells you generally if it was successful or not, and the enum gives more exact information.
There are a lot of ways to do it, and this one is unusual, but not against DRY if they plan to add more codes.
Another way is to just encapsulate into a Result
class that has the enum and a IsSuccess
property. You could even provide a conversion to bool to make it easy to use in if statements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With