Typical or converting method?
I have a container class with parameters that come from different config files (like text or xml).
So, I created a class for textConfigurationFiles and a class for xmlConfigurationFiles (I think I will implement the IConfigFile interface for this later).
My container class initialization looks like this:
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 each individual value, I would like to do some kind of conversion. Now I don't know if I should implement a type or just a simple method that returns a ContainerClass object?
myContainer = myTextConfig.ConvertToContainer();
or
myContainer = (ContainerClass)myTextConfig;
What would be your proposal?
Since I am 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);
breaks with SRP?
Perhaps I should mention that my ContainerClass is initialized with a text file or xml; it is not that the parameters are the result of both text and xml file.
[EDIT: additional information]
ContainerClass is a container with database configuration information (path, name, etc.).
textFile represents the database configuration.
xmlFile also contains database config but has more parameters (database config) than my textFile.
I am thinking about sth. as:
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 the conversion method, type, or both?
I think about performance first, best use and personal habits.
a source to share
I think I'll be doing something along these lines:
- Create a type that represents your configuration:
IConfig
- Create a type that represents a config loader
IConfigLoader
- Create various implementations of the config loader (
XmlConfigLoader
,TextConfigLoader
etc.) that return instancesIConfig
. - Choose a loader implementation to use either dynamically, config, hardcoding, etc. For example, you can use an abstract factory or an IoC container.
- Pass the loaded instance
IConfig
to the constructorContainerClass
and let it initialize itself.
As for your question, I'm not sure if passing a config object to a container will break the SRP. At the very least, you can have a factory for your container that takes and reads the configuration to instantiate and initialize the container. Perhaps this is too complex designed - it will depend on the level of complexity of the task of creating the container, I think.
a source to share