Skip to content

Commit c6b0ff4

Browse files
committed
Added ability to change collection type in a ICollectionConvention
(Had to refactor the individual collection mappings into a single CollectionMapping. Less OO, but easier to deal with.)
1 parent fb8b9af commit c6b0ff4

File tree

117 files changed

+1256
-2576
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+1256
-2576
lines changed

src/FluentNHibernate.5.1.ReSharper

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@
216216
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="EnumMember" />
217217
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="Other" />
218218
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="PrivateInstanceFields" />
219-
<PredefinedRule Inspect="True" Prefix="_" Suffix="" Style="aaBb" ElementKind="PrivateStaticFields" />
219+
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="PrivateStaticFields" />
220220
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PrivateConstants" />
221221
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PrivateStaticReadonly" />
222222
<UserRule Inspect="False" Prefix="" Suffix="" Style="aa_bb" StaticnessKind="Static, Instance" AccessRight="Private, Protected, ProtectedInternal, Internal, Public" Description="MSpec naming">

src/FluentNHibernate.Specs/Automapping/AutomappingSpecs.ElementCollections.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public class when_the_automapper_is_told_to_map_a_list_of_simple_types : AutoMap
119119
container.Collections.Count().ShouldEqual(1);
120120

121121
It should_create_a_collection_that_s_a_bag = () =>
122-
container.Collections.Single().ShouldBeOfType<BagMapping>();
122+
container.Collections.Single().Collection.ShouldEqual(Collection.Bag);
123123

124124
It should_create_an_element_for_the_collection = () =>
125125
container.Collections.Single().Element.ShouldNotBeNull();

src/FluentNHibernate.Specs/Conventions/ApplyFilterSpecs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class when_applying_a_filter_to_a_one_to_many_using_conventions
5858
mapping.Filters.Single().Name.ShouldEqual(FilterHasManyConvention.FilterName);
5959

6060
static FluentNHibernate.PersistenceModel model;
61-
static ICollectionMapping mapping;
61+
static CollectionMapping mapping;
6262
}
6363

6464
public class when_applying_a_filter_to_a_many_to_many_using_conventions
@@ -87,6 +87,6 @@ public class when_applying_a_filter_to_a_many_to_many_using_conventions
8787
mapping.Filters.Single().Name.ShouldEqual(FilterHasManyConvention.FilterName);
8888

