From: <fab...@us...> - 2011-04-03 16:25:23
|
Revision: 5594 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5594&view=rev Author: fabiomaulo Date: 2011-04-03 16:25:16 +0000 (Sun, 03 Apr 2011) Log Message: ----------- Natural Id mapping fixed Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Mapping/ByCode/IClassMapper.cs trunk/nhibernate/src/NHibernate/Mapping/ByCode/IPlainPropertyContainerMapper.cs trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/ClassCustomizer.cs trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/PropertyContainerCustomizer.cs trunk/nhibernate/src/NHibernate/NHibernate.csproj trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/NaturalIdCustomizer.cs trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExpliticMappingTests/NaturalIdTests.cs trunk/nhibernate/src/NHibernate.Test/MappingByCode/For.cs Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/IClassMapper.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/IClassMapper.cs 2011-04-03 14:46:14 UTC (rev 5593) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/IClassMapper.cs 2011-04-03 16:25:16 UTC (rev 5594) @@ -36,7 +36,8 @@ void Schema(string schemaName); void Mutable(bool isMutable); void Version<TProperty>(Expression<Func<TEntity, TProperty>> versionProperty, Action<IVersionMapper> versionMapping); - void NaturalId(Action<INaturalIdAttributesMapper> naturalIdMapping); + void NaturalId(Action<IBasePlainPropertyContainerMapper<TEntity>> naturalIdPropertiesMapping, Action<INaturalIdAttributesMapper> naturalIdMapping); + void NaturalId(Action<IBasePlainPropertyContainerMapper<TEntity>> naturalIdPropertiesMapping); void Cache(Action<ICacheMapper> cacheMapping); void Filter(string filterName, Action<IFilterMapper> filterMapping); void Where(string whereClause); Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/IPlainPropertyContainerMapper.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/IPlainPropertyContainerMapper.cs 2011-04-03 14:46:14 UTC (rev 5593) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/IPlainPropertyContainerMapper.cs 2011-04-03 16:25:16 UTC (rev 5594) @@ -19,19 +19,23 @@ void OneToOne(MemberInfo property, Action<IOneToOneMapper> mapping); } - public interface IPlainPropertyContainerMapper<TContainer> + public interface IBasePlainPropertyContainerMapper<TContainer> { void Property<TProperty>(Expression<Func<TContainer, TProperty>> property); void Property<TProperty>(Expression<Func<TContainer, TProperty>> property, Action<IPropertyMapper> mapping); void Property(FieldInfo member, Action<IPropertyMapper> mapping); void Component<TComponent>(Expression<Func<TContainer, TComponent>> property, - Action<IComponentMapper<TComponent>> mapping) where TComponent : class; + Action<IComponentMapper<TComponent>> mapping) where TComponent : class; void ManyToOne<TProperty>(Expression<Func<TContainer, TProperty>> property, Action<IManyToOneMapper> mapping) where TProperty : class; void ManyToOne<TProperty>(Expression<Func<TContainer, TProperty>> property) where TProperty : class; - void OneToOne<TProperty>(Expression<Func<TContainer, TProperty>> property, Action<IOneToOneMapper> mapping) where TProperty : class; void Any<TProperty>(Expression<Func<TContainer, TProperty>> property, System.Type idTypeOfMetaType, Action<IAnyMapper> mapping) where TProperty : class; } + + public interface IPlainPropertyContainerMapper<TContainer> : IBasePlainPropertyContainerMapper<TContainer> + { + void OneToOne<TProperty>(Expression<Func<TContainer, TProperty>> property, Action<IOneToOneMapper> mapping) where TProperty : class; + } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/ClassCustomizer.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/ClassCustomizer.cs 2011-04-03 14:46:14 UTC (rev 5593) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/ClassCustomizer.cs 2011-04-03 16:25:16 UTC (rev 5594) @@ -75,11 +75,17 @@ CustomizersHolder.AddCustomizer(typeof (TEntity), (IClassAttributesMapper m) => m.Version(member, versionMapping)); } - public void NaturalId(Action<INaturalIdAttributesMapper> naturalIdMapping) + public void NaturalId(Action<IBasePlainPropertyContainerMapper<TEntity>> naturalIdPropertiesMapping, Action<INaturalIdAttributesMapper> naturalIdMapping) { - CustomizersHolder.AddCustomizer(typeof (TEntity), (IClassAttributesMapper m) => m.NaturalId(nidm => naturalIdMapping(nidm))); + naturalIdPropertiesMapping(new NaturalIdCustomizer<TEntity>(ExplicitDeclarationsHolder, CustomizersHolder)); + CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassAttributesMapper m) => m.NaturalId(nidm => naturalIdMapping(nidm))); } + public void NaturalId(Action<IBasePlainPropertyContainerMapper<TEntity>> naturalIdPropertiesMapping) + { + NaturalId(naturalIdPropertiesMapping, mapAttr => { }); + } + public void Cache(Action<ICacheMapper> cacheMapping) { CustomizersHolder.AddCustomizer(typeof (TEntity), (IClassAttributesMapper m) => m.Cache(cacheMapping)); Added: trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/NaturalIdCustomizer.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/NaturalIdCustomizer.cs (rev 0) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/NaturalIdCustomizer.cs 2011-04-03 16:25:16 UTC (rev 5594) @@ -0,0 +1,61 @@ +using System.Reflection; + +namespace NHibernate.Mapping.ByCode.Impl.CustomizersImpl +{ + public class NaturalIdCustomizer<TEntity> : PropertyContainerCustomizer<TEntity>, IBasePlainPropertyContainerMapper<TEntity> where TEntity : class + { + public NaturalIdCustomizer(IModelExplicitDeclarationsHolder explicitDeclarationsHolder, ICustomizersHolder customizersHolder) + : base(explicitDeclarationsHolder, customizersHolder, null) {} + + public override void Property<TProperty>(System.Linq.Expressions.Expression<System.Func<TEntity, TProperty>> property, System.Action<IPropertyMapper> mapping) + { + MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property); + MemberInfo memberOf = TypeExtensions.DecodeMemberAccessExpressionOf(property); + ExplicitDeclarationsHolder.AddAsNaturalId(member); + ExplicitDeclarationsHolder.AddAsNaturalId(memberOf); + base.Property(property, mapping); + } + + public override void Property(FieldInfo member, System.Action<IPropertyMapper> mapping) + { + ExplicitDeclarationsHolder.AddAsNaturalId(member); + base.Property(member, mapping); + } + + public override void Component<TComponent>(System.Linq.Expressions.Expression<System.Func<TEntity, TComponent>> property, System.Action<IComponentMapper<TComponent>> mapping) + { + MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property); + MemberInfo memberOf = TypeExtensions.DecodeMemberAccessExpressionOf(property); + ExplicitDeclarationsHolder.AddAsNaturalId(member); + ExplicitDeclarationsHolder.AddAsNaturalId(memberOf); + base.Component(property, mapping); + } + + public override void Any<TProperty>(System.Linq.Expressions.Expression<System.Func<TEntity, TProperty>> property, System.Type idTypeOfMetaType, System.Action<IAnyMapper> mapping) + { + MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property); + MemberInfo memberOf = TypeExtensions.DecodeMemberAccessExpressionOf(property); + ExplicitDeclarationsHolder.AddAsNaturalId(member); + ExplicitDeclarationsHolder.AddAsNaturalId(memberOf); + base.Any(property, idTypeOfMetaType, mapping); + } + + public override void ManyToOne<TProperty>(System.Linq.Expressions.Expression<System.Func<TEntity, TProperty>> property) + { + MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property); + MemberInfo memberOf = TypeExtensions.DecodeMemberAccessExpressionOf(property); + ExplicitDeclarationsHolder.AddAsNaturalId(member); + ExplicitDeclarationsHolder.AddAsNaturalId(memberOf); + base.ManyToOne(property); + } + + public override void ManyToOne<TProperty>(System.Linq.Expressions.Expression<System.Func<TEntity, TProperty>> property, System.Action<IManyToOneMapper> mapping) + { + MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property); + MemberInfo memberOf = TypeExtensions.DecodeMemberAccessExpressionOf(property); + ExplicitDeclarationsHolder.AddAsNaturalId(member); + ExplicitDeclarationsHolder.AddAsNaturalId(memberOf); + base.ManyToOne(property, mapping); + } + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/PropertyContainerCustomizer.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/PropertyContainerCustomizer.cs 2011-04-03 14:46:14 UTC (rev 5593) +++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/PropertyContainerCustomizer.cs 2011-04-03 16:25:16 UTC (rev 5594) @@ -22,13 +22,17 @@ protected ICustomizersHolder CustomizersHolder { get; private set; } protected PropertyPath PropertyPath { get; private set; } + protected IModelExplicitDeclarationsHolder ExplicitDeclarationsHolder + { + get { return explicitDeclarationsHolder; } + } public void Property<TProperty>(Expression<Func<TEntity, TProperty>> property) { Property(property, x => { }); } - public void Property<TProperty>(Expression<Func<TEntity, TProperty>> property, Action<IPropertyMapper> mapping) + public virtual void Property<TProperty>(Expression<Func<TEntity, TProperty>> property, Action<IPropertyMapper> mapping) { MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property); MemberInfo memberOf = TypeExtensions.DecodeMemberAccessExpressionOf(property); @@ -38,13 +42,13 @@ explicitDeclarationsHolder.AddAsProperty(memberOf); } - public void Property(FieldInfo member, Action<IPropertyMapper> mapping) + public virtual void Property(FieldInfo member, Action<IPropertyMapper> mapping) { CustomizersHolder.AddCustomizer(new PropertyPath(PropertyPath, member), mapping); explicitDeclarationsHolder.AddAsProperty(member); } - public void Component<TComponent>(Expression<Func<TEntity, TComponent>> property, + public virtual void Component<TComponent>(Expression<Func<TEntity, TComponent>> property, Action<IComponentMapper<TComponent>> mapping) where TComponent : class { MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property); @@ -53,7 +57,7 @@ mapping(new ComponentCustomizer<TComponent>(explicitDeclarationsHolder, CustomizersHolder, new PropertyPath(PropertyPath, memberOf))); } - public void ManyToOne<TProperty>(Expression<Func<TEntity, TProperty>> property, Action<IManyToOneMapper> mapping) + public virtual void ManyToOne<TProperty>(Expression<Func<TEntity, TProperty>> property, Action<IManyToOneMapper> mapping) where TProperty : class { MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property); @@ -64,7 +68,7 @@ explicitDeclarationsHolder.AddAsManyToOneRelation(memberOf); } - public void ManyToOne<TProperty>(Expression<Func<TEntity, TProperty>> property) where TProperty : class + public virtual void ManyToOne<TProperty>(Expression<Func<TEntity, TProperty>> property) where TProperty : class { ManyToOne(property, x => { }); } @@ -80,7 +84,7 @@ explicitDeclarationsHolder.AddAsOneToOneRelation(memberOf); } - public void Any<TProperty>(Expression<Func<TEntity, TProperty>> property, System.Type idTypeOfMetaType, Action<IAnyMapper> mapping) + public virtual void Any<TProperty>(Expression<Func<TEntity, TProperty>> property, System.Type idTypeOfMetaType, Action<IAnyMapper> mapping) where TProperty : class { MemberInfo member = TypeExtensions.DecodeMemberAccessExpression(property); Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj =================================================================== --- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2011-04-03 14:46:14 UTC (rev 5593) +++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2011-04-03 16:25:16 UTC (rev 5594) @@ -353,6 +353,7 @@ <Compile Include="Mapping\ByCode\Impl\CustomizersImpl\MapKeyManyToManyCustomizer.cs" /> <Compile Include="Mapping\ByCode\Impl\CustomizersImpl\MapKeyRelationCustomizer.cs" /> <Compile Include="Mapping\ByCode\Impl\CustomizersImpl\MapPropertiesCustomizer.cs" /> + <Compile Include="Mapping\ByCode\Impl\CustomizersImpl\NaturalIdCustomizer.cs" /> <Compile Include="Mapping\ByCode\Impl\CustomizersImpl\OneToManyCustomizer.cs" /> <Compile Include="Mapping\ByCode\Impl\CustomizersImpl\PropertyContainerCustomizer.cs" /> <Compile Include="Mapping\ByCode\Impl\CustomizersImpl\SetPropertiesCustomizer.cs" /> Added: trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExpliticMappingTests/NaturalIdTests.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExpliticMappingTests/NaturalIdTests.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/MappingByCode/ExpliticMappingTests/NaturalIdTests.cs 2011-04-03 16:25:16 UTC (rev 5594) @@ -0,0 +1,55 @@ +using System; +using NHibernate.Mapping.ByCode; +using NUnit.Framework; +using SharpTestsEx; + +namespace NHibernate.Test.MappingByCode.ExpliticMappingTests +{ + public class NaturalIdTests + { + private class MyClass + { + public int Id { get; set; } + public string Name { get; set; } + public Related Related { get; set; } + public MyComponent MyComponent { get; set; } + public object Any { get; set; } + } + + private class Related + { + public int Id { get; set; } + } + + private class MyComponent + { + public string FirstName { get; set; } + } + + [Test] + public void WhenDefineRootEntityThenRegister() + { + var inspector = new ExplicitlyDeclaredModel(); + var mapper = new ModelMapper(inspector); + mapper.Class<MyClass>(map => + { + map.Id(x => x.Id, idmap => { }); + map.NaturalId(nidm => + { + nidm.Property(x => x.Name); + nidm.ManyToOne(x => x.Related); + nidm.Component(x => x.MyComponent, cmap => + { + cmap.Property(y => y.FirstName); + }); + nidm.Any(x => x.Any, typeof(int), anymap => { }); + }); + }); + + inspector.IsMemberOfNaturalId(For<MyClass>.Property(x => x.Name)).Should().Be.True(); + inspector.IsMemberOfNaturalId(For<MyClass>.Property(x => x.Related)).Should().Be.True(); + inspector.IsMemberOfNaturalId(For<MyClass>.Property(x => x.MyComponent)).Should().Be.True(); + inspector.IsMemberOfNaturalId(For<MyClass>.Property(x => x.Any)).Should().Be.True(); + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/MappingByCode/For.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/MappingByCode/For.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/MappingByCode/For.cs 2011-04-03 16:25:16 UTC (rev 5594) @@ -0,0 +1,19 @@ +using System; +using System.Linq.Expressions; +using System.Reflection; +using NHibernate.Mapping.ByCode; + +namespace NHibernate.Test.MappingByCode +{ + public static class For<T> + { + public static MemberInfo Property(Expression<Func<T, object>> propertyGetter) + { + if (propertyGetter == null) + { + return null; + } + return TypeExtensions.DecodeMemberAccessExpression(propertyGetter); + } + } +} \ 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-03 14:46:14 UTC (rev 5593) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-04-03 16:25:16 UTC (rev 5594) @@ -519,6 +519,8 @@ <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\UnionSubclassMappingStrategyTests.cs" /> <Compile Include="MappingByCode\ExplicitlyDeclaredModelTests\UnionSubclassSequenceRegistrationTests.cs" /> <Compile Include="MappingByCode\ExpliticMappingTests\MappingOfPrivateMembersOnRootEntity.cs" /> + <Compile Include="MappingByCode\ExpliticMappingTests\NaturalIdTests.cs" /> + <Compile Include="MappingByCode\For.cs" /> <Compile Include="MappingByCode\NatureDemo\Naturalness\Address.cs" /> <Compile Include="MappingByCode\NatureDemo\Naturalness\Animal.cs" /> <Compile Include="MappingByCode\NatureDemo\Naturalness\Classification.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |