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 wou开发者_如何学JAVAld 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.
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");
精彩评论