Permalink
Browse files

Fixed KeyReference and KeyProperty ordering

When using mixed KeyReference and KeyProperty's in a CompositeId
mapping, the order that you called them in wasn't being respected.

This has needed a refactor of the compositeId model, to store those two
mappings in a single collection to respect the order.

Fixes #163
  • Loading branch information...
1 parent 2c406bc commit 0e97db54df6416acbdd48481016a9b5beb7ab5d2 @jagregory jagregory committed May 16, 2010
Showing with 81 additions and 65 deletions.
  1. +1 −1 src/FluentNHibernate.Specs/FluentInterface/ClassMapSpecs/ClassMapSpecs.Id.cs
  2. +3 −3 src/FluentNHibernate.Testing/ConventionsTests/AccessConventionTests.cs
  3. +1 −1 src/FluentNHibernate.Testing/ConventionsTests/ApplyingToModel/KeyManyToOneConventionTests.cs
  4. +1 −1 src/FluentNHibernate.Testing/ConventionsTests/ApplyingToModel/KeyPropertyConventionTests.cs
  5. +5 −5 ...entNHibernate.Testing/ConventionsTests/Inspection/CompositeIdInspectorMapsToCompositeIdMapping.cs
  6. +1 −1 ...luentNHibernate.Testing/ConventionsTests/OverridingFluentInterface/KeyManyToOneConventionTests.cs
  7. +1 −1 ...FluentNHibernate.Testing/ConventionsTests/OverridingFluentInterface/KeyPropertyConventionTests.cs
  8. +13 −0 src/FluentNHibernate.Testing/DomainModel/Mapping/CompositeIdentityPartTester.cs
  9. +2 −2 src/FluentNHibernate.Testing/FluentInterfaceTests/CompositeIdMutablePropertyModelGenerationTests.cs
  10. +2 −2 src/FluentNHibernate.Testing/MappingModel/Equality/MappingEqualitySpecs.cs
  11. +2 −2 src/FluentNHibernate.Testing/MappingModel/Output/XmlCompositeIdWriterTester.cs
  12. +6 −4 src/FluentNHibernate/Conventions/Inspections/CompositeIdentityInspector.cs
  13. +6 −4 src/FluentNHibernate/Conventions/Instances/CompositeIdentityInstance.cs
  14. +6 −5 src/FluentNHibernate/FluentNHibernate.csproj
  15. +4 −6 src/FluentNHibernate/Mapping/CompositeIdentityPart.cs
  16. +14 −25 src/FluentNHibernate/MappingModel/Identity/CompositeIdMapping.cs
  17. +11 −0 src/FluentNHibernate/MappingModel/Identity/ICompositeIdKeyMapping.cs
  18. +1 −1 src/FluentNHibernate/MappingModel/Identity/KeyManyToOneMapping.cs
  19. +1 −1 src/FluentNHibernate/MappingModel/Identity/KeyPropertyMapping.cs
@@ -44,7 +44,7 @@ public class when_class_map_has_a_composite_id_with_a_key_reference_with_multipl
It should_add_all_the_columns_to_the_composite_id_mapping = () =>
mapping.Id.As<CompositeIdMapping>()
- .KeyManyToOnes.Single()
+ .Keys.Single()
.Columns.Select(x => x.Name)
.ShouldContain("col1", "col2");
@@ -54,12 +54,12 @@ public void CompositeIdIsSet()
id = ((CompositeIdMapping)compositeId.Id);
Assert.AreEqual(expectedAccess, id.Access);
- Assert.AreEqual(expectedAccess, id.KeyProperties.First(x => x.Name.Equals("IdA")).Access);
- Assert.AreEqual(expectedAccess, id.KeyProperties.First(x => x.Name.Equals("IdB")).Access);
+ Assert.AreEqual(expectedAccess, id.Keys.First(x => x.Name.Equals("IdA")).Access);
+ Assert.AreEqual(expectedAccess, id.Keys.First(x => x.Name.Equals("IdB")).Access);
id = ((CompositeIdMapping)oneToOne.Id);
Assert.AreEqual(expectedAccess, id.Access);
- Assert.AreEqual(expectedAccess, id.KeyManyToOnes.First(x => x.Name.Equals("Parent")).Access);
+ Assert.AreEqual(expectedAccess, id.Keys.First(x => x.Name.Equals("Parent")).Access);
}
[Test]
@@ -77,7 +77,7 @@ private void VerifyModel(Action<KeyManyToOneMapping> modelVerification)
.Classes.First()
.Id;
- modelVerification(((CompositeIdMapping)modelInstance).KeyManyToOnes.First());
+ modelVerification((KeyManyToOneMapping)((CompositeIdMapping)modelInstance).Keys.First(x => x is KeyManyToOneMapping));
}
#endregion
@@ -53,7 +53,7 @@ private void VerifyModel(Action<KeyPropertyMapping> modelVerification)
.Classes.First()
.Id;
- modelVerification(((CompositeIdMapping)modelInstance).KeyProperties.First());
+ modelVerification((KeyPropertyMapping)((CompositeIdMapping)modelInstance).Keys.First(x => x is KeyPropertyMapping));
}
#endregion
@@ -73,14 +73,14 @@ public void ClassIsNotSet()
[Test]
public void KeyManyToOnesCollectionHasSameCountAsMapping()
{
- mapping.AddKeyManyToOne(new KeyManyToOneMapping());
+ mapping.AddKey(new KeyManyToOneMapping());
inspector.KeyManyToOnes.Count().ShouldEqual(1);
}
[Test]
public void KeyManyToOnesCollectionOfInspectors()
{
- mapping.AddKeyManyToOne(new KeyManyToOneMapping());
+ mapping.AddKey(new KeyManyToOneMapping());
inspector.KeyManyToOnes.First().ShouldBeOfType<IKeyManyToOneInspector>();
}
@@ -93,15 +93,15 @@ public void KeyManyToOnesCollectionIsEmpty()
[Test]
public void KeyPropertiesCollectionHasSameCountAsMapping()
{
- mapping.AddKeyProperty(new KeyPropertyMapping());
+ mapping.AddKey(new KeyPropertyMapping());
inspector.KeyProperties.Count().ShouldEqual(1);
}
[Test]
public void KeyPropertiesCollectionOfInspectors()
{
- mapping.AddKeyProperty(new KeyPropertyMapping());
- inspector.KeyProperties.First().ShouldBeOfType<IKeyPropertyInspector>();
+ mapping.AddKey(new KeyPropertyMapping());
+ inspector.KeyProperties.First().ShouldBeOfType<KeyPropertyInspector>();
}
[Test]
@@ -91,7 +91,7 @@ private void VerifyModel(Action<KeyManyToOneMapping> modelVerification)
.Classes.First()
.Id;
- modelVerification(((CompositeIdMapping)modelInstance).KeyManyToOnes.First());
+ modelVerification((KeyManyToOneMapping)((CompositeIdMapping)modelInstance).Keys.First(x => x is KeyManyToOneMapping));
}
#endregion
@@ -61,7 +61,7 @@ private void VerifyModel(Action<KeyPropertyMapping> modelVerification)
.Classes.First()
.Id;
- modelVerification(((CompositeIdMapping)modelInstance).KeyProperties.First());
+ modelVerification((KeyPropertyMapping)((CompositeIdMapping)modelInstance).Keys.First(x => x is KeyPropertyMapping));
}
#endregion
@@ -138,6 +138,19 @@ public void ComponentCompositeIdWillSetNameAndClass()
.HasAttribute("class", typeof(ComponentKey).AssemblyQualifiedName);
}
+ [Test]
+ public void MixedKeyPropertyAndManyToOneOrdering()
+ {
+ new MappingTester<CompIdTarget>()
+ .ForMapping(c => c.CompositeId()
+ .KeyReference(x => x.Child)
+ .KeyProperty(x => x.LongId))
+ .Element("class/composite-id/*[1]")
+ .HasName("key-many-to-one")
+ .RootElement.Element("class/composite-id/*[2]")
+ .HasName("key-property");
+ }
+
public class CompIdTarget
{
public virtual long LongId { get; set; }
@@ -20,15 +20,15 @@ public void WithKeyPropertyShouldAddToModelKeyPropertiesCollection()
{
CompositeId<IdentityTarget>()
.Mapping(m => m.KeyProperty(x => x.IntId))
- .ModelShouldMatch(x => x.KeyProperties.Count().ShouldEqual(1));
+ .ModelShouldMatch(x => x.Keys.Count().ShouldEqual(1));
}
[Test]
public void WithKeyReferenceShouldAddToModelKeyManyToOnesCollection()
{
CompositeId<IdentityTarget>()
.Mapping(m => m.KeyReference(x => x.IntId))
- .ModelShouldMatch(x => x.KeyManyToOnes.Count().ShouldEqual(1));
+ .ModelShouldMatch(x => x.Keys.Count().ShouldEqual(1));
}
[Test]
@@ -203,8 +203,8 @@ public override CompositeIdMapping create_mapping()
UnsavedValue = "unsaved"
};
- mapping.AddKeyManyToOne(new KeyManyToOneMapping());
- mapping.AddKeyProperty(new KeyPropertyMapping());
+ mapping.AddKey(new KeyManyToOneMapping());
+ mapping.AddKey(new KeyPropertyMapping());
return mapping;
}
@@ -68,7 +68,7 @@ public void ShouldWriteKeyProperties()
{
var mapping = new CompositeIdMapping();
- mapping.AddKeyProperty(new KeyPropertyMapping());
+ mapping.AddKey(new KeyPropertyMapping());
writer.VerifyXml(mapping)
.Element("key-property").Exists();
@@ -79,7 +79,7 @@ public void ShouldWriteKeyManyToOnes()
{
var mapping = new CompositeIdMapping();
- mapping.AddKeyManyToOne(new KeyManyToOneMapping());
+ mapping.AddKey(new KeyManyToOneMapping());
writer.VerifyXml(mapping)
.Element("key-many-to-one").Exists();
@@ -47,8 +47,9 @@ public IEnumerable<IKeyManyToOneInspector> KeyManyToOnes
{
get
{
- return mapping.KeyManyToOnes
- .Select(x => new KeyManyToOneInspector(x))
+ return mapping.Keys
+ .Where(x => x is KeyManyToOneMapping)
+ .Select(x => new KeyManyToOneInspector((KeyManyToOneMapping)x))
.Cast<IKeyManyToOneInspector>();
}
}
@@ -57,8 +58,9 @@ public IEnumerable<IKeyPropertyInspector> KeyProperties
{
get
{
- return mapping.KeyProperties
- .Select(x => new KeyPropertyInspector(x))
+ return mapping.Keys
+ .Where(x => x is KeyPropertyMapping)
+ .Select(x => new KeyPropertyInspector((KeyPropertyMapping)x))
.Cast<IKeyPropertyInspector>();
}
}
@@ -59,8 +59,9 @@ public new IEnumerable<IKeyPropertyInstance> KeyProperties
{
get
{
- return mapping.KeyProperties
- .Select(x => new KeyPropertyInstance(x))
+ return mapping.Keys
+ .Where(x => x is KeyPropertyMapping)
+ .Select(x => new KeyPropertyInstance((KeyPropertyMapping)x))
.Cast<IKeyPropertyInstance>();
}
}
@@ -69,8 +70,9 @@ public new IEnumerable<IKeyManyToOneInstance> KeyManyToOnes
{
get
{
- return mapping.KeyManyToOnes
- .Select(x => new KeyManyToOneInstance(x))
+ return mapping.Keys
+ .Where(x => x is KeyManyToOneMapping)
+ .Select(x => new KeyManyToOneInstance((KeyManyToOneMapping)x))
.Cast<IKeyManyToOneInstance>();
}
}
@@ -103,6 +103,11 @@
<Compile Include="Automapping\Steps\CollectionStep.cs" />
<Compile Include="Automapping\Steps\IAutomappingStep.cs" />
<Compile Include="CombinedAssemblyTypeSource.cs" />
+ <Compile Include="Conventions\Inspections\IKeyManyToOneInspector.cs" />
+ <Compile Include="Conventions\Inspections\IKeyPropertyInspector.cs" />
+ <Compile Include="Conventions\Inspections\KeyPropertyInspector.cs" />
+ <Compile Include="Conventions\Instances\IKeyPropertyInstance.cs" />
+ <Compile Include="Conventions\Instances\KeyPropertyInstance.cs" />
<Compile Include="ITypeSource.cs" />
<Compile Include="Automapping\PrivateAutoPersistenceModel.cs" />
<Compile Include="Automapping\SubclassStrategy.cs" />
@@ -236,8 +241,6 @@
<Compile Include="Conventions\Inspections\IIndexManyToManyInspector.cs" />
<Compile Include="Conventions\Inspections\IJoinedSubclassInspector.cs" />
<Compile Include="Conventions\Inspections\IJoinInspector.cs" />
- <Compile Include="Conventions\Inspections\IKeyManyToOneInspector.cs" />
- <Compile Include="Conventions\Inspections\IKeyPropertyInspector.cs" />
<Compile Include="Conventions\Inspections\IManyToManyCollectionInspector.cs" />
<Compile Include="Conventions\Inspections\IMetaValueInspector.cs" />
<Compile Include="Conventions\Inspections\IndexInspector.cs" />
@@ -253,7 +256,6 @@
<Compile Include="Conventions\Inspections\JoinInspector.cs" />
<Compile Include="Conventions\Inspections\JoinInstance.cs" />
<Compile Include="Conventions\Inspections\KeyManyToOneInspector.cs" />
- <Compile Include="Conventions\Inspections\KeyPropertyInspector.cs" />
<Compile Include="Conventions\Inspections\ListInspector.cs" />
<Compile Include="Conventions\Inspections\LowerCasePrefix.cs" />
<Compile Include="Conventions\Inspections\ManyToManyInspector.cs" />
@@ -302,7 +304,6 @@
<Compile Include="Conventions\Instances\IJoinedSubclassInstance.cs" />
<Compile Include="Conventions\Instances\IJoinInstance.cs" />
<Compile Include="Conventions\Instances\IKeyManyToOneInstance.cs" />
- <Compile Include="Conventions\Instances\IKeyPropertyInstance.cs" />
<Compile Include="Conventions\Instances\IListInstance.cs" />
<Compile Include="Conventions\Instances\IManyToManyInstance.cs" />
<Compile Include="Conventions\Instances\IKeyInstance.cs" />
@@ -330,7 +331,6 @@
<Compile Include="Conventions\Instances\IVersionInstance.cs" />
<Compile Include="Conventions\Instances\JoinedSubclassInstance.cs" />
<Compile Include="Conventions\Instances\KeyManyToOneInstance.cs" />
- <Compile Include="Conventions\Instances\KeyPropertyInstance.cs" />
<Compile Include="Conventions\Instances\ListInstance.cs" />
<Compile Include="Conventions\Instances\ManyToManyCollectionInstance.cs" />
<Compile Include="Conventions\Instances\MapInstance.cs" />
@@ -399,6 +399,7 @@
<Compile Include="MappingModel\Collections\Lazy.cs" />
<Compile Include="MappingModel\Collections\NestedCompositeElementMapping.cs" />
<Compile Include="MappingModel\EqualityExtensions.cs" />
+ <Compile Include="MappingModel\Identity\ICompositeIdKeyMapping.cs" />
<Compile Include="MappingModel\IRelationship.cs" />
<Compile Include="MappingModel\NaturalIdMapping.cs" />
<Compile Include="MappingModel\Output\XmlNaturalIdWriter.cs" />
@@ -16,8 +16,7 @@ public class CompositeIdentityPart<T> : ICompositeIdMappingProvider
{
private readonly AccessStrategyBuilder<CompositeIdentityPart<T>> access;
private readonly AttributeStore<CompositeIdMapping> attributes = new AttributeStore<CompositeIdMapping>();
- private readonly IList<KeyPropertyMapping> keyProperties = new List<KeyPropertyMapping>();
- private readonly IList<KeyManyToOneMapping> keyManyToOnes = new List<KeyManyToOneMapping>();
+ private readonly IList<ICompositeIdKeyMapping> keys = new List<ICompositeIdKeyMapping>();
private bool nextBool = true;
public CompositeIdentityPart()
@@ -90,7 +89,7 @@ protected virtual CompositeIdentityPart<T> KeyProperty(Member member, string col
if(!string.IsNullOrEmpty(columnName))
key.AddColumn(new ColumnMapping { Name = columnName });
- keyProperties.Add(key);
+ keys.Add(key);
return this;
}
@@ -152,7 +151,7 @@ protected virtual CompositeIdentityPart<T> KeyReference(Member property, IEnumer
if (customMapping != null)
customMapping(keyPart);
- keyManyToOnes.Add(key);
+ keys.Add(key);
return this;
}
@@ -194,8 +193,7 @@ CompositeIdMapping ICompositeIdMappingProvider.GetCompositeIdMapping()
mapping.ContainingEntityType = typeof(T);
- keyProperties.Each(mapping.AddKeyProperty);
- keyManyToOnes.Each(mapping.AddKeyManyToOne);
+ keys.Each(mapping.AddKey);
return mapping;
}
@@ -10,8 +10,7 @@ namespace FluentNHibernate.MappingModel.Identity
public class CompositeIdMapping : MappingBase, IIdentityMapping
{
private readonly AttributeStore<CompositeIdMapping> attributes;
- private readonly IList<KeyPropertyMapping> keyProperties = new List<KeyPropertyMapping>();
- private readonly IList<KeyManyToOneMapping> keyManyToOnes = new List<KeyManyToOneMapping>();
+ private readonly IList<ICompositeIdKeyMapping> keys = new List<ICompositeIdKeyMapping>();
public CompositeIdMapping()
: this(new AttributeStore())
@@ -28,11 +27,13 @@ public override void AcceptVisitor(IMappingModelVisitor visitor)
{
visitor.ProcessCompositeId(this);
- foreach (var key in keyProperties)
- visitor.Visit(key);
-
- foreach (var key in keyManyToOnes)
- visitor.Visit(key);
+ foreach (var key in keys)
+ {
+ if (key is KeyPropertyMapping)
+ visitor.Visit((KeyPropertyMapping)key);
+ if (key is KeyManyToOneMapping)
+ visitor.Visit((KeyManyToOneMapping)key);
+ }
}
public string Name
@@ -69,26 +70,16 @@ public string UnsavedValue
set { attributes.Set(x => x.UnsavedValue, value); }
}
- public IEnumerable<KeyPropertyMapping> KeyProperties
+ public IEnumerable<ICompositeIdKeyMapping> Keys
{
- get { return keyProperties; }
- }
-
- public IEnumerable<KeyManyToOneMapping> KeyManyToOnes
- {
- get { return keyManyToOnes; }
+ get { return keys; }
}
public Type ContainingEntityType { get; set; }
- public void AddKeyProperty(KeyPropertyMapping mapping)
- {
- keyProperties.Add(mapping);
- }
-
- public void AddKeyManyToOne(KeyManyToOneMapping mapping)
+ public void AddKey(ICompositeIdKeyMapping mapping)
{
- keyManyToOnes.Add(mapping);
+ keys.Add(mapping);
}
public override bool IsSpecified(string property)
@@ -111,8 +102,7 @@ public bool Equals(CompositeIdMapping other)
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other.attributes, attributes) &&
- other.keyProperties.ContentEquals(keyProperties) &&
- other.keyManyToOnes.ContentEquals(keyManyToOnes) &&
+ other.keys.ContentEquals(keys) &&
Equals(other.ContainingEntityType, ContainingEntityType);
}
@@ -129,8 +119,7 @@ public override int GetHashCode()
unchecked
{
int result = (attributes != null ? attributes.GetHashCode() : 0);
- result = (result * 397) ^ (keyProperties != null ? keyProperties.GetHashCode() : 0);
- result = (result * 397) ^ (keyManyToOnes != null ? keyManyToOnes.GetHashCode() : 0);
+ result = (result * 397) ^ (keys != null ? keys.GetHashCode() : 0);
result = (result * 397) ^ (ContainingEntityType != null ? ContainingEntityType.GetHashCode() : 0);
return result;
}
@@ -0,0 +1,11 @@
+using System.Collections.Generic;
+
+namespace FluentNHibernate.MappingModel.Identity
+{
+ public interface ICompositeIdKeyMapping
+ {
+ IEnumerable<ColumnMapping> Columns { get; }
+ string Name { get; }
+ string Access { get; }
+ }
+}
Oops, something went wrong.

0 comments on commit 0e97db5

Please sign in to comment.