0

In .NET Framework 4.5, System.Guid represents a globally unique identifier. Should I use alyaws Guid class for a unique identifier in my architecture? Which situation I should use it? Which situation I shouldn't ?

Can anyone make detailed explenation for this situation?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

2 Answers2

1

I think the name is pretty telling. If you need something to be GLOBALLY unique so all ambiguity is removed, use a Guid. If you need an item to be unique only among its peers, use something simple like a serial primary key.

The only major advantages of a serial primary key is (1) baked in support across nearly all existing storage medium, (2) simplicity in communicating verbally, and (3) storage size.

That being said, if the ID will never/rarely need to be communicated/keyed manually, and storage isn't of an upmost concern (such as in a document library where taxonomy is negligible compared to the primary object), there's not really a good reason to NOT use a Guid.

Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
1

That's pretty vague. A "unique identifier" is typically needed to label data. Database engines are already very good at doing that without your help, always be sure to leverage their capabilities first.

Generating a unique number is intuitively simple: one more than the previous one. The devil is in the detail, what was the previous one? There are plenty of cases where you don't care to resume numbering beyond the end of the process session so a simple variable will do. The value of System.Guid is that it helps you avoid having to solve the storage problem when you do care. At the cost of generating an unspeakable one.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536