从集合中的项目访问通用集合的实例

Accessing instance of generic collection from items in collection

我有一个通用集合,它代表一个订单,每个订单项都是一个 "Item" class 对象。订单的总价值基于所有订单项的组合价值。我目前有一种方法可以在检索到 "TotalValue" 属性 时获取订单的总价值,但我发现这种方法有点不优雅,因为它感觉像薛定谔的变量;它只有在观察时才成为总值。

有什么方法可以让 "Item" 成员保存在 "Quote" 集合中,以触发包含 Quote class 的实例的 "updateValue" 方法每当 "Item" 对象去修改它自己的成本值时?项目 class 自动修改它自己的 "TotalCost" 但我希望它把它踢到链上并同时修改订单的总数 cost/value。

我试图削减它以仅保留相关位。

namespace Arbitrary
{
    public class Order<T> : IEnumerable<T> where T : Item
    {
        private List<T> _lines = new List<T>();

        private decimal _totalValue;
        public decimal TotalValue
        {
            get { return updateValue(); }
        }

        private decimal updateValue()
        {
            _totalValue = 0;
            foreach(T Item in _lines)
            {
                _totalValue += Item.TotalCost;
            }
            return _totalValue;
        }
    }

    public class Item
    {
        private decimal _cost;
        private int _quantity;
        private decimal _totalCost;

        public decimal Cost
        {
            get { return _cost; }
            set 
            { 
                _cost = value;
                _totalCost = (_cost * _quantity);
            }
        }
        public int Quantity
        {
            get { return _quantity; }
            set 
            { 
                _quantity = value;
                _totalCost = (_cost * _quantity);
            }
        }
        public decimal TotalCost
        {
            get { return _totalCost; }
        }
    }
}

更新: 多亏了 Blorgbeard 的提示解决了这个问题。我将 "Item" 的构造函数切换为获取 Quote 集合的对象引用(因为它们永远不会在没有 Quote 集合的情况下存在)。我将 "updateValue" 方法更改为内部方法,以便项目 class 可以访问它,因为它们都在同一个程序集中。更新的更改注释为“//已修改”

namespace Arbitrary
{
    public class Order<T> : IEnumerable<T> where T : Item
    {
        private List<T> _lines = new List<T>();

        private decimal _totalValue;
        public decimal TotalValue
        {
            get { return updateValue(); }
        }

        internal decimal updateValue()    // modified
        {
            _totalValue = 0;
            foreach(T Item in _lines)
            {
                _totalValue += Item.TotalCost;
            }
            return _totalValue;
        }
    }

    public class Item
    {
        private decimal _cost;
        private int _quantity;
        private decimal _totalCost;

        private Quote<Item> q;    // modified
        public Item(Quote<Item> q)    // modified
        {
            this.q = q;    // modified
        }

        public decimal Cost
        {
            get { return _cost; }
            set 
            { 
                _cost = value;
                _totalCost = (_cost * _quantity);
                q.updateValue();    // modified
            }
        }
        public int Quantity
        {
            get { return _quantity; }
            set 
            { 
                _quantity = value;
                _totalCost = (_cost * _quantity);
                q.updateValue();    // modified
            }
        }
        public decimal TotalCost
        {
            get { return _totalCost; }
        }
    }
}