Determine what type of object a GUID belongs to.
-
If in my application I have an ID of an object (GUID) and this ID could belong to one of three different types of object, what is the best way to determine which object the ID belongs to? Let me explain a little more. I have 3 classes which inherit from the same super class. Any instance of these three classes can be the owner of a service. Given the service, which has an owner ID, what is the best way to determine the owner? i.e. what type of object. I have some ideas but each one seems to have it's flaws. I thought of having 3 fields in the database table for owner instead of one, one for each type of object but surely this goes against good database design. I thought of having, in the database table, an owner field and an owner type field (this seems the best way so far). Or, in my aplication, try to instantiate each class using the ID, the one that works wins! But this doesn't seem very efficient at all! Any advice is appreciated. Thanks.
-
If in my application I have an ID of an object (GUID) and this ID could belong to one of three different types of object, what is the best way to determine which object the ID belongs to? Let me explain a little more. I have 3 classes which inherit from the same super class. Any instance of these three classes can be the owner of a service. Given the service, which has an owner ID, what is the best way to determine the owner? i.e. what type of object. I have some ideas but each one seems to have it's flaws. I thought of having 3 fields in the database table for owner instead of one, one for each type of object but surely this goes against good database design. I thought of having, in the database table, an owner field and an owner type field (this seems the best way so far). Or, in my aplication, try to instantiate each class using the ID, the one that works wins! But this doesn't seem very efficient at all! Any advice is appreciated. Thanks.
The best way (given the limited explanation you have), would be a lookup table. Store the ID and the object it belongs to, or the object type, in some structure. Trying to create an object to "see if it works" is probably the worst way you can go about this.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
The best way (given the limited explanation you have), would be a lookup table. Store the ID and the object it belongs to, or the object type, in some structure. Trying to create an object to "see if it works" is probably the worst way you can go about this.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008OK great, thanks for your help.
-
If in my application I have an ID of an object (GUID) and this ID could belong to one of three different types of object, what is the best way to determine which object the ID belongs to? Let me explain a little more. I have 3 classes which inherit from the same super class. Any instance of these three classes can be the owner of a service. Given the service, which has an owner ID, what is the best way to determine the owner? i.e. what type of object. I have some ideas but each one seems to have it's flaws. I thought of having 3 fields in the database table for owner instead of one, one for each type of object but surely this goes against good database design. I thought of having, in the database table, an owner field and an owner type field (this seems the best way so far). Or, in my aplication, try to instantiate each class using the ID, the one that works wins! But this doesn't seem very efficient at all! Any advice is appreciated. Thanks.
Liqz wrote:
I thought of having, in the database table, an owner (ID?) field and an owner type field (this seems the best way so far).
You thought correctly, it enables you to lookup the type based on the OwnerID without storing duplicates :) It is dangerous to instantiate something that you do not know - since it might, for example, delete all records in a table on startup, as initialization. Even if you're sure that nothing can go wrong, then it would still be slow, as you already stated.
Liqz wrote:
what is the best way to determine the owner? i.e. what type of object.
If you can't ask the owner-object itself, then you'll have to resort to keeping track of that information elsewhere. Like tracking it in a database :thumbsup:
I are troll :)