|
From: <fab...@us...> - 2011-04-27 13:51:46
|
Revision: 5772
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5772&view=rev
Author: fabiomaulo
Date: 2011-04-27 13:51:34 +0000 (Wed, 27 Apr 2011)
Log Message:
-----------
Implemented ambiguous mapping checking for Id
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/ClassMapper.cs
trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
Added Paths:
-----------
trunk/nhibernate/src/NHibernate.Test/MappingByCode/MappersTests/ClassMapperTests/CheckMixingPOIDStrategiesTests.cs
Modified: trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/ClassMapper.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/ClassMapper.cs 2011-04-27 12:08:50 UTC (rev 5771)
+++ trunk/nhibernate/src/NHibernate/Mapping/ByCode/Impl/ClassMapper.cs 2011-04-27 13:51:34 UTC (rev 5772)
@@ -11,6 +11,9 @@
{
private readonly HbmClass classMapping;
private readonly IIdMapper idMapper;
+ private bool simpleIdPropertyWasUsed;
+ private bool composedIdWasUsed;
+ private bool componentAsIdWasUsed;
private Dictionary<string, IJoinMapper> joinMappers;
private ICacheMapper cacheMapper;
private IDiscriminatorMapper discriminatorMapper;
@@ -64,16 +67,40 @@
public void Id(MemberInfo idProperty, Action<IIdMapper> mapper)
{
- var id = (HbmId) classMapping.Item;
+ var id = classMapping.Item as HbmId;
+ if(id == null)
+ {
+ var propertyDescription = idProperty != null ? " '" + idProperty.Name + "'" : ", with generator, ";
+ throw new MappingException(string.Format("Abiguous mapping of {0} id. A ComponentAsId or a ComposedId was used and you are trying to map the property{1} as id.",
+ Container.FullName, propertyDescription));
+ }
mapper(new IdMapper(idProperty, id));
+ if(idProperty != null)
+ {
+ simpleIdPropertyWasUsed = true;
+ }
}
public void ComponentAsId(MemberInfo idProperty, Action<IComponentAsIdMapper> mapper)
{
+ if (idProperty == null)
+ {
+ return;
+ }
if (!IsMemberSupportedByMappedContainer(idProperty))
{
throw new ArgumentOutOfRangeException("idProperty", "Can't use, as component id property, a property of another graph");
}
+ if (composedIdWasUsed)
+ {
+ throw new MappingException(string.Format("Abiguous mapping of {0} id. A composed id was defined and you are trying to map the component {1}, of property '{2}', as id for {0}."
+ , Container.FullName, idProperty.GetPropertyOrFieldType().FullName, idProperty.Name));
+ }
+ if (simpleIdPropertyWasUsed)
+ {
+ throw new MappingException(string.Format("Abiguous mapping of {0} id. An id property, with generator, was defined and you are trying to map the component {1}, of property '{2}', as id for {0}."
+ , Container.FullName, idProperty.GetPropertyOrFieldType().FullName, idProperty.Name));
+ }
var id = classMapping.Item as HbmCompositeId;
if(id == null)
{
@@ -81,10 +108,20 @@
classMapping.Item = id;
}
mapper(new ComponentAsIdMapper(idProperty.GetPropertyOrFieldType(), idProperty, id, mapDoc));
+ componentAsIdWasUsed = true;
}
public void ComposedId(Action<IComposedIdMapper> idPropertiesMapping)
{
+ if(componentAsIdWasUsed)
+ {
+ throw new MappingException(string.Format("Abiguous mapping of {0} id. A Component as id was used and you are trying to map an id composed by various properties of {0}.", Container.FullName));
+ }
+ if (simpleIdPropertyWasUsed)
+ {
+ throw new MappingException(string.Format("Abiguous mapping of {0} id. An id property, with generator, was defined and you are trying to map an id composed by various properties of {0}.", Container.FullName));
+ }
+
var id = classMapping.Item as HbmCompositeId;
if (id == null)
{
@@ -92,6 +129,7 @@
classMapping.Item = id;
}
idPropertiesMapping(new ComposedIdMapper(Container, id, mapDoc));
+ composedIdWasUsed = true;
}
public void Discriminator(Action<IDiscriminatorMapper> discriminatorMapping)
Added: trunk/nhibernate/src/NHibernate.Test/MappingByCode/MappersTests/ClassMapperTests/CheckMixingPOIDStrategiesTests.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/MappingByCode/MappersTests/ClassMapperTests/CheckMixingPOIDStrategiesTests.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/MappingByCode/MappersTests/ClassMapperTests/CheckMixingPOIDStrategiesTests.cs 2011-04-27 13:51:34 UTC (rev 5772)
@@ -0,0 +1,99 @@
+using NHibernate.Cfg.MappingSchema;
+using NHibernate.Mapping.ByCode.Impl;
+using NUnit.Framework;
+using SharpTestsEx;
+
+namespace NHibernate.Test.MappingByCode.MappersTests.ClassMapperTests
+{
+ public class CheckMixingPoidStrategiesTests
+ {
+ private class PersonId
+ {
+ public string Email { get; set; }
+ }
+
+ private class Person
+ {
+ private PersonId id;
+ public PersonId Id
+ {
+ get { return id; }
+ }
+
+ public int Poid { get; set; }
+ public string Name { get; set; }
+ }
+
+ [Test]
+ public void WhenMixComposedIdWithComponentAsIdThenThrows()
+ {
+ var mapdoc = new HbmMapping();
+ var mapper = new ClassMapper(typeof (Person), mapdoc, For<Person>.Property(x => x.Id));
+
+ mapper.ComposedId(map => map.Property(For<Person>.Property(x => x.Name), pm => { }));
+ Executing.This(() =>
+ mapper.ComponentAsId(For<Person>.Property(x => x.Id), map => map.Property(For<PersonId>.Property(x => x.Email), pm => { }))
+ ).Should().Throw<MappingException>();
+ }
+
+ [Test]
+ public void WhenMixComponentAsIdWithComposedIdThenThrows()
+ {
+ var mapdoc = new HbmMapping();
+ var mapper = new ClassMapper(typeof(Person), mapdoc, For<Person>.Property(x => x.Id));
+
+ mapper.ComponentAsId(For<Person>.Property(x => x.Id), map => map.Property(For<PersonId>.Property(x => x.Email), pm => { }));
+ Executing.This(() =>
+ mapper.ComposedId(map => map.Property(For<Person>.Property(x => x.Name), pm => { }))
+ ).Should().Throw<MappingException>();
+ }
+
+ [Test]
+ public void WhenMixComposedIdWithSimpleIdThenThrows()
+ {
+ var mapdoc = new HbmMapping();
+ var mapper = new ClassMapper(typeof(Person), mapdoc, For<Person>.Property(x => x.Id));
+
+ mapper.ComposedId(map => map.Property(For<Person>.Property(x => x.Name), pm => { }));
+ Executing.This(() =>
+ mapper.Id(For<Person>.Property(x => x.Poid), pm => { })
+ ).Should().Throw<MappingException>();
+ }
+
+ [Test]
+ public void WhenMixComponentAsIdWithSimpleIdThenThrows()
+ {
+ var mapdoc = new HbmMapping();
+ var mapper = new ClassMapper(typeof(Person), mapdoc, For<Person>.Property(x => x.Id));
+
+ mapper.ComponentAsId(For<Person>.Property(x => x.Id), map => map.Property(For<PersonId>.Property(x => x.Email), pm => { }));
+ Executing.This(() =>
+ mapper.Id(For<Person>.Property(x => x.Poid), pm => { })
+ ).Should().Throw<MappingException>();
+ }
+
+ [Test]
+ public void WhenMixSimpleIdWithComposedIdThenThrows()
+ {
+ var mapdoc = new HbmMapping();
+ var mapper = new ClassMapper(typeof(Person), mapdoc, For<Person>.Property(x => x.Id));
+
+ mapper.Id(For<Person>.Property(x => x.Poid), pm => { });
+ Executing.This(() =>
+ mapper.ComposedId(map => map.Property(For<Person>.Property(x => x.Name), pm => { }))
+ ).Should().Throw<MappingException>();
+ }
+
+ [Test]
+ public void WhenMixSimpleIdWithComponentAsIdThenThrows()
+ {
+ var mapdoc = new HbmMapping();
+ var mapper = new ClassMapper(typeof(Person), mapdoc, For<Person>.Property(x => x.Id));
+
+ mapper.Id(For<Person>.Property(x => x.Poid), pm => { });
+ Executing.This(() =>
+ mapper.ComponentAsId(For<Person>.Property(x => x.Id), map => map.Property(For<PersonId>.Property(x => x.Email), pm => { }))
+ ).Should().Throw<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-27 12:08:50 UTC (rev 5771)
+++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-04-27 13:51:34 UTC (rev 5772)
@@ -548,6 +548,7 @@
<Compile Include="MappingByCode\ExpliticMappingTests\VersionTests.cs" />
<Compile Include="MappingByCode\For.cs" />
<Compile Include="MappingByCode\MappersTests\AbstractPropertyContainerMapperTest.cs" />
+ <Compile Include="MappingByCode\MappersTests\ClassMapperTests\CheckMixingPOIDStrategiesTests.cs" />
<Compile Include="MappingByCode\MappersTests\ClassMapperTests\ClassMapperWithJoinPropertiesTest.cs" />
<Compile Include="MappingByCode\MappersTests\ClassMapperTests\ComponetAsIdTests.cs" />
<Compile Include="MappingByCode\MappersTests\ClassMapperTests\ComposedIdTests.cs" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|