Practical usage of the Unit Of Work & Repository patterns

I'm building an ORM, and try to find out what are the exact responsibilities of each pattern. 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. Правильный ли следующий подход?

  1. Получить их из репозитория
  2. Прикрепить их к моей единице работы
  3. Выполнить бизнес-транзакцию и зафиксировать?

Пример:

from = acccountRepository.find(fromAccountId);
to = accountRepository.find(toAccountId);

unitOfWork.attach(from);
unitOfWork.attach(to);    

unitOfWork.begin();
from.withdraw(amount);
to.deposit(amount);
unitOfWork.commit();

Должен, как в этом примере, Единица работы и Репозиторий должны использоваться независимо, или:

  • Должна ли Единица работы использовать внутри репозиторий и иметь возможность загружать объекты?
  • ... или Репозиторий должен использовать внутри Единицу работы и автоматически прикрепить любой загруженный объект?

Все комментарии приветствуются!

17
задан Benjamin 15 December 2012 в 12:49
поделиться