Calculating Shipping refactor

12 views
Skip to first unread message

John Whish

unread,
Mar 17, 2011, 1:23:17 PM3/17/11
to Object-Oriented Programming in ColdFusion
Hi,

I've built this several different ways but I've never really been
happy with it so would welcome your thoughts.

I have an ecommerce store with a Basket object which has 1..1
BillingAddress and 1..1 ShippingAddress relationships. The
ShippingAddress has a Country object.

Depending on which Country is set for the ShippingAddress, the
delivery cost changes. In the Basket I have a getShippingCost method
which looks something like:

numeric function getShippingCostGross()
{
return variables.ShippingAddress().getCountry().getShippingCost();
}

This is OK, the cost belongs to the country. However, the shipping
costs is also dependant on the total value of the items in the basket.
So now I have:

numeric function getShippingCostGross()
{
return
variables.ShippingAddress().getCountry().getShippingCost( getItemCostGross() );
}

and in the Country I do something like:

numeric function getShippingCostGross( basketvalue )
{

var DeliveryOption = ORMExecuteQuery(
"from DeliveryOptions as do
where do.Country = :Country
and do.triggermin <= :trigger and triggermax > :trigger ",
{ trigger=arguments.trigger, Country=this },
true
);

return DeliveryOption.getShippingCost();
}

This just seems nasty to me. I've tried abstracting the shipping
calculations out into a ShippingCalculator object and injecting that
into the Basket as well, but I just think this should be so much
cleaner.

Any thoughts?

Thanks.

- john

Sean Corfield

unread,
Mar 17, 2011, 2:29:19 PM3/17/11
to coldfu...@googlegroups.com
Well, there's a certain amount of inherent complexity: you have a
calculation / DB lookup that depends on the country and the total
order value - and that has to go somewhere.

Personally, I'd have a DeliveryOptions object and have the logic in
there and inject it into the basket so I would have

numeric function getShippingCostGross()
{
return variables.DeliveryOptions.getShippingCost(
ShippingAddress().getCountry(), getItemCostGross() );
}

Of course, there's no "One True Way" so it's all about preferences and
trade offs. However, you have a DeliveryOptions table/object that
you're querying and that seems the obvious place for this
calculation...

John Whish

unread,
Mar 18, 2011, 5:38:25 AM3/18/11
to coldfu...@googlegroups.com
Thanks Sean,

Makes sense. Sadly, it gets a whole lot more complicated (recorded /
express / heavy all affect the cost as well!), so I just needed bounce
it off someone else.

Thanks again,

- John

> --
> You received this message because you are subscribed to the Google Groups "Object-Oriented Programming in ColdFusion" group.
> To post to this group, send email to coldfu...@googlegroups.com.
> To unsubscribe from this group, send email to coldfusionoo...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/coldfusionoo?hl=en.
>
>

Sean Corfield

unread,
Mar 18, 2011, 3:52:14 PM3/18/11
to coldfu...@googlegroups.com
On Fri, Mar 18, 2011 at 2:38 AM, John Whish <john....@googlemail.com> wrote:
> Makes sense. Sadly, it gets a whole lot more complicated (recorded /
> express / heavy all affect the cost as well!), so I just needed bounce
> it off someone else.

All the more reason to put it in a ShippingCalculator object then :)
--
Sean A Corfield -- (904) 302-SEAN
Railo Technologies, Inc. -- http://getrailo.com/
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

Reply all
Reply to author
Forward
0 new messages