I have an IAuditable interface that defines the domain of my class as verified.
public interface IAuditable { DateTime CreateAt { get; } IUser CreateBy { get; } DateTime? UpdateAt { get; } IUser UpdateBy { get; } }
For these classes (there are many) that implement this interface, the configuration is the same! So I decided to redefine the convention:
public class AudityConvention : IAutoMappingOverride<IAuditable> { public void Override(AutoMapping<IAuditable> mapping) { mapping.Map(p => p.CreateAt).ReadOnly().Access.Property().Not.Nullable().Not.Update().Default("getDate()").Generated.Insert(); mapping.References<Usuario>(p => p.CreateBy).Not.Nullable().Not.Update(); mapping.Map(p => p.UpdateAt).ReadOnly().Access.Property().Default("getDate()").Not.Insert().Generated.Always(); mapping.References<Usuario>(p => p.UpdateBy).Nullable().Not.Insert(); } }
And tune it
_configuration = Fluently.Configure() // All config from app.config .Mappings(m => { m.AutoMappings.Add( AutoMap.AssemblyOf<Usuario>() .UseOverridesFromAssemblyOf<AudityConvention>() .Conventions.Setup(c => c.AddFromAssemblyOf<EnumConvention>()) ); m.FluentMappings .AddFromAssemblyOf<UsuarioMap>() .Conventions.AddFromAssemblyOf<EnumConvention>() ; }) .BuildConfiguration(); SessionFactory = _configuration.BuildSessionFactory(); Session = SessionFactory.OpenSession(); var export = new SchemaExport(_configuration); export.Drop(false, true); // drop and recreate the database (Just to make sure that the settings are being applied) export.Execute(false, true, false); // Create de database
Using this app.config
<appSettings> <add key="FluentAssertions.TestFramework" value="mstest"/> </appSettings> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name="connection.connection_string_name">Data</property> <property name="show_sql">true</property> </session-factory> </hibernate-configuration>
Example domain class:
public class Entidade : IAuditable { public virtual int Id { get; protected set; } [StringLength(255)] public virtual string Nome { get; set; }
And display it:
public class EntidadeMap : ClassMap<Entidade> { public EntidadeMap() { Id(p => p.Id); Map(p => p.Nome); Table("Entidades"); } }
Results:

Question
What am I doing wrong? How to create an agreement for all classes that implement the IAuditable settings IAuditable same!
Part of the configuration below was added later. From what I read, rewrite support only with AutoMappings .
m.AutoMappings.Add( AutoMap.AssemblyOf<Usuario>() .UseOverridesFromAssemblyOf<AudityConvention>() .Conventions.Setup(c => c.AddFromAssemblyOf<EnumConvention>()) );
source share