C # The task of static constructors - you need to specify a parameter

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. , -, .

, ?

+3
6

. app.config, , , app.config. appSettings , . , . , .

    public interface ICoreLib
    {
        void SomeMethod();
    }
    public static class CoreLibManager
    {
        private static ICoreLib coreLib;
        private static volatile bool initialized;
        private static readonly object lockObject = new object();
        public static ICoreLib CoreLib
        {
            get
            {
                Inititialize();
                return coreLib;
            }
        }

        /// <summary>
        /// The inititialize.
        /// </summary>
        private static void Inititialize()
        {
            if (initialized)
            {
                lock (lockObject)
                {
                    if (!initialized)
                    {
                        string configFile =  // Fech from common location
                        coreLib = new MyCoreLib(configFile);
                        initialized = true;
                    }
                }
            }
        }

        /// <summary>
        /// The my core lib.
        /// </summary>
        private class MyCoreLib : ICoreLib
        {
            public MyCoreLib(string configPath)
            {
            }
            public void SomeMethod()
            {
            }
        }
    }
+1

, , , (, ) , , , Load . .

, , , , , , , . , , , . , , , . , .

+1

.net. - <appsettings> web.config appname.exe.config. :

ConfigurationManager.AppSettings["property_name"]

. , - Windows, .

, , , :)

+1

singleton, , .

0

.

( , () CoreLib , , .

MyCoreLib. , , , .

0

, . CoreLib . . , ConfigFile. , - . reset, .

public interface IConfig
{
    void SomeMethod();
}

public static class ConfigurationManager
{
    private static IConfig config;
    private static volatile bool initialized;
    private static readonly object lockObject = new object();

    public static string ConfigFile
    {
        get { return Properties.Settings.Default.ConfigFile; }
        set
        {
            if (Properties.Settings.Default.ConfigFile == value) return;

            lock (lockObject)
            {
                Properties.Settings.Default.Save();
                initialized = false;
            }
        }
    }

    public static IConfig Config
    {
        get
        {
            Inititialize();
            return config;
        }
    }

    private static void Inititialize()
    {
        lock (lockObject)
        {
            if (initialized) return;

            config = new Configuration(Properties.Settings.Default.ConfigFile);
            initialized = true;
        }
    }
}

internal class Configuration : IConfig
{
    public ClientConfig(string configFile)
    {
        // Parse & validate config file
    }

    public void SomeMethod()
    {
    }
}

So, now at startup, first check the saved ConfigFile parameter, and then try to access the Configuration instance through the Config Manager property. Any exceptions for parsing can be handled here and handled accordingly. Then the developer should handle any exceptions for the IConfig methods.

if (!System.IO.File.Exists(ConfigurationManager.ConfigFile))
{
    // Display config file locator dialog
    ConfigurationManager.ConfigFile = someDialog.FileName;
}

try
{
    IConfig config = ConfigurationManager.Config;
}
catch
{
    // Error parsing config file
}
0
source

Source: https://habr.com/ru/post/1742197/


All Articles