【问题标题】:Efficient way to access constant key value data访问常量键值数据的有效方法
【发布时间】:2013-01-31 21:59:10
【问题描述】:

我想存储键值数据并能够以有效的方式访问它。

基本上:我有一个自定义对象(EquipmentObj),并且该对象中有一个名为“DeviceType”的属性。在对象的构造函数中,我将一个字符串传递给字典(设备对象的局部变量),如果字典有键,则返回一个值。

为了尽量减少在堆上初始化字典 25 次,(EquipmentObj 被实例化 25-50 次)我想知道是否有更有效的方法来做到这一点。

我的第一个想法是XML,但是我不能添加反序列化;我不会进入这个。

我的下一个想法可能是使用静态类。但是我仍然需要定义 KeyValuePair 或 Dictionary 并且静态类不能有实例成员。

大家有什么建议?

这是我现在基本上正在做的一个示例。

class EquipmentObj
    {
        public EquipmentObj(string deviceType)
        {
            addItems(); 
            this.DeviceType = EquipmentList.ContainsKey(device_Type) ? EquipmentList[deviceType] : "Default";
        }
        public string DeviceType { get; set; }
        private Dictionary<string, string> EquipmentList = new Dictionary<string, string>();

        private void addItems()
        {
            //Add items to Dictionary 
        }
    }

【问题讨论】:

  • 字典首先是如何填充的?
  • 静态类可以有静态成员。但是你不需要那个,你为什么不让 EquipmentList 成为 EquipmentObj 的静态成员?
  • 这听起来很像微优化 - 在它被证明是一个问题之前不要浪费你的时间,只是我的建议。
  • @Casperah - 通常,我会同意,这不值得仅仅为了性能问题而做。但首先制作字典static更好的设计,因此值得这样做。
  • EquipmentList 字典是否代表 EquipmentObj 中包含的设备?还是这是别的什么?

标签: c# dictionary keyvaluepair


【解决方案1】:

static 类不能有实例成员,但非静态类可以有 static 成员。您可以将EquipmentListaddItems() 都设为static,而无需更改EquipmentObj 本身。

class EquipmentObj
{
    public EquipmentObj(string deviceType)
    {
        addItems(); 
        this.DeviceType = EquipmentList.ContainsKey(device_Type) ? EquipmentList[deviceType] : "Default";
    }
    public string DeviceType { get; set; }
    private static Dictionary<string, string> EquipmentList = new Dictionary<string, string>();

    public static void addItems()
    {
        //Add items to Dictionary 
    }
}

你可以这样称呼它:

EquipmentObj.addItems();

【讨论】:

    【解决方案2】:

    您可以创建一个 Manager 类来处理设备类型,这不是必需的,但它确实分离了逻辑并使从应用程序中的其他区域访问设备类型更容易一些。

    public class EquipmentObj
    {
        public EquipmentObj(string deviceType)
        {
            this.DeviceType = EquipmentObjManager.GetDeviceType(deviceType);
        }
        public string DeviceType { get; set; }
    }
    
    public class EquipmentObjManager
    {
        private static Dictionary<string, string> EquipmentList = new Dictionary<string, string>();
    
        public static string GetDeviceType(string deviceType)
        {
            return EquipmentList.ContainsKey(deviceType) ? EquipmentList[deviceType] : "Default";
        }
    
        public static void AddDeviceType(string deviceTypeKey, string deviceType)
        {
            if (!EquipmentList.ContainsKey(deviceTypeKey))
            {
                EquipmentList.Add(deviceTypeKey, deviceType);
            }
        }
    }
    

    我不确定您在哪里填充您的字典,但您可以致电 EquipmentObjManager.AddDeviceType 将项目添加到字典中。

    可能不是最适合您的解决方案,但它可能会有所帮助:)

    【讨论】:

    • 我正在填充字典,只是将项目省略了,因为它们是业务敏感的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2022-10-30
    • 1970-01-01
    相关资源
    最近更新 更多