银行交易的域和服务方法

时间:2014-04-22 11:58:26

标签: spring domain-driven-design

我正在尝试学习Spring框架并看到许多示例使用域和服务对象(域驱动设计?)但我无法真正理解如何实现它们。例如,我正在尝试为具有客户,帐户和交易的简单银行应用程序建模。以下是我的草案模型:

Domain objects:
     Customer
         id
         userName
         password
     Account:
         id
         customerId
         balance
     Transaction:
         id
         accountId
         amount
         transactionDate
         transactionType

Service objects:
     AccountService:
         create(Account)
         update(Account)
         debit(Account,amount,date,remarks)
         credit(Account,amount,date,remarks)
         transfer(fromAccount,toAccount,amount,remarks)
         findAccountsByCustomerId(customerId)?
     CustomerService:
         findCustomerByName()
         findAccountsByCustomerId(customerId)?
  1. CustomerService或AccountService是否应该使用方法findAccountsByCustomerId(customerId)?

  2. 哪些域/服务对象应代表借记/贷记交易?要定义的Account域对象中的debit()和credit()方法,还是在服务对象中?我想坚持交易,而不仅仅是更新余额。

  3. 所有业务逻辑都应该在服务层吗?我看到大多数春天的例子都是这样的。

1 个答案:

答案 0 :(得分:1)

  1. 由于这里的想法是检索帐户,它应该在 AccountService上。

  2. 这两种方法在AccountService中看起来都不错,因为你是     经营账户。如果你想坚持你的交易     可能有一个TransactionDao处理这个,你和你     每次需要时都会从AccountService中调用它。干     在您的AccountService方法中都允许您这样做     事务性的。如果,您不想保留事务对象     更新余额引发了例外。

  3. 当您的业务逻辑没有时,服务非常有用     属于你的DAO层。后者应该查询你的     数据库,并返回适当的域对象,而     服务主要用于进行额外的治疗,如处理     交易或DTO映射。

  4. 您应该查看官方的Spring示例应用PetClinic,以便了解您的想法。