Fluent NHibernate
Auto Mapping Configuration Alterations

Alterations provide a place to configure your AutoPersistenceModel without cluttering your Fluent Configuration. They are similar to the custom behaviors seen in Auto Mapping Overrides.

An alteration is a self contained piece of configuration logic, they are simple to use, and very powerful.

You create alterations classes by implmenting the IAutoMappingAlteration interface (see below). Youe alterations can then be applied to your AutoPersistenceModel before the mappings are generated.

public class WhereAlteration : IAutoMappingAlteration
{
  public void Alter(AutoPersistenceModel model)
  {
    model.Where(type => IsMappable(type));
  }

  private bool IsMappable(Type type)
  {
    // some logic
  }
}

The Alter(AutoPersistenceModel model) method is where you place your logic for altering the model, you can do anything in here you like. For example, the Auto Mapping Overrides uses an alteration to do it's business, and in it's Alter method it inspects an assembly looking for any overrides to run.

You need to instruct your AutoPersistenceModel to use any alterations you may have, and you do that using the WithAlterations method. Typically this would be done in the context of a Fluent Configuration setup, but I'll just illustrate with the AutoPersistenceModel on it's own.

AutoPersistenceModel.MapEntitiesFromAssemblyOf<Person>()  
  .WithAlterations(alterations =>
    alterations.AddFromAssemblyOf<WhereAlteration>());

The WithAlterations method takes a lambda action that allows you to set multiple alterations on your model; you can add single alterations with Add, and everything from an assembly like the above example.

Before your mappings are generated, the alterations are all run against the AutoPersistenceModel. There's currently no ordering of alterations, so you cannot rely on the ability to stack alterations.