Skip to content

Commit f84c9a2

Browse files
committed
- Checkpoint: Refactor Commands
1 parent 838ecad commit f84c9a2

38 files changed

+127
-433
lines changed
Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using SourceFlow.Aggregate;
22
using SourceFlow.ConsoleApp.Commands;
33
using SourceFlow.ConsoleApp.Events;
4-
using SourceFlow.Messaging;
54

65
namespace SourceFlow.ConsoleApp.Aggregates
76
{
@@ -11,53 +10,50 @@ public class AccountAggregate : BaseAggregate<BankAccount>,
1110
{
1211
public void CreateAccount(int accountId, string holder, decimal amount)
1312
{
14-
Send(new CreateAccount
13+
Send(new CreateAccount(new AccountPayload
1514
{
16-
Entity = new Source(accountId, typeof(BankAccount)),
17-
Payload = new AccountPayload
18-
{
19-
AccountName = holder,
20-
InitialAmount = amount
21-
}
22-
});
15+
Id = accountId,
16+
AccountName = holder,
17+
InitialAmount = amount
18+
}));
2319
}
2420

2521
public void Deposit(int accountId, decimal amount)
2622
{
27-
Send(Command.For<BankAccount>(accountId)
28-
.Create<DepositMoney, TransactPayload>(new TransactPayload
29-
{
30-
Amount = amount,
31-
Type = TransactionType.Deposit
32-
}));
23+
Send(new DepositMoney(new TransactPayload
24+
{
25+
Id = accountId,
26+
Amount = amount,
27+
Type = TransactionType.Deposit
28+
}));
3329
}
3430

3531
public void Withdraw(int accountId, decimal amount)
3632
{
37-
Send(Command.For<BankAccount>(accountId)
38-
.Create<WithdrawMoney, TransactPayload>(new TransactPayload
39-
{
40-
Amount = amount,
41-
Type = TransactionType.Withdrawal
42-
}));
33+
Send(new WithdrawMoney(new TransactPayload
34+
{
35+
Id = accountId,
36+
Amount = amount,
37+
Type = TransactionType.Withdrawal
38+
}));
4339
}
4440

4541
public void Close(int accountId, string reason)
4642
{
47-
Send(Command.For<BankAccount>(accountId)
48-
.Create<CloseAccount, ClosurePayload>(new ClosurePayload
49-
{
50-
ClosureReason = reason
51-
}));
43+
Send(new CloseAccount(new ClosurePayload
44+
{
45+
Id = accountId,
46+
ClosureReason = reason
47+
}));
5248
}
5349

5450
public Task Handle(AccountCreated @event)
5551
{
56-
return Send(Command.For<BankAccount>(@event.Payload.Id)
57-
.Create<ActivateAccount, ActivationPayload>(new ActivationPayload
58-
{
59-
ActiveOn = DateTime.UtcNow,
60-
}));
52+
return Send(new ActivateAccount(new ActivationPayload
53+
{
54+
Id = @event.Payload.Id,
55+
ActiveOn = DateTime.UtcNow,
56+
}));
6157
}
6258
}
6359
}

