What is the preferred method for generating locally unique identifiers in VB.NET?

I am looking for a method for generating unique identifiers for a local record class, and then saved in various formats (XML, SQL, etc.).

I have seen people using DateTime and GetHashCode, but it seems to duplicate identifiers depending on the size of the selection.

The GUID is a bit overkill, since I do not need anything unique (large) at the global level. I also know about using a GUID and GetHashCode to slightly reduce the size, but duplicates also appear here.

Can anyone suggest a best practice or method for generating simple unique identifiers?

+1
Aug 31 '10 at 20:48
source share
3 answers

Pointer or increment counter.

The guide can be generated "from the air", but takes up more space

For the counter, you need to save the counter somewhere, and you will need to make it thread safe if you use streams. But it is smaller in size.

+5
Aug 31 '10 at 20:51
source share

It is almost always a mistake to implement your own. Small unique numbers require a truly reliable record of the last used number. It is difficult to understand, there are malfunctions on hard drives, registries are sometimes damaged. You will need to organize it so that there is one point of failure. Similar to a dbase auto-increment column. Make sure there is a loss of the last number, there is also a complete data loss. This means that for one there should be no way for the user to copy data without also copying the record of the last number. Or you always have a reliable copy of all the data and you can restore the last number from it. Quickly.

You will also have a hard time making the number increase atomically. Just running two instances of your program is usually enough to cause chaos. This is difficult to fix, you will need an independent arbiter process in which the number will be saved.

Then there is the burden of guessing what will happen in 10 or 20 years. Will your application scale to meet the requirements of that time? This is very rare, considering that cars are getting faster and running out. Now the local unique number becomes globally unique. Very different requirements for this.

Don’t mess with it, 16 bytes is nothing. Use the manual.

+5
Aug 31 '10 at 21:07
source share

GetHashCode definitely not a good choice.

How much do you want the identifier to be?

If you have persistent storage for remembering the "last generated" identifier (for example, a database), then it will simply increase the number of times each time. If you don't have persistent storage, use DateTime to get the number of milliseconds since some fixed date will also work. This will obviously limit you only to the ability to generate one identifier per millisecond, but depending on your situation, this should be OK.

You can combine a timestamp with a process identifier or something to avoid duplication if you have multiple processes generating identifiers at the same time.

+1
Aug 31 '10 at 20:54
source share



All Articles