8989
static FluentNHibernate.PersistenceModel model;
90-
static ICollectionMapping mapping;
90+
static CollectionMapping mapping;
9191
}
9292
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using FluentNHibernate.MappingModel.ClassBased;
6+
using FluentNHibernate.MappingModel.Collections;
7+
using FluentNHibernate.Specs.Conventions.Fixtures;
8+
using Machine.Specifications;
9+
10+
namespace FluentNHibernate.Specs.Conventions
11+
{
12+
public class when_changing_the_collection_type_with_conventions
13+
{
14+
Establish context = () =>
15+
{
16+
model = new FluentNHibernate.PersistenceModel();
17+
model.Conventions.Add<CollectionConvention>();
18+
model.Add(new CollectionTargetMap());
19+
model.Add(new CollectionChildTargetMap());
20+
};
21+
22+
Because of = () =>
23+
mapping = model.BuildMappingFor<CollectionTarget>();
24+
25+
It should_be_able_to_change_a_bag_to_a_list = () =>
26+
mapping.Collections
27+
.Single(x => x.Name == "Bag")
28+
.Collection.ShouldEqual(Collection.List);
29+
30+
It should_be_able_to_change_a_set_to_a_list = () =>
31+
mapping.Collections
32+
.Single(x => x.Name == "Set")
33+
.Collection.ShouldEqual(Collection.List);
34+
35+
static FluentNHibernate.PersistenceModel model;
36+
static ClassMapping mapping;
37+
}
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using FluentNHibernate.Conventions;
3+
using FluentNHibernate.Conventions.Instances;
4+
5+
namespace FluentNHibernate.Specs.Conventions.Fixtures
6+
{
7+
public class CollectionConvention : ICollectionConvention
8+
{
9+
public const string FilterName = "TestFilterName";
10+
public const string FilterCondition = "TestFilterCondition";
11+
12+
public void Apply(ICollectionInstance instance)
13+
{
14+
instance.AsList();
15+
}
16+
}
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using FluentNHibernate.Mapping;
3+
using Iesi.Collections.Generic;
4+
5+
namespace FluentNHibernate.Specs.Conventions.Fixtures
6+
{
7+
public class CollectionTarget
8+
{
9+
public int Id { get; set; }
10+
public IList<CollectionChildTarget> Bag { get; set; }
11+
public ISet<CollectionChildTarget> Set { get; set; }
12+
}
13+
14+
public class CollectionChildTarget
15+
{
16+
public int Id { get; set; }
17+
}
18+
19+
public class CollectionTargetMap : ClassMap<CollectionTarget>
20+
{
21+
public CollectionTargetMap()
22+
{
23+
Id(x => x.Id);
24+
HasMany(x => x.Bag);
25+
HasMany(x => x.Set);
26+
}
27+
}
28+
29+
public class CollectionChildTargetMap : ClassMap<CollectionChildTarget>
30+
{
31+
public CollectionChildTargetMap()
32+
{
33+
Id(x => x.Id);
34+
}
35+
}
36+
}

src/FluentNHibernate.Specs/FluentInterface/ClasslikeBehaviors.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class ClasslikeBagBehaviour
5353
mapping.Collections.Count().ShouldEqual(1);
5454

5555
It should_add_a_bag_to_the_collections_of_the_mapping = () =>
56-
mapping.Collections.Single().ShouldBeOfType<BagMapping>();
56+
mapping.Collections.Single().Collection.ShouldEqual(Collection.Bag);
5757

5858
It should_use_the_property_name_as_the_collection_name = () =>
5959
mapping.Collections.Single().Name.ShouldEqual("BagOfChildren");
@@ -74,7 +74,7 @@ public class ClasslikeSetBehaviour
7474
mapping.Collections.Count().ShouldEqual(1);
7575

7676
It should_add_a_set_to_the_collections_of_the_mapping = () =>
77-
mapping.Collections.Single().ShouldBeOfType<SetMapping>();
77+
mapping.Collections.Single().Collection.ShouldEqual(Collection.Set);
7878

7979
It should_use_the_property_name_as_the_collection_name = () =>
8080
mapping.Collections.Single().Name.ShouldEqual("SetOfChildren");
@@ -95,29 +95,29 @@ public class ClasslikeSetBehaviour
9595
public class ClasslikeListWithDefaultIndexBehaviour : ClasslikeListBehaviour
9696
{
9797
It should_create_an_index_for_the_collection_mapping = () =>
98-
mapping.Collections.Single().As<ListMapping>().Index.ShouldNotBeNull();
98+
mapping.Collections.Single().Index.ShouldNotBeNull();
9999

100100
It should_create_a_single_column_for_the_index = () =>
101-
mapping.Collections.Single().As<ListMapping>().Index.Columns.Count().ShouldEqual(1);
101+
mapping.Collections.Single().Index.Columns.Count().ShouldEqual(1);
102102

103103
It should_use_index_as_the_index_column_name = () =>
104-
mapping.Collections.Single().As<ListMapping>().Index.Columns.Single().Name.ShouldEqual("Index");
104+
mapping.Collections.Single().Index.Columns.Single().Name.ShouldEqual("Index");
105105
}
106106

107107
[Behaviors]
108108
public class ClasslikeListWithCustomIndexBehaviour : ClasslikeListBehaviour
109109
{
110110
It should_create_an_index_for_the_collection_mapping = () =>
111-
mapping.Collections.Single().As<ListMapping>().Index.ShouldNotBeNull();
111+
mapping.Collections.Single().Index.ShouldNotBeNull();
112112

113113
It should_create_a_single_column_for_the_index = () =>
114-
mapping.Collections.Single().As<ListMapping>().Index.Columns.Count().ShouldEqual(1);
114+
mapping.Collections.Single().Index.Columns.Count().ShouldEqual(1);
115115

116116
It should_use_specified_column_name_as_the_index_column_name = () =>
117-
mapping.Collections.Single().As<ListMapping>().Index.Columns.Single().Name.ShouldEqual("custom-column");
117+
mapping.Collections.Single().Index.Columns.Single().Name.ShouldEqual("custom-column");
118118

119119
It should_use_specified_type_as_the_index_type = () =>
120-
mapping.Collections.Single().As<ListMapping>().Index.As<IndexMapping>().Type.ShouldEqual(new TypeReference(typeof(IndexTarget)));
120+
mapping.Collections.Single().Index.As<IndexMapping>().Type.ShouldEqual(new TypeReference(typeof(IndexTarget)));
121121
}
122122

123123
public abstract class ClasslikeListBehaviour
@@ -126,7 +126,7 @@ public abstract class ClasslikeListBehaviour
126126
mapping.Collections.Count().ShouldEqual(1);
127127

128128
It should_add_a_list_to_the_collections_of_the_mapping = () =>
129-
mapping.Collections.Single().ShouldBeOfType<ListMapping>();
129+
mapping.Collections.Single().Collection.ShouldEqual(Collection.List);
130130

131131
It should_use_the_property_name_as_the_collection_name = () =>
132132
mapping.Collections.Single().Name.ShouldEqual("ListOfChildren");
@@ -150,7 +150,7 @@ public class ClasslikeArrayBehaviour
150150
mapping.Collections.Count().ShouldEqual(1);
151151

152152
It should_add_an_array_to_the_collections_of_the_mapping = () =>
153-
mapping.Collections.Single().ShouldBeOfType<ArrayMapping>();
153+
mapping.Collections.Single().Collection.ShouldEqual(Collection.Array);
154154

155155
It should_use_the_property_name_as_the_collection_name = () =>
156156
mapping.Collections.Single().Name.ShouldEqual("ArrayOfChildren");
@@ -165,13 +165,13 @@ public class ClasslikeArrayBehaviour
165165
mapping.Collections.Single().Key.Columns.Single().Name.ShouldEqual("EntityWithCollections_id");
166166

167167
It should_create_an_index_for_the_collection_mapping = () =>
168-
mapping.Collections.Single().As<ArrayMapping>().Index.ShouldNotBeNull();
168+
mapping.Collections.Single().Index.ShouldNotBeNull();
169169

170170
It should_create_a_single_column_for_the_index = () =>
171-
mapping.Collections.Single().As<ArrayMapping>().Index.Columns.Count().ShouldEqual(1);
171+
mapping.Collections.Single().Index.Columns.Count().ShouldEqual(1);
172172

173173
It should_use_specified_property_as_the_index_column_name = () =>
174-
mapping.Collections.Single().As<ArrayMapping>().Index.Columns.Single().Name.ShouldEqual("Position");
174+
mapping.Collections.Single().Index.Columns.Single().Name.ShouldEqual("Position");
175175

176176
protected static ClassMappingBase mapping;
177177
}
@@ -194,6 +194,6 @@ public class HasManyElementBehaviour
194194
It should_use_the_default_column_name_for_the_element = () =>
195195
collection.Element.Columns.Single().Name.ShouldEqual("value");
196196

197-
protected static ICollectionMapping collection;
197+
protected static CollectionMapping collection;
198198
}
199199
}

