我需要一个将在启动时配置并且永远不会更改的字典。 要求:
有没有办法达到所有这些目标?
我考虑的选项
这是展示这两种方法的代码的简化版本:
public class Foo
{
private NestedSingleton nestedSingleton = NestedSingleton.GetInstance;
public string SomeItemFromStatic { get { return (staticDict[1]); } }
public string SomeItemFromSingleton { get { return (nestedSingleton[1]); } }
private static Dictionary<int, double> staticDict = new Dictionary<int, double>()
{ //Need to instantiate values from config file. But don't know how...
{ 1, 1.345396 },
{ 2, 29.34396 },
{ 3, 17.34396 },
};
class NestedSingleton : Dictionary<int, double>
{
private static readonly NestedSingleton _instance;
static NestedSingleton()
{
_instance = new NestedSingleton() //Actual code reads from config file here
{
{ 1, 1.345396 },
{ 2, 29.34396 },
{ 3, 17.34396 },
};
}
private NestedSingleton() { } // Private ctor enforces Singleton pattern
public static NestedSingleton GetInstance { get { return _instance; } }
}
}
有没有办法满足所有规定的要求? (注意:此时我还没有引入DI框架。如果可能的话,我更喜欢不依赖于其中的答案。)
答案 0 :(得分:1)
不是创建字典,而是创建字典&#34; loader&#34;抽象,将此抽象/接口的实例传递给被测试的类。
你可以模拟装载机,它将返回任何&#34;伪造的&#34;字典。
然后将loader实现为&#34; CachedLoader&#34;这将在本地保存加载的字典,并通过属性或方法为消费者返回实例。
加载程序的实现可以保持单例
public interface IConfiguration
{
public IDictionary<int, int> Data { get; }
}
public class YourClass
{
private readonly IConfiguration _configuration;
public YourClass(IConfiguration configuration)
{
_configuration = configuration
}
public bool GetValue(int id)
{
return _configuration.Data[id];
}
}
public class CachedConfiguartion : IConfiguartion
{
public IDictionary<int, int> _cachedData;
public IDictionary<int, int> Data { get { return _cachedCData; } }
public CachedConfiguartion() {}
}
对于CachedConfiguartion
,您可以使用专用方法加载字典,或者制作Data
类型的Lazy<Dictionary<int, int>>
并在首次使用时加载