Last active
June 23, 2018 20:57
-
-
Save ivanpaulovich/21ca4c7b445764adcfc676c503a13348 to your computer and use it in GitHub Desktop.
DDD-TDD-Rich-Domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class Account : IEntity, IAggregateRoot | |
{ | |
public Guid Id { get; private set; } | |
public Guid CustomerId { get; private set; } | |
public IReadOnlyCollection<ITransaction> GetTransactions() | |
{ | |
IReadOnlyCollection<ITransaction> readOnly = _transactions.GetTransactions(); | |
return readOnly; | |
} | |
private TransactionCollection _transactions; | |
public Account(Guid customerId) | |
{ | |
Id = Guid.NewGuid(); | |
_transactions = new TransactionCollection(); | |
CustomerId = customerId; | |
} | |
public void Deposit(Amount amount) | |
{ | |
Credit credit = new Credit(Id, amount); | |
_transactions.Add(credit); | |
} | |
public void Withdraw(Amount amount) | |
{ | |
if (_transactions.GetCurrentBalance() < amount) | |
throw new InsuficientFundsException($"The account {Id} does not have enough funds to withdraw {amount}."); | |
Debit debit = new Debit(Id, amount); | |
_transactions.Add(debit); | |
} | |
public void Close() | |
{ | |
if (_transactions.GetCurrentBalance() > 0) | |
throw new AccountCannotBeClosedException($"The account {Id} can not be closed because it has funds."); | |
} | |
public Amount GetCurrentBalance() | |
{ | |
Amount totalAmount = _transactions.GetCurrentBalance(); | |
return totalAmount; | |
} | |
public ITransaction GetLastTransaction() | |
{ | |
ITransaction transaction = _transactions.GetLastTransaction(); | |
return transaction; | |
} | |
private Account() { } | |
public static Account Load(Guid id, Guid customerId, TransactionCollection transactions) | |
{ | |
Account account = new Account(); | |
account.Id = id; | |
account.CustomerId = customerId; | |
account._transactions = transactions; | |
return account; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment