Settings properties of entitites - Using object references or identifiers

Posts   
 
    
Posts: 254
Joined: 16-Nov-2006
# Posted on: 04-Apr-2007 00:39:28   

Guys,

If you can write code such as

            purchaseOrder.CreatingUserId = user.UserId;

and

            purchaseOrder.CreatingUser = user;

Which code is preferable or does it not matter?

I think if you have object references to hand I would prefer code which uses the object references to make it slightly clearer so the client doesn't have to read and understand the part of the entity which is the identifier.

However if you don't have the object reference and know the identifier then it would be more performant to simply set the identifier directly rather than have to retrieve an object reference to use just to set a property.

Thoughts?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Apr-2007 10:51:07   

Actually it doesn't matter.

But setting the object automatically takes care of synchronizing the PK-FK values, but setting the FK won't set assign an object to its corresponding property.

In other words the 2nd line automatically executes the first line, but not the other way around.

Posts: 254
Joined: 16-Nov-2006
# Posted on: 04-Apr-2007 11:22:01   

Sure however the question was also in terms of code readability and maintainence the object reference setting approach should be better.

This is clearer and if the identifier for the objects change the client code wouldn't have to change as the generated LLBLGen code with the updated identifier field would simply change.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39926
Joined: 17-Aug-2003
# Posted on: 16-Apr-2007 11:14:40   

if you don't have the object at hand, the FK route is easier, but in your first example you already have the object so I'd go for the second way then, as that makes sure 'user' is included in the graph simple_smile

Frans Bouma | Lead developer LLBLGen Pro