Re: Pets & Owners
Re: Pets & Owners
- Subject: Re: Pets & Owners
- From: Arturo Pérez <email@hidden>
- Date: Mon, 17 Nov 2003 08:36:46 -0500
On Nov 16, 2003, at 9:31 AM, Drew Thoeni wrote:
On Nov 16, 2003, at 12:34 AM, Arturo Pirez wrote:
My background looks like this: lots of web development but using
scripting
languages, so my thinking is still fairly procedural.
A procedural mentality is very very bad for this sort of thing.
Here's the
trick. When you use WO to create a popup list do you spend time
trying to figure out how to convert the selected string into
something else?
Here's a contrived example. I have a bunch of pets with associated
owners.
I create a popup list to display the pets' names. When a pet's name
is selected and
I hit submit I want to display the owner's address. How would you go
about
doing that?
Arturo,
I too am new to WebObjects and suffer pretty much the same background
as Alice. I think I understand your comment, above, but it would be
helpful if you could clarify by providing the "answer". I know how I'd
go about it in a procedural language. But, not being sure about the OO
answer, I'm not sure my guess would get me any further along.
How would you suggest it be done?
Drew
Daviid LeBer's email seemed to have the most lucid explanation of how
it is typically done with WO.
I was thinking of how to contrast with various procedural models as
well though. For example,
excluding all the state management (session, etc) that may be required
you'd do something like
the following in Perl. But let's say they do (in Perl
quasi-pseudocode):
...
# populate the Pet Name selection Popup.
$stmt = dbi->statement("select pet_name, owner_id from pet");
$stmt->execute();
print "<select name="petList" multiple>";
%o = undef;
while (@t = stmt->fetchrowarray()) {
print "<option value='" . $t[0] . "'>" . $t[0];
$o{$t[0]} = $t[1];
}
print "</select>";
...
And then on submit something like
# find matching owner name
$ownerID = $o{$selectedPetName};
$stmt = dbi->statement("select * from address where owner_id =
$ownerID");
# or, often, a nasty join with Pet, Owner, and Address based on
pet_name.
#now build the page displaying owner info
....
There are all kinds of problems with the above code (one example: what
if two pets have the same name?). But I know from experience that this
is the type of code that happens procedurally. Compare this with
binding
5 "thingies" to a WOPopupList (as David said) and then doing
public NSArray pets = EOUtilities.objectsForEntityNamed(ec, "Pet");
// bound to WOPopup's list
public EOEnterpriseObject aPet; // bound to WOPopup's item
public EOEnterpriseObject selectedPet; // bound to WOPopup's selection
// the fourth binding is displayName to aPet.name
public WOComponent showAddress() { // bound to submit button
WOComponent w = pageWithName("ShowOwnerAddress");
w.takeValueForKey(selectedPet.getOwner(), "owner");
return w;
}
These two examples are 100% equivalent. Unfortunately, with Perl, you
have to
construct the page by hand while WO gives you WOBuilder and that makes
the examples
difficult to compare line by line.
But you can do the above even if the data is not backed by an RDBMS.
All you
need to do is make sure that your object (Pet in this case) implements
the NSKeyValueCoding
interface.
I've known a fair bunch of people who bind a String to WOPopup's
selectedValue binding
and an array to the list binding and then use it like the Perl example.
Blech!
A lot of the procedurality (if I may coin a term) comes from thinking
that everything is
a java.lang.String. But that's wrong. In Java everything is a
java.lang.Object (mostly).
That should be taken advantage of.
So, the way I use WO, is:
1) On input convert (using java.text.Format.parseObject) everything
into my Object Model. If
it doesn't parse then something is wrong with the input.
2) Work with my Objects (eg. pet.owner()) as opposed to juggling
associate arrays (hashes) etc.
3) On output format (using java.text.Format.format).
Works for me.
-------
WebObjects in Philadelphia. You want a cheesesteak with that?
Visit http://webobjects.meetup.com
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.