Skip to content

Commit 6277f51

Browse files
committed
Add ability to set custom collection type as string in mapping by code
This allows to use TypeDefs that allow setting parameters for the collection.
1 parent 1aeed38 commit 6277f51

File tree

9 files changed

+154
-1
lines changed

9 files changed

+154
-1
lines changed

src/NHibernate.Test/MappingByCode/MappersTests/IdBagMapperTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void SetCollectionTypeByWrongTypeShouldThrow()
121121
{
122122
var hbm = new HbmIdbag();
123123
var mapper = new IdBagMapper(typeof(Animal), typeof(Animal), hbm);
124-
Assert.That(() => mapper.Type(null), Throws.TypeOf<ArgumentNullException>());
124+
Assert.That(() => mapper.Type(default(System.Type)), Throws.TypeOf<ArgumentNullException>());
125125
Assert.That(() => mapper.Type(typeof(object)), Throws.TypeOf<ArgumentOutOfRangeException>());
126126
}
127127

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using NHibernate.Cfg;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Mapping.ByCode;
4+
using NHibernate.Util;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.UserCollection.Parameterized
8+
{
9+
public class ParameterizedUserCollectionTypeByCodeFixture : TestCaseMappingByCode
10+
{
11+
protected override string MappingsAssembly => "NHibernate.Test";
12+
13+
protected override void AddMappings(Configuration configuration)
14+
{
15+
configuration.TypeDefinition<DefaultableListType>(
16+
c =>
17+
{
18+
c.Alias = "DefaultableList";
19+
c.Properties = new {@default = "Hello"};
20+
});
21+
22+
base.AddMappings(configuration);
23+
}
24+
25+
protected override HbmMapping GetMappings()
26+
{
27+
var mapper = new ModelMapper();
28+
29+
mapper.Class<Entity>(
30+
c =>
31+
{
32+
c.Id(e => e.Name, im => im.Access(Accessor.Field));
33+
c.List(
34+
e => e.Values,
35+
lpm =>
36+
{
37+
lpm.Fetch(CollectionFetchMode.Join);
38+
lpm.Table("ENT_VAL");
39+
lpm.Type("DefaultableList");
40+
lpm.Key(km => km.Column("ENT_ID"));
41+
lpm.Index(lim => lim.Column("POS"));
42+
},
43+
lm =>
44+
{
45+
lm.Element(em => em.Column("VAL"));
46+
});
47+
});
48+
49+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
50+
}
51+
52+
[Test]
53+
public void BasicOperation()
54+
{
55+
using (var s = OpenSession())
56+
using (var t = s.BeginTransaction())
57+
{
58+
var entity = new Entity("tester");
59+
entity.Values.Add("value-1");
60+
s.Persist(entity);
61+
t.Commit();
62+
}
63+
64+
using (var s = OpenSession())
65+
using (var t = s.BeginTransaction())
66+
{
67+
var entity = s.Get<Entity>("tester");
68+
Assert.That(NHibernateUtil.IsInitialized(entity.Values), Is.True);
69+
Assert.That(entity.Values.Count, Is.EqualTo(1));
70+
Assert.That(((IDefaultableList) entity.Values).DefaultValue, Is.EqualTo("Hello"));
71+
72+
s.Delete(entity);
73+
t.Commit();
74+
}
75+
}
76+
}
77+
}

src/NHibernate/Mapping/ByCode/ICollectionPropertiesMapper.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
22
using System.Linq.Expressions;
33
using System.Reflection;
4+
using NHibernate.Mapping.ByCode.Impl;
5+
using NHibernate.Mapping.ByCode.Impl.CustomizersImpl;
46
using NHibernate.Persister.Collection;
57
using NHibernate.UserTypes;
8+
using NHibernate.Util;
69

