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
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 November 2nd, 2010, 01:03 PM
Registered User
 
Join Date: Nov 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question Layered example page 40, Price class

At the top of the Price.cs class on page 40 there is the line:

Code:
private IDiscountStrategy _discountStrategy = new NullDiscountStrategy();
Which defaults the Price to use the null discount strategy. My question is why don't we use this instead:

Code:
private IDiscountStrategy _discountStrategy;
...

public Price( decimal rrp, decimal sellingPrice )
{
... 
  _discountStrategy = DiscountFactory.GetDiscountStrategyFor( CustomerType.Standard );
}
In this way the Price class is not directly dependent on the NullDiscountStrategy class, although is now has dependencies on the factory method and the CustomerType enumeration.
I would like to know whether this would be considered an improvement to the code or does it not really make any difference? I seem to get bogged down in little things like this!

Many thanks

Steven Gibson
Reply With Quote
  #2 (permalink)  
Old November 2nd, 2010, 01:25 PM
elbandit's Avatar
Wrox Author
Points: 569, Level: 8
Points: 569, Level: 8 Points: 569, Level: 8 Points: 569, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2007
Location: Southsea, Portsmouth, Hampshire, United Kingdom.
Posts: 104
Thanks: 11
Thanked 17 Times in 15 Posts
Default

Hi Steven,

Thanks for buying the book. I think if you were going to use the Factory within the Price class you would probably need to inject it. So maybe for a clearer view to the client of what is going on is to modify the code to:

Code:
public Price(decimal RRP, decimal SellingPrice, IDiscountFactory discountFactory) { _rrp = RRP; _sellingPrice = SellingPrice; _discountFactory = discountFactory; }
public decimal SellingPriceTo(CustomerType customerType) { get { return discountFactory.GetDiscountStrategyFor(customerType) .ApplyExtraDiscountsTo(_sellingPrice); }
}
What do you think?
Cheers
Scott
Reply With Quote
  #3 (permalink)  
Old November 3rd, 2010, 01:33 PM
Registered User
 
Join Date: Nov 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is an interesting idea, however this would surely break the Discount and Savings properties as they are reliant on SellingPrice.
To make this work would require the CustomerType to be passed in to both these properties (making them methods), also in the class ProductMapperExtensionMethods (page 46)
when the ProductViewModel is created, the CustomerType will now be required here.

Therefore it would seem that to overcome these problems we will effectively be replacing the SetDiscountStrategyTo(IDiscountStrategy) method
with a method SetCustomerTypeForDiscount(CustomerType) - to allow the Price to remember which discount to apply.

This would allow us to remove the CustomerType parameter from the 3 properties (SellingPrice, Discount and Savings).

However this still takes us back to the problem of the 'default' case - i.e. what
should the instance variable that holds the CustomerType initially be set to?

The book text already requires clients to use the CustomerType when using the ProductService classes GetAllProductsFor method.
This then converts the CustomerType to a DiscountStrategy and sets the strategy using the Apply extension method (pages 43-44).
As all this happens within the domain (Model project) I can't see how your change would give a 'clearer view to the client'.

I have assumed that your method SellingPriceTo replaces the property SellingPrice and that the variable _discountStrategy is no longer required.
Reply With Quote
  #4 (permalink)  
Old November 3rd, 2010, 04:41 PM
elbandit's Avatar
Wrox Author
Points: 569, Level: 8
Points: 569, Level: 8 Points: 569, Level: 8 Points: 569, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2007
Location: Southsea, Portsmouth, Hampshire, United Kingdom.
Posts: 104
Thanks: 11
Thanked 17 Times in 15 Posts
Default

Ooops! Yeah good point, I wrote the snippet of code from memory and had forgottern about the other dependant properties. What would be more clear is something like...

Code:
 
public Price(decimal RRP, decimal SellingPrice, IDiscountStrategy defaultStrategy)
{
_rrp = RRP;
_sellingPrice = SellingPrice;
_discountStrategy = defaultStrategy;
}
public void SetDiscountStrategyTo(IDiscountStrategy DiscountStrategy)
{
_discountStrategy = DiscountStrategy; 
}
public decimal SellingPrice
{
get { return _discountStrategy.ApplyExtraDiscountsTo(_sellingPrice); }
}

Considering that an instance of IDiscountStrategy is required for the Price class to work.
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
Page 40 missing Ivor Horton's Beginning Visual C++ margoskafu Visual C++ 2005 0 December 12th, 2007 10:38 PM
Properties box for the body element -- Page 40-41 zcorker ASP.NET 1.0 and 1.1 Basics 3 October 25th, 2007 01:14 AM
Layered Applications Muhammad Zeeshan General .NET 5 July 25th, 2007 04:08 AM
Send me 40 page please!!! Sherbek Visual C++ 2005 0 July 18th, 2007 03:27 PM
page 40 not in pdf ver ??? AmR EiSa BOOK: Ivor Horton's Beginning Visual C++ 2005 1 June 27th, 2006 09:26 PM



All times are GMT -4. The time now is 06:28 PM.


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