Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle user preferences?

Like most software, users are able to specify how they'd like to handle certain things. In my case, users can specify what kind of formatting they would prefer. There are 3 options, leave unformatted, camel case or proper case. I currently have it working but it feels very clunky and repetitive. Here is a jist of the class.

public static class Extensions
{
    public static string GetPreferenceFormattedText(this string text, ApplicationPreferences applicationPreferences, bool pluralize)
    {
        if (applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.SameAsDatabase))
            return text;
        string formattedText = text.Replace('_', ' ');
        formattedText = formattedText.MakeTitleCase();
        formattedText = formattedText.Replace(" ", "");

        if (applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.Prefixed))
            return applicationPreferences.Prefix + formattedText;

        return applicationPreferences.FieldNamingConvention.Equals(FieldNamingConvention.CamelCase)
                   ? formattedText.MakeFirstCharLowerCase()
                   : formattedText;
    }
}

The method itself doesn't really feel clunky. It's the way it's being called. Always having to pass the user preferences every time I want to obtain the formatted text doesn't seem like the best way to go. Would I be better off making a regular class and pass the application preferences object through the constructor?

Thank you.

like image 885
Mike Avatar asked Sep 11 '10 19:09

Mike


People also ask

Where do you navigate to set user preferences?

To set your preferred preferences, click user name > Preferences at the top of the application home page. The Preferences page appears.

What is user preferences in ServiceNow?

One example of a user preference is the Compact the user interface toggle in the settings menu after clicking the cog icon in the top-right of the ServiceNow interface: There is a user preference record corresponding to that change, called glide. ui.


1 Answers

One option would be to create some kind of factory class, you can then instantiate the factory class with or from the instance of the class containing the preferences.

Using the factory class you can get a TextFormatter, the instance of the formatter returned will depend on the preferences.

Here is a very simple example just to clarify my answer with some code. This is not super fancy and can potentially use more sofisticated patterns, but it is hopefully the right starting point.

Define an interface and some formatters

  public interface IIdentifierFormatter
  {
    string FormatText(string text);
  }

  public class UnformattedIdenifierFormatter : IIdentifierFormatter
  {
    public string FormatText(string text)
    {
      return text;
    }
  }

  public class CamelCaseIdenifierFormatter : IIdentifierFormatter
  {
    public string FormatText(string text)
    {
      // Camel case formatting here
      return text;
    }
  }

  public class ProperCaseIdenifierFormatter : IIdentifierFormatter
  {
    public string FormatText(string text)
    {
      // Proper case formatting here
      return text;
    }
  }

Now a sample preferences class

  enum NamingConvention 
  {
    Unformatted,
    CamelCase,
    ProperCase
  }

  public class Preferences
  {
    public NamingConvention FieldNamingConvention { get; set; }
    // .. Other settings


    // Function to get the formatter depending on the FieldNamingConvention
    public IIdentifierFormatter GetFieldNameFormatter()
    {
      switch (FieldNamingConvention)
      {
        case NamingConvention.Unformatted:
          return new ProperCaseIdenifierFormatter();
        case NamingConvention.CamelCase:
          return new ProperCaseIdenifierFormatter();
        case NamingConvention.ProperCase:
          return new ProperCaseIdenifierFormatter();          
        default:
          throw new Exception("Invalid or unsupported field naming convention.");
      }      
    }
  }

Using the code

// Preferences loaded from some source,
// for the example I just initialized it here.      
  Preferences pref = new Preferences();
  pref.FieldNamingConvention = NamingConvention.CamelCase;

  // Get the formatter
  IIdentifierFormatter formatter = pref.GetFieldNameFormatter();

  string formatted = formatter.FormatText("the_name_to_format");
like image 174
Chris Taylor Avatar answered Sep 30 '22 08:09

Chris Taylor