Dynamically setting related entities

Posts   
 
    
Gibby
User
Posts: 7
Joined: 06-Apr-2006
# Posted on: 12-Apr-2006 15:53:44   

Here is an example of the scenario.

Customer entity. Orders entity.

Customer has Orders IEntityCollection.

A recursive save is needed. It is not possible use the recommended method below.

CustomerEntity customer = new Customer(); ....fill object members.... ...................................... ...................................... OrderEntity order = new OrdersEntity(); ....fill object members.... ...................................... ...................................... Customer.Orders.Add(order) ....save customer entity........

It will not be possible to do this as the primary entity and the FK relation will not be known until runtime. The question is - is it possible to get an entity collection member variable through specifying the name of the collection you require. I don't want to use reflection due to performance. I have thought of a possible solution to this - changing the generated code so each entity has a dictionary<string, entityCollection> of all the related entities. This can then return the desired entity collection. I do not really want to change the templates so is there any other way of achieving this. I dont want to go through the arrayList that is returned from method GetMemberEntityCollections and check the name of each of the entity collections either as this is long winded.

So something like this is desired

string entityName //set somewhere else string relatedEntityName //set somewhere else

IEntity2 primaryEntity = new EntityFactory.CreateInstance(entityName); ..............fill member variables......... ....................................................... IEntity2 relatedEntity = new EntityFactory.CreateInstance(entityName); ..............fill member variables......... .......................................................

primaryEntity.GetRelatedEntityCollection(relatedEntityName).Add(relatedEntity);

save primaryEntity

Thanks in advance

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 12-Apr-2006 16:26:46   

The first code snippet you posted will work just fine

And for the following line:

It will not be possible to do this as the primary entity and the FK relation will not be known until runtime

PK-FK synchronization will be done for you in the saving procedure.

Please refer to the FK-PK synchronization section under "Using the generated code -> Adapter -> Using the entity classes" in the LLBLGen Pro manual

Gibby
User
Posts: 7
Joined: 06-Apr-2006
# Posted on: 12-Apr-2006 18:59:04   

I cannot use that code that was a hyperhetical situation of the desired functionality that needs to be recreated.

I have a generic control that contains custom textbox controls as child controls.

The custom textboxes inherit from the textbox class but have additional properties (which I am later going to make into interface properties as I need to make other controls which inherit these properties).

for example the below

PeristedTextBox : TextBox { property PersistedObject property Field

}

the main control is meant to persist an entity, this is the primary entity. The designer of the control (this is an input control in question) can then put as many of these textboxes in a control. For exmaple I have an input form for a new track, other details relating to other entities can be filled in here. So the designer puts these textboxes in the control.

At runtime all the require entities are obtained, filled and validated (this is already done). But then the related entities have to be added to the primary entities collection.

Creating a new UserEntity and then doing User.Orders.Add for example isnt viable as it isnt generic in the slightest and requires a load of similar code to be written for pretty much the same scenario. Get the values from the screen and put them in an object, call save on that object.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 13-Apr-2006 02:57:39   

This person was dealing with a similar scenario and ended up creating their own templates that they posted on the forum, they may help if you end up having to go in this direction. http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=2019&HighLight=1