Exemplo n.º 1
0
        //只有子类可以调用,外界不能调用
        //这里,Money这个领域依赖了ICurrencyLookup接口,保持领域对外界没有依赖
        protected Money(decimal amount, string currencyCode, ICurrencyLookup currencyLookup)
        {
            if (string.IsNullOrEmpty(currencyCode))
            {
                throw new ArgumentNullException(nameof(currencyCode), "Currency code must be specified");
            }

            var currency = currencyLookup.FindCurrency(currencyCode);

            if (!currency.InUse)
            {
                throw new ArgumentException($"Currency {currencyCode} is not valid");
            }

            if (decimal.Round(amount, currency.DecimalPlaces) != amount)
            {
                throw new ArgumentOutOfRangeException(nameof(amount), $"Amount in {currencyCode} cannot have more than {currency.DecimalPlaces} decimals");
            }
            Amount   = amount;
            Currency = currency;
        }
Exemplo n.º 2
0
 protected Money(decimal amount, CurrencyDetails currency)
 {
     Amount   = amount;
     Currency = currency;
 }