【问题标题】:How can i make my business logic only execute once, so it alters the object only once我怎样才能让我的业务逻辑只执行一次,所以它只改变一次对象
【发布时间】:2021-04-25 05:22:25
【问题描述】:

我有以下业务逻辑:

public decimal Price
        {
            get {
                if (OrderSize == Size.Small)
                {
                    _price = decimal.Multiply(_price, (decimal)0.8);
                }
                else if (OrderSize == Size.Large)
                {
                    _price = decimal.Multiply(_price, (decimal)1.2);
                }
            return _price;
            }
            set {
                _price = value;
            }
        }

价格只能根据所选的 OrderSize 更改一次。当检索到这个订单时,它会再次计算价格,这显然不是我想要的。让它只执行一次的最佳方法是什么?

【问题讨论】:

  • 除此之外的一切,看起来你在这里有一个命名问题。如果 price 字段表示与 price 属性不同的内容,则它们的名称应反映这一点。可能是字段的“basePrice”或其他一些调整,表明它没有考虑属性考虑的因素。

标签: c# model-view-controller model business-logic


【解决方案1】:

离你不远了。不要再次分配_price变量,只需返回计算即可。

public decimal Price
{
    get
    {
        if (OrderSize == Size.Small)
        {
            return decimal.Multiply(_price, (decimal)0.8);
        }
        else if (OrderSize == Size.Large)
        {
            return decimal.Multiply(_price, (decimal)1.2);
        }
        else
        {
            return _price;
        }
    }
    set
    {
        _price = value;
    }
}

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    相关资源
    最近更新 更多