in reply to Database design and Class::DBI
I'd use a different object model.
With the system you've described you've tightly coupled products with changes to the price type. Instead, I'd have a subclass of PriceType for each different type. These would be auto-generated from the price types table.
That way you can isolate the price type dependant information into the PriceType subclasses.
Pseudo-code:
package PriceType; use base qw(Class::Singleton); sub id { croak "this should return the price_type_id" }; my $dbh = DBI->connect(...); my $price_type = $dbh->prepare('select price_type_id, price_type_label + from price_type'); $price_type->execute; $price_type->bind_columns(my \($id, $label) ); while (my $type = $price_type->fetch) { $label =~ = s/[^a-z]//gs; my $method = join('::', __PACKAGE__, $label, "id"); my $id = $id; { no strict 'refs'; *{$method} = sub { $id } }; };
and get_price in the product class becomes:
sub get_price { my ($self, $price_type) = @_; croak "need PriceType" unless UNIVERSAL::isa($price_type, 'PriceTyp +e'); my $sth = $self->sql_find_price; $sth->execute($self->id, $price_type->id); my ($price) = $sth->fetchrow_array; return $price; }
So rather than:
my $cost = $product->cost; my $list = $product->list_price; my $sale = $product->sale_price;
you would have
my $cost = $product->price(PriceType::Cost->instance); my $list = $product->price(PriceType::ListPrice->instance); my $sale = $product->price(PriceType::SalePrice->instance);
A tad more verbose, but you can now change price types to your hearts content without touching your product class.
As an alternative, you could argue that the price is more intrinsic to the PriceType than the Product, so you might want to shift get_price into PriceType giving you:
my $cost = PriceType::Cost->instance->get_price($product); my $list = PriceType::ListPrice->instance->get_price($product); my $sale = PriceType::SalePrice->instance->get_price($product);
Hope these vague ramblings make some sort of sense :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Database design and Class::DBI
by pg (Canon) on Mar 06, 2003 at 05:35 UTC | |
by adrianh (Chancellor) on Mar 07, 2003 at 23:57 UTC |