【问题标题】:Transactional Architecture事务架构
【发布时间】: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类应该负责

  1. 调用数据模型进行充值、提现、转账
  2. 将之前的操作记录为事务。

    业务逻辑层应该调用 repo 类来执行所有操作。

    我的理解正确吗?我可能错过了一些关于“事务架构”的设计模式文档。但我用谷歌搜索没有任何发现。谁能分享我这个特定主题的链接?

  • 您可能有兴趣阅读领域驱动设计如何处理这个问题

标签: c# design-patterns


【解决方案1】:

根据 Martin Fowler 的说法,存储库模式使用类似于集合的接口在域和数据映射层之间进行调解,以访问域对象。在这个例子中。如果 Account 和 AccountTransaction 是数据模型。然后你需要 2 个存储库,AccountRepository 和 AccountTransaction 来处理 AccountModel 和 AccountTransactionModel 的集合

public class AccountRepository
{
    AccountModel Get(int id);
    IEnumerable<AccountModel> GetAll();
    void Add(AccountModel account);
}

public class AccountTransactionRepository
{
    AccountTransactionModel Get(int id);
    IEnumerable<AccountTransactionModel> GetAll();
    void Add(AccountTransactionModel account);
}

更新和删除等其他收集操作是可选的。 Save() 将不在存储库模式中。它将在 UnitOfWork 类中。

我假设每次执行存款、取款、转账操作时,都会调用帐户模型并创建一个新的 AccountTransaction 对象。这就是这道题的业务逻辑,应该留在数据访问层之上的Domain层。

namespace BusinessLogic
{
    public class AccountOperationFacade
    {
        void Transfer(AccountModel from, AccountMode to, decimal amount, string description, DateTime date)
        {
            from.Withdraw(amount, description, date);
            to.Deposit(amount, description, date);
            unitOfWork.AccountTransactionRepository.Add(new AccountTransactionModel()
            {
                From = from.Name,
                To = to.Name,
                Amount = amount
                Description = description,
                Date = date
            });
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2013-04-10
    • 2019-06-18
    • 1970-01-01
    • 2021-09-04
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多