View Single Post
  #2 (permalink)  
Old May 15th, 2014, 06:49 PM
jkhill jkhill is offline
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default not sure I'm answering the question but here goes...

I am probably the last person to try to answer this, but since I've been studying the UnitOfWork example myself I'm going to give it a shot. My apologies in advance if I make it worse.

I think you're referring to the _unitOfWork variable in the AccountService class on page 203. Is that correct?
Code:
    public class AccountService
    {
        private IAccountRepository _accountRepository;
        private IUnitOfWork _unitOfWork;

        public AccountService (IAccountRepository accountRepository, IUnitOfWork unitOfWork)
        {
            _accountRepository = accountRepository;
            _unitOfWork = unitOfWork;
        }
    ...
    }

I had to work through this very carefully. I created the following console project to reproduce Figure 7-1 and stepped through it very slowly.

Code:
using ASPPatterns.Chap7.UnitOfWork.Model;
using ASPPatterns.Chap7.UnitOfWork.Repository;


namespace ASPPatterns.Chap7.UnitOfWork.Console
{
    class Program
    {
        static void Main()
        {
            //variables to perform the example from Figure 7-1
            Account accountFrom = new Account();
            Account accountTo = new Account();
            decimal transfer = 250;
            accountFrom.balance = 1000;
            accountTo.balance = 1000;

            //additional variables required; I'm not sure of this part - I thought the client wouldn't want to know about the infrastructure or repository layers but I can't find any other way to make it work.
            UnitOfWork.Infrastructure.UnitOfWork uow = new UnitOfWork.Infrastructure.UnitOfWork();  //creates the AddedEntities, ChangedEntities and RemovedEntities dictionaries within the UnitOfWork class.
            UnitOfWork.Repository.AccountRepository accountRepository = new UnitOfWork.Repository.AccountRepository(uow);  //assigns uow to AccountRepository's _unitOfWork variable.

            AccountService accountService = new AccountService(accountRepository, uow);  //assigns accountRepository to AccountService's _accountRepository variable and uow to AccountService's _unitOfWork variable and 
            accountService.Transfer(accountFrom, accountTo, transfer);  
        }
    }
}
That last line requires a fairly large amount of explanation:
  • accountService.Transfer first calls this class's accountRepository.Save(accountFrom), which calls uow.RegisterAmended(accountFrom, accountRepository).
  • That then adds a dictionary entry containing accountFrom as the index and uow as the data.
  • Next, accountService.Transfer calls accountRepository.Save(accountTo), which calls uow.RegisterAmended(accountTo, accountRepository)
  • And then a dictionary entry containing accountTo as the index and uow as the data is added to changedEntities
  • Finally, accountService.Transfer calls uow.Commit, which loops through the changedEntities dictionary and calls accountRepository.PersistCreationOf(accountFrom), then .PersistCreationOf(accountNew).

I think the answer to your question comes from this line of the console app:
AccountService accountService = new AccountService(accountRepository, uow);

That line links the AccountRepository and UnitOfWork classes together.

Let me know if this makes any sense at all. I think I put the pieces together correctly, but to be honest this part of the pattern is still a bit magical to me (meaning I'm staring at it in glassy-eyed wonder) so I can't be entirely sure.

Jon
Reply With Quote