private HbmMapping GetMappingWithParentInCompo() { var mapper = new ConventionModelMapper(); mapper.Class<MyClass>(x => { x.Id(c => c.Id); x.Component(c => c.Compo); x.Bag(c => c.Compos, cm => { }); }); mapper.Component<MyCompo>(x => { x.Parent(c => c.Parent); x.Component(c => c.NestedCompo); }); mapper.Component<MyNestedCompo>(x => { x.Component(c => c.Owner); x.Property(c => c.Something); }); return mapper.CompileMappingForAllExplicitlyAddedEntities(); }
public static void WithConventions(this ConventionModelMapper mapper, Configuration configuration) { var baseEntityType = typeof(Entity); mapper.IsEntity((type, declared) => IsEntity(type)); mapper.IsComponent((type, b) => IsComponent(type)); mapper.IsRootEntity((type, declared) => baseEntityType.Equals(type.BaseType)); mapper.BeforeMapClass += (modelInspector, type, classCustomizer) => { classCustomizer.Id(c => c.Column("Id")); classCustomizer.Id(c => c.Generator(Generators.HighLow)); classCustomizer.Table(Inflector.Net.Inflector.Pluralize(type.Name.ToString())); }; mapper.IsPersistentProperty((memberinfo, currentlyPersistent) => { return(memberinfo.Name != "Owner"); }); mapper.BeforeMapManyToOne += (modelInspector, propertyPath, map) => { map.Column(propertyPath.LocalMember.GetPropertyOrFieldType().Name + "Id"); map.Cascade(Cascade.Persist); }; mapper.BeforeMapBag += (modelInspector, propertyPath, map) => { map.Key(keyMapper => keyMapper.Column(propertyPath.GetContainerEntity(modelInspector).Name + "Id")); map.Cascade(Cascade.All); }; mapper.BeforeMapProperty += (inspector, member, customizer) => { // This is pure guesswork, but seems to be the only way I can think of to alter // the column naming of a property mapped as part of a component if (typeof(IComponent).IsAssignableFrom(member.LocalMember.DeclaringType)) { if (member.LocalMember.Name == "Value") { customizer.Column(member.PreviousPath.LocalMember.Name); } else if (member.LocalMember.Name != "Owner") { customizer.Column(member.PreviousPath.LocalMember.Name + member.LocalMember.Name); } } }; mapper.Component <RestrictedVisibility <string> >(x => { x.Property(c => c.Value); x.Property(c => c.Visibility); x.Parent(c => c.Owner); }); mapper.Component <RestrictedVisibility <bool> >(x => { x.Property(c => c.Value); x.Property(c => c.Visibility); x.Parent(c => c.Owner); }); // The following probably works, but not if we stop "Owner" from being a persistent property // using mapping.IsPersistentProperty, and if we don't do that we get a Too Many Properties exception. // There's an old thread on nhusers about how there's no documentation in this area... //mapper.BeforeMapComponent += (inspector, member, customizer) => { // if (member.LocalMember.Name == "Owner") { // customizer.Parent(member.LocalMember); // } //}; AddConventionOverrides(mapper); HbmMapping mapping = mapper.CompileMappingFor( typeof(Entity).Assembly.GetExportedTypes().Where(IsEntity)); configuration.AddDeserializedMapping(mapping, "MyStoreMappings"); }