Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 11th, 2014, 07:33 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old May 15th, 2014, 05:49 PM
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
  #3 (permalink)  
Old May 19th, 2014, 09:46 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #4 (permalink)  
Old May 19th, 2014, 10:10 AM
elbandit's Avatar
Wrox Author
Points: 599, Level: 8
Points: 599, Level: 8 Points: 599, Level: 8 Points: 599, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2007
Location: Southsea, Portsmouth, Hampshire, United Kingdom.
Posts: 107
Thanks: 11
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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 09: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



All times are GMT -4. The time now is 07:47 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.