C#静态构造函数的设计问题 - 需要指定参数 [英] C# Static constructors design problem - need to specify parameter

查看:147
本文介绍了C#静态构造函数的设计问题 - 需要指定参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个重新发生设计问题与需要一次性初始化用参数诸如外部资源的名称,例如配置文件某些类

I have a re-occurring design problem with certain classes which require one-off initialization with a parameter such as the name of an external resource such as a config file.

举例来说,我有一个corelib的项目,提供应用程序级的日志,配置和一般的辅助方法。这个对象可以使用静态构造函数初始化本身,而是它需要获得它不能找到一个自己的配置文件。

For example, I have a corelib project which provides application-wide logging, configuration and general helper methods. This object could use a static constructor to initialize itself but it need access to a config file which it can't find itself.

我可以看到一对夫妇的解决方案,但这两种似乎不太对劲:

I can see a couple of solutions, but both of these don't seem quite right:

1)使用带参数的构造函数。但随后这需要corelib的功能,每个对象也应该知道的配置文件的名称,所以这在应用程序中传递。另外,如果我实现corelib的作为一个单身我也必须通过配置文件作为参数传递给getInstance方法,我认为也是不对的。

1) Use a constructor with a parameter. But then each object which requires corelib functionality should also know the name of the config file, so this has to be passed around the application. Also if I implemented corelib as a singleton I would also have to pass the config file as a parameter to the GetInstance method, which I believe is also not right.

2)创建一个静态属性或方法都可以通过配置文件或其他外部参数。

2) Create a static property or method to pass through the config file or other external parameter.

我那种用后一种方法,创造它初始化一个内部类中的Load方法它通过在构造函数中的配置文件。那么这个内部类是通过公共财产MyCoreLib暴露。

I have sort of used the latter method and created a Load method which initializes an inner class which it passes through the config file in the constructor. Then this inner class is exposed through a 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进行访问。也没有什么可以阻止有人再次调用加载方法。

I'm still not happy though. The inner class is not initialized until you call the load method, so that needs to be considered anywhere the MyCoreLib is accessed. Also there is nothing to stop someone calling the load method again.

任何其他图案或想法如何做到这一点?

Any other patterns or ideas how to accomplish this?

推荐答案

您需要一个共同的位置来存储这一点。你可以使用在app.config,即使这是通过定义库组件的配置部分,并引用它,你proceess的app.config一个单独的组件。或者你可以只添加一个通用设置的appSettings和参考,如果不使用强类型设置。如果该值是用户输入的,那么你可以使用独立存储。
最后,你可以把它放在一个众所周知的位置,在安装时的注册表。

You need a common location to store this. You could use the app.config even if this is a seperate assembly by defining a config section in the library assembly and referencing it you proceess app.config. Or you could just add a generic setting to appSettings and reference that without using strongly typed settings. If the value is user entered then you could use isolated storage. Finally you could put it in a well known location in the registry at install time.

有关代码下面是encapsulted好

For code the following is encapsulted better

    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()
            {
            }
        }
    }

这篇关于C#静态构造函数的设计问题 - 需要指定参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