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; }
protected Money(decimal amount, string currencyCode, ICurrencyLookup currencyLookup) { if (amount == default) { throw new ArgumentException("Value must be specified", nameof(amount)); } if (currencyCode == default) { throw new ArgumentException("currency Code must be specified", nameof(currencyCode)); } var currency = currencyLookup.FindCurrency(currencyCode); if (currency == Currency.None) { throw new ArgumentException($"Invalid currency code: {currencyCode}"); } if (Math.Round(amount, currency.DecimalPlaces) != amount) { throw new ArgumentException($"amount must have no more than {currency.DecimalPlaces} decimals for currency {currency.CurrencyCode}", nameof(amount)); } Amount = amount; CurrencyCode = currency.CurrencyCode; }
protected Money(Decimal amount, string currencyCode, ICurrencyLookup currencyLookup) { this.CheckRule(new CurrencyShouldBeSpecified(currencyCode)); this.CheckRule(new AmountShouldBePositive(amount)); var currency = currencyLookup.FindCurrency(currencyCode); this.CheckRule(new CurrencySouldBeInUse(currencyCode, currency.InUse)); this.CheckRule(new AmountShouldBeDecimalPlacesLessThanCurrencypublic(amount, currency.DecimalPlaces, currencyCode)); this.Amount = amount; this.Currency = currency; }
protected Money(decimal amount, CurrencyCode currencyCode, ICurrencyLookup currencyLookup) { 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; }
private Money(decimal value, string currencyCode, ICurrencyLookup currencyLookup) { if (string.IsNullOrWhiteSpace(currencyCode)) { throw new ArgumentException($"{ nameof(currencyCode) } is required"); } var currency = currencyLookup.FindCurrency(currencyCode); if (!currency.IsEnabled) { throw new CurrencyException($"{ nameof(currencyCode) } is not enabled"); } if (decimal.Round(value, currency.DecimalDigits) != value) { throw new ArgumentOutOfRangeException($"Amount in { currencyCode } must have { currency.DecimalDigits } as maximum"); } Value = value; Currency = currency; }