views:

86

answers:

2

In my current asp.net mvc project a user should be able to log in. When logged in a set of preferences can optionally be set. Some of those preferences are general (e.g. prefered site language etc), but some are specific to this project only (pre-defined query filtering etc).

Since these preferences are present in a lot of different places of my site I would define this as cross-concern. Preferably I would have an attribute handle this rather than every action on it's own.

How can I design such an attribute that is generic enough to re-use in future projects yet knows enough about the current project to use all the project specific settings too?

--EDIT--
Getting and setting the preferences is not the problem. I connected a UserSettings class to the asp.net profile provider. My problem is how to pull this cross concern out of my controllers into an attribute.

A: 

independently if you are storing that preferences in a text file, xml or database, wouldn't be easier to create a class (for example Utility.UserPreferences) that will load those preferences from the user and store them in a Session variable, then using an enum call them to retrieve / update

namespace Utility
{
    public class UserPreferences
    {
        public UserPreferences(int userID)
        {
            // Load Preferences from text file, xml file or DB
            string loadedPreferences = "us|20|yes|no";

            HttpContext.Current.Session["userPreferences"] = loadedPreferences;
        }

        public void Savepreferences(string[] pref, int userID)
        { 
            // Save preferences for that user
        }

        public static string GetPreferences(PreferencesType type)
        {
            string[] pref = HttpContext.Current.Session["userPreferences"].ToString().Split('|');

            switch (type)
            {
                case PreferencesType.Language: return pref[0];
                case PreferencesType.ShowHowManyResults: return pref[1];
                case PreferencesType.ShowNavigation: return pref[2];
                case PreferencesType.GetEmailAlerts: return pref[3];
            }
        }

        public enum PreferencesType
        {
            Language, ShowHowManyResults, ShowNavigation, GetEmailAlerts
        }
    }
}

then ...

// Login sucessfully...
Utility.UserPreferences pref = new Utility.UserPreferences(CurrentUser.ID);


// to retrieve a preference
string language = Utility.UserPreferences.GetPreferences(
                        Utility.UserPreferences.PreferencesType.Language,
                        CurrentUser.ID);

it's just an idea... the way I would do it... it's simple and it's project(s) wide, cause you just need to change the class to hold more preferences...

balexandre
Getting and setting the preferences is not the problem. I connected a UserSettings class to the asp.net profile provider.The problem is, how can I pull this cross concern out of my controllers into an attribute.
borisCallens
A: 

For user preferences you should use the ASP.NET Profile Provider framework. Some resources:

You could build attribute-based user preference handling on top of the Profile provider framework, but I imagine that sort of thing would be specific to your app.

DSO
I am using the profile providers for authentication. For preferences I have connected my own class. But where these preferences are comming from doesn't really matter. What does matter is how I work with this info.
borisCallens