Skip to content

Commit c546ebe

Browse files
committed
Merge pull request #92 from icambron/composite_key_types
added ability to set a custom type on composite keys
2 parents e390f4f + 3cd8858 commit c546ebe

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/FluentNHibernate.Testing/AutoMapping/Overrides/CompositeIdOverrides.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using System;
12
using System.Linq;
23
using FluentNHibernate.Automapping;
4+
using FluentNHibernate.Mapping;
5+
using FluentNHibernate.MappingModel.Identity;
36
using NUnit.Framework;
47

58
namespace FluentNHibernate.Testing.AutoMapping.Overrides
@@ -38,6 +41,60 @@ public void ShouldntMapReferencesUsedInCompositeId()
3841

3942
classMapping.References.ShouldNotContain(x => x.Name == "Child");
4043
}
44+
45+
[Test]
46+
public void ShouldMapEnumIdAsAString()
47+
{
48+
var model = AutoMap.Source(new StubTypeSource(new[] {typeof(CompositeIdEntityWithEnum)}))
49+
.Override<CompositeIdEntityWithEnum>(o =>
50+
o.CompositeId()
51+
.KeyProperty(x => x.FirstId)
52+
.KeyProperty(x => x.SecondId));
53+
54+
55+
VerifyMapping(model, idMap =>
56+
{
57+
var firstKey = idMap.Keys.First();
58+
59+
//this part is dumb because i'm asserting a specific implementation. i don't have any other way
60+
//of getting to the key type though
61+
firstKey.ShouldBeOfType(typeof(KeyPropertyMapping));
62+
var keyProp = (KeyPropertyMapping)firstKey;
63+
keyProp.Type.GetUnderlyingSystemType().ShouldEqual(typeof(GenericEnumMapper<>).MakeGenericType(typeof(SomeEnum)));
64+
});
65+
}
66+
67+
[Test]
68+
public void ShouldMapEnumIdAsOverridenType()
69+
{
70+
var model = AutoMap.Source(new StubTypeSource(new[] { typeof(CompositeIdEntityWithEnum) }))
71+
.Override<CompositeIdEntityWithEnum>(o =>
72+
o.CompositeId()
73+
.KeyProperty(x => x.FirstId).CustomType<SomeEnum>()
74+
.KeyProperty(x => x.SecondId).CustomType<SomeEnum>());
75+
76+
77+
VerifyMapping(model, idMap =>
78+
{
79+
var firstKey = idMap.Keys.First();
80+
firstKey.ShouldBeOfType(typeof(KeyPropertyMapping));
81+
var firstKeyProp = (KeyPropertyMapping)firstKey;
82+
firstKeyProp.Type.GetUnderlyingSystemType().ShouldEqual(typeof(SomeEnum));
83+
});
84+
}
85+
86+
private void VerifyMapping(AutoPersistenceModel model, Action<CompositeIdMapping> verifier)
87+
{
88+
var idMapping = model.BuildMappings()
89+
.First()
90+
.Classes
91+
.First()
92+
.Id
93+
;
94+
95+
idMapping.ShouldBeOfType(typeof(CompositeIdMapping));
96+
verifier((CompositeIdMapping)idMapping);
97+
}
4198
}
4299

43100
internal class CompositeIdEntity
@@ -46,4 +103,16 @@ internal class CompositeIdEntity
46103
public int SecondId { get; set; }
47104
public Child Child { get; set; }
48105
}
106+
107+
internal class CompositeIdEntityWithEnum
108+
{
109+
public SomeEnum FirstId { get; set; }
110+
public SomeEnum SecondId { get; set; }
111+
}
112+
113+
internal enum SomeEnum
114+
{
115+
PossiblityOne,
116+
PossibilityTwo
117+
}
49118
}

src/FluentNHibernate/Mapping/CompositeIdentityPart.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45
using System.Linq.Expressions;
56
using FluentNHibernate.Mapping.Providers;
67
using FluentNHibernate.MappingModel;
@@ -161,6 +162,16 @@ protected virtual CompositeIdentityPart<T> KeyReference(Member property, IEnumer
161162
return this;
162163
}
163164

165+
public virtual CompositeIdentityPart<T> CustomType<CType>()
166+
{
167+
var key = keys.Where(x => x is KeyPropertyMapping).Cast<KeyPropertyMapping>().LastOrDefault();
168+
if (key != null)
169+
{
170+
key.Set(x => x.Type, Layer.Defaults, new TypeReference(typeof(CType)));
171+
}
172+
return this;
173+
}
174+
164175
/// <summary>
165176
/// Set the access and naming strategy for this identity.
166177
/// </summary>

0 commit comments

Comments
 (0)