-1

I have the following business logic:

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

The price should be only changed once based on the chosen OrderSize. When this order is retrieved it calculates the price again which is obviously not what i want. What is the best way to make this only execute once?

  • Everything else aside, looks like you have a naming problem here. If the price field represents something different from the price property their names should reflect that. Perhaps a "basePrice" for the field or some other adjustment to indicate that it isn't accounting for a factor that the property *is* accounting for. – Servy Jan 20 '21 at 20:33

1 Answers1

0

You're not far off. Don't assign the _price variable again, just return the calculation.

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