710
namespace NHibernate.Mapping.ByCode
811
{
@@ -54,4 +57,46 @@ public interface ICollectionPropertiesMapper<TEntity, TElement> : IEntityPropert
5457
void Fetch(CollectionFetchMode fetchMode);
5558
void Persister<TPersister>() where TPersister : ICollectionPersister;
5659
}
60+
61+
public static class CollectionPropertiesMapperExtensions
62+
{
63+
//6.0 TODO: Merge into ICollectionPropertiesMapper<TEntity, TElement>
64+
public static void Type<TEntity, TElement>(
65+
this ICollectionPropertiesMapper<TEntity, TElement> mapper,
66+
string collectionType)
67+
{
68+
ReflectHelper
69+
.CastOrThrow<CollectionPropertiesCustomizer<TEntity, TElement>>(mapper, "Type(string)")
70+
.Type(collectionType);
71+
}
72+
73+
//6.0 TODO: Merge into ICollectionPropertiesMapper
74+
public static void Type(
75+
this ICollectionPropertiesMapper mapper,
76+
string collectionType)
77+
{
78+
if (mapper == null) throw new ArgumentNullException(nameof(mapper));
79+
80+
switch (mapper)
81+
{
82+
case BagMapper bagMapper:
83+
bagMapper.Type(collectionType);
84+
break;
85+
case IdBagMapper idBagMapper:
86+
idBagMapper.Type(collectionType);
87+
break;
88+
case ListMapper listMapper:
89+
listMapper.Type(collectionType);
90+
break;
91+
case MapMapper mapMapper:
92+
mapMapper.Type(collectionType);
93+
break;
94+
case SetMapper setMapper:
95+
setMapper.Type(collectionType);
96+
break;
97+
default:
98+
throw new NotSupportedException($"{mapper.GetType().FullName} does not support Type(string)");
99+
}
100+
}
101+
}
57102
}

src/NHibernate/Mapping/ByCode/Impl/BagMapper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,15 @@ public void Type(System.Type collectionType)
136136
"The collection type should be an implementation of IUserCollectionType.({0})",
137137
collectionType));
138138
}
139+
139140
mapping.collectiontype = collectionType.AssemblyQualifiedName;
140141
}
141142

143+
public void Type(string collectionType)
144+
{
145+
mapping.collectiontype = collectionType ?? throw new ArgumentNullException(nameof(collectionType));
146+
}
147+
142148
public void Table(string tableName)
143149
{
144150
mapping.table = tableName;

src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/CollectionPropertiesCustomizer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public void Type(System.Type collectionType)
8888
CustomizersHolder.AddCustomizer(PropertyPath, (ICollectionPropertiesMapper x) => x.Type(collectionType));
8989
}
9090

91+
public void Type(string collectionType)
92+
{
93+
CustomizersHolder.AddCustomizer(PropertyPath, (ICollectionPropertiesMapper x) => x.Type(collectionType));
94+
}
95+
9196
public void Table(string tableName)
9297
{
9398
CustomizersHolder.AddCustomizer(PropertyPath, (ICollectionPropertiesMapper x) => x.Table(tableName));

src/NHibernate/Mapping/ByCode/Impl/IdBagMapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ public void Type(System.Type collectionType)
145145
mapping.collectiontype = collectionType.AssemblyQualifiedName;
146146
}
147147

148+
public void Type(string collectionType)
149+
{
150+
mapping.collectiontype = collectionType ?? throw new ArgumentNullException(nameof(collectionType));
151+
}
152+
148153
public void Table(string tableName)
149154
{
150155
mapping.table = tableName;

src/NHibernate/Mapping/ByCode/Impl/ListMapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ public void Type(System.Type collectionType)
143143
mapping.collectiontype = collectionType.AssemblyQualifiedName;
144144
}
145145

146+
public void Type(string collectionType)
147+
{
148+
mapping.collectiontype = collectionType ?? throw new ArgumentNullException(nameof(collectionType));
149+
}
150+
146151
public void Table(string tableName)
147152
{
148153
mapping.table = tableName;

src/NHibernate/Mapping/ByCode/Impl/MapMapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ public void Type(System.Type collectionType)
160160
mapping.collectiontype = collectionType.AssemblyQualifiedName;
161161
}
162162

163+
public void Type(string collectionType)
164+
{
165+
mapping.collectiontype = collectionType ?? throw new ArgumentNullException(nameof(collectionType));
166+
}
167+
163168
public void Table(string tableName)
164169
{
165170
mapping.table = tableName;

src/NHibernate/Mapping/ByCode/Impl/SetMapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ public void Type(System.Type collectionType)
145145
mapping.collectiontype = collectionType.AssemblyQualifiedName;
146146
}
147147

148+
public void Type(string collectionType)
149+
{
150+
mapping.collectiontype = collectionType ?? throw new ArgumentNullException(nameof(collectionType));
151+
}
152+
148153
public void Table(string tableName)
149154
{
150155
mapping.table = tableName;

0 commit comments

Comments
 (0)