|
From: Ian B. <ia...@co...> - 2003-08-20 16:56:28
|
On Tuesday, August 5, 2003, at 05:59 PM, Magnus Lyck=E5 wrote:
> order_line
> |id |order_id|spec |price_amt|price_currency|
> +---+--------+--------------+---------+--------------+
> | 1| 123|Dog collar |12.50 |EUR |
> | 2| 123|Dog food |0.34 |USD |
>
>
> In this case you could say that the price is a value from a
> design point of view, but an object from a programming point
> of view. Martin Fowler calls such objects "Value Objects" in
> the book "Patterns of Enterprise Application Architechture".
Indeed, any value object (i.e., immutable) will work fine with=20
SQLObject, using a certain idiom:
class OrderLine(SQLObject):
priceAmt =3D CurrencyCol()
priceCurrency =3D String(length=3D3)
def _get_price(self):
return Price(self.priceAmt, self.priceCurrency)
def _set_price(self, value):
self.set(priceAmt=3Dvalue.amount, priceCurrency=3Dvalue.currency)=
class Price(object):
def __init__(self, amount, currency):
self.amount =3D amount
self.currency =3D currency
# and probably more methods too
It is important that Price be immutable (or at least treated that way),=20=
as the OrderLine instance won't detect changes to a Price instance, so=20=
they won't be saved in the database.
In fact, Price instances could be mutable if the instances were also=20
given a reference to the containing OrderLine, and notified it of=20
changes.
The whole idiom could be encapsulated in something like a=20
compositeproperty (analogous to property), but that would be only=20
slightly more compact while being less clear, IMHO.
I think this idiom is sufficient, so I don't plan any other support. =20
Except for the primary key, which is somewhat unique in that SQLObject=20=
uses that column internally.
Ian
|