How do you handle user preferences?

Like most programs, users can specify how they want to handle certain things. In my case, users can specify which formatting they prefer. There are 3 options, leave an unformatted, camel case or a suitable case. I am currently working, but it feels very awkward and repetitive. Here is a jist 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 does not feel awkward. This is what it is called. Always being able to pass custom settings every time I want to get formatted text doesn't seem like the best way. Would it be better to create a regular class and pass the application preferences object through the constructor?

Thanks.

+7
c #
source share
1 answer

One option is to create some kind of factory class, then you can create an instance of the factory class with or from an instance of the class containing the settings.

Using the factory class, you can get a TextFormatter, an instance of the returned formatting will depend on the settings.

Here is a very simple example to clarify my answer with some code. This is not very fantasy and could potentially use more complex patterns, but hopefully this is the right starting point.

Define an interface and some formats

  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 an example of a preference 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 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"); 
+6
source share

All Articles