I am creating a banking application. Currently, there is one primary service, the transaction service. This service allows getting transactions by id, and creating transactions. In order to create a transaction, I first want to check if the transaction is valid, by checking the balance of the account it is trying to deduct from, and seeing if they have enough balance. Right now I am doing
TransactionController
calls TransactionService
. TransactionService
creates Transaction
, then checks if this is a valid transaction. At this point, I have created an AccountsService
, that queries the AccountsRepository
, returns an Account
. I then do the comparison based on Account.balance > Transaction.amount
.
I am conscious here that the TransactionService
create method is relying on the AccountService
get method. Additionally, AccountService
is never directly called from a controller.
Is this an ok way to architect, or is there a more elegant way to do this?
CodePudding user response:
In your case, I would say it is ok because I guess that if Account.balance < Transaction.amount
you don't really want to go forward with the transaction. So you must at some point get the needed data from the AccountService
, there is no way around that.
If you just wanted o trigger some side-effect task (like sending an email or something) you could rely on an event-based approach on which TransactionService
would publish an event and a hypothetical NotificationsService
would react to it at some point in time and do its thing.
CodePudding user response:
Your logic seems fine. If you only need the Account Service from the Transaction service (or another Service), this is valid. No need to call an Account Service from a Controller just to do so if the logic makes no sense. In fact some Services that are invoked from a Controller may call many other services - such as an Email Service, a Text Message Service, and so on.