I had a problem creating a design with some classes that require a one-time initialization with a parameter such as the name of an external resource, for example, a configuration file.
For example, I have a corelib project that provides logging methods, settings, and general helper methods at the application level. This object can use a static constructor to initialize, but it needs access to a configuration file that it cannot find.
I see a couple of solutions, but both of them do not look quite right:
1) Use a constructor with a parameter. But then every object that requires corelib functionality should also know the name of the configuration file, so this should be passed around the application. In addition, if I implemented corelib as a singleton, I would also have to pass the configuration file as a parameter to the GetInstance method, which, I believe, is also not right.
2) Create a static property or method to pass through the configuration file or other external parameter.
I use the last method and created the Load method, which initializes the inner class, which it passes through the configuration file in the constructor. This inner class is then opened through the public property MyCoreLib.
public static class CoreLib
{
private static MyCoreLib myCoreLib;
public static void Load(string configFile)
{
myCoreLib = new MyCoreLib(configFile);
}
public static MyCoreLib MyCoreLib
{
get { return myCoreLib; }
}
public class MyCoreLib
{
private string configFile;
public MyCoreLib(string configFile)
{
this.configFile = configFile;
}
public void DoSomething()
{
}
}
}
. , , , MyCoreLib. , -, .
, ?