简体   繁体   English

C#-获取和设置访问器中的树/递归?

[英]C# - Tree / Recursion in Get and Set Accessors?

I have a tree (a List<T> ) that contains a number of ItemType classes (see code below); 我有一棵树( List<T> ),其中包含许多ItemType类(请参见下面的代码); the class has the properties OverrideDiscount (which could be null , indicating to use DefaultDiscount (which could be null , indicating to use the parent ItemType 's CalculatedDiscount )) 该类具有OverrideDiscount属性(可以为null ,指示使用DefaultDiscount (可以为null ,指示使用 ItemTypeCalculatedDiscount ))

So you see I need to recurse up the tree (which incidentally is a List<ItemType> ) to get the parent's CalculatedDiscount , because that could be null , which means you need to get the parent's parent's CalculatedDiscount and so on... 因此,您看到我需要递归树(顺便说一句是List<ItemType> )以获取父级的CalculatedDiscount ,因为它可以为null ,这意味着您需要获取父级的父级的CalculatedDiscount等等。

Is it a bad idea to put the code for this in the Get accessor? 将此代码放在Get访问器中是个坏主意吗?

How would you handle it? 您将如何处理?

Just as a sidenote, all this data comes via an SqlDataReader from a database in no particular order, then after that the Children property list is populated by looping through the tree and adding to the Children list as appropriate. 就像一个旁注一样,所有这些数据都是通过SqlDataReader从数据库中获取的,而没有特定的顺序,然后,通过遍历树并适当地添加到Children列表中,来填充Children属性列表。 So the parents are unaware of the children until AFTER the Set accessor has been called, ruling out putting anything useful in the Set accessor (eg setting all children's CalculatedDiscount in the Set accessor). 所以父母不知道孩子后才的Set访问被调用,排除了将任何有用的Set访问(例如,设置所有儿童的CalculatedDiscountSet访问)。 Unless I've missed some other way of doing it (very possible, recursion fries my brain sometimes). 除非我错过了其他方法(极有可能,递归有时使我的大脑发疯)。

Thanks in advance 提前致谢

The class so far: 到目前为止的课程:

    public class ItemType
    {
        public int ID;
        public int? ParentID;
        public List<ItemType> Children;

        public double? DefaultDiscount; 
        public double? OverrideDiscount; 
        public double CalculatedDiscount
        {
            get
            {
                if (OverrideDiscount != null)
                {
                    return (double)OverrideDiscount; //+ Autospec qty
                }
                else
                {
                    if (DefaultDiscount != null)
                    {
                        return (double)DefaultDiscount;
                    }
                    else
                    {

                        //I need to get this ItemType's parent's discount 
                        //here by recursing up the tree...is this a bad idea?
                    }
                }
            }
        }
    }

Instead of just storing the Id of the Parent item, I would store the complete object. 我将存储完整的对象,而不仅仅是存储父项的ID。 That would make this a lot easier (I would also convert those public variables to properties): 这将使这变得容易得多(我还将这些公共变量转换为属性):

public class ItemType
{
    public int Id { get; set; }
    public ItemType Parent { get; set; }
    public List<ItemType> Children; { get; set; }

    public double? DefaultDiscount { get; set; }
    public double? OverridenDiscount { get; set; }

    public double CalculatedDiscount
    {
        get
        {
            return (double)(OverridenDiscount ?? 
                            DefaultDiscount ?? 
                            (Parent != null ? Parent.CalculatedDiscount : 0));
        }
    }
}

I don't see any reason why this is not a good idea. 我看不出为什么这不是一个好主意。 Maybe specify it in Xml comments for that property to make sure others are aware of that behavior but if it represents your program's logic then why not. 也许在Xml注释中为该属性指定它,以确保其他人知道该行为,但是如果它表示您程序的逻辑,那为什么不这样做。

Properties are normally considered as not doing much. 通常认为属性做得不多。 Because of that, I suggest, you create a method GetCalculatedDiscount that does all the traversing. 因此,我建议您创建一个执行所有遍历的方法GetCalculatedDiscount

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

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