-
Notifications
You must be signed in to change notification settings - Fork 27
/
Order.Events.cs
executable file
·83 lines (71 loc) · 2.49 KB
/
Order.Events.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Collections.Immutable;
using MyCompany.ECommerce.Sales.Pricing;
using MyCompany.ECommerce.Sales.Products;
using MyCompany.ECommerce.TechnicalStuff.Metadata;
namespace MyCompany.ECommerce.Sales.Orders;
public partial class Order
{
private readonly List<Event> _newEvents = new();
public IReadOnlyList<Event> NewEvents => _newEvents.AsReadOnly();
private void AddAndApply(Event @event)
{
@event.Apply(this);
_newEvents.Add(@event);
}
[DomainEvent]
public interface Event
{
void Apply(Order order);
}
[DomainEvent]
public class CreatedFromOffer(ImmutableArray<Item> items) : Event
{
public ImmutableArray<Item> Items { get; } = items;
public void Apply(Order order)
{
foreach (var item in Items)
order._data.Add(item);
}
}
[DomainEvent]
public class ProductAmountAdded(ProductAmount productAmount) : Event
{
public ProductAmount ProductAmount { get; } = productAmount;
public void Apply(Order order) => order.AddToItem(ProductAmount);
}
[DomainEvent]
public class PricesConfirmed : Event
{
public ImmutableArray<Quote> Quotes { get; }
public PriceAgreementType AgreementType { get; }
public DateTime? AgreementExpiresOn { get; }
public PricesConfirmed(ImmutableArray<Quote> quotes, PriceAgreementType agreementType,
DateTime? agreementExpiresOn)
{
switch (agreementType)
{
case PriceAgreementType.Non:
throw new ArgumentException();
case PriceAgreementType.Temporary:
if (!agreementExpiresOn.HasValue)
throw new ArgumentException();
break;
case PriceAgreementType.Final:
if (agreementExpiresOn.HasValue)
throw new ArgumentException();
break;
default:
throw new ArgumentOutOfRangeException(nameof(agreementType), agreementType, null);
}
Quotes = quotes;
AgreementType = agreementType;
AgreementExpiresOn = agreementExpiresOn;
}
public void Apply(Order order) => order.ApplyPriceAgreements(Quotes, AgreementType, AgreementExpiresOn);
}
[DomainEvent]
public class Placed : Event
{
public void Apply(Order order) => order._data.IsPlaced = true;
}
}