Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > Other ASP.NET > BOOK: Professional ASP.NET Design Patterns
Password Reminder
Register
| FAQ | Members List | 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 Search this Thread Display Modes
  #1 (permalink)  
Old November 11th, 2010, 01:29 PM
Authorized User
 
Join Date: Jan 2006
Location: , , Canada.
Posts: 13
Thanks: 6
Thanked 1 Time in 1 Post
Default Tester-Doer pattern Or TryParse?

I have finished studying the code up to the client proxy section on chapter 6. I have a few questions regarding the

CanReserveTicket

method of the TicketService class.

Why does Event.ReserveTicket need to throw an exception when it cannot complete? Why not just return null and let TicketService.ReserveTicket test for Null?

Like

Code:
 
public TicketReservation ReserveTicket(int tktQty)
{
if (!CanReserveTicket(tktQty))
    return null;
    //ThrowExceptionWithDetailsOnWhyTicketsCannotBeReserved();
 
    TicketReservation reservation = TicketReservationFactory.CreateReservation(this, tktQty);
 
    ReservedTickets.Add(reservation); 
    return reservation;
}
If throwing an exception is necessary, why do we not just assume Event.ReserveTicket will not fail since there is a surrounding try catch block on the TicketService.ReserveTicket method? Since we are already testing it in Event.ReserveTicket, why do we need to test again using the tester-doer pattern?

Like

Code:
 
try
{
    Event Event = _eventRepository.FindBy(newGuid(reserveTicketRequest.EventId));
    TicketReservation reservation;
    reservation = Event.ReserveTicket(reserveTicketRequest.TicketQuantity);
    _eventRepository.Save(Event);
    response = reservation.ConvertToReserveTicketResponse();
    response.Success = true;
}
catch (Exception ex)
{
    // Shield Exceptions
    response.Message = ErrorLog.GenerateErrorRefMessageAndLog(ex);
    response.Success = false;
}
Or maybe a TryParse pattern would be a better choice (similar to my first question about testing for null)?

Like

Code:
reservation = Event.TryReserveTicket(reserveTicketRequest.TicketQuantity);
 
if (reservation == null) {
    response = String.Format("There are {0} ticket(s) available", Event.AvailableAllocation());
    response.Success = false;
}
else {
    _eventRepository.Save(Event);
    response = reservation.ConvertToReserveTicketResponse();
    response.Success = true;
}
Reply With Quote
The Following User Says Thank You to samvan For This Useful Post:
elbandit (November 12th, 2010)
  #2 (permalink)  
Old November 12th, 2010, 04:59 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: 10
Thanked 17 Times in 15 Posts
Default

Samvan,

The exception handling is designed to handle any exceptions thrown and log them.I didn't set up and WCF exception fault contracts intead I handled all exceptions and just returned a success flag to show if the call worked or not.

Quote:
if (Event.CanReserveTicket(reserveTicketRequest.Ticke tQuantity)) // Tester
{
reservation =
Event.ReserveTicket(reserveTicketRequest.TicketQua ntity); // Doer
_eventRepository.Save(Event);
response = reservation.ConvertToReserveTicketResponse();
response.Success = true;
}
else
{
response.Success = false;
response.Message = String.Format(
“There are {0} ticket(s) available.”,
Event.AvailableAllocation());
}
The exception in the ReserveTicket method is for cases when the client by passes the check on whether he can reserve a ticket. I guess its just personal preference or whatever you feel is more clear for the client using it. Thinking about it there might be another way to communicate an issue when reserving a ticket that didn't make use of exceptions to exporess a failure. Maybe have the ReserveTicket method return a response with the number of tickets reserved?

Cheers
Scott
Reply With Quote
The Following User Says Thank You to elbandit For This Useful Post:
samvan (November 12th, 2010)
  #3 (permalink)  
Old November 12th, 2010, 07:29 AM
Authorized User
 
Join Date: Jan 2006
Location: , , Canada.
Posts: 13
Thanks: 6
Thanked 1 Time in 1 Post
Default

Thanks Scott for your reply.

I think what I am really asking is this:

Since the doer method (Event.ReserveTicket) actually has a tester built in (to throw exception in case the client bypass the tester in the service layer), we are effectively testing "twice" if the client uses the tester-doer pattern in the service layer (as in our case).

In the example in the book it may not be a problem, but if the tester needs to access something outside the machine it may not be ideal.

Maybe a flag to store whether CanReserveTicket has been called, and check the flag in the Event.ReserveTicket method?
Reply With Quote
  #4 (permalink)  
Old November 12th, 2010, 08:52 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: 10
Thanked 17 Times in 15 Posts
Default

Yes good point. In this senario there is no overhead for checking twice but in other systems there may well be. Yeah makes sense to include a flag to store whether CanReserveTicket has been called, and check the flag in the Event.ReserveTicket method as long as nothing else can affect the outcome in between.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Regular Expression Tester sgtwwilson BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 2 May 10th, 2009 11:35 AM
TryParse() Method afterzero BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 2 May 10th, 2009 10:15 AM
TryParse issues rpjamess1 Visual Basic 2005 Basics 0 December 1st, 2006 11:54 AM
DirectoryInfo.GetFiles(pattern): search pattern fo arif_1947 VS.NET 2002/2003 1 October 19th, 2004 11:59 PM
HTTP_REFERER Tester Sometimes Redirects OK Visitor markw707 Classic ASP Basics 4 August 21st, 2003 11:41 PM



All times are GMT -4. The time now is 02:55 AM.


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