Fluent NHibernate
Mapping Patterns Open Generics

It's not uncommon to extract properties common to all (or most) of your entities into a base-class. Sometimes you may want your base-class to be an open generic type, with which each inheritor can change the generic argument for different behavior.

An example of this is using a single generic argument to dictate the Id type.

public abstract class BaseEntity<T>
{
  public T Id { get; private set; }
}

When using this kind of base-class, it's important to know that you need to update your auto-mappings to recognise that it's an open generic type. To do this you need to use the GetGenericTypeDefinition available on the Type object, and then compare it with the open version of your generic class.

automappings.WithSetup(c => c.IsBaseType =
  type => type.IsGenericType &&
          type.GetGenericTypeDefinition() == typeof(BaseEntity<>));

We have to do this because type definitions aren't recognised as actual types. So when iterating the types from your assembly, all the automapper finds is the closed types (BaseEntity<int>, BaseEntity<long>, etc...). We have to check the definition of each generic type to see if it matches our open type.