Skip to content

Commit d492cfd

Browse files
committed
Added ability to specify collection element type in conventions.
1 parent c6b0ff4 commit d492cfd

13 files changed

+277
-0
lines changed

src/FluentNHibernate.Testing/ConventionsTests/ApplyingToModel/HasManyCollectionConventionTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ public void ShouldSetKeyColumnNameProperty()
105105
VerifyModel(x => x.Key.Columns.First().Name.ShouldEqual("xxx"));
106106
}
107107

108+
[Test]
109+
public void ShouldSetElementTypePropertyUsingGeneric()
110+
{
111+
Convention(x => x.Element.Type<string>());
112+
113+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
114+
}
115+
116+
[Test]
117+
public void ShouldSetElementTypePropertyUsingTypeOf()
118+
{
119+
Convention(x => x.Element.Type(typeof(string)));
120+
121+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
122+
}
123+
124+
[Test]
125+
public void ShouldSetElementTypePropertyUsingString() {
126+
Convention(x => x.Element.Type(typeof(string).AssemblyQualifiedName));
127+
128+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
129+
}
130+
108131
[Test]
109132
public void ShouldSetLazyProperty()
110133
{

src/FluentNHibernate.Testing/ConventionsTests/ApplyingToModel/HasManyConventionTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ public void ShouldSetKeyColumnNameProperty()
112112
VerifyModel(x => x.Key.Columns.First().Name.ShouldEqual("xxx"));
113113
}
114114

115+
[Test]
116+
public void ShouldSetElementTypePropertyUsingGeneric()
117+
{
118+
Convention(x => x.Element.Type<string>());
119+
120+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
121+
}
122+
123+
[Test]
124+
public void ShouldSetElementTypePropertyUsingTypeOf()
125+
{
126+
Convention(x => x.Element.Type(typeof(string)));
127+
128+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
129+
}
130+
131+
[Test]
132+
public void ShouldSetElementTypePropertyUsingString()
133+
{
134+
Convention(x => x.Element.Type(typeof(string).AssemblyQualifiedName));
135+
136+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
137+
}
138+
115139
[Test]
116140
public void ShouldSetLazyProperty()
117141
{

src/FluentNHibernate.Testing/ConventionsTests/ApplyingToModel/HasManyToManyCollectionConventionTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ public void ShouldSetParentKeyColumnNameProperty()
105105
VerifyModel(x => x.Key.Columns.First().Name.ShouldEqual("xxx"));
106106
}
107107

108+
[Test]
109+
public void ShouldSetElementTypePropertyUsingGeneric()
110+
{
111+
Convention(x => x.Element.Type<string>());
112+
113+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
114+
}
115+
116+
[Test]
117+
public void ShouldSetElementTypePropertyUsingTypeOf()
118+
{
119+
Convention(x => x.Element.Type(typeof(string)));
120+
121+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
122+
}
123+
124+
[Test]
125+
public void ShouldSetElementTypePropertyUsingString()
126+
{
127+
Convention(x => x.Element.Type(typeof(string).AssemblyQualifiedName));
128+
129+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
130+
}
131+
108132
[Test]
109133
public void ShouldSetLazyProperty()
110134
{

src/FluentNHibernate.Testing/ConventionsTests/ApplyingToModel/HasManyToManyConventionTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ public void ShouldSetParentKeyColumnNameProperty()
104104
VerifyModel(x => x.Key.Columns.First().Name.ShouldEqual("xxx"));
105105
}
106106

