From: <fab...@us...> - 2011-04-02 20:32:37
|
Revision: 5580 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5580&view=rev Author: fabiomaulo Date: 2011-04-02 20:32:31 +0000 (Sat, 02 Apr 2011) Log Message: ----------- JoinedSubclass Sequence registration fix 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/JoinedSubclassSequenceRegistrationTests.cs Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs 2011-04-02 19:38:43 UTC (rev 5579) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/ExplicitlyDeclaredModel.cs 2011-04-02 20:32:31 UTC (rev 5580) @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -28,6 +29,7 @@ private readonly HashSet<System.Type> tablePerClassHierarchyJoinEntities = new HashSet<System.Type>(); private readonly HashSet<System.Type> tablePerConcreteClassEntities = new HashSet<System.Type>(); private readonly HashSet<MemberInfo> versionProperties = new HashSet<MemberInfo>(); + private Dictionary<System.Type, Action<System.Type>> delayedTypeRegistration = new Dictionary<System.Type, Action<System.Type>>(); #region IModelExplicitDeclarationsHolder Members @@ -157,14 +159,19 @@ public void AddAsTablePerClassEntity(System.Type type) { + AddAsTablePerClassEntity(type, false); + } + + public void AddAsTablePerClassEntity(System.Type type, bool rootEntityMustExists) + { if (IsComponent(type)) { throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered as entity and as component", type.FullName)); } var rootEntity = GetRootEntityOrNull(type); - if(rootEntity != null) + if (rootEntity != null) { - if(rootEntity.Equals(type)) + if (rootEntity.Equals(type)) { throw new MappingException(string.Format("Abiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-class strategy", type.FullName)); } @@ -174,6 +181,14 @@ } tablePerClassEntities.Add(rootEntity); } + else + { + if(rootEntityMustExists) + { + throw new MappingException(string.Format("The root entity for {0} was never registered", type.FullName)); + } + EnlistTypeRegistration(type, t => AddAsTablePerClassEntity(t, true)); + } } public void AddAsTablePerClassHierarchyEntity(System.Type type) @@ -355,6 +370,7 @@ public bool IsTablePerClass(System.Type type) { + ExecuteDelayedTypeRegistration(type); return IsMappedFor(tablePerClassEntities, type); } @@ -483,5 +499,19 @@ } return isExplicitMapped || isDerived; } + + private void EnlistTypeRegistration(System.Type type, Action<System.Type> registration) + { + delayedTypeRegistration.Add(type, registration); + } + + private void ExecuteDelayedTypeRegistration(System.Type type) + { + Action<System.Type> registration; + if(delayedTypeRegistration.TryGetValue(type, out registration)) + { + registration(type); + } + } } } \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/JoinedSubclassSequenceRegistrationTests.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/JoinedSubclassSequenceRegistrationTests.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExplicitlyDeclaredModelTests/JoinedSubclassSequenceRegistrationTests.cs 2011-04-02 20:32:31 UTC (rev 5580) @@ -0,0 +1,37 @@ +using NHibernate.Mapping.ByCode; +using NUnit.Framework; +using SharpTestsEx; + +namespace NHibernate.Test.MappingByCode.ExplicitlyDeclaredModelTests +{ + public class JoinedSubclassSequenceRegistrationTests + { + private class MyClass + { + + } + private class Inherited1 : MyClass + { + + } + + [Test] + public void WhenRegisterJoinedSubclassBeforeRootThenIsRegistered() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsTablePerClassEntity(typeof(Inherited1)); + + inspector.AddAsRootEntity(typeof(MyClass)); + inspector.IsTablePerClass(typeof(Inherited1)).Should().Be.True(); + } + + [Test] + public void WhenRegisterJoinedSubclassWithNoRootThenThrows() + { + var inspector = new ExplicitlyDeclaredModel(); + inspector.AddAsTablePerClassEntity(typeof(Inherited1)); + + inspector.Executing(x => x.IsTablePerClass(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 19:38:43 UTC (rev 5579) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-04-02 20:32:31 UTC (rev 5580) @@ -510,6 +510,7 @@ <Compile Include="MappingByCode\ColumnsNamingConvetions.cs" /> <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\ComponentMappingRegistrationTests.cs" /> <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\JoinedSubclassMappingStrategyTests.cs" /> + <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\JoinedSubclassSequenceRegistrationTests.cs" /> <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\RootClassMappingStrategyTests.cs" /> <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\SubclassJoinMappingStrategyTests.cs" /> <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\SubclassMappingStrategyTests.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |