tags:

views:

122

answers:

1

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.

+3  A: 

I think I would do something along theses lines:

  • Create a type that represent your configuration : IConfig
  • Create a type that represent your configuration loader IConfigLoader
  • Create different configuration loader implementations (XmlConfigLoader, TextConfigLoader, etc.) that returns IConfig instances.
  • Choose the loader implementation to use either dynamically, or by config, or by hard coding, etc. You could use an abstract factory or an IoC container for example.
  • Pass the loaded IConfig instance to the ContainerClass constructor, and let it initialize itself.

Regarding your question, I'm not sure that passing a configuration object to the container will break the SRP. At least, you could have a factory for your container, which would take and read the configuration in order to instantiate and initialize the container. It may be over engineered - that would depend on the complexity level of the container creation task, I guess.

Romain Verdier
There are several reasons why a typecast or convert method could be more appropriate:ContainerClass contains a database configuration (paths, databaseName, kind of database etc.).textFile contains a database configuration.xmlFile contains a database configuration, too, but has more parameters than my textFile.IMHO it would be the best to take all existing parameters from my textFile and keep the ContainerClass standard values for all missing parameters.
Inno