views:

42

answers:

1

I'm looking for some advice in dealing with UI, specifically web UI, where a multitude of different settings and toggles change what is displayed to the user. The settings are tied to the user, so each user has a unique experience (depending on a variety of factors). The existing approach of using a plethora of conditional statements; obviously doesn't scale and makes for a maintenance nightmare.

I will be implementing the MVC pattern to separate concerns, but I have to imagine there's a design pattern or best practice for configuring highly-dynamic views.

Based on user credentials, I have:

  • Features that can be toggled on and off
  • Features can be extended or enhanced
    • Think of a checkbox list where one checkbox is not visible for one group and the whole list may be unavailable to other groups. (Like a list of options, and the user doesn't have access to all of them; maybe some are disabled, maybe some are invisible.)

The question is: how do I keep the view as clean as possible while maintaining the context and intent of the view? Do I create separate "handlers" that output specific HTML (if they are chained together through some authorization provider)? If so, how do I work with features that are shared among multiple groups, but may change slightly from one to the other?

I don't know that it matters what platform I'm developing on, so examples or suggestions in any particular language or framework are fine.

EDIT
For example, say I have ProductViewModel (in C#):

public class ProductViewModel {
   public bool DisplayPrice { get; set; }
}

In my view, then, I can check if Model.DisplayPrice == true, and if so, display the product's price. At this level, I don't care why it's being displayed. So, this would replace something like if ( UserInRole("Vendor") || UserInRole("Distributor") ).

Obviously I'd need some way to change this setting based on context. Ideally, I want to avoid ever having the conditional statement if ( UserInRole("Vendor") || UserInRole("Distributor") ) because the list of conditions can grow quickly. For instance, say I want to display the price to vendors, but only if they have VIP access, and have been a user for more than 30 days, and have made previous purchases, and..

Any best practices, or suggestions, for modifying/stacking the settings at runtime, per user? Anyone with experience implementing "preferences" with a huge number of permutations?

+1  A: 

There may be a few different things going on here...

When you talk about a list of checkboxes being conditionally shown for a user, it makes me think of ACLs. You'd filter the list of checkboxes to display before they even get to the view. The view would simply loop through the list of checkboxes to display and display them.

For features that can be toggled on and off, I think it really depends on how the feature is presented to the user. If it's a button, for example, I would create a method that takes a required role or right and displays itself depending on whether the user has that role or right. So you still have the same amount of conditionals, but they are encapsulated inside a method that can be reused across views. It would certainly be cleaner, but I think it gets a little hairy as far as whether that's a recommended practice or not...

Andorbal