What's the best way to map the gender (sex) property in NHibernate
My first implementation of this was to use a full grown entity class to represent gender. Then I would have a many-to-one relationship from e.g. Patient to Gender. The more I look at this, it doesn't feel right to me any more, as gender is something that's not likely change any soon. And as I'm using Guid comb for mapping the primary keys, rather lot of space is taken up for the foreign key and when looking at the raw database table, it's hard to figure out what the Guid represents. So, I'd like to know how other developers are tackling this and simi开发者_运维技巧lar mappings (e.g colour). It's surely something is something used quite often.
As they are properties which rarely change (without being too political, there are only 2 human genders), I map them as enum
.
public enum Genre
{
Unknown = 0,
Male = 1,
Female = 2
}
By default, this would save the string value to the database (i.e., "Unknown", "Male" or "Female"), so to get it to save the integer value (i.e., 0, 1 or 2), I map it as:
Map(x => x.Genre).CustomType(typeof(Genre));
Sometimes I find it better to save the string value instead of the integer, so I juggle between the two mappings accordingly.
精彩评论