Skip to content

Commit

Permalink
Fixed issue #261
Browse files Browse the repository at this point in the history
Added zero length string prefix to the stack to prevent it from being
reset after the first nested commit.
  • Loading branch information
Rob Scaduto committed Jan 29, 2015
1 parent 30edae2 commit 535e978
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
62 changes: 62 additions & 0 deletions src/FluentNHibernate.Specs/FluentInterface/ComponentMapSpecs.cs
Expand Up @@ -274,4 +274,66 @@ private class Component
public string Property { get; set; }
}
}

public class when_compiling_the_mappings_with_multiple_nested_component_mappings
{
Establish context = () => {
var component_map = new ComponentMap<Component>();
component_map.Component(x => x.NestedComponent1,
c => c.Map(y => y.Property, "PROP1"));
component_map.Component(x => x.NestedComponent2,
c => c.Map(y => y.Property, "PROP2"));
var class_map = new ClassMap<Target>();
class_map.Id(x => x.Id);
class_map.Component(x => x.Component)
.ColumnPrefix("A_");
persistence_model = new FluentNHibernate.PersistenceModel();
persistence_model.Add(class_map);
persistence_model.Add(component_map);
};

Because of = () => {
mappings = persistence_model.BuildMappings();
class_mapping = mappings.SelectMany(x => x.Classes).First();
};

It should_use_the_column_prefix_for_all_nested_component_columns = () => {
var root_component = class_mapping.Components.First(x => x.Name == "Component");
root_component
.Components.First(x => x.Name == "NestedComponent1")
.Properties.SelectMany(x => x.Columns)
.Select(x => x.Name)
.ShouldContain("A_PROP1");
root_component
.Components.First(x => x.Name == "NestedComponent2")
.Properties.SelectMany(x => x.Columns)
.Select(x => x.Name)
.ShouldContain("A_PROP2");
};

private static FluentNHibernate.PersistenceModel persistence_model;
private static IEnumerable<HibernateMapping> mappings;
private static ClassMapping class_mapping;

private class Target
{
public int Id { get; set; }
public Component Component { get; set; }
}

private class Component
{
public NestedComponent NestedComponent1 { get; set; }
public NestedComponent NestedComponent2 { get; set; }
}

private class NestedComponent
{
public string Property { get; set; }
}
}
}
10 changes: 7 additions & 3 deletions src/FluentNHibernate/Visitors/ComponentColumnPrefixVisitor.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentNHibernate.MappingModel;
Expand All @@ -18,7 +19,7 @@ public override void Visit(IComponentMapping mapping)

public override void ProcessColumn(ColumnMapping columnMapping)
{
if (prefixes.Any())
if (prefixes.Any(x => x != String.Empty))
columnMapping.Set(x => x.Name, Layer.UserSupplied, GetPrefix() + columnMapping.Name);
}

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

private void StorePrefix(IComponentMapping mapping)
{
if (mapping.HasColumnPrefix)
prefixes.Push(mapping.ColumnPrefix.Replace("{property}", mapping.Member.Name));
var prefix = mapping.HasColumnPrefix
? mapping.ColumnPrefix.Replace("{property}", mapping.Member.Name)
: String.Empty;

prefixes.Push(prefix);
}

private void ResetPrefix()
Expand Down

0 comments on commit 535e978

Please sign in to comment.