简体   繁体   English

有什么更好的方法来实现静态全局集合?

[英]What is better way to implement static global collections?

I am thinking what is the best way to load data to collections if it is global per application; 我在想如果每个应用程序都是全局的,将数据加载到集合的最佳方法是什么?

public static class ErrorValues
{
        public static readonly Dictionary<int, string> errorInfo = new Dictionary<int, string>
        {
            {0, "Error 404"},
            {1, "Error 500"},
            {2, "Error 301"}
        };
}

or like this 或像这样

public static class ErrorValues
{
    public static Dictionary<int, string> errorInfo;

    static ErrorValues()
    {
      if (errorInfo == null)
      {
        errorInfo = LoadDataToDictionary();
      }
   }
}

better solutions? 更好的解决方案? Why? 为什么?

If your data is static, I recommend creating a meaningful type 如果您的数据是静态的,建议您创建一个有意义的类型

Example: 例:

public class ErrorValue
{
    private static Dictionary<Int32, ErrorValue> _errors;

    public static readonly ErrorValue Error404 = new ErrorValue(404, "Error 404");
    public static readonly ErrorValue Error500 = new ErrorValue(500, "Error 500");
    public static readonly ErrorValue Error301 = new ErrorValue(301, "Error 301");

    public String ErrorName { get; private set; }
    public Int32 ErrorCode { get; private set; }

    private ErrorValue(Int32 errorCode, String errorName)
    {
        if (_errors == null)
            _errors = new Dictionary<int, ErrorValue>();

        ErrorName = errorName;
        ErrorCode = errorCode;

        _errors.Add(errorCode, this);
    }

    public static IEnumerable<ErrorValue> Errors { get { return _errors.Values; } }

    public static ErrorValue GetErrorByCode(Int32 errorCode)
    {
        return _errors[errorCode];
    }
}

This will lead to a less error-prone code due to type safety, since you can write methods with paramters of type ErrorValue : 由于类型安全,这将导致不太容易出错的代码,因为您可以使用类型为ErrorValue参数编写方法:

void HandleError(ErrorValue ev)
{ 
    // bla bla
}

Another benefit is that with this approach, you can easily extend the type; 另一个好处是,使用这种方法,您可以轻松扩展类型。 eg add other properties like Description , without huge changes to your code. 例如,添加其他属性,例如Description ,而无需对代码进行大量更改。

If you need similar static global collections, you can extract a common generic base class to provide methods like GetById or GetByName or similar. 如果您需要类似的静态全局集合,则可以提取一个通用的通用基类以提供诸如GetByIdGetByName类的方法。

除了beforefieldinit标志外,生成的IL不应有任何区别。

I think the first one is simple if items are static/hard coded and not to be loaded from DB or some other data source. 我认为,如果项目是静态/硬编码的,而不是要从数据库或其他数据源加载的,则第一个很简单。

Second one is using singleton pattern that is used heavily in applications where object is to be created only once and reuse that object reference throughout the life cycle of application. 第二个方法是使用单例模式,该模式在仅创建对象一次并在应用程序的整个生命周期中重复使用该对象引用的应用程序中使用率很高。 and offers to initialize collection from any other data sources. 并提供初始化任何其他数据源的收集的功能。

Conclusion: both are good but depends on what you need. 结论:两者都很好,但是取决于您的需求。 personally i like the second way as it follows a design pattern. 我个人喜欢遵循设计模式的第二种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM