Wrox Programmer Forums
|
BOOK: Professional ASP.NET Design Patterns
This is the forum to discuss the Wrox book Professional ASP.NET Design Patterns by Scott Millett; ISBN: 978-0-470-29278-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET Design Patterns section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 11th, 2014, 07:33 AM
Registered User
 
Join Date: May 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 7 UnitOfWork example

Having coded up the Account example in chapter 7 I'm having difficulty understanding how the Service._unitOfWork will contain any amended entities as these seem to be maintained in the _unitOfWork associated to the AccountRepository.
I'm relatively new to software engineering and am finding the book very useful, but seem to be misunderstanding this example.
A quick explanation would be hugely welcome.

Thanks
Sean
 
Old May 15th, 2014, 05:49 PM
Registered User
 
Join Date: May 2014
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
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
The Following User Says Thank You to jkhill For This Useful Post:
JanVolante (January 22nd, 2018)
 
Old May 19th, 2014, 09:46 AM
Registered User
 
Join Date: May 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default UnitOfWork confusion

Thanks Jon

The way you have done it makes sense. The repository and service are injected with the same unitofWork.
I used dependency injection in the same way as the example and Agatha's example.
In the Agathas.Storefront OrderService the UnitOFWork is injected in the constructor

public OrderService(IOrderRepository orderRepository,
IBasketRepository basketRepository,
ICustomerRepository customerRepository,
IUnitOfWork uow)
{
_customerRepository = customerRepository;
_orderRepository = orderRepository;
_basketRepository = basketRepository;
_uow = uow;
}

The OrderService binds to the concrete implementation UnitOfWork as per the details in BootStrapper as would the repositories. As I see it these would
be different UnitOfWork instances.
Does it work for you using the DI framework.
 
Old May 19th, 2014, 10:10 AM
elbandit's Avatar
Wrox Author
 
Join Date: May 2007
Posts: 107
Thanks: 10
Thanked 17 Times in 15 Posts
Default

Hi,

Here are some more update to date repository samples for various ORM frameworks that may be of use.

https://github.com/elbandit/PPPDDDChap22

Cheers
Scott

Last edited by elbandit; May 19th, 2014 at 10:13 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
UnitOfWork and NHibernate in Agathas case study dpant BOOK: Professional ASP.NET Design Patterns 2 March 7th, 2014 10:28 AM
Chapter 4 --> Chapter 5 Continuity?? theboffin BOOK: Professional ASP.NET MVC 4 4 July 8th, 2013 03:36 AM
Chapter 6 - Code Download Missing for this Chapter dbaechtel BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 0 August 11th, 2009 11:02 AM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.