From: <fab...@us...> - 2011-04-05 18:28:49
|
Revision: 5613 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5613&view=rev Author: fabiomaulo Date: 2011-04-05 18:28:43 +0000 (Tue, 05 Apr 2011) Log Message: ----------- registration/execution of Join customizers for Subclass Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Mapping/ByCode/ISubclassMapper.cs trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersHolder.cs trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/SubclassCustomizer.cs trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/ICustomizersHolder.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExpliticMappingTests/SubclassPropertiesSplitsTests.cs Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/ISubclassMapper.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/ISubclassMapper.cs 2011-04-05 17:48:10 UTC (rev 5612) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/ISubclassMapper.cs 2011-04-05 18:28:43 UTC (rev 5613) @@ -20,5 +20,6 @@ public interface ISubclassMapper<TEntity> : ISubclassAttributesMapper<TEntity>, IPropertyContainerMapper<TEntity> where TEntity : class { + void Join(string splitGroupId, Action<IJoinMapper<TEntity>> splittedMapping); } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersHolder.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersHolder.cs 2011-04-05 17:48:10 UTC (rev 5612) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersHolder.cs 2011-04-05 18:28:43 UTC (rev 5613) @@ -59,8 +59,8 @@ private readonly Dictionary<PropertyPath, List<Action<ISetPropertiesMapper>>> setCustomizers = new Dictionary<PropertyPath, List<Action<ISetPropertiesMapper>>>(); - private readonly Dictionary<System.Type, List<Action<ISubclassAttributesMapper>>> subclassCustomizers = - new Dictionary<System.Type, List<Action<ISubclassAttributesMapper>>>(); + private readonly Dictionary<System.Type, List<Action<ISubclassMapper>>> subclassCustomizers = + new Dictionary<System.Type, List<Action<ISubclassMapper>>>(); private readonly Dictionary<System.Type, List<Action<IUnionSubclassAttributesMapper>>> unionClassCustomizers = new Dictionary<System.Type, List<Action<IUnionSubclassAttributesMapper>>>(); @@ -75,7 +75,7 @@ AddCustomizer(rootClassCustomizers, type, classCustomizer); } - public void AddCustomizer(System.Type type, Action<ISubclassAttributesMapper> classCustomizer) + public void AddCustomizer(System.Type type, Action<ISubclassMapper> classCustomizer) { AddCustomizer(subclassCustomizers, type, classCustomizer); } @@ -180,7 +180,7 @@ InvokeCustomizers(rootClassCustomizers, type, mapper); } - public void InvokeCustomizers(System.Type type, ISubclassAttributesMapper mapper) + public void InvokeCustomizers(System.Type type, ISubclassMapper mapper) { InvokeCustomizers(subclassCustomizers, type, mapper); } Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/SubclassCustomizer.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/SubclassCustomizer.cs 2011-04-05 17:48:10 UTC (rev 5612) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/SubclassCustomizer.cs 2011-04-05 18:28:43 UTC (rev 5613) @@ -1,10 +1,13 @@ using System; +using System.Collections.Generic; using NHibernate.Persister.Entity; namespace NHibernate.Mapping.ByCode.Impl.CustomizersImpl { public class SubclassCustomizer<TEntity> : PropertyContainerCustomizer<TEntity>, ISubclassMapper<TEntity> where TEntity : class { + private Dictionary<string, IJoinMapper<TEntity>> joinCustomizers; + public SubclassCustomizer(IModelExplicitDeclarationsHolder explicitDeclarationsHolder, ICustomizersHolder customizersHolder) : base(explicitDeclarationsHolder, customizersHolder, null) { @@ -19,51 +22,70 @@ public void DiscriminatorValue(object value) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.DiscriminatorValue(value)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.DiscriminatorValue(value)); } #endregion + private Dictionary<string, IJoinMapper<TEntity>> JoinCustomizers + { + get { return joinCustomizers ?? (joinCustomizers = new Dictionary<string, IJoinMapper<TEntity>>()); } + } + + public void Join(string splitGroupId, Action<IJoinMapper<TEntity>> splittedMapping) + { + // add the customizer only to ensure the creation of the JoinMapper instance for the group + CustomizersHolder.AddCustomizer(typeof(TEntity), (ISubclassMapper m) => m.Join(splitGroupId, j => { })); + + IJoinMapper<TEntity> joinCustomizer; + if (!JoinCustomizers.TryGetValue(splitGroupId, out joinCustomizer)) + { + joinCustomizer = new JoinCustomizer<TEntity>(splitGroupId, ExplicitDeclarationsHolder, CustomizersHolder); + JoinCustomizers.Add(splitGroupId, joinCustomizer); + } + splittedMapping(joinCustomizer); + } + #region Implementation of IEntityAttributesMapper public void EntityName(string value) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.EntityName(value)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.EntityName(value)); } public void Proxy(System.Type proxy) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.Proxy(proxy)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.Proxy(proxy)); } public void Lazy(bool value) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.Lazy(value)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.Lazy(value)); } public void DynamicUpdate(bool value) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.DynamicUpdate(value)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.DynamicUpdate(value)); } public void DynamicInsert(bool value) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.DynamicInsert(value)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.DynamicInsert(value)); } public void BatchSize(int value) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.BatchSize(value)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.BatchSize(value)); } public void SelectBeforeUpdate(bool value) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.SelectBeforeUpdate(value)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.SelectBeforeUpdate(value)); } public void Persister<T>() where T : IEntityPersister { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.Persister<T>()); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.Persister<T>()); } #endregion @@ -72,27 +94,27 @@ public void Loader(string namedQueryReference) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.Loader(namedQueryReference)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.Loader(namedQueryReference)); } public void SqlInsert(string sql) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.SqlInsert(sql)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.SqlInsert(sql)); } public void SqlUpdate(string sql) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.SqlUpdate(sql)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.SqlUpdate(sql)); } public void SqlDelete(string sql) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.SqlDelete(sql)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.SqlDelete(sql)); } public void Subselect(string sql) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassAttributesMapper m) => m.Subselect(sql)); + CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.Subselect(sql)); } #endregion Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/ICustomizersHolder.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/ICustomizersHolder.cs 2011-04-05 17:48:10 UTC (rev 5612) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/ICustomizersHolder.cs 2011-04-05 18:28:43 UTC (rev 5613) @@ -5,7 +5,7 @@ public interface ICustomizersHolder { void AddCustomizer(System.Type type, Action<IClassMapper> classCustomizer); - void AddCustomizer(System.Type type, Action<ISubclassAttributesMapper> classCustomizer); + void AddCustomizer(System.Type type, Action<ISubclassMapper> classCustomizer); void AddCustomizer(System.Type type, Action<IJoinedSubclassAttributesMapper> classCustomizer); void AddCustomizer(System.Type type, Action<IUnionSubclassAttributesMapper> classCustomizer); void AddCustomizer(System.Type type, Action<IComponentAttributesMapper> classCustomizer); @@ -24,7 +24,7 @@ void AddCustomizer(PropertyPath member, Action<IComponentAttributesMapper> propertyCustomizer); void InvokeCustomizers(System.Type type, IClassMapper mapper); - void InvokeCustomizers(System.Type type, ISubclassAttributesMapper mapper); + void InvokeCustomizers(System.Type type, ISubclassMapper mapper); void InvokeCustomizers(System.Type type, IJoinedSubclassAttributesMapper mapper); void InvokeCustomizers(System.Type type, IUnionSubclassAttributesMapper mapper); void InvokeCustomizers(System.Type type, IComponentAttributesMapper mapper); Added: trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExpliticMappingTests/SubclassPropertiesSplitsTests.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExpliticMappingTests/SubclassPropertiesSplitsTests.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExpliticMappingTests/SubclassPropertiesSplitsTests.cs 2011-04-05 18:28:43 UTC (rev 5613) @@ -0,0 +1,79 @@ +using System.Collections.Generic; +using System.Linq; +using NHibernate.Mapping.ByCode; +using NUnit.Framework; +using SharpTestsEx; + +namespace NHibernate.Test.MappingByCode.ExpliticMappingTests +{ + public class SubclassPropertiesSplitsTests + { + private class MyClass + { + public int Id { get; set; } + } + + private class Inherited : MyClass + { + public string Something0 { get; set; } + public string SomethingA1 { get; set; } + public string SomethingA2 { get; set; } + public string SomethingB1 { get; set; } + public string SomethingB2 { get; set; } + } + + [Test] + public void WhenSplittedPropertiesThenRegisterSplitGroupIds() + { + var inspector = new ExplicitlyDeclaredModel(); + var mapper = new ModelMapper(inspector); + mapper.Subclass<Inherited>(map => + { + map.Join("MyClassSplit1", mj => + { + mj.Property(x => x.SomethingA1); + mj.Property(x => x.SomethingA2); + }); + map.Join("MyClassSplit2", mj => + { + mj.Property(x => x.SomethingB1); + mj.Property(x => x.SomethingB2); + }); + map.Property(x => x.Something0); + }); + + IEnumerable<string> tablePerClassSplits = inspector.GetPropertiesSplits(typeof(Inherited)); + tablePerClassSplits.Should().Have.SameValuesAs("MyClassSplit1", "MyClassSplit2"); + } + + [Test] + public void WhenSplittedPropertiesThenRegister() + { + var inspector = new ExplicitlyDeclaredModel(); + var mapper = new ModelMapper(inspector); + mapper.Subclass<Inherited>(map => + { + map.Join("MyClassSplit1", mj => + { + mj.Property(x => x.SomethingA1); + mj.Property(x => x.SomethingA2); + }); + map.Join("MyClassSplit2", mj => + { + mj.Property(x => x.SomethingB1); + mj.Property(x => x.SomethingB2); + }); + map.Property(x => x.Something0); + }); + + inspector.IsTablePerClassSplit(typeof(MyClass), "MyClassSplit1", For<Inherited>.Property(x => x.Something0)).Should().Be.False(); + inspector.IsTablePerClassSplit(typeof(MyClass), "MyClassSplit2", For<Inherited>.Property(x => x.Something0)).Should().Be.False(); + + inspector.IsTablePerClassSplit(typeof(MyClass), "MyClassSplit1", For<Inherited>.Property(x => x.SomethingA1)).Should().Be.True(); + inspector.IsTablePerClassSplit(typeof(MyClass), "MyClassSplit1", For<Inherited>.Property(x => x.SomethingA2)).Should().Be.True(); + inspector.IsTablePerClassSplit(typeof(MyClass), "MyClassSplit2", For<Inherited>.Property(x => x.SomethingB1)).Should().Be.True(); + inspector.IsTablePerClassSplit(typeof(MyClass), "MyClassSplit2", For<Inherited>.Property(x => x.SomethingB2)).Should().Be.True(); + } + + } +} \ 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-05 17:48:10 UTC (rev 5612) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-04-05 18:28:43 UTC (rev 5613) @@ -523,6 +523,7 @@ <Compile Include="MappingByCode\ExpliticMappingTests\NaturalIdTests.cs" /> <Compile Include="MappingByCode\ExpliticMappingTests\PoidTests.cs" /> <Compile Include="MappingByCode\ExpliticMappingTests\RootClassPropertiesSplitsTests.cs" /> + <Compile Include="MappingByCode\ExpliticMappingTests\SubclassPropertiesSplitsTests.cs" /> <Compile Include="MappingByCode\ExpliticMappingTests\VersionTests.cs" /> <Compile Include="MappingByCode\For.cs" /> <Compile Include="MappingByCode\MappersTests\AbstractPropertyContainerMapperTest.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |