I'm developing an app which stores some different user settings in app.config such as connection strings, paths for saving reports, default printer e.t.c For retrieving them I made a static class StoredSettingsTools which contains an Enum of settings types and a public method which calls(depending of setting type) some private method and get the settings from app.config:
public static class StoredSettingsTools
{
public enum StoredSettingsType
{
DBConnection,
Printer,
ReportsFolder
};
private static ConnectionStringsSection GetConfigurationConnectionStringsSection()
{
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location);
ConnectionStringsSection conectionSettings = (ConnectionStringsSection)config.SectionGroups.Get("UserSettingsGroup")
.SectionGroups.Get("StoredConnectionSettings")
.Sections.Get("defaultConnectionString");
return conectionSettings;
}
private static Hashtable GetConfigurationPrinterSection()
{
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location);
ConfigurationSectionGroup userSettingsGroup = config.SectionGroups["UserSettingsGroup"];
Hashtable sectionSettings = ConfigurationManager.GetSection(userSettingsGroup.Sections.Get("printerSettings").SectionInformation.SectionName) as Hashtable;
return sectionSettings;
}
private static Hashtable GetConfigurationReportsFolderSection()
{
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location);
ConfigurationSectionGroup userSettingsGroup = config.SectionGroups["UserSettingsGroup"];
Hashtable sectionSettings = ConfigurationManager.GetSection(userSettingsGroup.Sections.Get("reportsFolderSettings").SectionInformation.SectionName) as Hashtable;
return sectionSettings;
}
public static Hashtable GetStoredSettings(StoredSettingsType sectionType)
{
Hashtable table = new Hashtable();
switch (sectionType)
{
case StoredSettingsType.DBConnection:
{
ConfigurationSection section = GetConfigurationConnectionStringsSection();
ConnectionStringSettingsCollection col = ((ConnectionStringsSection)section).ConnectionStrings;
foreach (ConnectionStringSettings settings in col)
{
table.Add(settings.Name, settings.ConnectionString);
}
return table;
}
case StoredSettingsType.Printer:
{
table = GetConfigurationPrinterSection();
return table;
}
case StoredSettingsType.ReportsFolder:
{
table = GetConfigurationReportsFolderSection();
return table;
}
}
return null;
}
}
Now I thhink about how I can convert this code to OOP i.e. move away from the static class and enumerations and use the OOP paradigms such as an interface, abstraction, polymorphism and inheritance. But I do not know which is better, and which is would be correct in this case:
1) Create an abstract StoredSettings class with some methods to get/set settings and inherit from it.
2) Use an interface IStoredSettings and inherit from it.
Please could you give me some advice with code example how to use the OOP for this task?
Aucun commentaire:
Enregistrer un commentaire