类型转换还是转换方法?
我有一个容器类,其参数来自不同类型的配置文件(例如文本或 xml)。
所以我为textConfigurationFiles创建了一个类,为xmlConfigurationFiles创建了一个类(我想我稍后会为此实现一个接口IConfigFile)。
我的容器类的初始化如下所示:
ContainerClass myContainer = new ContainerClass();
myTextConfig = new TextConfig(textFile);
myContainer.ParameterA = myTextConfig.ParameterA;
myContainer.ParameterB = myTextConfig.ParameterB;
myContainer.ParameterC = myTextConfig.ParameterC;
或者对于 xml
ContainerClass myContainer = new ContainerClass();
myXMLConfig = new XMLConfig(xmlFile);
myContainer.ParameterA = myXMLConfig.ParameterA;
myContainer.ParameterB = myXMLConfig.ParameterB;
myContainer.ParameterC = myXMLConfig.ParameterC;
,但我不想分配每个值,而是想做某种转换。 现在我不知道是否应该实现类型转换或只是返回 ContainerClass 对象的简单方法?
myContainer = myTextConfig.ConvertToContainer();
或者
myContainer = (ContainerClass)myTextConfig;
你的建议是什么?
因为我正在尝试实现单一职责原则,所以我希望我的 ContainerClass 代表一个简单的容器。 所以我认为类似的东西
ContainerClass myContainer = new ContainerClass(configFile);
会打破SRP?
也许我应该提到我的 ContainerClass 是由文本或 xml 文件初始化的; 参数并不是由文本和 xml 文件产生的。
[编辑:附加信息]
ContainerClass 表示具有数据库配置信息(路径、名称等)的容器。
textFile 代表数据库配置。
xmlFile 也包含数据库配置,但比我的 textFile 有更多(数据库配置)参数。
我在想某事。 像:
IConfig config = new TextFileConfig(fileName);
ContainerClass container = config.ConverToContainer();
或
IConfig config = new TextFileConfig(fileName);
ContainerClass container = (ContainerClass)config;
在 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;
}
}
那么我应该更喜欢转换方法、类型转换还是两者兼而有之?
首先我会考虑性能、最佳使用方式和个人习惯。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我会按照以下方式做一些事情:
IConfig
IConfigLoader
IConfig
>XmlConfigLoader、TextConfigLoader
等),返回IConfig
实例。IConfig
实例传递给ContainerClass
构造函数,并让它初始化自身。关于你的问题,我不确定将配置对象传递到容器会破坏 SRP。 至少,您可以为容器建立一个工厂,它将获取并读取配置以实例化和初始化容器。 它可能是过度设计的——我猜这取决于容器创建任务的复杂程度。
I think I would do something along theses lines:
IConfig
IConfigLoader
XmlConfigLoader
,TextConfigLoader
, etc.) that returnsIConfig
instances.IConfig
instance to theContainerClass
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.