src/FluentNHibernate.Specs/FluentNHibernate.Specs.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@
9494
<Compile Include="Automapping\PrivateAutomapperSpecs.cs" />
9595
<Compile Include="Automapping\PrivateFieldsSpecs.cs" />
9696
<Compile Include="Conventions\ApplyFilterSpecs.cs" />
97+
<Compile Include="Conventions\CollectionTypeChangeSpecs.cs" />
9798
<Compile Include="Conventions\ConventionBuilderSpecs.cs" />
9899
<Compile Include="Conventions\Fixtures\Child.cs" />
100+
<Compile Include="Conventions\Fixtures\CollectionTarget.cs" />
101+
<Compile Include="Conventions\Fixtures\CollectionConvention.cs" />
99102
<Compile Include="Conventions\Fixtures\FilterClassConvention.cs" />
100103
<Compile Include="Conventions\Fixtures\FilterTarget.cs" />
101104
<Compile Include="Conventions\Fixtures\SetCollectionEntity.cs" />

src/FluentNHibernate.Specs/Visitors/BiDirectionalManyToManyPairingVisitorSpecs.cs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_two_m
3838
It should_set_other_side_for_the_supervisored_queues_collection = () =>
3939
supervised_queues_in_user.OtherSide.ShouldEqual(supervisors_in_queue);
4040

