tags:

views:

2425

answers:

6

so I have a 3rd party application that I have to interface with, this application needs the userID from my users table. the problem is that I store my userIDs as GUIDs and the 3rd party app only accepts an integer. so I figure, if there is a way to convert a GUID to an integer then be able to convert it back (as i get communications from their server regarding the userID) then I don't have to recode a too much. any ideas?

+1  A: 

A Guid much bigger than an integer, and can't fit in a integer.

tuinstoel
+3  A: 

You would need a 128-bit integer type to hold the equivalent of a GUID. You can use the Guid's ToByteArray() method to get the bytes and the constructor that accepts a byte array to restore the Guid.

You can also you the GetHashCode() method to get an integer, but two different GUIDs may have the same hashcode since there are more possible Guids than 32-bit integers.

Mark Cidade
+5  A: 

An integer uses 32 bits, whereas a GUID is 128 bits - therefore, there's no way to convert a GUID into an integer without losing information, so you can't convert it back.

EDIT: you could perhaps store the GUID into a GUIDs table where you assign each a unique integer ID. That way, you could get the GUID back given the integer ID.

Mike Scott
The size of integer is system dependant.
Filip Ekberg
But then, you limit yourself to 2^32 ids.
Jason Lepack
The problem is the clearly the GUID then... =)
StingyJack
@Filip, it's not on .NET
Hans Passant
+7  A: 

You'll need a store a mapping from your Guid to the 3rd party's integer: either a new "int thirdPartyId" field in your existing user table, or a new table.

ChrisW
i was really hoping to not have to modify the dal but as it turns out my gut instinct was right.
Russ Bradberry
A: 

Add and integer Identity column, that will basically be a second ID column, and give that columnID to the application.

Pop Catalin
A: 

Create a new Table (MyUserMappings) With Two Columns

1) ThirdPartyUserID (INT, NOT NULL, IDENTITY(1,1))

2) MyUserID (GUID, NOTNULL)

Insert All GUID FROM your UserID to This table by doing

INSERT INTO MyUserMappings(MyUserID) SELECT MyGUIDUserID FROM MyUsers

You also need to put a trigger on your MyUsers Table to insert a new row in MyUsersMappings table every time your MyUsers table gets new row.

Now Create a View That shows all the fields from your MyUsers Table and ThirdParty UserID Field from your MyUsermappingsTable.

Now you will not require any DAL changes to your existing application and your original table is unaffected. So your existing application will stay as it is. And your new code can query the view instead of the table to return the Integer ID and all other user info.

Cheers.