Confusion with Child object default value setting.

Confusion with Child object default value setting.

Old forum URL: forums.lhotka.net/forums/t/2122.aspx


Tommybouy posted on Thursday, January 11, 2007

Hello,

I am getting confused with child objects and setting their default values. I have been doing alot of reading and I see Rocky mention that children don't use the DataPortal_Create method but then in other posts and in the book it mentions that you set defaults there.

We use VB and also use the CodeSmith SplitPartial templates so the generated code for a EditableChildList never calls the DataPortal_Create. It instead calls the Object.ObjectNew method. I tried adding a call to a SetDefaults method inside of the Initialize method but this runs before the variables are created.

What am I doing wrong here??

Brian Criswell replied on Thursday, January 11, 2007

The DataPortal_XYZ methods are called by the DataPortal against an object once the object is in the DataPortal.  You call DataPortal_Create when you need to prefill an object (even a child one) from a data source.  If your child needs to get default information from the database then ChildObject.NewChildObject would need to call DataPortal.Create.  If nothing from the data source is needed to create a ChildObject, then just initialise the variables when they are created or put it in the constructor.

ajj3085 replied on Thursday, January 11, 2007

That's not correct.  There's no need in NewChildObject to call DataPortal.Create; you're already running on the remote server at that point.

Instead, NewChildObject (which may take parameters, which may determine which set of defaults to load) should look something like this:

internal static ChildObject NewChildObject() {
    ChildObject result;

    result = new ChildObject();
    result.CreateSelf();  // This would be the eviquolent of DP_C, if this was a root object

    return result;
}

Brian Criswell replied on Friday, January 12, 2007

Not if the child object is a child of a list and the new items are added after the root object has returned from the DataPortal.

ajj3085 replied on Friday, January 12, 2007

Ahh, fair enough.  I was concidering the case when the root is being created new as well.

Copyright (c) Marimer LLC