I have some entities in use in my project, and to make things easier, I would like to have the type of the key for that entity defined via a generic. E.g.:
public abstract class Entity<T>
{
public virtual T Id { get; set; }
}
This way, I can pull the Id field into the base entity and not have to define it every time (There's a few other fields on Entity in the real system, so there is other utility to doing this)
Then, since I'm using Fluent NHibernate, I make a generic mapping class to go along with this generic entity:
internal abstract class EntityMapping<T,TK> : ClassMap<T> where T : Entity<TK>
{
protected EntityMapping()
{
Id(m => m.Id);
}
}
And starting here is when I wonder if there's a better way of handling this. C# doesn't seem to infer the TK type parameter from the Entity definition, and having to add to all of my mapping classes the key parameter for the entity they map seems redundant, all the more so since it also extends to some of the higher generic data access classes, e.g.:
public abstract class EntityRepository<T,TK> where T : Entity<TK> { }
Is there a tidier way of handling this, or is this what I have to do if I want to have the key type of my entities passed in?