Randomize

Richard Tallent’s occasional blog

IFormatProvider for GUIDs?

I’ve used uniqueidentifiers in databases before as primary keys, now I’m more serious about integrating them into my base O/R library, using the COMB method to avoid the INSERT performance hit and to get a freebie datestamp.

SQL Server allows constant GUIDs in both a character format (‘xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx’) and a binary format (0x00000000000000000000000000000000), but code I’ve seen exclusively uses the character format, but I would rather use the binary format. Doing this will ensure that any existing code that creates dynamic SQL assuming an int ID type will continue to work without having to escape the ID (I’m already protected from injection attacks, this is a few layers deeper).

Unfortunately, it doesn’t look like the .NET GUID.ToString() method supports this format. I could write my IFormatProvider, but before I get into byte arrays and swapping words around, does anyone know of some existing code that does this efficiently?


Share

comments powered by Disqus