41-
static ICollectionMapping members_in_queue;
42-
static ICollectionMapping supervisors_in_queue;
43-
static ICollectionMapping membership_queues_in_user;
44-
static ICollectionMapping supervised_queues_in_user;
41+
static CollectionMapping members_in_queue;
42+
static CollectionMapping supervisors_in_queue;
43+
static CollectionMapping membership_queues_in_user;
44+
static CollectionMapping supervised_queues_in_user;
4545

4646
private class User
4747
{
@@ -84,10 +84,10 @@ public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_two_m
8484
eggs_in_queue.OtherSide.ShouldBeNull();
8585
};
8686

87-
static ICollectionMapping fish_in_queue;
88-
static ICollectionMapping chips_in_queue;
89-
static ICollectionMapping bacon_in_queue;
90-
static ICollectionMapping eggs_in_queue;
87+
static CollectionMapping fish_in_queue;
88+
static CollectionMapping chips_in_queue;
89+
static CollectionMapping bacon_in_queue;
90+
static CollectionMapping eggs_in_queue;
9191
static Exception ex;
9292

9393
private class User
@@ -131,10 +131,10 @@ public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_two_m
131131
eueues_in_user.OtherSide.ShouldBeNull();
132132
};
133133

134-
static ICollectionMapping dsers_in_queue;
135-
static ICollectionMapping fsers_in_queue;
136-
static ICollectionMapping wueues_in_user;
137-
static ICollectionMapping eueues_in_user;
134+
static CollectionMapping dsers_in_queue;
135+
static CollectionMapping fsers_in_queue;
136+
static CollectionMapping wueues_in_user;
137+
static CollectionMapping eueues_in_user;
138138
static Exception ex;
139139

140140
private class User
@@ -175,9 +175,9 @@ public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_a_man
175175
It shouldnt_link_the_orphaned_member_with_anything = () =>
176176
users2_in_queue.OtherSide.ShouldBeNull();
177177

178-
static ICollectionMapping users_in_queue;
179-
static ICollectionMapping users2_in_queue;
180-
static ICollectionMapping queues_in_user;
178+
static CollectionMapping users_in_queue;
179+
static CollectionMapping users2_in_queue;
180+
static CollectionMapping queues_in_user;
181181

