Auto create child entities

Posts   
 
    
Banane avatar
Banane
User
Posts: 67
Joined: 01-Sep-2004
# Posted on: 04-Mar-2005 01:29:17   

Hi,

Is there a way to auto create child entities. In the exemple the child entity is address and the parent is contact. It is a relation 1:1 and they use the same PK:contactID. ie


ContactEntity contact = new ContactEntity();

contact.Name = "new contact";

// ***
// Do we need to create the address entity contact.Address = new AddressEntity() ???
// Or it can be auto created
contact.Address.Street = "123 jkdj";
//***

contact.Save(true);

tx

cmartinbot
User
Posts: 147
Joined: 08-Jan-2004
# Posted on: 04-Mar-2005 10:01:09   

What's so wrong with creating a new entity manuallY?

Just curious. Because 1 LOC aint nothin.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 04-Mar-2005 10:16:57   

You have to create it manually.

Frans Bouma | Lead developer LLBLGen Pro
Banane avatar
Banane
User
Posts: 67
Joined: 01-Sep-2004
# Posted on: 04-Mar-2005 15:19:47   

I'm asking that because I have a framework that works with a parent entity and every time I have to create the parent I don't know what will be the possible children that will be created.

Is there a way to know if an entity has been created ? Checking Null or something like that?

ie I have Client, Contact, Adress tables...it is 1:1-1:1 relation

A) I create the client....but not the contact and the address because I don't know yet if I will have data for it...so I don't want to create empty record or SQL !

So If the user decides to enter information for the contact/address; I want to check if the contact and address have been created and assigned to the client?

ie


ClientEntity client = new ClientEntity();
client.Name = "client1";
.
.
In an other page....

// ** IT IS NOT NULL HERE ** it doesn't go there!!!!!! 
if( client.Contact == null)
      client.Contact = new ContactEntity();

client.Contact.Name = "Jim";


*** I Found ***
client.ContactReturnsNewIfNotFound = false;
if( client.Contact == null)
      client.Contact = new ContactEntity(); // goes here now...


*** ReturnsNewIfNotFound is set to true by default !!!! is there a way to set it to false by default ???? I have to change the template I guess??

that should resolve that problem??? smile smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 06-Mar-2005 11:11:09   

Banane wrote:

I'm asking that because I have a framework that works with a parent entity and every time I have to create the parent I don't know what will be the possible children that will be created. Is there a way to know if an entity has been created ? Checking Null or something like that?

ie I have Client, Contact, Adress tables...it is 1:1-1:1 relation

A) I create the client....but not the contact and the address because I don't know yet if I will have data for it...so I don't want to create empty record or SQL !

So If the user decides to enter information for the contact/address; I want to check if the contact and address have been created and assigned to the client?

ie


ClientEntity client = new ClientEntity();
client.Name = "client1";
.
.
In an other page....

// ** IT IS NOT NULL HERE ** it doesn't go there!!!!!! 
if( client.Contact == null)
      client.Contact = new ContactEntity();

client.Contact.Name = "Jim";

You should do: if(client.Contact.IsNew) { // create new }

Lazy loading code returns a new entity if the entity required is not found.


*** I Found ***
client.ContactReturnsNewIfNotFound = false;
if( client.Contact == null)
      client.Contact = new ContactEntity(); // goes here now...


*** ReturnsNewIfNotFound is set to true by default !!!! is there a way to set it to false by default ???? I have to change the template I guess??

It is set to true by default because the initial behavior of the code is as it is today: returning a new entity if the entity is not found. Changing this will break existing code, so I can only do that if I know there isn't existing code using the library, which is not an option at the moment. I made the flag so people can set it to false if they want to.

With 1.0.2004.2, which is almost done so will go in beta next week (I thought I'd finish it before the weekend however it took longer than expected), you can add a template binding to a template file to your templateset in which you add the flag settings and which are then generated into each and every entity by default and that code will be ran during entity initialization. At the moment you're out of luck, you have to set it manually or test for IsNew.

Frans Bouma | Lead developer LLBLGen Pro
Banane avatar
Banane
User
Posts: 67
Joined: 01-Sep-2004
# Posted on: 07-Mar-2005 14:31:42   

Well isNew is not enough the second time it is still isNew....but I tk I have to check is dirty as well...so


if(Client.Contact.IsNew && Client.Contact.IsDirty)
     Client.Contact = new ContactEntity();

That should resolve the problem! At least less complicated a bit in the code sunglasses .

TX for your answer you rock man! simple_smile I like your product !

Banane avatar
Banane
User
Posts: 67
Joined: 01-Sep-2004
# Posted on: 07-Mar-2005 15:52:21   

Oups...it doest'n work....

the isDirty is always false after I have created the entity. What is wrong??

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Mar-2005 15:57:58   

Hmm. Well, a new entity isn't dirty yet, (no values set) so it won't get saved without setting any values.

what you should do is this: // determine if Contact is already set to an entity ContactEntity contact = client.Contact; if(contact.IsNew && !contact.IsDirty) { // not set yet; contact = new ContactEntity(); client.Contact = contact; }

// proceed with normal contact field setting of contact, either on contact or client.Contact.

Frans Bouma | Lead developer LLBLGen Pro
Banane avatar
Banane
User
Posts: 67
Joined: 01-Sep-2004
# Posted on: 07-Mar-2005 16:04:44   

It doest'n work...It is always creating the contact!!

IsDirty Is always false.... Note that i'm not saving yet...

But...this is working



Client.ContactReturnsNewIfNotFound = false;
if(Client.Contact == null)
    Client.Contact = new ContactEntity();
CClient.ContactReturnsNewIfNotFound = true;


Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Mar-2005 17:57:28   

Banane wrote:

It doest'n work...It is always creating the contact!!

yes, but it doesn't if you already manually have set one and you've altered a value. it doesn't matter if you set it again, as the entity object set is empty.

Frans Bouma | Lead developer LLBLGen Pro