【发布时间】:2022-08-07 01:14:46
【问题描述】:
我有一个面试问题来设计一个小型个人会计应用程序来记录银行账户活动。我有如下帐户数据模型:
public class AccountModel
{
public string Name { get; set; }
public string AccountNumber { get; set; }
public AccountType Type { get; set; } // checking, saving, credit, etc
public decimal Balance { get; set; }
public bool Deposit(decimal amount, string description, DateTime date, ref string error)
{
Balance += amount;
return true;
}
public bool Withdraw(decimal amount, string description, DateTime date, ref string error)
{
//do something
return true;
}
public bool Transfer(decimal amount, string description, DateTime date, AccountModel targetAccount, ref string error)
{
// do something
return true;
}
}
public class AccountTransactionModel
{
public enum TranslactionType
{
Deposit,
Withdraw,
Transfer,
}
public long TransactionId { get; set; }
public DateTime Date { get; set; }
public decimal Amount { get; set; }
public string Description { get; set; }
public TranslactionType Type { get; set; }
public AccountModel Account { get; set; }
}
使用存储库模式,我有如下的存储库接口和实现:
public interface IAccountOpInterface
{
bool CreateAccount(string name, string accountNumber, AccountModel.AccountType type, decimal initialBalance);
List<AccountModel> GetAccounts();
AccountModel GetAccount(long accountId);
bool Deposit(AccountModel account, decimal amount, string description, DateTime date, ref string error);
bool Withdraw(AccountModel account, decimal amount, string description, DateTime date, ref string error);
public bool Transfer(AccountModel fromAccount, decimal amount, string description, DateTime date, AccountModel toAccount, ref string error);
List<AccountTransactionModel> RunQuery(Query query);
bool Load();
bool Save();
void CreateTransaction(AccountTransactionModel accountTransactionModel);
}
然后面试官说我用的是“Transactional Architecture”,这不是一个好的设计。所有的操作都应该放在数据模型而不是 repo 类中。
我对 Repository 类和数据模型之间的责任有点困惑。 我认为账户模型应该负责针对特定账户类型的存款、取款和转账操作的自定义操作。
并且repo类应该负责
- 调用数据模型进行充值、提现、转账
- 将之前的操作记录为事务。
业务逻辑层应该调用 repo 类来执行所有操作。
我的理解正确吗?我可能错过了一些关于“事务架构”的设计模式文档。但我用谷歌搜索没有任何发现。谁能分享我这个特定主题的链接?
-
您可能有兴趣阅读领域驱动设计如何处理这个问题
标签: c# design-patterns