How to store the ambiguous "object" used as key to Membership records

The ASP.NET Membership API calls for System.Object to the be the type used as the ProviderUserKey. This way, your provider can use whatever you'd like as the key field for your user records. The built-in SQL Server provider, for example, uses a Guid.

The provider I wrote uses an int instead. That part doesn't concern me... it's what I use in other calling code. For example, I might have an Orders table that is associated with Membership users. But how do I keep it provider agnostic? Sure, the object representation in .NET can simple be System.Object, but when it's time to store something in that Orders table, how do I get object to the database?

Any suggestions?

2 Comments

  • Are your talking about serializing it, or converting it for the database? If just a serialization issue, use LosFormatter or BinaryFormatter. If you are merely talking about using the provider System.Object key field as your key... well you can etiher:



    A) Map all the provider specified keys to your own user table (in a sepeate field), then use that row's primary key as your in-system key.



    B) Store store your in-system key in the Membership profile when the user is associated with your application.



    C) Serialize the key to a string and use that as the provider-key in your user table.

  • I suppose the last option makes the most sense, but your first two options ignore the fact that I have no idea what the incoming type of the key will be. Again... Membership isn't the issue, as it's the "center of the universe" in this scheme. It will always be exposed as an object, while the Membership provider will do any type conversion. Anything else will have to take an object and figure out what to do with it.

Comments have been disabled for this content.