182182
private class User
183183
{
@@ -211,8 +211,8 @@ public class when_the_bi_directional_many_to_many_visitor_is_asked_to_pair_two_c
211211
It should_set_other_side_for_the_queues_collection = () =>
212212
queues_in_user.OtherSide.ShouldEqual(users_in_queue);
213213

214-
static ICollectionMapping users_in_queue;
215-
static ICollectionMapping queues_in_user;
214+
static CollectionMapping users_in_queue;
215+
static CollectionMapping queues_in_user;
216216

217217
private class User
218218
{
@@ -235,20 +235,21 @@ public abstract class BiDirectionalManyToManyPairingVisitorSpec
235235
static RelationshipPairingVisitor visitor;
236236
protected static bool udf_was_called;
237237

238-
protected static ICollectionMapping collection<T>(Expression<Func<T, object>> expression)
238+
protected static CollectionMapping collection<T>(Expression<Func<T, object>> expression)
239239
{
240240
var member = expression.ToMember();
241241

242-
return new BagMapping
243-
{
244-
ContainingEntityType = typeof(T),
245-
Member = member,
246-
Relationship = new ManyToManyMapping(),
247-
ChildType = member.PropertyType.GetGenericArguments()[0]
248-
};
242+
var bag = CollectionMapping.Bag();
243+
244+
bag.ContainingEntityType = typeof(T);
245+
bag.Member = member;
246+
bag.Relationship = new ManyToManyMapping();
247+
bag.ChildType = member.PropertyType.GetGenericArguments()[0];
248+
249+
return bag;
249250
}
250251

251-
protected static void visit(params ICollectionMapping[] mappings)
252+
protected static void visit(params CollectionMapping[] mappings)
252253
{
253254
mappings.Each(visitor.Visit);
254255
visitor.Visit(new HibernateMapping[0]); // simulate end of visit

src/FluentNHibernate.Testing/AutoMapping/Steps/HasManyStepTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void ShouldMapListAsBag()
5959
mapper.Map(classMapping, typeof(PropertyTarget).GetProperty("List").ToMember());
6060

6161
classMapping.Collections
62-
.First().ShouldBeOfType(typeof(BagMapping));
62+
.First().Collection.ShouldEqual(Collection.Bag);
6363
}
6464

6565
[Test]
@@ -73,7 +73,7 @@ public void ShouldMapSetAsSet()
7373
mapper.Map(classMapping, typeof(PropertyTarget).GetProperty("Set").ToMember());
7474

7575
classMapping.Collections
76-
.First().ShouldBeOfType(typeof(SetMapping));
76+
.First().Collection.ShouldEqual(Collection.Set);
7777
}
7878

7979
[Test]
@@ -87,7 +87,7 @@ public void ShouldMapHashSetAsSet()
8787
mapper.Map(classMapping, typeof(PropertyTarget).GetProperty("HashSet").ToMember());
8888

8989
classMapping.Collections
90-
.First().ShouldBeOfType(typeof(SetMapping));
90+
.First().Collection.ShouldEqual(Collection.Set);
9191
}
9292

9393
protected void ShouldMap(Expression<System.Func<PropertyTarget, object>> property)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private void Convention(Action<IArrayInstance> convention)
172172
model.Conventions.Add(new ArrayConventionBuilder().Always(convention));
173173
}
174174

175-
private void VerifyModel(Action<ArrayMapping> modelVerification)
175+
private void VerifyModel(Action<CollectionMapping> modelVerification)
176176
{
177177
var classMap = new ClassMap<ExampleParentClass>();
178178
classMap.Id(x => x.Id);
@@ -187,7 +187,7 @@ private void VerifyModel(Action<ArrayMapping> modelVerification)
187187
.Classes.First()
188188
.Collections.First();
189189

190-
modelVerification((ArrayMapping)modelInstance);
190+
modelVerification(modelInstance);
191191
}
192192

193193
#endregion

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private void Convention(Action<ICollectionInstance> convention)
176176
model.Conventions.Add(new CollectionConventionBuilder().Always(convention));
177177
}
178178

179-
private void VerifyModel(Action<ICollectionMapping> modelVerification)
179+
private void VerifyModel(Action<CollectionMapping> modelVerification)
180180
{
181181
var classMap = new ClassMap<ExampleInheritedClass>();
182182
classMap.Id(x => x.Id);

0 commit comments

Comments
 (0)