Skip to content

Commit 79c51b7

Browse files
committed
Fix for issue #104: FNH should mark ReferenceComponent as mapped when it is overridden in IAutoMappingOverride
1 parent 9bdc9d3 commit 79c51b7

File tree

5 files changed

+74
-24
lines changed

5 files changed

+74
-24
lines changed

src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace FluentNHibernate.Testing.AutoMapping.Apm
1111
{
1212
[TestFixture]
13-
public partial class AutoPersistenceModelTests : BaseAutoPersistenceTests
13+
public partial class AutoPersistenceModelTests
1414
{
1515
[Test]
1616
public void CanSearchForOpenGenericTypes()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using FluentNHibernate.Automapping;
3+
using NUnit.Framework;
4+
5+
namespace FluentNHibernate.Testing.AutoMapping.Overrides
6+
{
7+
public class ReferenceComponentOverrides
8+
{
9+
[Test]
10+
public void should_be_able_to_build_mapping()
11+
{
12+
var source = new StubTypeSource(typeof(IncomingDocument), typeof(DocumentNumber));
13+
14+
var persistenceModel = AutoMap.Source(source, new AutomappingConfiguration())
15+
.Override<IncomingDocument>(m => m.Component(x => x.IncomingNumber)
16+
.ColumnPrefix("INCOMING_"));
17+
18+
Assert.DoesNotThrow(() => persistenceModel.BuildMappings());
19+
}
20+
21+
class AutomappingConfiguration : DefaultAutomappingConfiguration
22+
{
23+
public override bool IsComponent(Type type)
24+
{
25+
return type == typeof(DocumentNumber);
26+
}
27+
28+
public override bool ShouldMap(Type type)
29+
{
30+
return type == typeof(IncomingDocument);
31+
}
32+
}
33+
34+
class DocumentNumber
35+
{}
36+
37+
class IncomingDocument
38+
{
39+
public virtual int Id { get; set; }
40+
public virtual DocumentNumber IncomingNumber { get; set; }
41+
}
42+
}
43+
}

src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<Compile Include="AutoMapping\Apm\AlterationTests.cs" />
102102
<Compile Include="AutoMapping\Apm\ConcreteBaseClassTests.cs" />
103103
<Compile Include="AutoMapping\Apm\Conventions\VersionConventionTests.cs" />
104+
<Compile Include="AutoMapping\Overrides\ReferenceComponentOverrides.cs" />
104105
<Compile Include="AutoMapping\Overrides\AutoMappingOverrideAlterationTests.cs" />
105106
<Compile Include="AutoMapping\Apm\CacheOverrideTests.cs" />
106107
<Compile Include="AutoMapping\Apm\Conventions\HasManyConventionTests.cs" />

src/FluentNHibernate/Automapping/AutoMapping.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ void IAutoClasslike.AlterModel(ClassMappingBase mapping)
4040
{
4141
mapping.MergeAttributes(attributes.Clone());
4242

43-
if (mapping is ClassMapping)
43+
var classMapping = mapping as ClassMapping;
44+
if (classMapping != null)
4445
{
45-
var classMapping = (ClassMapping)mapping;
46-
4746
if (providers.Id != null)
4847
classMapping.Set(x => x.Id, Layer.Defaults, providers.Id.GetIdentityMapping());
4948

@@ -129,8 +128,8 @@ IPropertyIgnorer IPropertyIgnorer.IgnoreProperties(Func<Member, bool> predicate)
129128
return this;
130129
}
131130

132-
public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyColumn, Action<AutoJoinedSubClassPart<TSubclass>> action)
133-
where TSubclass : T
131+
public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyColumn, Action<AutoJoinedSubClassPart<TSubclass>> action)
132+
where TSubclass : T
134133
{
135134
var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(typeof(TSubclass));
136135
var joinedclass = (AutoJoinedSubClassPart<TSubclass>)Activator.CreateInstance(genericType, keyColumn);
@@ -140,7 +139,7 @@ public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyCol
140139

141140
providers.Subclasses[typeof(TSubclass)] = joinedclass;
142141

143-
return joinedclass;
142+
return joinedclass;
144143
}
145144

146145
public IAutoClasslike JoinedSubClass(Type type, string keyColumn)
@@ -155,31 +154,31 @@ public IAutoClasslike JoinedSubClass(Type type, string keyColumn)
155154
}
156155

157156
public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyColumn)
158-
where TSubclass : T
159-
{
160-
return JoinedSubClass<TSubclass>(keyColumn, null);
161-
}
157+
where TSubclass : T
158+
{
159+
return JoinedSubClass<TSubclass>(keyColumn, null);
160+
}
162161

163-
public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue, Action<AutoSubClassPart<TSubclass>> action)
164-
where TSubclass : T
162+
public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue, Action<AutoSubClassPart<TSubclass>> action)
163+
where TSubclass : T
165164
{
166165
var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass));
167166
var subclass = (AutoSubClassPart<TSubclass>)Activator.CreateInstance(genericType, null, discriminatorValue);
168-
167+
169168
if (action != null)
170169
action(subclass);
171170

172171
// remove any mappings for the same type, then re-add
173172
providers.Subclasses[typeof(TSubclass)] = subclass;
174173

175-
return subclass;
174+
return subclass;
176175
}
177176

178177
public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue)
179-
where TSubclass : T
180-
{
181-
return SubClass<TSubclass>(discriminatorValue, null);
182-
}
178+
where TSubclass : T
179+
{
180+
return SubClass<TSubclass>(discriminatorValue, null);
181+
}
183182

184183
public IAutoClasslike SubClass(Type type, string discriminatorValue)
185184
{
@@ -191,10 +190,10 @@ public IAutoClasslike SubClass(Type type, string discriminatorValue)
191190

192191
return (IAutoClasslike)subclass;
193192
}
194-
193+
195194
// hide the base one D:
196-
private new void Join(string table, Action<JoinPart<T>> action)
197-
{ }
195+
new void Join(string table, Action<JoinPart<T>> action)
196+
{}
198197

199198
public void Join(string table, Action<AutoJoinPart<T>> action)
200199
{

src/FluentNHibernate/Mapping/ClasslikeMapBase.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,14 @@ DynamicComponentPart<IDictionary> DynamicComponent(Member member, Action<Dynamic
238238
/// <returns>Component reference builder</returns>
239239
public virtual ReferenceComponentPart<TComponent> Component<TComponent>(Expression<Func<T, TComponent>> member)
240240
{
241-
var part = new ReferenceComponentPart<TComponent>(member.ToMember(), typeof(T));
241+
return Component<TComponent>(member.ToMember());
242+
}
243+
244+
ReferenceComponentPart<TComponent> Component<TComponent>(Member member)
245+
{
246+
OnMemberMapped(member);
247+
248+
var part = new ReferenceComponentPart<TComponent>(member, typeof(T));
242249

243250
providers.Components.Add(part);
244251

@@ -287,7 +294,7 @@ ComponentPart<TComponent> Component<TComponent>(Member member, Action<ComponentP
287294

288295
var part = new ComponentPart<TComponent>(typeof(T), member);
289296

290-
action(part);
297+
if (action != null) action(part);
291298

292299
providers.Components.Add(part);
293300

0 commit comments

Comments
 (0)