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 November 11th, 2010, 01:29 PM
Authorized User
 
Join Date: Jan 2006
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;
}
The Following User Says Thank You to samvan For This Useful Post:
elbandit (November 12th, 2010)
 
Old November 12th, 2010, 04:59 AM
elbandit's Avatar
Wrox Author
 
Join Date: May 2007
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
The Following User Says Thank You to elbandit For This Useful Post:
samvan (November 12th, 2010)
 
Old November 12th, 2010, 07:29 AM
Authorized User
 
Join Date: Jan 2006
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?
 
Old November 12th, 2010, 08:52 AM
elbandit's Avatar
Wrox Author
 
Join Date: May 2007
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.





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





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