107+
[Test]
108+
public void ShouldSetElementTypePropertyUsingGeneric()
109+
{
110+
Convention(x => x.Element.Type<string>());
111+
112+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
113+
}
114+
115+
[Test]
116+
public void ShouldSetElementTypePropertyUsingTypeOf()
117+
{
118+
Convention(x => x.Element.Type(typeof(string)));
119+
120+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
121+
}
122+
123+
[Test]
124+
public void ShouldSetElementTypePropertyUsingString() {
125+
Convention(x => x.Element.Type(typeof(string).AssemblyQualifiedName));
126+
127+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
128+
}
129+
107130
[Test]
108131
public void ShouldSetChildKeyColumnNameProperty()
109132
{

src/FluentNHibernate.Testing/ConventionsTests/OverridingFluentInterface/HasManyCollectionConventionTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,36 @@ public void KeyColumnNameShouldntBeOverwritten()
125125
VerifyModel(x => x.Key.Columns.First().Name.ShouldEqual("name"));
126126
}
127127

128+
[Test]
129+
public void ElementTypeShouldntBeOverwrittenUsingGeneric()
130+
{
131+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
132+
133+
Convention(x => x.Element.Type<int>());
134+
135+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
136+
}
137+
138+
[Test]
139+
public void ElementTypeShouldntBeOverwrittenUsingTypeOf()
140+
{
141+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
142+
143+
Convention(x => x.Element.Type(typeof(int)));
144+
145+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
146+
}
147+
148+
[Test]
149+
public void ElementTypeShouldntBeOverwrittenUsingString()
150+
{
151+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
152+
153+
Convention(x => x.Element.Type(typeof(int).AssemblyQualifiedName));
154+
155+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
156+
}
157+
128158
[Test]
129159
public void LazyShouldntBeOverwritten()
130160
{

src/FluentNHibernate.Testing/ConventionsTests/OverridingFluentInterface/HasManyConventionTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,36 @@ public void KeyColumnNameShouldntBeOverwritten()
126126
VerifyModel(x => x.Key.Columns.First().Name.ShouldEqual("name"));
127127
}
128128

129+
[Test]
130+
public void ElementTypeShouldntBeOverwrittenUsingGeneric()
131+
{
132+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
133+
134+
Convention(x => x.Element.Type<int>());
135+
136+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
137+
}
138+
139+
[Test]
140+
public void ElementTypeShouldntBeOverwrittenUsingTypeOf()
141+
{
142+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
143+
144+
Convention(x => x.Element.Type(typeof(int)));
145+
146+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
147+
}
148+
149+
[Test]
150+
public void ElementTypeShouldntBeOverwrittenUsingString()
151+
{
152+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
153+
154+
Convention(x => x.Element.Type(typeof(int).AssemblyQualifiedName));
155+
156+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
157+
}
158+
129159
[Test]
130160
public void LazyShouldntBeOverwritten()
131161
{

src/FluentNHibernate.Testing/ConventionsTests/OverridingFluentInterface/HasManyToManyCollectionConventionTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,36 @@ public void ParentKeyColumnNameShouldntBeOverwritten()
127127
VerifyModel(x => x.Key.Columns.First().Name.ShouldEqual("name"));
128128
}
129129

130+
[Test]
131+
public void ElementTypeShouldntBeOverwrittenUsingGeneric()
132+
{
133+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
134+
135+
Convention(x => x.Element.Type<int>());
136+
137+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
138+
}
139+
140+
[Test]
141+
public void ElementTypeShouldntBeOverwrittenUsingTypeOf()
142+
{
143+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
144+
145+
Convention(x => x.Element.Type(typeof(int)));
146+
147+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
148+
}
149+
150+
[Test]
151+
public void ElementTypeShouldntBeOverwrittenUsingString()
152+
{
153+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
154+
155+
Convention(x => x.Element.Type(typeof(int).AssemblyQualifiedName));
156+
157+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
158+
}
159+
130160
[Test]
131161
public void LazyShouldntBeOverwritten()
132162
{

src/FluentNHibernate.Testing/ConventionsTests/OverridingFluentInterface/HasManyToManyConventionTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,36 @@ public void ParentKeyColumnNameShouldntBeOverwritten()
126126
VerifyModel(x => x.Key.Columns.First().Name.ShouldEqual("name"));
127127
}
128128

129+
[Test]
130+
public void ElementTypeShouldntBeOverwrittenUsingGeneric()
131+
{
132+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
133+
134+
Convention(x => x.Element.Type<int>());
135+
136+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
137+
}
138+
139+
[Test]
140+
public void ElementTypeShouldntBeOverwrittenUsingTypeOf()
141+
{
142+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
143+
144+
Convention(x => x.Element.Type(typeof(int)));
145+
146+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
147+
}
148+
149+
[Test]
150+
public void ElementTypeShouldntBeOverwrittenUsingString()
151+
{
152+
Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
153+
154+
Convention(x => x.Element.Type(typeof(int).AssemblyQualifiedName));
155+
156+
VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
157+
}
158+
129159
[Test]
130160
public void ChildKeyColumnNameShouldntBeOverwritten()
131161
{

src/FluentNHibernate/Conventions/Instances/CollectionInstance.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,17 @@ public void SetOrderBy(string orderBy)
269269
get { return new KeyInstance(mapping.Key); }
270270
}
271271

272+
public new IElementInstance Element
273+
{
274+
get
275+
{
276+
if (!mapping.IsSpecified("Element"))
277+
mapping.Element = new ElementMapping();
278+
279+
return new ElementInstance(mapping.Element);
280+
}
281+
}
282+
272283
public void ApplyFilter(string name, string condition)
273284
{
274285
mapping.AddFilter(new FilterMapping
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using FluentNHibernate.Conventions.Inspections;
3+
using FluentNHibernate.MappingModel;
4+
using FluentNHibernate.MappingModel.Collections;
5+
6+
namespace FluentNHibernate.Conventions.Instances
7+
{
8+
public class ElementInstance : ElementInspector, IElementInstance
9+
{
10+
private readonly ElementMapping mapping;
11+
12+
public ElementInstance(ElementMapping mapping)
13+
: base(mapping)
14+
{
15+
this.mapping = mapping;
16+
}
17+
18+
public new void Type<T>()
19+
{
20+
if (!mapping.IsSpecified("Type"))
21+
mapping.Type = new TypeReference(typeof(T));
22+
}
23+
24+
public new void Type(string type)
25+
{
26+
if (!mapping.IsSpecified("Type"))
27+
mapping.Type = new TypeReference(type);
28+
}
29+
30+
public new void Type(Type type)
31+
{
32+
if (!mapping.IsSpecified("Type"))
33+
mapping.Type = new TypeReference(type);
34+
}
35+
}
36+
}

src/FluentNHibernate/Conventions/Instances/ICollectionInstance.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace FluentNHibernate.Conventions.Instances
77
public interface ICollectionInstance : ICollectionInspector
88
{
99
new IKeyInstance Key { get; }
10+
new IElementInstance Element { get; }
1011
new IRelationshipInstance Relationship { get; }
1112
void Table(string tableName);
1213
new void Name(string name);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
using FluentNHibernate.Conventions.Inspections;
4+
5+
namespace FluentNHibernate.Conventions.Instances
6+
{
7+
public interface IElementInstance : IElementInspector
8+
{
9+
new void Type<T>();
10+
new void Type(string type);
11+
new void Type(Type type);
12+
}
13+
}

src/FluentNHibernate/FluentNHibernate.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@
129129
<Compile Include="Conventions\Inspections\IKeyManyToOneInspector.cs" />
130130
<Compile Include="Conventions\Inspections\IKeyPropertyInspector.cs" />
131131
<Compile Include="Conventions\Inspections\KeyPropertyInspector.cs" />
132+
<Compile Include="Conventions\Instances\ElementInstance.cs" />
133+
<Compile Include="Conventions\Instances\IElementInstance.cs" />
132134
<Compile Include="Conventions\Instances\IKeyPropertyInstance.cs" />
133135
<Compile Include="Conventions\Instances\KeyPropertyInstance.cs" />
134136
<Compile Include="Diagnostics\AutomappingType.cs" />

0 commit comments

Comments
 (0)