开发者

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); 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))

So you see I need to recurse up the tree (which incidentally is a List<ItemType>) to get the parent's CalculatedDiscount, because th开发者_如何转开发at could be null, which means you need to get the parent's parent's CalculatedDiscount and so on...

Is it a bad idea to put the code for this in the Get accessor?

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. 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 (e.g. setting all children's CalculatedDiscount in the Set accessor). 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. 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.


Properties are normally considered as not doing much. Because of that, I suggest, you create a method GetCalculatedDiscount that does all the traversing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