Re: EO/Java class default Values
Re: EO/Java class default Values
- Subject: Re: EO/Java class default Values
- From: Chuck Hill <email@hidden>
- Date: Wed, 27 Sep 2006 14:56:58 -0700
On Sep 27, 2006, at 1:49 PM, Zak Burke wrote:
Tarun Reddy wrote on 9/27/06 4:18 PM:
I assume that awakeFromInsertion (based on the name) does not execute
until the actual insertion of the object in the database, which
means I
can't rely on the value until the object has been saved. I am
currently
using the method to store creation date info, but I was hoping to
get an
initial value in before being saved to the database.
(Trying to generalize the EditIdea form for both existing objects
that
I'm editing, and newly created objects. Since the status field is
effectively a selector of sorts, I would like to have it always
populated, even before being saved to the database, for business
logic
checks.)
This has been an interesting sort-of Best Practices thread to follow.
I dunno if this is good or bad, but I always use static
constructors for
my EOs and I just do initialization there. I have code in my
EOEditingContext subclass that sets date-created and date-updated
fields
in saveChanges(). This keeps the EO constructors clean but still
lets me
set default values. For example:
public static SomeEO(EOEditingContext ec)
{
SomeEO eo = new SomeEO();
eo.insertObject(eo);
eo.setDefaultString("foo");
// ...
return eo;
}
The first two lines could be replaced by createAndInsertInstance().
This
is just the pattern I fell into before I knew about that method.
Hopefully, somebody will correct me if this is a terrible pattern.
Sadly, it wouldn't be the first time.
That pattern is fine and perfectly safe _as long as_ no other
framework code does this directly:
SomeEO eo = new SomeEO();
eo.insertObject(eo);
If it does, then
eo.setDefaultString("foo");
// ...
won't get run. In some cases this might be what you want. For all
other cases, putting that init code in awakeFromInsertion is the safe
thing to do.
Chuck
--
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems. http://www.global-village.net/products/practical_webobjects
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden