views:

83

answers:

2

Hi,

My current project connects to two separate databases almost identical in every respect except for the primary keys. Using NHibernate is it possible to map the ID to some kind of generic type?

Primarily my concern is with mapping the ID to an Int64 or Guid.

Recompiling the project to make the switch is not a problem, I am aware that the mapping file will need to know the correct type, I just want to try and avoid having two separate classes for every table.

Thanks,

Paul

+2  A: 

In case anyone's interested I've solved (hacked) this by using the object type for all the ID's. NHibernate doesn't actually allow you to map from an Int64 or Guid to an object so I've got round this by not referencing it in the mapping file (not using the name attribute on the Id tag). Then in the DAO I simply call session.GetIdentifier(...) and set it. It's a bit of a hack but as NHibernate uses object to reference Id's anyway, one I'm happy to live with.

I'm still interested in a prettier solution if anyone has one.

Paul
A: 

Maybe write a custom type that does string parsing to figure the type? It still wouldn't be directly an Int64 and Guid, but you could make it more formal anyway. Then add a few properties that you could call like if (theobject.Id.IsInt) { /* do stuff with */ theObject.Id.GetInt(); }

(Custom types are explained pretty well in the Hibernate docs.)

Mufasa