Page 1 of 1

getting an object from its instance id

Posted: Tue Jul 09, 2013 9:37 am
by ghosttie
If you know the class of an object and you have its instance id, is there a better way to get the object than this?

Code: Select all

user := (User.number.String & "." & id.String).asObject.User;
I expected a method like Class::getInstance(id : Decimal) : InstanceType; to exist but I can't see one.

Re: getting an object from its instance id

Posted: Tue Jul 09, 2013 4:26 pm
by darrell
I'd just use String::asOid, e.g.

"2216.35".asOid().inspect();

Re: getting an object from its instance id

Posted: Tue Jul 09, 2013 4:27 pm
by murray
Hi ghosttie,
That's pretty much the same as what I have ended up doing in the past.
(Note: one shouldn't normally rely on object IDs - only in special cases).
I seem to recall looking into it and asking some folks at the time.
My code looks like yours, so I did not find a "better" way.

Re: getting an object from its instance id

Posted: Tue Jul 09, 2013 5:05 pm
by BeeJay
I'd just use String::asOid, e.g.

"2216.35".asOid().inspect();
Be careful of code like this if your system ever gets to the point where it needs to use the extended class number range for some of your classes. Instances of the extended class number range classes are not correctly handled by the "oid" versions of these methods and you should instead use the "object" versions of these methods. ie:

Use the "object" versions:
Object::getObjectStringForObject(someObject)
String::asObject()

Instead of the "oid" versions:
Object::getOidString()
Object::getOidStringForObject(someObject)
String::asOid()

Cheers,
BeeJay.

PS: Not that I'm in any way promoting the use of either version of these methods to turn a "string" back into an "object reference" or vice versa as part of regularly executed production code of course.... ;)