public virtual void MapTypes(List<Type> allTypes, NHibernate.Cfg.Configuration cfg, Func<string, string> formatter) { var m = new HbmMapping(); m.Items = allTypes.Select(t => { var sc = new HbmSubclass(); sc.name = GetName(t); sc.extends = GetName(t.BaseType); sc.discriminatorvalue = map.GetOrCreateDefinition(t).Discriminator ?? t.Name; sc.lazy = false; sc.lazySpecified = true; var propertyMappings = GetPersistables(t) .Select(p => p.Attribute.GetPropertyMapping(p.DeclaringProperty, formatter)) .ToList(); if (propertyMappings.Count > 0) { if (sc.Items == null) sc.Items = propertyMappings.ToArray(); else sc.Items = sc.Items.Union(propertyMappings).ToArray(); } return sc; }).ToArray(); var dbg = m.AsString(); cfg.AddDeserializedMapping(m, "N2"); }
public void WhenMapExternalMemberAsComponentIdThenThrows() { var mapdoc = new HbmMapping(); var mapper = new ClassMapper(typeof(Person), mapdoc, For<Person>.Property(x => x.Id)); mapper.Executing(m => m.ComponentAsId(For<User>.Property(x => x.Id), map => map.Access(Accessor.Field))).Throws<ArgumentOutOfRangeException>(); }
public void ConfiguringIdGeneratorUsingAttributeAndElementIsEquivalent() { // <id name="Employer1"><generator class="guid" /></id> var mapIdentityGeneratorByElement = new HbmMapping() { Items = new object[] { new HbmClass() { name = "Employer1", Item = new HbmId() { name = "Id", generator1 = "guid" }, Items = new object[] {new HbmProperty() {name = "Name"}} } } }; // <id name="Employer1" generator="guid" /> var mapIdentityGeneratorByAttribute = new HbmMapping() { Items = new object[] { new HbmClass() { name = "Employer1", Item = new HbmId() { name = "Id", generator = new HbmGenerator() { @class = "guid" } }, Items = new object[] {new HbmProperty() {name = "Name"}} } } }; VerifyMapping(mapIdentityGeneratorByElement); VerifyMapping(mapIdentityGeneratorByAttribute); }
public void CanSetPersister() { var mapdoc = new HbmMapping(); var rc = new SubclassMapper(typeof(HineritedSimple), mapdoc); rc.Persister<SingleTableEntityPersister>(); mapdoc.SubClasses[0].Persister.Should().Contain("SingleTableEntityPersister"); }
public ComponentNestedElementMapper(Type componentType, HbmMapping mapDoc, HbmNestedCompositeElement component, MemberInfo declaringComponentMember) { this.componentType = componentType; this.mapDoc = mapDoc; this.component = component; accessorPropertyMapper = new AccessorPropertyMapper(declaringComponentMember.DeclaringType, declaringComponentMember.Name, x => component.access = x); }
public void AtCreationSetIdType() { var hbmMapping = new HbmMapping(); var hbmAny = new HbmAny(); new AnyMapper(null, typeof(int), hbmAny, hbmMapping); hbmAny.idtype.Should().Be("Int32"); }
public void WhenSetMoreSyncThenAddAll() { var mapdoc = new HbmMapping(); var rc = new ClassMapper(typeof(EntitySimple), mapdoc, For<EntitySimple>.Property(x => x.Id)); rc.Synchronize("T1", "T2", "T3", null); mapdoc.RootClasses[0].Synchronize.Select(x => x.table).Should().Have.SameValuesAs("T1", "T2", "T3"); }
public ComponentMapper(HbmComponent component, System.Type componentType, IAccessorPropertyMapper accessorMapper, HbmMapping mapDoc) : base(componentType, mapDoc) { _component = component; _component.@class = componentType.GetShortClassName(mapDoc); _accessorPropertyMapper = accessorMapper; }
public void AtCreationSetIdType() { var hbmMapping = new HbmMapping(); var hbmAny = new HbmAny(); new AnyMapper(null, typeof(int), hbmAny, hbmMapping); Assert.That(hbmAny.idtype, Is.EqualTo("Int32")); }
public void WhenSetSyncMixedWithNullAndEmptyThenAddOnlyValid() { var mapdoc = new HbmMapping(); var rc = new ClassMapper(typeof(EntitySimple), mapdoc, For<EntitySimple>.Property(x => x.Id)); rc.Synchronize("", " ATable ", " ", null); mapdoc.RootClasses[0].Synchronize.Single().table.Should().Be("ATable"); }
public void AssignsNHibernateMappingSchema() { var mapping = new HbmMapping(); var serializer = new MappingXmlSerializer(); XmlDocument document = serializer.Serialize(mapping); Assert.That(document.Schemas.Contains("urn:nhibernate-mapping-2.2")); }
/// <summary> /// Allows to execute custom code before the given mapping document is built /// </summary> /// <param name="mapping">The mapping doc</param> public override void BeforeBuildMappings(HbmMapping mapping) { if (BeforeBuildMappingsDelegate != null) { BeforeBuildMappingsDelegate(mapping); } }
/// <summary> /// Allows to execute custom code after the entity types were compiled to the given mapping document /// </summary> /// <param name="entityTypes">The entity types</param> public override void AfterCompileMappings(IList<Type> entityTypes, HbmMapping mapping) { if (AfterCompileMappingsDelegate != null) { AfterCompileMappingsDelegate(entityTypes, mapping); } }
/// <summary> /// Allows to execute custom code after the given mapping document is built /// </summary> /// <param name="mapping">The mapping doc</param> public override void AfterBuildMappings(HbmMapping mapping) { if (AfterBuildMappingsDelegate != null) { AfterBuildMappingsDelegate(mapping); } }
public void CallSetCache() { var mapdoc = new HbmMapping(); var rc = new ClassMapper(typeof(EntitySimple), mapdoc, typeof(EntitySimple).GetProperty("Id")); rc.Cache(ch=> ch.Region("pizza")); mapdoc.RootClasses[0].cache.Should().Not.Be.Null(); }
public void CanSetPersister() { var mapdoc = new HbmMapping(); var rc = new UnionSubclassMapper(typeof(InheritedSimple), mapdoc); rc.Persister<UnionSubclassEntityPersister>(); Assert.That(mapdoc.UnionSubclasses[0].Persister, Is.StringContaining("UnionSubclassEntityPersister")); }
public void CallSetDiscriminator() { var mapdoc = new HbmMapping(); var rc = new ClassMapper(typeof(EntitySimple), mapdoc, typeof(EntitySimple).GetProperty("Id")); rc.DiscriminatorValue("X"); mapdoc.RootClasses[0].discriminator.Should().Not.Be.Null(); }
public void Add(HbmMapping document) { if (document == null) throw new ArgumentNullException("document"); documents.Add(document); }
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("Entities"))); }
public void CanSetPersister() { var mapdoc = new HbmMapping(); var rc = new ClassMapper(typeof(EntitySimple), mapdoc, For<EntitySimple>.Property(x => x.Id)); rc.Persister<SingleTableEntityPersister>(); mapdoc.RootClasses[0].Persister.Should().Contain("SingleTableEntityPersister"); }
public void WhenCreateThenSetDefaultTableName() { var mapdoc = new HbmMapping(); var hbmJoin = new HbmJoin(); new JoinMapper(typeof(MyClass), " AA ", hbmJoin, mapdoc); Assert.That(hbmJoin.table, Is.EqualTo("AA")); }
public ComponentAsIdMapper(System.Type componentType, MemberInfo declaringTypeMember, HbmCompositeId id, HbmMapping mapDoc) { this.id = id; this.mapDoc = mapDoc; id.@class = componentType.GetShortClassName(mapDoc); accessorPropertyMapper = new AccessorPropertyMapper(declaringTypeMember.DeclaringType, declaringTypeMember.Name, x => id.access = x); }
public virtual void MapTypes(List<Type> allTypes, NHibernate.Cfg.Configuration cfg, Func<string, string> formatter) { var m = new HbmMapping(); m.Items = allTypes.Select(t => { var sc = new HbmSubclass(); sc.name = GetName(t); sc.extends = GetName(t.BaseType); sc.discriminatorvalue = map.GetOrCreateDefinition(t).Discriminator ?? t.Name; sc.lazy = false; sc.lazySpecified = true; var propertyMappings = GetPersistables(t) .Select(p => p.Attribute.GetPropertyMapping(p.DeclaringProperty, formatter)) .ToList(); if (propertyMappings.Count > 0) { if (sc.Items == null) sc.Items = propertyMappings.ToArray(); else sc.Items = sc.Items.Union(propertyMappings).ToArray(); } logger.DebugFormat("Generating subclass {0} with discriminator {1} extending {2} with {3} items ({4} property mappings)", sc.name, sc.discriminatorvalue, sc.extends, sc.Items != null ? sc.Items.Length.ToString() : "(null)", propertyMappings.Count); return sc; }).ToArray(); if (Debugger.IsAttached) { var dbg = m.AsString(); } cfg.AddDeserializedMapping(m, "N2"); }
public void CanValidateXmlAgainstSchema() { var prevCulture = Thread.CurrentThread.CurrentUICulture; try { Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; // Invalid, cannot use a default meta. Schema validation should fail. var hbmMapping = new HbmMapping(); hbmMapping.meta = new HbmMeta[] {new HbmMeta()}; var serializer = new MappingXmlSerializer(); XmlDocument document = serializer.Serialize(hbmMapping); MappingXmlValidator validator = new MappingXmlValidator(); var result = validator.Validate(document); result.Success.ShouldBeFalse(); result.Messages.ShouldContain("Element 'meta': The required attribute 'attribute' is missing."); } finally { Thread.CurrentThread.CurrentUICulture = prevCulture; } }
public void WhenSetSyncMixedWithNullAndEmptyThenAddOnlyValid() { var mapdoc = new HbmMapping(); var rc = new UnionSubclassMapper(typeof(InheritedSimple), mapdoc); rc.Synchronize("", " ATable ", " ", null); mapdoc.UnionSubclasses[0].Synchronize.Single().table.Should().Be("ATable"); }
public void WhenSetMoreSyncThenAddAll() { var mapdoc = new HbmMapping(); var rc = new UnionSubclassMapper(typeof(InheritedSimple), mapdoc); rc.Synchronize("T1", "T2", "T3", null); mapdoc.UnionSubclasses[0].Synchronize.Select(x => x.table).Should().Have.SameValuesAs("T1", "T2", "T3"); }
private static string GetFileName(HbmMapping hbmMapping) { string name = "MyMapping"; HbmClass rc = hbmMapping.RootClasses.FirstOrDefault(); if (rc != null) { name = rc.Name; } HbmSubclass sc = hbmMapping.SubClasses.FirstOrDefault(); if (sc != null) { name = sc.Name; } HbmJoinedSubclass jc = hbmMapping.JoinedSubclasses.FirstOrDefault(); if (jc != null) { name = jc.Name; } HbmUnionSubclass uc = hbmMapping.UnionSubclasses.FirstOrDefault(); if (uc != null) { name = uc.Name; } return name + ".hbm.xml"; }
public void WhenCreateThenSetDefaultTableName() { var mapdoc = new HbmMapping(); var hbmJoin = new HbmJoin(); new JoinMapper(typeof(MyClass), " AA ", hbmJoin, mapdoc); hbmJoin.table.Should().Be("AA"); }
public void CanSetPersister() { var mapdoc = new HbmMapping(); var rc = new JoinedSubclassMapper(typeof(InheritedSimple), mapdoc); rc.Persister<JoinedSubclassEntityPersister>(); mapdoc.JoinedSubclasses[0].Persister.Should().Contain("JoinedSubclassEntityPersister"); }
public ComponentMapper(HbmComponent component, Type componentType, MemberInfo declaringTypeMember, HbmMapping mapDoc) : base(componentType, mapDoc) { this.component = component; component.@class = componentType.GetShortClassName(mapDoc); accessorPropertyMapper = new AccessorPropertyMapper(declaringTypeMember.DeclaringType, declaringTypeMember.Name, x => component.access = x); }
public void Add(Stream stream) { HbmMapping document = parser.Parse(stream); Add(document); }