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

1
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:

1
result.IsAuthorized

and you can also do this:

1
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:

1
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:

1
2
3
4
5
6
7
8
9
10
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.