Skip to content

Commit 3100cf5

Browse files
committed
Made ComponentMap compatible with automapping
1 parent 5b2868b commit 3100cf5

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

src/FluentNHibernate.Specs/Automapping/AutomappingSpecs.Components.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using FluentNHibernate.Automapping;
5+
using FluentNHibernate.Mapping;
56
using FluentNHibernate.MappingModel.ClassBased;
67
using FluentNHibernate.Specs.Automapping.Fixtures;
78
using FluentNHibernate.Utils;
@@ -42,6 +43,51 @@ public override bool IsComponent(Type type)
4243
}
4344
}
4445

46+
public class when_the_automapper_is_told_to_map_an_entity_and_a_component_that_has_an_associated_ComponentMap
47+
{
48+
Establish context = () =>
49+
mapper = AutoMap.Source(
50+
new StubTypeSource(typeof(Entity), typeof(Component)),
51+
new TestConfiguration())
52+
.AddMappingsFromSource(new StubTypeSource(typeof(CompMap)));
53+
54+
Because of = () =>
55+
mappings = mapper.BuildMappings()
56+
.SelectMany(x => x.Classes);
57+
58+
It should_map_the_entity = () =>
59+
mappings.ShouldContain(x => x.Type == typeof(Entity));
60+
61+
It should_not_map_the_component_as_an_entity = () =>
62+
mappings.ShouldNotContain(x => x.Type == typeof(Component));
63+
64+
It should_map_the_component_as_a_component_of_the_entity = () =>
65+
mappings.Single(x => x.Type == typeof(Entity))
66+
.Components.ShouldContain(x => x.Type == typeof(Component));
67+
68+
It should_use_the_component_map_for_mapping = () =>
69+
mappings.Single().Components.Single().Access.ShouldEqual("none");
70+
71+
static AutoPersistenceModel mapper;
72+
static IEnumerable<ClassMapping> mappings;
73+
74+
class TestConfiguration : DefaultAutomappingConfiguration
75+
{
76+
public override bool IsComponent(Type type)
77+
{
78+
return type == typeof(Component);
79+
}
80+
}
81+
82+
class CompMap : ComponentMap<Component>
83+
{
84+
public CompMap()
85+
{
86+
Access.None();
87+
}
88+
}
89+
}
90+
4591
public class when_the_automapper_maps_nested_comonents
4692
{
4793
Establish context = () =>

src/FluentNHibernate/Automapping/AutoPersistenceModel.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ public AutoPersistenceModel(IAutomappingConfiguration cfg)
4343
componentResolvers.Add(new AutomappedComponentResolver(autoMapper, cfg));
4444
}
4545

46+
public AutoPersistenceModel AddMappingsFromAssemblyOf<T>()
47+
{
48+
return AddMappingsFromAssembly(typeof(T).Assembly);
49+
}
50+
51+
public new AutoPersistenceModel AddMappingsFromAssembly(Assembly assembly)
52+
{
53+
AddMappingsFromSource(new AssemblyTypeSource(assembly));
54+
return this;
55+
}
56+
57+
public new AutoPersistenceModel AddMappingsFromSource(ITypeSource source)
58+
{
59+
base.AddMappingsFromSource(source);
60+
return this;
61+
}
62+
4663
/// <summary>
4764
/// Specify alterations to be used with this AutoPersisteceModel
4865
/// </summary>

src/FluentNHibernate/Cfg/AutoMappingsContainer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public AutoMappingsContainer ExportTo(TextWriter textWriter)
7171
/// Applies any added mappings to the NHibernate Configuration
7272
/// </summary>
7373
/// <param name="cfg">NHibernate Configuration instance</param>
74-
internal void Apply(Configuration cfg)
74+
/// <param name="model"></param>
75+
internal void Apply(Configuration cfg, PersistenceModel model)
7576
{
7677
foreach (var mapping in mappings)
7778
{
@@ -81,6 +82,7 @@ internal void Apply(Configuration cfg)
8182
if (exportTextWriter != null)
8283
mapping.WriteMappingsTo(exportTextWriter);
8384

85+
mapping.ImportProviders(model);
8486
mapping.Configure(cfg);
8587
}
8688
}

src/FluentNHibernate/Cfg/MappingConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void Apply(Configuration cfg)
8383

8484
HbmMappings.Apply(cfg);
8585
FluentMappings.Apply(model);
86-
AutoMappings.Apply(cfg);
86+
AutoMappings.Apply(cfg, model);
8787

8888
model.Configure(cfg);
8989
}

src/FluentNHibernate/PersistenceModel.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,27 @@ public bool ValidationEnabled
309309
get { return validationVisitor.Enabled; }
310310
set { validationVisitor.Enabled = value; }
311311
}
312+
313+
internal void ImportProviders(PersistenceModel model)
314+
{
315+
model.classProviders.Each(x =>
316+
{
317+
if (!classProviders.Contains(x))
318+
classProviders.Add(x);
319+
});
320+
321+
model.subclassProviders.Each(x =>
322+
{
323+
if (!subclassProviders.Contains(x))
324+
subclassProviders.Add(x);
325+
});
326+
327+
model.componentProviders.Each(x =>
328+
{
329+
if (!componentProviders.Contains(x))
330+
componentProviders.Add(x);
331+
});
332+
}
312333
}
313334

314335
public interface IMappingProvider

0 commit comments

Comments
 (0)