Table of Contents

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

MonetaryAmount

Methods

Add(MonetaryAmount)

Adds two monetary amounts.

public Result<MonetaryAmount> Add(MonetaryAmount other)

Parameters

other MonetaryAmount

Returns

Result<MonetaryAmount>

Multiply(decimal)

Multiplies by a non-negative decimal multiplier.

public Result<MonetaryAmount> Multiply(decimal multiplier)

Parameters

multiplier decimal

Returns

Result<MonetaryAmount>

Multiply(int)

Multiplies by a non-negative integer quantity.

public Result<MonetaryAmount> Multiply(int quantity)

Parameters

quantity int

Returns

Result<MonetaryAmount>

Parse(string?, IFormatProvider?)

Parses a string into a value.

public static MonetaryAmount Parse(string? s, IFormatProvider? provider)

Parameters

s string

The string to parse.

provider IFormatProvider

An object that provides culture-specific formatting information about s.

Returns

MonetaryAmount

The result of parsing s.

Exceptions

ArgumentNullException

s is null.

FormatException

s is not in the correct format.

OverflowException

s is not representable by MonetaryAmount.

Subtract(MonetaryAmount)

Subtracts a monetary amount. Fails if result would be negative.

public Result<MonetaryAmount> Subtract(MonetaryAmount other)

Parameters

other MonetaryAmount

Returns

Result<MonetaryAmount>

Sum(IEnumerable<MonetaryAmount>)

Sums a collection of MonetaryAmount values.

public static Result<MonetaryAmount> Sum(IEnumerable<MonetaryAmount> values)

Parameters

values IEnumerable<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

string

TryCreate(decimal, string?)

Attempts to create a MonetaryAmount from the specified decimal.

public static Result<MonetaryAmount> TryCreate(decimal value, string? fieldName = null)

Parameters

value decimal

The decimal value (must be non-negative).

fieldName string

Optional 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

value decimal?
fieldName string

Returns

Result<MonetaryAmount>

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

value string

The string value to parse (must be a valid decimal).

provider IFormatProvider

The format provider for culture-sensitive parsing. Defaults to InvariantCulture when null.

fieldName string

Optional 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

value string

The string value to parse (must be a valid decimal).

fieldName string

Optional 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

s string

The string to parse.

provider IFormatProvider

An object that provides culture-specific formatting information about s.

result MonetaryAmount

When this method returns, contains the result of successfully parsing s or an undefined value on failure.

Returns

bool

true if s was successfully parsed; otherwise, false.

Operators

explicit operator MonetaryAmount(decimal)

Explicitly converts a decimal to a MonetaryAmount.

public static explicit operator MonetaryAmount(decimal value)

Parameters

value decimal

Returns

MonetaryAmount