If you're interested in configuring your AutoPersistenceModel
instance separately from the Fluent Configuration, or you want to implement some custom behavior similar to how the Auto Mapping Overrides works, then you may want to consider using an IAutoMappingAlteration
.
An alteration is a self contained piece of configuration logic, that can be applied with others to an AutoPersistenceModel
prior to any mappings being generated. These are simple to use, but very powerful.
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.