src/SourceFlow.ConsoleApp/Commands/AccountPayload.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@ namespace SourceFlow.ConsoleApp.Commands
55
{
66
public class ActivationPayload : IPayload
77
{
8+
public int Id { get; set; }
89
public DateTime ActiveOn { get; set; }
910
}
1011

1112
public class AccountPayload : IPayload
1213
{
14+
public int Id { get; set; }
1315
public decimal InitialAmount { get; set; }
1416
public string AccountName { get; set; }
1517
}
1618

1719
public class TransactPayload : IPayload
1820
{
21+
public int Id { get; set; }
1922
public TransactionType Type { get; set; }
2023
public decimal Amount { get; set; }
2124
public decimal CurrentBalance { get; set; }
2225
}
2326

2427
public class ClosurePayload : IPayload
2528
{
29+
public int Id { get; set; }
2630
public bool IsClosed { get; set; }
2731
public string ClosureReason { get; set; } = string.Empty;
2832
}

src/SourceFlow.ConsoleApp/Commands/ActivateAccount.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ namespace SourceFlow.ConsoleApp.Commands
44
{
55
public class ActivateAccount : BaseCommand<ActivationPayload>
66
{
7+
public ActivateAccount(ActivationPayload payload) : base(payload)
8+
{
9+
}
710
}
811
}

src/SourceFlow.ConsoleApp/Commands/CloseAccount.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ namespace SourceFlow.ConsoleApp.Commands
44
{
55
public class CloseAccount : BaseCommand<ClosurePayload>
66
{
7+
public CloseAccount(ClosurePayload payload) : base(payload)
8+
{
9+
}
710
}
811
}

src/SourceFlow.ConsoleApp/Commands/CreateAccount.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ namespace SourceFlow.ConsoleApp.Commands
44
{
55
public class CreateAccount : BaseCommand<AccountPayload>
66
{
7+
public CreateAccount(AccountPayload payload) : base(payload)
8+
{
9+
}
710
}
811
}

src/SourceFlow.ConsoleApp/Commands/DepositMoney.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ namespace SourceFlow.ConsoleApp.Commands
44
{
55
public class DepositMoney : BaseCommand<TransactPayload>
66
{
7+
public DepositMoney(TransactPayload payload) : base(payload)
8+
{
9+
}
710
}
811
}

src/SourceFlow.ConsoleApp/Commands/WithdrawMoney.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ namespace SourceFlow.ConsoleApp.Commands
44
{
55
public class WithdrawMoney : BaseCommand<TransactPayload>
66
{
7+
public WithdrawMoney(TransactPayload payload) : base(payload)
8+
{
9+
}
710
}
811
}

src/SourceFlow.ConsoleApp/Impl/InMemoryEventStore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class InMemoryEventStore : IEventStore
99

1010
public Task Append(ICommand @event)
1111
{
12-
if (!_store.ContainsKey(@event.Entity.Id))
13-
_store[@event.Entity.Id] = new List<ICommand>();
12+
if (!_store.ContainsKey(@event.Payload.Id))
13+
_store[@event.Payload.Id] = new List<ICommand>();
1414

15-
_store[@event.Entity.Id].Add(@event);
15+
_store[@event.Payload.Id].Add(@event);
1616

1717
return Task.CompletedTask;
1818
}

src/SourceFlow.ConsoleApp/Sagas/AccountSaga.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class AccountSaga : BaseSaga<BankAccount>,
1717
public async Task Handle(CreateAccount command)
1818
{
1919
logger.LogInformation("Action=Account_Created, Account={AccountId}, Holder={AccountName}, Initial_Balance={InitialBalance}",
20-
command.Entity.Id, command.Payload.AccountName, command.Payload.InitialAmount);
20+
command.Payload.Id, command.Payload.AccountName, command.Payload.InitialAmount);
2121

2222
if (string.IsNullOrEmpty(command.Payload.AccountName))
2323
throw new ArgumentException("Account create requires account holder name.", nameof(command.Payload.AccountName));
@@ -27,7 +27,7 @@ public async Task Handle(CreateAccount command)
2727

2828
var account = new BankAccount
2929
{
30-
Id = command.Entity.Id,
30+
Id = command.Payload.Id,
3131
AccountName = command.Payload.AccountName,
3232
Balance = command.Payload.InitialAmount
3333
};
@@ -39,9 +39,9 @@ public async Task Handle(CreateAccount command)
3939

4040
public async Task Handle(ActivateAccount command)
4141
{
42-
logger.LogInformation("Action=Account_Activate, ActivatedOn={ActiveOn}, Account={AccountId}", command.Payload.ActiveOn, command.Entity.Id);
42+
logger.LogInformation("Action=Account_Activate, ActivatedOn={ActiveOn}, Account={AccountId}", command.Payload.ActiveOn, command.Payload.Id);
4343

44-
var account = await repository.Get<BankAccount>(command.Entity.Id);
44+
var account = await repository.Get<BankAccount>(command.Payload.Id);
4545

4646
if (account.IsClosed)
4747
throw new InvalidOperationException("Cannot deposit to a closed account");
@@ -58,9 +58,9 @@ public async Task Handle(ActivateAccount command)
5858

5959
public async Task Handle(DepositMoney command)
6060
{
61-
logger.LogInformation("Action=Money_Deposited, Amount={Amount}, Account={AccountId}", command.Payload.Amount, command.Entity.Id);
61+
logger.LogInformation("Action=Money_Deposited, Amount={Amount}, Account={AccountId}", command.Payload.Amount, command.Payload.Id);
6262

63-
var account = await repository.Get<BankAccount>(command.Entity.Id);
63+
var account = await repository.Get<BankAccount>(command.Payload.Id);
6464

6565
if (account.IsClosed)
6666
throw new InvalidOperationException("Cannot deposit to a closed account");
@@ -78,9 +78,9 @@ public async Task Handle(DepositMoney command)
7878

7979
public async Task Handle(WithdrawMoney command)
8080
{
81-
logger.LogInformation("Action=Money_Withdrawn, Amount={Amount}, Account={AccountId}", command.Payload.Amount, command.Entity.Id);
81+
logger.LogInformation("Action=Money_Withdrawn, Amount={Amount}, Account={AccountId}", command.Payload.Amount, command.Payload.Id);
8282

83-
var account = await repository.Get<BankAccount>(command.Entity.Id);
83+
var account = await repository.Get<BankAccount>(command.Payload.Id);
8484

8585
if (account.IsClosed)
8686
throw new InvalidOperationException("Cannot deposit to a closed account");
@@ -98,12 +98,12 @@ public async Task Handle(WithdrawMoney command)
9898

9999
public async Task Handle(CloseAccount command)
100100
{
101-
logger.LogInformation("Action=Account_Closed, Account={AccountId}, Reason={Reason}", command.Entity.Id, command.Payload.ClosureReason);
101+
logger.LogInformation("Action=Account_Closed, Account={AccountId}, Reason={Reason}", command.Payload.Id, command.Payload.ClosureReason);
102102

103103
if (string.IsNullOrWhiteSpace(command.Payload.ClosureReason))
104104
throw new ArgumentException("Reason for closing cannot be empty", nameof(command.Payload.ClosureReason));
105105

106-
var account = await repository.Get<BankAccount>(command.Entity.Id);
106+
var account = await repository.Get<BankAccount>(command.Payload.Id);
107107

108108
if (account.IsClosed)
109109
throw new InvalidOperationException("Cannot close account on a closed account");

src/SourceFlow.ConsoleApp/Services/AccountService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task ReplayHistoryAsync(int accountId)
7272

7373
var account = await CreateAggregate<AccountAggregate>();
7474

75-
await account.ReplayEvents(accountId);
75+
await account.Replay(accountId);
7676
}
7777
}
7878
}

0 commit comments

Comments
 (0)