Skip to content

Commit 535e978

Browse files
author
Rob Scaduto
committed
Fixed issue #261
Added zero length string prefix to the stack to prevent it from being reset after the first nested commit.
1 parent 30edae2 commit 535e978

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/FluentNHibernate.Specs/FluentInterface/ComponentMapSpecs.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,66 @@ private class Component
274274
public string Property { get; set; }
275275
}
276276
}
277+
278+
public class when_compiling_the_mappings_with_multiple_nested_component_mappings
279+
{
280+
Establish context = () => {
281+
var component_map = new ComponentMap<Component>();
282+
component_map.Component(x => x.NestedComponent1,
283+
c => c.Map(y => y.Property, "PROP1"));
284+
component_map.Component(x => x.NestedComponent2,
285+
c => c.Map(y => y.Property, "PROP2"));
286+
287+
var class_map = new ClassMap<Target>();
288+
class_map.Id(x => x.Id);
289+
class_map.Component(x => x.Component)
290+
.ColumnPrefix("A_");
291+
292+
persistence_model = new FluentNHibernate.PersistenceModel();
293+
persistence_model.Add(class_map);
294+
persistence_model.Add(component_map);
295+
};
296+
297+
Because of = () => {
298+
mappings = persistence_model.BuildMappings();
299+
class_mapping = mappings.SelectMany(x => x.Classes).First();
300+
};
301+
302+
It should_use_the_column_prefix_for_all_nested_component_columns = () => {
303+
var root_component = class_mapping.Components.First(x => x.Name == "Component");
304+
305+
root_component
306+
.Components.First(x => x.Name == "NestedComponent1")
307+
.Properties.SelectMany(x => x.Columns)
308+
.Select(x => x.Name)
309+
.ShouldContain("A_PROP1");
310+
311+
root_component
312+
.Components.First(x => x.Name == "NestedComponent2")
313+
.Properties.SelectMany(x => x.Columns)
314+
.Select(x => x.Name)
315+
.ShouldContain("A_PROP2");
316+
};
317+
318+
private static FluentNHibernate.PersistenceModel persistence_model;
319+
private static IEnumerable<HibernateMapping> mappings;
320+
private static ClassMapping class_mapping;
321+
322+
private class Target
323+
{
324+
public int Id { get; set; }
325+
public Component Component { get; set; }
326+
}
327+
328+
private class Component
329+
{
330+
public NestedComponent NestedComponent1 { get; set; }
331+
public NestedComponent NestedComponent2 { get; set; }
332+
}
333+
334+
private class NestedComponent
335+
{
336+
public string Property { get; set; }
337+
}
338+
}
277339
}

src/FluentNHibernate/Visitors/ComponentColumnPrefixVisitor.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using FluentNHibernate.MappingModel;
@@ -18,7 +19,7 @@ public override void Visit(IComponentMapping mapping)
1819

1920
public override void ProcessColumn(ColumnMapping columnMapping)
2021
{
21-
if (prefixes.Any())
22+
if (prefixes.Any(x => x != String.Empty))
2223
columnMapping.Set(x => x.Name, Layer.UserSupplied, GetPrefix() + columnMapping.Name);
2324
}
2425

@@ -29,8 +30,11 @@ private string GetPrefix()
2930

3031
private void StorePrefix(IComponentMapping mapping)
3132
{
32-
if (mapping.HasColumnPrefix)
33-
prefixes.Push(mapping.ColumnPrefix.Replace("{property}", mapping.Member.Name));
33+
var prefix = mapping.HasColumnPrefix
34+
? mapping.ColumnPrefix.Replace("{property}", mapping.Member.Name)
35+
: String.Empty;
36+
37+
prefixes.Push(prefix);
3438
}
3539

3640
private void ResetPrefix()

0 commit comments

Comments
 (0)