Skip to content

Commit ae546f6

Browse files
di97mnipaulbatum
authored andcommitted
Making it possible to automap nested classes
1 parent 35609a0 commit ae546f6

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using FluentNHibernate.Automapping;
5+
using FluentNHibernate.MappingModel.ClassBased;
6+
using FluentNHibernate.Specs.Automapping.Fixtures;
7+
using Machine.Specifications;
8+
9+
namespace FluentNHibernate.Specs.Automapping
10+
{
11+
public class when_the_automapper_is_told_to_map_an_entity_with_a_nested_entity
12+
{
13+
Establish context = () =>
14+
mapper = AutoMap.Source(
15+
new StubTypeSource(typeof(Order), typeof(Order.OrderLine)));
16+
17+
Because of = () =>
18+
mappings = mapper.BuildMappings()
19+
.SelectMany(x => x.Classes);
20+
21+
It should_map_the_entity = () =>
22+
mappings.ShouldContain(x => x.Type == typeof(Order));
23+
24+
It should_not_automap_the_nested_entity_per_default = () =>
25+
mappings.ShouldNotContain(x => x.Type == typeof(Order.OrderLine));
26+
27+
static AutoPersistenceModel mapper;
28+
static IEnumerable<ClassMapping> mappings;
29+
}
30+
31+
public class when_changing_the_behaviour_of_automapping_configuration_to_also_map_nested_entities
32+
{
33+
Establish context = () =>
34+
mapper = AutoMap.Source(
35+
new StubTypeSource(typeof(Order), typeof(Order.OrderLine)), new TestConfiguration());
36+
37+
Because of = () =>
38+
mappings = mapper.BuildMappings()
39+
.SelectMany(x => x.Classes);
40+
41+
It should_map_the_entity = () =>
42+
mappings.ShouldContain(x => x.Type == typeof(Order));
43+
44+
It should_not_automap_the_nested_entity_per_default = () =>
45+
mappings.ShouldContain(x => x.Type == typeof(Order.OrderLine));
46+
47+
static AutoPersistenceModel mapper;
48+
static IEnumerable<ClassMapping> mappings;
49+
50+
class TestConfiguration : DefaultAutomappingConfiguration
51+
{
52+
public override bool ShouldMap(Type type)
53+
{
54+
return base.ShouldMap(type) || type.IsNested;
55+
}
56+
}
57+
}
58+
public class Order
59+
{
60+
public int Id { get; set; }
61+
public class OrderLine
62+
{
63+
public int Id { get; set; }
64+
}
65+
}
66+
67+
}

src/FluentNHibernate.Specs/FluentNHibernate.Specs.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@
143143
<Compile Include="PersistenceModel\SubclassSpecs.cs" />
144144
<Compile Include="SerializableSpecs.cs" />
145145
</ItemGroup>
146+
<ItemGroup>
147+
<Compile Include="Automapping\AutomappingSpecs.NestedClasses.cs" />
148+
</ItemGroup>
146149
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
147150
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
148151
Other similar extension points exist, see Microsoft.Common.targets.

src/FluentNHibernate/Automapping/AutoPersistenceModel.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private void CompileMappings()
140140

141141
foreach (var type in mappingTypes)
142142
{
143-
if (!type.Type.IsClass || !IsNotInnerClass(type)) continue;
143+
if (!type.Type.IsClass) continue;
144144
if (type.IsMapped) continue;
145145

146146
AddMapping(type.Type);
@@ -170,11 +170,6 @@ public override void Configure(NHibernate.Cfg.Configuration configuration)
170170
base.Configure(configuration);
171171
}
172172

173-
static bool IsNotInnerClass(AutoMapType type)
174-
{
175-
return type.Type.ReflectedType == null;
176-
}
177-
178173
private void AddMapping(Type type)
179174
{
180175
Type typeToMap = GetTypeToMap(type);

src/FluentNHibernate/Automapping/DefaultAutomappingConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public virtual bool ShouldMap(Member member)
1818
public virtual bool ShouldMap(Type type)
1919
{
2020
return !type.ClosesInterface(typeof(IAutoMappingOverride<>)) &&
21-
!type.HasInterface(typeof(IMappingProvider));
21+
!type.HasInterface(typeof(IMappingProvider)) &&
22+
!type.IsNested;
2223
}
2324

2425
public virtual bool IsId(Member member)

0 commit comments

Comments
 (0)