Say for example, in this mock bank ATM app solution, I have 3 projects:
- UI Console
- Domain Model (POCO)
- Persistence (Entity Framework for CRUD)
Domain Model (POCO): I have 2 classes
public class BankAccount
{
public int Id { get; set; }
public string AccountName { get; set; }
public decimal Balance { get; set; }
public decimal CheckBalance()
{
return Balance;
}
public void Deposit(int amount)
{
// Domain logic
Balance += amount;
// Add transaction
var transaction = new Transaction()
{
Id = 1,
TransactionDateTime = DateTime.Now,
Amount = amount,
TransactionType = TransactionType.Deposit
};
// Save changes into DB via EF. Data Logic should be here? Then where?
}
public void Withdraw(int amount)
{
// Domain logic
if(amount < balance)
Balance -= amount;
var transaction = new Transaction()
{
Id = 2,
TransactionDateTime = DateTime.Now,
Amount = amount,
TransactionType = TransactionType.Withdraw
};
// Save changes into DB via EF. Data Logic should be here? Then where?
}
}
public class Transaction
{
public int Id { get; set; }
public DateTime TransactionDateTime { get; set; }
public TransactionType TransactionType { get; set; }
public int Amount { get; set; }
}
public enum TransactionType
{
Deposit, Withdraw
}
Console UI
class Program
{
static void Main(string[] args)
{
var bankAccount = new BankAccount() { Id = 1, AccountName = "John", Balance = 250M };
Console.WriteLine("1. Check balance");
Console.WriteLine("2. Deposit");
Console.WriteLine("3. Withdraw");
Console.WriteLine("Enter option: ");
string opt = Console.ReadLine();
switch (opt)
{
case "1":
Console.WriteLine($"Your balance is ${bankAccount.CheckBalance()}");
break;
case "2":
bankAccount.Deposit(50);
Console.WriteLine("Deposit successfully");
break;
case "3":
bankAccount.Withdraw(20);
Console.WriteLine("Withdraw successfully");
break;
default:
break;
}
}
}
I have few questions here.
- If a pure method suppose to just do one thing, does my Deposit method violate this principle? If yes, how it should be then?
- In POCO class, I have the domain logic as comment. But should not have my data logic (CRUD operation) there right? Then where should I put the data logic. For example, db.SaveChanges (for Entity Framework).