From: <fab...@us...> - 2011-04-02 18:49:32
|
Revision: 5575 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5575&view=rev Author: fabiomaulo Date: 2011-04-02 18:49:26 +0000 (Sat, 02 Apr 2011) Log Message: ----------- joined subclasses registration Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/JoinedSubclassMappingStrategyTests.cs Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs 2011-04-02 18:31:03 UTC (rev 5574) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs 2011-04-02 18:49:26 UTC (rev 5575) @@ -168,6 +168,10 @@ { throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-class-hierarchy strategy", type.FullName)); } + if(IsMappedFor(tablePerClassEntities, type)) + { + throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered with more than one class-hierarchy strategy", type.FullName)); + } tablePerClassHierarchyEntities.Add(rootEntity); } } @@ -181,6 +185,10 @@ { throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-class-hierarchy strategy", type.FullName)); } + if (IsMappedFor(tablePerClassEntities, type)) + { + throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered with more than one class-hierarchy strategy", type.FullName)); + } tablePerClassHierarchyEntities.Add(rootEntity); } tablePerClassHierarchyJoinEntities.Add(type); @@ -195,6 +203,10 @@ { throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-concrete-class strategy", type.FullName)); } + if (IsMappedFor(tablePerClassEntities, type)) + { + throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered with more than one class-hierarchy strategy", type.FullName)); + } tablePerConcreteClassEntities.Add(rootEntity); } } @@ -314,7 +326,7 @@ public bool IsTablePerClass(System.Type type) { - return tablePerClassEntities.Contains(type); + return IsMappedFor(tablePerClassEntities, type); } public bool IsTablePerClassHierarchy(System.Type type) @@ -426,5 +438,21 @@ } return entityType.GetBaseTypes().SingleOrDefault(IsRootEntity); } + + protected bool IsMappedFor(ICollection<System.Type> explicitMappedEntities, System.Type type) + { + bool isExplicitMapped = explicitMappedEntities.Contains(type); + bool isDerived = false; + + if (!isExplicitMapped) + { + isDerived = type.GetBaseTypes().Any(explicitMappedEntities.Contains); + if (isDerived) + { + explicitMappedEntities.Add(type); + } + } + return isExplicitMapped || isDerived; + } } } \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/JoinedSubclassMappingStrategyTests.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/JoinedSubclassMappingStrategyTests.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/JoinedSubclassMappingStrategyTests.cs 2011-04-02 18:49:26 UTC (rev 5575) @@ -0,0 +1,78 @@ +using NHibernate.Mapping.ByCode; +using NUnit.Framework; +using SharpTestsEx; + +namespace NHibernate.Test.MappingByCode.ExplicitlyDeclaredModelTests +{ + public class JoinedSubclassMappingStrategyTests + { + private class MyClass + { + + } + private class Inherited1 : MyClass + { + + } + private class Inherited2 : Inherited1 + { + + } + + [Test] + public void WhenRegisteredAsJoinedSubclassThenIsRegistered() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassEntity(typeof(Inherited1)); + + inspector.IsTablePerClass(typeof(Inherited1)).Should().Be.True(); + inspector.IsTablePerClassHierarchy(typeof(Inherited1)).Should().Be.False(); + inspector.IsTablePerClassHierarchyJoin(typeof(Inherited1)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(Inherited1)).Should().Be.False(); + } + + [Test] + public void WhenRegisteredAsDeppJoinedSubclassThenIsRegistered() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassEntity(typeof(Inherited2)); + + inspector.IsTablePerClass(typeof(Inherited2)).Should().Be.True(); + inspector.IsTablePerClassHierarchy(typeof(Inherited2)).Should().Be.False(); + inspector.IsTablePerClassHierarchyJoin(typeof(Inherited2)).Should().Be.False(); + inspector.IsTablePerConcreteClass(typeof(Inherited2)).Should().Be.False(); + } + + [Test] + public void WhenRegisteredAsJoinedSubclassThenCantRegisterAsSubclass() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassEntity(typeof(Inherited1)); + + inspector.Executing(x => x.AddAsTablePerClassHierarchyEntity(typeof(Inherited1))).Throws<MappingException>(); + } + + [Test] + public void WhenRegisteredAsJoinedSubclassThenCantRegisterAsSubclassJoin() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassEntity(typeof(Inherited1)); + + inspector.Executing(x => x.AddAsTablePerClassHierarchyJoinEntity(typeof(Inherited1))).Throws<MappingException>(); + } + + [Test] + public void WhenRegisteredAsJoinedSubclassThenCantRegisterAsUnionSubclass() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.AddAsTablePerClassEntity(typeof(Inherited1)); + + inspector.Executing(x => x.AddAsTablePerConcreteClassEntity(typeof(Inherited1))).Throws<MappingException>(); + } + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-04-02 18:31:03 UTC (rev 5574) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-04-02 18:49:26 UTC (rev 5575) @@ -508,6 +508,7 @@ <Compile Include="Logging\LoggerProviderTest.cs" /> <Compile Include="MappingByCode\BasicMappingOfSimpleClass.cs" /> <Compile Include="MappingByCode\ColumnsNamingConvetions.cs" /> + <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\JoinedSubclassMappingStrategyTests.cs" /> <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\RootClassMappingStrategyTests.cs" /> <Compile Include="MappingByCode\MappingOfPrivateMembersOnRootEntity.cs" /> <Compile Include="NHSpecificTest\AccessAndCorrectPropertyName\Fixture.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |