Class MonetaryAmount
- Namespace
- Trellis.Primitives
- Assembly
- Trellis.Primitives.dll
A non-negative monetary amount without currency — for single-currency systems.
Use MonetaryAmount when your bounded context operates in a single currency
(e.g., all USD). The currency is a system-wide policy, not per-row data.
Maps to a single decimal(18,2) column in EF Core via ApplyTrellisConventions.
For multi-currency systems where each value carries its own currency code, use Money instead.
[JsonConverter(typeof(ParsableJsonConverter<MonetaryAmount>))]
public class MonetaryAmount : ScalarValueObject<MonetaryAmount, decimal>, IComparable<ValueObject>, IComparable, IEquatable<ValueObject>, IConvertible, IFormattable, IFormattableScalarValue<MonetaryAmount, decimal>, IScalarValue<MonetaryAmount, decimal>, IParsable<MonetaryAmount>
- Inheritance
-
MonetaryAmount
- Implements
- Inherited Members
- Extension Methods
Properties
Zero
A zero monetary amount.
public static MonetaryAmount Zero { get; }
Property Value
Methods
Add(MonetaryAmount)
Adds two monetary amounts.
public Result<MonetaryAmount> Add(MonetaryAmount other)
Parameters
otherMonetaryAmount
Returns
Multiply(decimal)
Multiplies by a non-negative decimal multiplier.
public Result<MonetaryAmount> Multiply(decimal multiplier)
Parameters
multiplierdecimal
Returns
Multiply(int)
Multiplies by a non-negative integer quantity.
public Result<MonetaryAmount> Multiply(int quantity)
Parameters
quantityint
Returns
Parse(string?, IFormatProvider?)
Parses a string into a value.
public static MonetaryAmount Parse(string? s, IFormatProvider? provider)
Parameters
sstringThe string to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
s.
Returns
- MonetaryAmount
The result of parsing
s.
Exceptions
- ArgumentNullException
sis null.- FormatException
sis not in the correct format.- OverflowException
sis not representable by MonetaryAmount.
Subtract(MonetaryAmount)
Subtracts a monetary amount. Fails if result would be negative.
public Result<MonetaryAmount> Subtract(MonetaryAmount other)
Parameters
otherMonetaryAmount
Returns
Sum(IEnumerable<MonetaryAmount>)
Sums a collection of MonetaryAmount values.
public static Result<MonetaryAmount> Sum(IEnumerable<MonetaryAmount> values)
Parameters
valuesIEnumerable<MonetaryAmount>The monetary amounts to sum.
Returns
- Result<MonetaryAmount>
Success with the total, or Zero if the collection is empty. Failure if addition overflows.
ToString()
Returns the amount as an invariant-culture decimal string.
public override string ToString()
Returns
TryCreate(decimal, string?)
Attempts to create a MonetaryAmount from the specified decimal.
public static Result<MonetaryAmount> TryCreate(decimal value, string? fieldName = null)
Parameters
valuedecimalThe decimal value (must be non-negative).
fieldNamestringOptional field name for validation error messages.
Returns
- Result<MonetaryAmount>
Success with the MonetaryAmount if valid; Failure with ValidationError if negative.
TryCreate(decimal?, string?)
Attempts to create a MonetaryAmount from the specified nullable decimal.
public static Result<MonetaryAmount> TryCreate(decimal? value, string? fieldName = null)
Parameters
Returns
TryCreate(string?, IFormatProvider?, string?)
Attempts to create a MonetaryAmount from a string using the specified format provider.
public static Result<MonetaryAmount> TryCreate(string? value, IFormatProvider? provider, string? fieldName = null)
Parameters
valuestringThe string value to parse (must be a valid decimal).
providerIFormatProviderThe format provider for culture-sensitive parsing. Defaults to InvariantCulture when null.
fieldNamestringOptional field name for validation error messages.
Returns
- Result<MonetaryAmount>
Success with the MonetaryAmount if valid; Failure with ValidationError otherwise.
TryCreate(string?, string?)
Attempts to create a MonetaryAmount from a string representation.
public static Result<MonetaryAmount> TryCreate(string? value, string? fieldName = null)
Parameters
valuestringThe string value to parse (must be a valid decimal).
fieldNamestringOptional field name for validation error messages.
Returns
- Result<MonetaryAmount>
Success with the MonetaryAmount if valid; Failure with ValidationError otherwise.
TryParse(string?, IFormatProvider?, out MonetaryAmount)
Tries to parse a string into a value.
public static bool TryParse(string? s, IFormatProvider? provider, out MonetaryAmount result)
Parameters
sstringThe string to parse.
providerIFormatProviderAn object that provides culture-specific formatting information about
s.resultMonetaryAmountWhen this method returns, contains the result of successfully parsing
sor an undefined value on failure.
Returns
Operators
explicit operator MonetaryAmount(decimal)
Explicitly converts a decimal to a MonetaryAmount.
public static explicit operator MonetaryAmount(decimal value)
Parameters
valuedecimal