21
I'm building an ORM, and try to find out what the exact responsibilities of each pattern are. Let's say I want to transfer money between two accounts, using the Unit Of Work to manage the updates in a single database transaction.
Is the following approach correct?
- Get them from the Repository
- Attach them to my Unit Of Work
- Do the business transaction & commit?
Example:
from = accountRepository.find(fromAccountId);
to = accountRepository.find(toAccountId);
unitOfWork.attach(from);
unitOfWork.attach(to);
unitOfWork.begin();
from.withdraw(amount);
to.deposit(amount);
unitOfWork.commit();
Should, as in this example, the Unit Of Work and the Repository be used independently, or:
- Should the Unit Of Work use internally a Repository and have the ability to load objects?
- ... or should the Repository use internally a Unit Of Work and automatically attach any loaded entity?