C# 你如何处理用户偏好?
像大多数软件一样,用户可以指定他们希望如何处理某些事情。在我的例子中,用户可以指定他们喜欢哪种格式。有3个选项,保持无格式、驼峰式或适当的情况。我目前有它的工作,但它感觉非常笨重和重复。这是这个班的jistC# 你如何处理用户偏好?,c#,user-preferences,C#,User Preferences,像大多数软件一样,用户可以指定他们希望如何处理某些事情。在我的例子中,用户可以指定他们喜欢哪种格式。有3个选项,保持无格式、驼峰式或适当的情况。我目前有它的工作,但它感觉非常笨重和重复。这是这个班的jist public static class Extensions { public static string GetPreferenceFormattedText(this string text, ApplicationPreferences applicationPreferenc
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;
}
}
这个方法本身并不觉得很笨拙。这就是人们对它的称呼。每次我想要获得格式化文本时,总是要传递用户偏好似乎不是最好的方式。我是否最好创建一个常规类并通过构造函数传递应用程序首选项对象
谢谢。一个选项是创建某种factory类,然后可以使用包含首选项的类的实例或从该实例实例化factory类 使用factory类可以获得TextFormatter,返回的格式化程序实例将取决于首选项 下面是一个非常简单的示例,用一些代码来澄清我的答案。这不是超级花哨,可能会使用更多的沙发图案,但希望这是正确的起点 定义一个接口和一些格式化程序
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;
}
}
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.");
}
}
}
现在是一个示例首选项类
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;
}
}
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.");
}
}
}
使用代码
// 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");