Код IT
← Каталог

Типы классов в DDD — Value Object (объект-значение)

Фрагмент из «Типы классов в DDD»: Value Object (объект-значение).

csharp projectencyclopedia7-06-proektirovanie-i-arhitektura-1141 embed URL статья в энциклопедии
C# main.cs
public readonly record struct Money(decimal Amount, string Currency)
{
    public Money(decimal amount, string currency)
    {
        if (amount < 0) throw new ArgumentOutOfRangeException(nameof(amount));
        if (string.IsNullOrWhiteSpace(currency)) throw new ArgumentException(nameof(currency));
        Amount = amount;
        Currency = currency;
    }

    public static Money operator +(Money left, Money right)
    {
        if (left.Currency != right.Currency)
            throw new InvalidOperationException("Складывать можно только одну валюту.");
        return new Money(left.Amount + right.Amount, left.Currency);
    }
}
public readonly record struct Money(decimal Amount, string Currency)
{
    public Money(decimal amount, string currency)
    {
        if (amount < 0) throw new ArgumentOutOfRangeException(nameof(amount));
        if (string.IsNullOrWhiteSpace(currency)) throw new ArgumentException(nameof(currency));
        Amount = amount;
        Currency = currency;
    }

    public static Money operator +(Money left, Money right)
    {
        if (left.Currency != right.Currency)
            throw new InvalidOperationException("Складывать можно только одну валюту.");
        return new Money(left.Amount + right.Amount, left.Currency);
    }
}