I have a container class with parameters which come from different kinds of configuration files (text or xml for example).
So I made a class for textConfigurationFiles and a class for xmlConfigurationFiles (I think I will implement an interface IConfigFile for this later).
The initialisation of my container class looks like the following:
ContainerClass myContainer = new ContainerClass();
myTextConfig = new TextConfig(textFile);
myContainer.ParameterA = myTextConfig.ParameterA;
myContainer.ParameterB = myTextConfig.ParameterB;
myContainer.ParameterC = myTextConfig.ParameterC;
or for xml
ContainerClass myContainer = new ContainerClass();
myXMLConfig = new XMLConfig(xmlFile);
myContainer.ParameterA = myXMLConfig.ParameterA;
myContainer.ParameterB = myXMLConfig.ParameterB;
myContainer.ParameterC = myXMLConfig.ParameterC;
but instead of assigning every single value I would like to do some kind of conversion. Now I don't know if I should implement a typecast or just a simple method that returns a ContainerClass-object?
myContainer = myTextConfig.ConvertToContainer();
or
myContainer = (ContainerClass)myTextConfig;
What would be your suggestion?
Because I'm trying to implement the Single Responsibility Principle I want my ContainerClass to represent a simple Container. So I think something like
ContainerClass myContainer = new ContainerClass(configFile);
would break with SRP?
Perhaps I should mention that my ContainerClass is initialized by a text or a xml file; it's not that the parameters result from both a text and a xml file.
[EDIT: Additional information]
ContainerClass represents a container with database configuration information (paths, name etc.).
textFile represents a database configuration.
xmlFile contains a database configuration, too, but has more (database configuration) parameters than my textFile.
I think about sth. like:
IConfig config = new TextFileConfig(fileName);
ContainerClass container = config.ConverToContainer();
or
IConfig config = new TextFileConfig(fileName);
ContainerClass container = (ContainerClass)config;
In TextFileConfig:
public class TextFileConfig : IConfig
{
ContainerClass container;
//
// Constructor
//
public TextFileConfig(string fileName)
{
//
// Initialize all values from file fileName
//
...
//
// Initialize container with standard values
//
container = new ContainerClass();
//
// Merge container with values from textFile
//
...
//
// Now container contains all values from textFile,
// all missing parameters are initialized with
// standard values.
//
}
public ContainerClass ConvertMethodOrTypecastThatsTheQuestion()
{
return container;
}
}
So should I prefer a convert method, a typecast or both?
At first I think about performance, best usage and personal habits.