To centralize some of my logic for each FluentNHibernate class mapping (specifically for mapping auditing properties and the actual primary ID), I’ve got a base ClassMap called AuditedEntityClassMap which is something like the following:
public class AuditedEntityClassMap<TEntity, T> : ClassMap<TEntity> where TEntity : AuditedPersistentObject<T>
{
public AuditedEntityClassMap()
{
Cache.ReadWrite();
DynamicUpdate();
ApplyId();
this.ChangeAuditInfo();
}
protected virtual void ApplyId()
{
if (typeof(T) == typeof(int))
map.Id(x => x.Id).GeneratedBy.HiLo(HiLoConstants.NHibernateHiLoTable, HiLoConstants.NHibernateHiLoColumn, HiLoConstants.NHibernateHiLoMaxLo);
else if (typeof(T) == typeof(Guid))
map.Id(x => x.Id).GeneratedBy.GuidComb();
else
throw new InvalidIdTypeInClassMappingException("Invalid type set in class mapping: " + typeof(T));
}
}
My question is that the type checking of the generic in order to determine the type of NHibernate generator mapping that should be used for it smells and I’m wondering if there’s a better way of dealing with it.
- The generic type for my domain entities’ IDs is used throughout and works great everywhere else (and it’s used extensively).
- I’d prefer not to have to define the ID mapping in every entity’s ClassMap if I can avoid it. If this is the only way to do so, I’ll live with it.
Thoughts and suggestions?
Looks like there’s nothing better than what I’ve got now.