Class design issue: how to store unique inherited objects?

J

J.J. Feminella

I have an unfinished small class hierarchy that looks something like the
code snippet below. The intent is to have a simple company identification
system that ensures uniqueness by calling IsUnique() and allows for
variations between different types of ID. (This isn't at all close to what
I'm actually coding, just a good approximating example.)

[begin code snippet]
abstract class PersonID {
public string m_Name;
public int m_ID;
public static bool IsUnique(m_ID) { ... }
}

class EmployeeID : PersonID {
public int m_Keycode;
}

class SupervisorID : PersonID {
public int m_ClearanceLevel;
}

// several other PersonID-derived types ...
[end code snippet]

I have two issues:

(1) When a new PersonID or derived class is instantiated, I have to
guarantee that the ID generated by it is unique. The naive easy way is to
have a static counter increment in the constructors and to assign this value
to m_ID, but that seems unaesthetic. I'm leaning towards just using random
numbers, I think, but if anyone has a better idea I'd love to hear it.

(2) To completely guarantee uniqueness, I'd need to remember all the
previously assigned IDs and make sure the one in question hasn't been used
yet. This involves some kind of storage; a custom hashtable seems to suggest
itself. Where should this go? I'm thinking that some kind of IDManager class
might do the trick, perhaps along the lines of:

[begin code snippet]
// revised PersonID
class PersonID {
// other stuff ...
public static IDManager mgr;
}

public IDManager {
// strongly-typed custom collection
public IDHashtable IDTable;
}
[end code snippet]

(3) However, if I do something like that described in (2), there's no way
for me to tell which of the many PersonID types the object stored in the
Hashtable is without checking against each of the types in turn (e.g., "if
(obj is EmployeeID) { ... } else if (obj is SupervisorID) { ... } ...").
This involves some kind of nasty switch/case statement or if/else block and
the "is" keyword, I presume. Is there a good way to do this that I'm missing
here?

Any help is greatly appreciated. Thanks!

- JJ
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi JJ,

See my comments inlined
[begin code snippet]
abstract class PersonID {
public string m_Name;
public int m_ID;
public static bool IsUnique(m_ID) { ... }
}

class EmployeeID : PersonID {
public int m_Keycode;
}

class SupervisorID : PersonID {
public int m_ClearanceLevel;
}

// several other PersonID-derived types ...
[end code snippet]

I have two issues:

(1) When a new PersonID or derived class is instantiated, I have to
guarantee that the ID generated by it is unique. The naive easy way is to
have a static counter increment in the constructors and to assign this value
to m_ID, but that seems unaesthetic. I'm leaning towards just using random
numbers, I think, but if anyone has a better idea I'd love to hear it.

Frankly, I don't see it unaesthetic at all.
(2) To completely guarantee uniqueness, I'd need to remember all the
previously assigned IDs and make sure the one in question hasn't been used
yet. This involves some kind of storage; a custom hashtable seems to suggest
itself. Where should this go? I'm thinking that some kind of IDManager class
might do the trick, perhaps along the lines of:

[begin code snippet]
// revised PersonID
class PersonID {
// other stuff ...
public static IDManager mgr;
}

public IDManager {
// strongly-typed custom collection
public IDHashtable IDTable;
}
[end code snippet]

Isn't it the same as having static counter?
Anyways if you don't want to persist those ID's you can take a look at
System.Runtime.Serialization.ObjectIDGenerator. This is the same class used
by the framework when generating object graphs. Bare in mind that the ID's
are unique in the context of one concrete instance of this generator.

(3) However, if I do something like that described in (2), there's no way
for me to tell which of the many PersonID types the object stored in the
Hashtable is without checking against each of the types in turn (e.g., "if
(obj is EmployeeID) { ... } else if (obj is SupervisorID) { ... } ...").
This involves some kind of nasty switch/case statement or if/else block and
the "is" keyword, I presume. Is there a good way to do this that I'm missing
here?

I didn't get that. ID is porperty of the base class. Why do you need those
if-else-ifs?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top