Created
October 10, 2011 23:07
-
-
Save kylepace/1276835 to your computer and use it in GitHub Desktop.
NHibernate 3.2 Convention Mapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NHibernate.Mapping.ByCode; | |
namespace Lunchage.Domain.DataAccess.Mappings | |
{ | |
public class ConventionsMapper | |
{ | |
private readonly ConventionModelMapper conventionModelMapper; | |
private readonly HbmMapping mapping; | |
public ConventionsMapper() | |
{ | |
this.conventionModelMapper = new ConventionModelMapper(); | |
var baseEntityType = typeof(Entity); | |
this.conventionModelMapper.IsEntity((t, declared) => baseEntityType.IsAssignableFrom(t) && baseEntityType != t && !t.IsInterface); | |
this.conventionModelMapper.IsRootEntity((t, declared) => baseEntityType.Equals(t.BaseType)); | |
this.conventionModelMapper.Class<Entity>(map => { | |
map.Id(x => x.Id, m => m.Generator(Generators.Identity)); | |
map.Id(x => x.Id, m => m.Column("Id")); | |
map.Version(x => x.Version, m => m.Generated(VersionGeneration.Always)); | |
map.Version(x => x.Version, m => m.Column("Version")); | |
}); | |
this.conventionModelMapper.BeforeMapProperty += (insp, prop, map) => map.Column(prop.LocalMember.Name); | |
this.conventionModelMapper.BeforeMapManyToOne += (insp, prop, map) => map.Column((prop.LocalMember.GetPropertyOrFieldType().Name) + "Id"); | |
this.conventionModelMapper.BeforeMapManyToOne += (insp, prop, map) => map.Cascade(Cascade.Persist); | |
this.conventionModelMapper.BeforeMapBag += (insp, prop, map) => map.Table(prop.GetContainerEntity(insp).Name + prop.GetRootMember().Name); | |
this.conventionModelMapper.BeforeMapBag += (insp, prop, map) => map.Key(km => km.Column(prop.GetContainerEntity(insp).Name + "Id")); | |
this.conventionModelMapper.BeforeMapBag += (insp, prop, map) => map.Cascade(Cascade.All); | |
this.conventionModelMapper.BeforeMapClass += (insp, prop, map) => map.Table(prop.Name); | |
this.conventionModelMapper.AddMappings(typeof(RestaurantMappings).Assembly.GetTypes()); | |
this.mapping = this.conventionModelMapper.CompileMappingFor(baseEntityType.Assembly.GetExportedTypes().Where(t => t.Namespace != null && t.Namespace.EndsWith("Domain"))); | |
} | |
public HbmMapping Mapping | |
{ | |
get { return this.mapping; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment