Skip to content

Commit c2aed14

Browse files
committed
support HasMany in a Join
1 parent ba349cd commit c2aed14

File tree

8 files changed

+78
-0
lines changed

8 files changed

+78
-0
lines changed

src/FluentNHibernate.Testing/ConventionsTests/Inspection/JoinInspectorMapsToJoinMapping.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using FluentNHibernate.Conventions;
66
using FluentNHibernate.Conventions.Inspections;
77
using FluentNHibernate.MappingModel;
8+
using FluentNHibernate.MappingModel.Collections;
89
using FluentNHibernate.Utils.Reflection;
910
using NUnit.Framework;
1011

@@ -43,6 +44,27 @@ public void AnysCollectionIsEmpty()
4344
inspector.Anys.IsEmpty().ShouldBeTrue();
4445
}
4546

47+
[Test]
48+
public void CollectionsCollectionHasSameCountAsMapping()
49+
{
50+
mapping.AddCollection(new BagMapping());
51+
inspector.Collections.Count().ShouldEqual(1);
52+
}
53+
54+
[Test]
55+
public void CollectionsCollectionOfInspectors()
56+
{
57+
mapping.AddCollection(new BagMapping());
58+
inspector.Collections.First().ShouldBeOfType<ICollectionInspector>();
59+
}
60+
61+
[Test]
62+
public void CollectionsCollectionIsEmpty()
63+
{
64+
inspector.Collections.IsEmpty().ShouldBeTrue();
65+
}
66+
67+
4668
[Test]
4769
public void CatalogMapped()
4870
{

src/FluentNHibernate.Testing/FluentInterfaceTests/JoinSubPartModelGenerationTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public void ReferencesShouldAddToReferencesCollectionOnModel()
5656
.ModelShouldMatch(x => x.References.Count().ShouldEqual(1));
5757
}
5858

59+
[Test]
60+
public void HasManyShouldAddToCollectionsCollectionOnModel()
61+
{
62+
Join<OneToManyTarget>("table")
63+
.Mapping(m => m.HasMany(x => x.BagOfChildren))
64+
.ModelShouldMatch(x => x.Collections.Count().ShouldEqual(1));
65+
}
66+
5967
[Test]
6068
public void ReferencesAnyShouldAddToAnyCollectionOnModel()
6169
{

src/FluentNHibernate.Testing/MappingModel/Output/XmlJoinWriterTester.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FluentNHibernate.MappingModel;
22
using FluentNHibernate.MappingModel.ClassBased;
3+
using FluentNHibernate.MappingModel.Collections;
34
using FluentNHibernate.MappingModel.Output;
45
using FluentNHibernate.Testing.DomainModel.Mapping;
56
using FluentNHibernate.Testing.Testing;
@@ -148,5 +149,16 @@ public void ShouldWriteAny()
148149
writer.VerifyXml(mapping)
149150
.Element("any").Exists();
150151
}
152+
153+
[Test]
154+
public void ShouldWriteCollection()
155+
{
156+
var mapping = new JoinMapping();
157+
158+
mapping.AddCollection(new BagMapping());
159+
160+
writer.VerifyXml(mapping)
161+
.Element("bag").Exists();
162+
}
151163
}
152164
}

src/FluentNHibernate/Conventions/Inspections/IJoinInspector.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using FluentNHibernate.MappingModel.Collections;
23

34
namespace FluentNHibernate.Conventions.Inspections
45
{
@@ -11,6 +12,7 @@ public interface IJoinInspector : IInspector
1112
bool Optional { get; }
1213
IEnumerable<IPropertyInspector> Properties { get; }
1314
IEnumerable<IManyToOneInspector> References { get; }
15+
IEnumerable<ICollectionInspector> Collections { get;}
1416
string Schema { get; }
1517
string TableName { get; }
1618
string Catalog { get; }

src/FluentNHibernate/Conventions/Inspections/JoinInspector.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ public IEnumerable<IManyToOneInspector> References
8787
}
8888
}
8989

90+
91+
public IEnumerable<ICollectionInspector> Collections
92+
{
93+
get
94+
{
95+
return mapping.Collections
96+
.Select(x => new CollectionInspector(x))
97+
.Cast<ICollectionInspector>();
98+
}
99+
}
100+
90101
public string Schema
91102
{
92103
get { return mapping.Schema; }

src/FluentNHibernate/Mapping/JoinPart.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ JoinMapping IJoinMappingProvider.GetJoinMapping()
159159
foreach (var any in providers.Anys)
160160
mapping.AddAny(any.GetAnyMapping());
161161

162+
foreach (var collection in providers.Collections)
163+
mapping.AddCollection(collection.GetCollectionMapping());
164+
162165
return mapping;
163166
}
164167
}

src/FluentNHibernate/MappingModel/JoinMapping.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55
using FluentNHibernate.Mapping;
66
using FluentNHibernate.MappingModel.ClassBased;
7+
using FluentNHibernate.MappingModel.Collections;
78
using FluentNHibernate.Visitors;
89

910
namespace FluentNHibernate.MappingModel
@@ -51,6 +52,11 @@ public IEnumerable<AnyMapping> Anys
5152
get { return mappedMembers.Anys; }
5253
}
5354

55+
public IEnumerable<ICollectionMapping> Collections
56+
{
57+
get { return mappedMembers.Collections; }
58+
}
59+
5460
public void AddProperty(PropertyMapping property)
5561
{
5662
mappedMembers.AddProperty(property);
@@ -71,6 +77,11 @@ public void AddAny(AnyMapping mapping)
7177
mappedMembers.AddAny(mapping);
7278
}
7379

80+
public void AddCollection(ICollectionMapping collectionMapping)
81+
{
82+
mappedMembers.AddCollection(collectionMapping);
83+
}
84+
7485
public string TableName
7586
{
7687
get { return attributes.Get(x => x.TableName); }

src/FluentNHibernate/MappingModel/Output/XmlJoinWriter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Xml;
22
using FluentNHibernate.MappingModel.ClassBased;
3+
using FluentNHibernate.MappingModel.Collections;
34
using FluentNHibernate.Utils;
45
using FluentNHibernate.Visitors;
56

@@ -89,5 +90,13 @@ public override void Visit(AnyMapping mapping)
8990

9091
document.ImportAndAppendChild(xml);
9192
}
93+
94+
public override void Visit(ICollectionMapping mapping)
95+
{
96+
var writer = serviceLocator.GetWriter<ICollectionMapping>();
97+
var xml = writer.Write(mapping);
98+
99+
document.ImportAndAppendChild(xml);
100+
}
92101
}
93102
}

0 commit comments

Comments
 (0)