Skip to content

Commit 1b0efb7

Browse files
Support ICompositeUserType for ByCode Element, KeyProperty, MapKey
Fix nhibernate#1821
1 parent 385a861 commit 1b0efb7

File tree

6 files changed

+95
-9
lines changed

6 files changed

+95
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NHibernate.Cfg.MappingSchema;
2+
using NHibernate.Mapping.ByCode.Impl;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.MappingByCode.MappersTests
6+
{
7+
[TestFixture]
8+
public class ElementMapperTest
9+
{
10+
[Test]
11+
public void WhenSetTypeByICompositeUserTypeThenSetTypeName()
12+
{
13+
var mapping = new HbmElement();
14+
var mapper = new ElementMapper(typeof(object), mapping);
15+
16+
Assert.That(() => mapper.Type<MyCompoType>(), Throws.Nothing);
17+
Assert.That(mapping.Type.name, Does.Contain(nameof(MyCompoType)));
18+
Assert.That(mapping.type, Is.Null);
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NHibernate.Cfg.MappingSchema;
2+
using NHibernate.Mapping.ByCode.Impl;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.MappingByCode.MappersTests
6+
{
7+
[TestFixture]
8+
public class KeyPropertyMapperTest
9+
{
10+
private class MyClass
11+
{
12+
public string ReadOnly { get { return ""; } }
13+
}
14+
15+
[Test]
16+
public void WhenSetTypeByICompositeUserTypeThenSetTypeName()
17+
{
18+
var member = typeof(MyClass).GetProperty("ReadOnly");
19+
var mapping = new HbmKeyProperty();
20+
var mapper = new KeyPropertyMapper(member, mapping);
21+
22+
Assert.That(() => mapper.Type<MyCompoType>(), Throws.Nothing);
23+
Assert.That(mapping.Type.name, Does.Contain(nameof(MyCompoType)));
24+
Assert.That(mapping.type, Is.Null);
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NHibernate.Cfg.MappingSchema;
2+
using NHibernate.Mapping.ByCode.Impl;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.MappingByCode.MappersTests
6+
{
7+
[TestFixture]
8+
public class MapKeyMapperTest
9+
{
10+
[Test]
11+
public void WhenSetTypeByICompositeUserTypeThenSetTypeName()
12+
{
13+
var mapping = new HbmMapKey();
14+
var mapper = new MapKeyMapper(mapping);
15+
16+
Assert.That(() => mapper.Type<MyCompoType>(), Throws.Nothing);
17+
Assert.That(mapping.Type.name, Does.Contain(nameof(MyCompoType)));
18+
}
19+
}
20+
}

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,16 @@ public void Type(System.Type persistentType, object parameters)
133133
{
134134
throw new ArgumentNullException("persistentType");
135135
}
136-
if (!typeof (IUserType).IsAssignableFrom(persistentType) && !typeof (IType).IsAssignableFrom(persistentType))
136+
137+
if (!typeof(IUserType).IsAssignableFrom(persistentType) &&
138+
!typeof(IType).IsAssignableFrom(persistentType) &&
139+
!typeof(ICompositeUserType).IsAssignableFrom(persistentType))
137140
{
138-
throw new ArgumentOutOfRangeException("persistentType", "Expected type implementing IUserType or IType.");
141+
throw new ArgumentOutOfRangeException(
142+
nameof(persistentType),
143+
"Expected type implementing IUserType, ICompositeUserType or IType.");
139144
}
145+
140146
if (parameters != null)
141147
{
142148
elementMapping.type1 = null;
@@ -206,4 +212,4 @@ public void Formula(string formula)
206212

207213
#endregion
208214
}
209-
}
215+
}

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ public void Type(System.Type persistentType, object parameters)
8686
{
8787
throw new ArgumentNullException("persistentType");
8888
}
89-
if (!typeof (IUserType).IsAssignableFrom(persistentType) && !typeof (IType).IsAssignableFrom(persistentType))
89+
90+
if (!typeof(IUserType).IsAssignableFrom(persistentType) &&
91+
!typeof(IType).IsAssignableFrom(persistentType) &&
92+
!typeof(ICompositeUserType).IsAssignableFrom(persistentType))
9093
{
91-
throw new ArgumentOutOfRangeException("persistentType", "Expected type implementing IUserType or IType.");
94+
throw new ArgumentOutOfRangeException(
95+
nameof(persistentType),
96+
"Expected type implementing IUserType, ICompositeUserType or IType.");
9297
}
98+
9399
if (parameters != null)
94100
{
95101
propertyMapping.type1 = null;
@@ -235,4 +241,4 @@ private void ResetColumnPlainValues()
235241

236242
#endregion
237243
}
238-
}
244+
}

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,16 @@ public void Type(System.Type persistentType)
9292
{
9393
throw new ArgumentNullException("persistentType");
9494
}
95-
if (!typeof (IUserType).IsAssignableFrom(persistentType) && !typeof (IType).IsAssignableFrom(persistentType))
95+
96+
if (!typeof(IUserType).IsAssignableFrom(persistentType) &&
97+
!typeof(IType).IsAssignableFrom(persistentType) &&
98+
!typeof(ICompositeUserType).IsAssignableFrom(persistentType))
9699
{
97-
throw new ArgumentOutOfRangeException("persistentType", "Expected type implementing IUserType or IType.");
100+
throw new ArgumentOutOfRangeException(
101+
nameof(persistentType),
102+
"Expected type implementing IUserType, ICompositeUserType or IType.");
98103
}
104+
99105
hbmMapKey.type = persistentType.AssemblyQualifiedName;
100106
}
101107

@@ -140,4 +146,4 @@ private bool ColumnTagIsRequired(HbmColumn hbm)
140146
|| hbm.check != null;
141147
}
142148
}
143-
}
149+
}

0 commit comments

Comments
 (0)