Note that there are some explanatory texts on larger screens.

plurals
  1. POTypecast or convert method?
    text
    copied!<p>I have a container class with parameters which come from different kinds of configuration files (text or xml for example).</p> <p>So I made a class for textConfigurationFiles and a class for xmlConfigurationFiles (I think I will implement an interface IConfigFile for this later).</p> <p>The initialisation of my container class looks like the following:</p> <pre><code>ContainerClass myContainer = new ContainerClass(); myTextConfig = new TextConfig(textFile); myContainer.ParameterA = myTextConfig.ParameterA; myContainer.ParameterB = myTextConfig.ParameterB; myContainer.ParameterC = myTextConfig.ParameterC; </code></pre> <p>or for xml</p> <pre><code>ContainerClass myContainer = new ContainerClass(); myXMLConfig = new XMLConfig(xmlFile); myContainer.ParameterA = myXMLConfig.ParameterA; myContainer.ParameterB = myXMLConfig.ParameterB; myContainer.ParameterC = myXMLConfig.ParameterC; </code></pre> <p>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?</p> <pre><code>myContainer = myTextConfig.ConvertToContainer(); </code></pre> <p>or</p> <pre><code>myContainer = (ContainerClass)myTextConfig; </code></pre> <p>What would be your suggestion?</p> <p>Because I'm trying to implement the Single Responsibility Principle I want my ContainerClass to represent a simple Container. So I think something like</p> <pre><code>ContainerClass myContainer = new ContainerClass(configFile); </code></pre> <p>would break with SRP?</p> <p>Perhaps I should mention that my ContainerClass is initialized by a text <strong>or</strong> a xml file; it's not that the parameters result from both a text <strong>and</strong> a xml file.</p> <hr> <p><strong>[EDIT: Additional information]</strong></p> <p>ContainerClass represents a container with database configuration information (paths, name etc.).</p> <p>textFile represents a database configuration.</p> <p>xmlFile contains a database configuration, too, but has more (database configuration) parameters than my textFile.</p> <p>I think about sth. like:</p> <pre><code>IConfig config = new TextFileConfig(fileName); ContainerClass container = config.ConverToContainer(); </code></pre> <p>or</p> <pre><code>IConfig config = new TextFileConfig(fileName); ContainerClass container = (ContainerClass)config; </code></pre> <p>In TextFileConfig:</p> <pre><code>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; } } </code></pre> <p>So should I prefer a convert method, a typecast or both?</p> <p>At first I think about performance, best usage and personal habits.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload