C# - Get 和 Set 访问器中的树/递归?

发布于 2024-10-19 09:19:03 字数 1839 浏览 3 评论 0原文

我有一棵树(List),其中包含许多 ItemType 类(请参见下面的代码);该类具有属性 OverrideDiscount(可能为 null),表示使用 DefaultDiscount(可能为 null),指示使用 parent ItemTypeCalculatedDiscount))

所以你看我需要递归树(顺便说一句,这是一个 List) 来获取父级的 CalculatedDiscount,因为它可能为 null,这意味着您需要获取父级的父级的 CalculatedDiscount code> 等等...

将其代码放入 Get 访问器中是不是一个坏主意?

你会怎么处理?

顺便说一句,所有这些数据都通过 SqlDataReader 从数据库中获取,没有特定的顺序,然后通过循环遍历树并添加 Children 属性列表来填充根据需要添加到Children 列表中。因此,在调用 Set 访问器之前,父母不会意识到孩子的存在,排除在 Set 访问器中放置任何有用的内容(例如设置所有孩子的 CalculatedDiscount< /code> 在 Set 访问器中)。除非我错过了其他一些方法(很可能,递归有时会烧坏我的大脑)。

提前致谢

到目前为止的课程:

    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?
                    }
                }
            }
        }
    }

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 that 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?
                    }
                }
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阿楠 2024-10-26 09:19:03

我不只是存储父项的 ID,而是存储完整的对象。这将使事情变得更容易(我还将这些公共变量转换为属性):

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

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));
        }
    }
}
女中豪杰 2024-10-26 09:19:03

我不认为这不是一个好主意。也许在该属性的 Xml 注释中指定它,以确保其他人知道该行为,但如果它代表您的程序的逻辑,那么为什么不呢。

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.

酒绊 2024-10-26 09:19:03

属性通常被认为没有多大作用。因此,我建议您创建一个方法 GetCalculatedDiscount 来执行所有遍历。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文