Pregunta

I am trying to learn Spring framework and see the many of the examples use domain and service objects (domain-driven design?) but I am not able to really understand how to arrive at them. For example, I am trying to model a simple banking application which has customers,accounts and transactions. Below is the draft model i have:

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. Should the CustomerService or AccountService have the method findAccountsByCustomerId(customerId)?

  2. Which domain/service objects should represent the debit/credit transaction? debit() and credit() methods in Account domain object to be defined or in the service object? I would like to persist the transactions rather than just updating the balance.

  3. Should all the business logic be in Service layer? I see most of the spring examples out there are this way.

¿Fue útil?

Solución

  1. Since the idea here is to retrieve Accounts, it should be in AccountService.

  2. Both methods looks fine in AccountService to me because you're operating on accounts. If you want to persist the transaction you could have a TransactionDao handling this for you and you'd be calling it from your AccountService each time you need it. Doing both in your AccountService methods will allow you to be transactional. You don't want to persist the transaction object if updating the balance raised an exception.

  3. Services are useful when you have business logic which does not belong in your DAO layer. The latter is supposed to query your database and give you back appropriate domain objects whereas services are mostly used to do additional treatments like handling transactions or DTO mapping.

You should take a look at the official Spring sample app PetClinic to give you an idea.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top