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 :-)


In reply to Re: Database design and Class::DBI by adrianh
in thread Database design and Class::DBI by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.