最佳实践实例对象 - 字典与对象

Best practice instance objects - dictionary vs object

提问人:Doe 提问时间:11/17/2023 更新时间:11/17/2023 访问量:20

问:

假设我有一个具有数百个属性的复杂对象。 使用字典而不是分层对象是否有真正的缺点?

过去我使用过具有复杂结构的物体,但我不确定将来是否应该使用它们。

使用字典,我很灵活,只能返回所需的数据,但缺点是性能 - 至少,我的想法和代码的可读性当然是这样。

另一方面,对于对象,我认为它会更快,但我总是得到我并不真正需要的数据。

这是我的问题的两个例子。也许有人可以给我一个意见,我应该使用字典还是对象。这只是一个例子,在现实世界中,article 对象将具有大量属性和几个子类。

    // Dictonary class
    class ArticleDictionary {
      public int Id { get; set; }
      public Dictionary<string, object> Infos = new();
      public Dictionary<string, Dictionary<string, object>> SellingInfos = new();
      public Dictionary<string, object> DetailInfos = new();
      public Dictionary<string, object> DispatchInfos = new();
    }

    // Object classes
    class ArticleProperty {
      public int Id { get; set; }
      public string Model { get; set; }
      public string EanNumber { get; set; }
      public string CreationDate { get; set; }
      public string Description { get; set; }
      public int State { get; set; }
    }

    class ArticleDetailProperty : ArticleProperty {
      public string TechnicalData { get; set; }
      public string Features { get; set; }
      public Dictionary<string, ArticleSellingInfosProperty> SellingInfos = new();
    }

    class ArticleMoreDetailProperty : ArticleDetailProperty {
      public string DispatchInfos { get; set; }
    }

    class ArticleSellingInfosProperty {
      public string WarehouseKey { get; set; }
      public Decimal PriceA { get; set; }
      public Decimal PriceB { get; set; }
      public int Stock { get; set; }
    }
JSON 性能 字典 对象 渲染

评论


答: 暂无答案