Creating a list of objects with unique identifiers

D

Danny Liberty

I need some opionions on an issue here...
Suppose I want to keep a collection of objects, each need to be
uniquely identified by a number. This number has no meaning as long as
it's unique, so it should be automatically generated for each new
object. One more requirement is keeping the objects in the order in
which they were inserted, not sorted by their ID.
Considering the following options of implementation, which would be the
best approach (and if I'm missing something, please do advise...):

1. Keeping the objects in a dictionary with the key being the unique
identifier. This has the disadvantage of not being able to read the ID
directly from the object, only from it's parent (you can't use
myobject.ID unless you keep the ID in the object as well, which is
duplicate data). This also requires the "user" of the dictionary to
iterate through all objects and generate a new unique ID for each
object added.

2. Create an ID field for the object, and then keep the objects in a
List. Still requires the list user to generate an ID.

3. A more database-like approach: Create an ID field for the object,
and then create a new collection that implements IList<T>. When Add()
is called on the collection, the implementation will generate a new
unique ID and set the object's ID field. This is a similar approach to
adding a new record to a table with an identity column in a DB.

Thoughts and suggestions would be greatly appreciated.


Danny
 
P

PS

Danny Liberty said:
I need some opionions on an issue here...
Suppose I want to keep a collection of objects, each need to be
uniquely identified by a number. This number has no meaning as long as
it's unique, so it should be automatically generated for each new
object. One more requirement is keeping the objects in the order in
which they were inserted, not sorted by their ID.
Considering the following options of implementation, which would be the
best approach (and if I'm missing something, please do advise...):

1. Keeping the objects in a dictionary with the key being the unique
identifier. This has the disadvantage of not being able to read the ID
directly from the object, only from it's parent (you can't use
myobject.ID unless you keep the ID in the object as well, which is
duplicate data). This also requires the "user" of the dictionary to
iterate through all objects and generate a new unique ID for each
object added.

2. Create an ID field for the object, and then keep the objects in a
List. Still requires the list user to generate an ID.

3. A more database-like approach: Create an ID field for the object,
and then create a new collection that implements IList<T>. When Add()
is called on the collection, the implementation will generate a new
unique ID and set the object's ID field. This is a similar approach to
adding a new record to a table with an identity column in a DB.

Thoughts and suggestions would be greatly appreciated.

Look at KeyedCollection<TKey, TItem>. It pretty much covers all 3 items you
mentioned. You will need to have an ID property that is assigned an unique
value when the object is contructed.

PS
 

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