Search code examples
fluent-nhibernatenaming-conventionsconventions

Fluent NHibernate different Conventions for different base types


At this moment we are keeping all Entities and Mappings into same assembly. Our entities derived from a basic class Entity which is an EntityWithTypedId

Also we are having a table name Convention telling to pluralize the table names.

Now I want to create other two base types e.q. AggregateRootEntity and AggregateEntity, both derive from Entity. And I would like to create two set of conventions for both base entities:

Let's say: For for all entities derived from AggregateRootEntity tables should be prefixed with "ag_" and Id is incremental generated, but for all entities derived from AggregateEntity tables should be prefixed with "a_" and Ids should be assigned.

Is it possible to Set Conventions based on some conditions?


Solution

  • You can do it with multiple conventions, each checking for a specific type in their Accept methods

    something like:

    public class LegacyEntityTableConvention : IClassConvention, IClassConventionAcceptance
    {
      public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
      {
        criteria.Expect(x => x.EntityType.IsAny(typeof(OldClass), typeof(AnotherOldClass)));
      }
    
      public void Apply(IClassInstance instance)
      {
        instance.Table("tbl_" + instance.EntityType.Name);
      }
    }
    

    Just a block of code out of the FNH Wiki http://wiki.fluentnhibernate.org/Acceptance_criteria