I am currently developing a tool that aims to support backward compatibility with configuration files. The configuration files are in XML format, which the application parses and maps to the appropriate object type based on the version tag found in the XML.
In the application's business (services) classes, it determines which type to use based on the configuration version. Each configuration version is organized as follows:
Namespace: Common (includes base classes, shared types, and fields)
Namespace: V10 (for Config version 1.0) which consumes BaseConfig.cs from the Common namespace
Namespace: V15 (for Config version 1.5) which also consumes BaseConfig.cs from the Common namespace
... and so on
The application determines the appropriate type to use by referencing the configuration version specified in the XML file. Based on this version, the application knows which namespace to access and which specific type to use.
namespace Common.Utils
{
public static class ConfigMapper
{
public static IConfiguration? ConvertToIConfiguration(BaseConfiguration baseConfig)
{
var version = baseConfig.SchemaVersion;
if (version == SchemaVersion.V10)
{
var config = baseConfig as ConfigSchemaV2.V10.Configuration;
return config;
}
if (version == SchemaVersion.V11)
{
// ...
}
return null;
}
public static IConfiguration? ConvertToIConfiguration(string xmlConfig)
{
var version = ConfigParserFactory.GetSchemaVersion(xmlConfig);
if (version == SchemaVersion.V10)
{
IConfigParser<ConfigSchemaV2.V10.Configuration> parser = ConfigParserFactory.GetParser<ConfigSchemaV2.V10.Configuration>(xmlConfig);
var configV1 = parser.Parse(xmlConfig);
return configV1;
}
return null;
}
}
}
private static void Main(string[] args)
{
var configPath = @"C:\Users\User\Desktop\config-test.xml";
//TestCreateConfig(SchemaVersion.V10);
IConfiguration? conf = TestReadConfig(configPath);
if (conf is ConfigSchemaV2.V10.Configuration)
{
Configuration? config = conf as ConfigSchemaV2.V10.Configuration;
}
//else if (conf is ConfigSchemaV2.V11.Configuration)
//{
// Configuration? config = conf as ConfigSchemaV2.V11.Configuration;
//}
//else if (conf is ConfigSchemaV2.V12.Configuration)
//{
// Configuration? config = conf as ConfigSchemaV2.V12.Configuration;
//}
//else if (conf is ConfigSchemaV2.V13.Configuration)
//{
// Configuration? config = conf as ConfigSchemaV2.V13.Configuration;
//}
//else if (conf is ConfigSchemaV2.V14.Configuration)
//{
// Configuration? config = conf as ConfigSchemaV2.V14.Configuration;
//}
Console.Read();
}
As you can see, it's a mess, and there are many classes that need to use the config object directly without making a lot of if-statements.
What could be a good and efficient way to solve this problem?
Thanks.
Aucun commentaire:
Enregistrer un commentaire