-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathPartner.cs
87 lines (70 loc) · 2.98 KB
/
Partner.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
84
85
86
87
using MyWarehouse.Domain.Common;
using MyWarehouse.Domain.Products;
using MyWarehouse.Domain.Transactions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace MyWarehouse.Domain.Partners
{
/// <summary>
/// Simplified entity. In product context a partner would contain more fine grained name fields,
/// a complex address representation, phone number, invoicing/tax details, etc.
/// </summary>
public class Partner : MyEntity
{
[Required]
[StringLength(PartnerInvariants.NameMaxLength)]
public string Name { get; private set; }
[Required]
public Address Address { get; private set; }
public virtual IReadOnlyCollection<Transaction> Transactions => _transactions.AsReadOnly();
private List<Transaction> _transactions = new List<Transaction>();
private Partner() // EF
{}
public Partner(string name, Address address)
{
UpdateName(name);
UpdateAddress(address);
}
/// <summary>
/// Generate a new sales transaction with this partner.
/// </summary>
public Transaction SellTo(IEnumerable<(Product product, int quantity)> items)
=> CreateTransaction(items, TransactionType.Sales);
/// <summary>
/// Generate a new procurement transaction with this partner.
/// </summary>
public Transaction ProcureFrom(IEnumerable<(Product product, int quantity)> items)
=> CreateTransaction(items, TransactionType.Procurement);
public void UpdateName(string value)
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("Name cannot be empty.");
if (value.Length > PartnerInvariants.NameMaxLength)
throw new ArgumentException($"Length of value ({value.Length}) exceeds maximum name length ({ProductInvariants.NameMaxLength}).");
Name = value;
}
public void UpdateAddress(Address address)
{
Address = address ?? throw new ArgumentNullException(nameof(address));
}
private Transaction CreateTransaction(IEnumerable<(Product product, int quantity)> items, TransactionType transactionType)
{
if (items == null)
throw new ArgumentNullException(nameof(items));
if (!items.Any() || items.Any(x => x.product == null || x.quantity < 1))
throw new ArgumentException("List of items must be a non-empty list of non-null products and quantities of at least 1.", nameof(items));
var transaction = new Transaction(
type: transactionType,
partner: this
);
foreach (var (product, quantity) in items)
{
transaction.AddTransactionLine(product, quantity);
}
_transactions.Add(transaction);
return transaction;
}
}
}