Initialization Code Question As Well

Posts   
 
    
can1
User
Posts: 77
Joined: 16-Sep-2005
# Posted on: 07-Feb-2006 15:26:48   

Hello,

I am using derived adapter tempates. I need to put some field initialization code in place for new entities only. If the entity is being retrieved from the database, I don't want to overwrite the values.

I have reviewed the "Generated code - Adding your own code to the generated classes" section of the documentation and tried the following:

  1. the __LLBLGENPRO_USER_CODE_REGION_START InitClass area in the derived class.
  2. the __LLBLGENPRO_USER_CODE_REGION_START InitClassEmpty area in the base entity.
  3. the __LLBLGENPRO_USER_CODE_REGION_START InitClassMembers area in the base entity.

In all cases, my initialization code is being executed whether I am creating a new entity like:

MyEntity var = New MyEntity();

or whether I am loading the entity from the database using a DAA.Fetch method.

I have got around it in the short term by putting my initialization code in the public void MyEntity() constructor, but it gets overwritten when I regenerate the code of course.

Where should I put my initialization coding, and how can I detect that this is a new entity created by a call to the MyEntity() constructor or an entity being created to be loaded from the database, in which case I don't want to run the default value coding?

Thanks,

Can1

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 08-Feb-2006 02:36:58   

You should have a method called InitClass() in the subclasses that you could then place your initialization code in there and it won't be overwritten. When using this method you can check the IsNew property to determine if you should perform the initialization.

can1
User
Posts: 77
Joined: 16-Sep-2005
# Posted on: 28-Feb-2006 16:14:35   

The IsNew property is true whether I am creating a new object via:

DerRaterEntity test2 = new DerRaterEntity();

or if I load a collection of entities via

odapt.FetchEntityCollection(raters);

I am finding that the FetchEntityCollection calles the DerRaterEntity.InitClass to create an empty 'shell' object to load the data into and when it does, the this.IsNew property is true?

Is there anyway to put code in place or determine when a new entity is created via the New keyword, versus being created to load data into.

The other point I am not sure about, is that after creating the empty shell object for the FetchFromCollection, the values do not get overwritten by the database values?

I hope there is a way to do this?

Thanks,

Can1

can1
User
Posts: 77
Joined: 16-Sep-2005
# Posted on: 28-Feb-2006 16:33:00   

Never mind, figured it out. You have to use:

this.Fields.State != EntityState.Fetched

to determine if the entity is being created due to a fetch method.

Can1