How to use a complex type in a conditional
I am building a basic authorization framework, and I have really liked the use of it so far.
It basically looks something like this
var result = Is.CurrentUser.AuthorizedTo.DoSomething();
The result that I get back is not actually a boolean, as you might thing. It’s a complex type that I built so I could not only determine if authorization was successful or not, but also why not.
So you can do this:
result.IsAuthorized
and you can also do this:
result.WhyNot
WhyNot will actually give you back a list of Reasons, which you can then use to inform the system (or user) why they can’t do something.
So what’s the problem? Sometimes I just don’t care why not, I just want to do the check quickly and be done with it, which makes me want to do something like this instead:
if (Is.CurrentUser.AuthorizedTo.DoSomething())
This won’t compile, because my complex type can’t resolve to a bool. Not to worry, though, implicit operator to the rescue:
public class AuthorizationResult
{
public bool IsAuthorized { get; set; }
public static implicit operator bool(AuthorizationResult authorizationResult)
{
if (authorizationResult == null)
return false;
return authorizationResult.IsAuthorized;
}
}
Next post I’ll talk about how I do the same thing for outputting the reasons implicitly.

[...] Joseph on Sep.30, 2010, under Programming Along the same lines as resolving a complex type in a conditional, I also want to be able to take the same Authorization Result, and use it to broadcast a message to [...]