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
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