MVVM Fabric: Using a View Authorizer

A while back, I wrote about the navigation solution provided by MVVM Fabric.  In that post, I mentioned the interface IViewAuthorizer.  In this post, I will describe that interface and when to use it.

Overview

An IViewAuthorizer implementation is an optional parameter to the ViewController which, when provided, gives your application an opportunity to determine if a user is authorized to see a requested view prior to the view being shown.  If you do not provide an IViewAuthorizer implementation to the ViewController, the ViewController assumes that the user is authorized to see every view that is requested.

A First Look

First of all, let’s take a look at what the IViewAuthorizer interface looks like.

Pretty simple, really.  IViewAuthorizer has only one responsibility, to authorize views.  The AuthorizeView method takes a ViewTargets value as a parameter so it knows which view to authorize and returns a ViewAuthorizations enumeration value.  The ViewAuthorizations enum looks like the following.

The first option, Authorized, is self-explanatory.  If the view authorizer determines that the view is authorized, the ViewController will show the view.

The second option, NotAuthorized, is self-explanatory as well.  If the view authorizer determines that the view is NOT authorized, the ViewController will not show the view.  However, the ViewController will also throw an InvalidOperationException if the user is not authorized for a view.

The third option is for those more ambiguous situations where the view may be authorized, but it really shouldn’t be shown, yet you don’t want to throw an exception.  Let me give an example from my last project to help illustrate a situation where this is useful.

A DoNotDisplay Scenario

With my last project, there were views that depended on data provided by other views.  The other views had to be presented to the user so they could provide the data.  So, we had a situation where some views were prerequisites for other views.  Using the navigation solution provided by MVVM Fabric, it was easy to consolidate all the prerequisite checks in one place using an IViewAuthorizer implementation.

I developed a helper class that allowed me to specify which views were dependent on information from other views.  When the AuthorizeView method was called, the view authorizer would check to see if there were any prerequisites and, if there were, check to see if they were satisfied.  If they were not satisfied, the view authorizer would then request to show the view that will satisfy the prerequisite.  All that happens BEFORE the view that was originally requested was shown.  So, if the user cancels out of the prerequisite view before the prerequisite was satisfied, I did not want the originally requested view to be shown, but I also did not want the ViewController to throw an exception because the user opted to cancel the process before it could be completed.  So, I had the view authorizer return DoNotDisplay in that situation where the prerequisite was not met due to a user operation.

Conclusion

IViewAuthorizer provides you the opportunity to check whether a user is authorized for a view prior to showing the view.  It also provides you the ability to let the user gain the authorization to see the view prior to showing the view.  However, IViewAuthorizer is completely optional.  So, if your application has no need to prevent users from seeing any view, you do not need to provide an implementation for IViewAuthorizer.

Share