将 XML 对象解析为具有不同逻辑的多个类 [英] Parsing XML object to multiple classes with different logic

查看:59
本文介绍了将 XML 对象解析为具有不同逻辑的多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多由第三方生成的 XML 节点:

I have a lot of XML nodes generated by a 3rd party:

<node id='8440' name='nodeOne' property_two='7.22' sorting_index='20'/>
<node id='8440' name='nodeTwo' property_two='7.22' sorting_index='subItemThree;30;subItemTwenty;50'/>
...

除了名为 sorting_index 的节点之外,所有节点的每个属性都具有相同的类型和含义.通常,它包含一个 int 指示对象的排序索引.在上面的第一个节点中,对于名为nodeOne"的对象,排序索引为 20.

Every attribute has the same type and meaning among all nodes except one named sorting_index. Usually, it contains an int indicating, well, object's sorting index. In the first node above sorting index is 20 for an object named "nodeOne".

不幸的是,有时这些节点只是多个子对象的排序索引的载体".查看上面给出的第二个节点,我们可以看到它为名为subItemThree"和subItemTwenty"的对象分别提供了值 30 和 50 的排序索引.

Unfortunately, sometimes those nodes are just "carriers" of a sorting index for multiple subobjects. Looking into a second node given above, we can see that it provides sorting indeces for objects named "subItemThree" and "subItemTwenty" with values 30 and 50 respectively.

我的方法是,我创建了两个类,第一个类是具有简单 sorting_index 的节点的表示,第二个类是具有复杂 sort_index 逻辑的节点的表示.第二个类扩展了第一个类并添加了一个方法来从 SortingIndex 属性的 getter 中提取排序索引数组.所以我所做的是将所有内容解析为基类(SortingIndex 属性属于 string 类型),然后将其中一些转换为派生类.我在另一个问题中描述了这背后的逻辑:从基础对象转换为派生对象,同时拥有庞大的构造函数定义

Im my approach, I created two classes with first being a representation of a node with simple sorting_index and second class being a representation of a node with the complicated sorting_index logic. Second class extended the first one and added a method to extract an array of sorting indeces from SortingIndex property's getter. So what I did is I parsed everything to a base class (SortingIndex property is of string type) and then converted some of them to a derived class. I've described the logic behind this in another question: Converting from base to derived object while having a huge constructor definition

但是,有人告诉我进行这样的转换并不是一个好的设计方法.你有什么建议?我觉得拥有 2 个完全独立的类,除了一个属性之外,具有完全相同的逻辑是一种矫枉过正.

However, I was told that having such a conversion is not a good design approach. What are your suggestions? I feel like having 2 totally independent classes with absolutely same logic except for one property is an overkill.

推荐答案

是否应该从另一个继承你应该考虑派生类是否可以替代基类,参见Liskov 替换原则

Whether one should inherit from the other you should consider whether the derived class is substitutable for the base class, see the Liskov substitution principle

如果不是,您可以尝试使用具有除排序索引之外的所有属性的公共基类.

If its not, you could try something like this with a common base class that has all the properties apart from the sort index.

public abstract class BaseClass
{
    public string Property1 { get; }
    public int Property2 { get; }

    public BaseClass(string prop1, int prop2)
    {
        ...
    }
}

public class StringClass : BaseClass
{
    public int SortIndex { get; }

    public StringClass(string prop1, int prop2, int index) :
        base(prop1, prop2)
    {
        SortIndex = index;
    }
}

public class CollectionClass : BaseClass
{
    public Dictionary<string, int>  SortIndexes { get;}

    public CollectionClass(string prop1, int prop2, Dictionary<string, int> indexes) :
        base(prop1, prop2)
    {
        SortIndexes = indexes;
    }
} 

这篇关于将 XML 对象解析为具有不同逻辑的多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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