-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathProductAmount.cs
executable file
·36 lines (28 loc) · 1.2 KB
/
ProductAmount.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Diagnostics.CodeAnalysis;
using MyCompany.ECommerce.TechnicalStuff;
using P3Model.Annotations.Domain.DDD;
namespace MyCompany.ECommerce.Sales.Products;
[DddValueObject]
public record ProductAmount(ProductId ProductId, Amount Amount)
{
public ProductUnit ProductUnit => ProductUnit.Of(ProductId, Amount.Unit);
public static ProductAmount Of(ProductId productId, int value, AmountUnit unit) =>
new(productId, Amount.Of(value, unit));
public static ProductAmount Of(ProductId productId, Amount amount) => new(productId, amount);
[SuppressMessage("ReSharper", "UnusedMember.Local", Justification = "EF")]
private ProductAmount() : this(default, default!) { }
public static ProductAmount operator +(ProductAmount x, ProductAmount y)
{
CheckProductId(x, y);
return Of(x.ProductId, x.Amount + y.Amount);
}
public static ProductAmount operator -(ProductAmount x, ProductAmount y)
{
CheckProductId(x, y);
return Of(x.ProductId, x.Amount - y.Amount);
}
private static void CheckProductId(ProductAmount x, ProductAmount y)
{
if (!x.ProductId.Equals(y.ProductId)) throw new DomainError();
}
}