Skip to content

Commit 705a055

Browse files
committed
Merge pull request #160 from chester89/componentFieldPrefix
Added ability to use a prefix when a component is mapped using backing field
2 parents 4c7832c + 0411f6b commit 705a055

File tree

8 files changed

+77
-12
lines changed

8 files changed

+77
-12
lines changed

src/FluentNHibernate.1.2.dotCover

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Configuration>
2+
<SnapshotDialog>
3+
<InitialDirectory>D:\coding\fluent-nhibernate\src</InitialDirectory>
4+
</SnapshotDialog>
5+
<CoverageFilters>
6+
<IncludeFilters>
7+
<Filter ModuleMask="*" ClassMask="*" FunctionMask="*" />
8+
</IncludeFilters>
9+
<ExcludeFilters />
10+
</CoverageFilters>
11+
</Configuration>

src/FluentNHibernate.Testing/Visitors/ComponentColumnPrefixVisitorSpecs.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,57 @@ public void should_prefix_property_columns()
203203
}
204204
}
205205

206+
[TestFixture]
207+
public class when_the_component_column_prefix_visitor_processes_a_component_with_a_prefix_using_field_alias : ComponentColumnPrefixVisitorSpec
208+
{
209+
PersistenceModel model;
210+
IEnumerable<HibernateMapping> mappings;
211+
ClassMapping targetMapping;
212+
const string columnPrefix = "{property}";
213+
214+
public override void establish_context()
215+
{
216+
model = new PersistenceModel();
217+
218+
var componentMap = new ComponentMap<FieldComponent>();
219+
componentMap.Map(x => x.X);
220+
componentMap.Map(x => x.Y);
221+
222+
model.Add(componentMap);
223+
224+
var classMapping = new ClassMap<Root>();
225+
classMapping.Id(r => r.Id);
226+
classMapping.Component(Reveal.Member<Root, FieldComponent>("component"), cpt => cpt.Access.Field().ColumnPrefix(columnPrefix));
227+
model.Add(classMapping);
228+
}
229+
230+
public override void because()
231+
{
232+
mappings = model.BuildMappings().ToList();
233+
targetMapping = mappings.SelectMany(x => x.Classes).FirstOrDefault(x => x.Type == typeof(Root));
234+
}
235+
236+
[Test]
237+
public void should_prefix_field_columns()
238+
{
239+
targetMapping.Components.Single()
240+
.Properties.SelectMany(x => x.Columns)
241+
.Each(c => c.Name.ShouldStartWith("component"));
242+
}
243+
}
244+
245+
class Root
246+
{
247+
FieldComponent component;
248+
public int Id { get; set; }
249+
}
250+
251+
class FieldComponent
252+
{
253+
public string X { get; set; }
254+
public int? Y { get; set; }
255+
}
256+
206257
public abstract class ComponentColumnPrefixVisitorSpec : Specification
207258
{
208259
protected AnyMapping any_with_column(string column)

src/FluentNHibernate/Mapping/ComponentMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override ReferenceComponentPart<TComponent> Component<TComponent>(Express
5454

5555
protected override ComponentMapping CreateComponentMappingRoot(AttributeStore store)
5656
{
57-
return new ExternalComponentMapping(ComponentType.Component, attributes.Clone());
57+
return new ExternalComponentMapping(ComponentType.Component, attributes.Clone(), member);
5858
}
5959

6060
ExternalComponentMapping IExternalComponentMappingProvider.GetComponentMapping()

src/FluentNHibernate/Mapping/ComponentPart.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ private ComponentPart(Type entity, Member property, AttributeStore attributes)
2222
this.entity = entity;
2323
}
2424

25+
/// <summary>
26+
/// Sets the prefix for every column defined within the component. To refer to the name of a member that exposes
27+
/// the component use {property}
28+
/// </summary>
29+
/// <param name="prefix"></param>
2530
public void ColumnPrefix(string prefix)
2631
{
2732
columnPrefix = prefix;
@@ -44,7 +49,7 @@ IComponentMapping IComponentMappingProvider.GetComponentMapping()
4449

4550
protected override ComponentMapping CreateComponentMappingRoot(AttributeStore store)
4651
{
47-
var componentMappingRoot = new ComponentMapping(ComponentType.Component, store)
52+
var componentMappingRoot = new ComponentMapping(ComponentType.Component, store, member)
4853
{
4954
ContainingEntityType = entity
5055
};

src/FluentNHibernate/Mapping/ComponentPartBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace FluentNHibernate.Mapping
1010
public abstract class ComponentPartBase<TEntity, TBuilder> : ClasslikeMapBase<TEntity>
1111
where TBuilder : ComponentPartBase<TEntity, TBuilder>
1212
{
13-
readonly Member member;
13+
protected readonly Member member;
1414
readonly MappingProviderStore providers;
1515
readonly AccessStrategyBuilder<TBuilder> access;
1616
readonly AttributeStore attributes;

src/FluentNHibernate/Mapping/DynamicComponentPart.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private DynamicComponentPart(Type entity, Member member, AttributeStore underlyi
2323

2424
protected override ComponentMapping CreateComponentMappingRoot(AttributeStore store)
2525
{
26-
return new ComponentMapping(ComponentType.DynamicComponent, store)
26+
return new ComponentMapping(ComponentType.DynamicComponent, store, member)
2727
{
2828
ContainingEntityType = entity
2929
};

src/FluentNHibernate/MappingModel/ClassBased/ComponentMapping.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ public class ComponentMapping : ComponentMappingBase, IComponentMapping
1111
public ComponentType ComponentType { get; set; }
1212
readonly AttributeStore attributes;
1313

14-
public ComponentMapping(ComponentType componentType)
15-
: this(componentType, new AttributeStore())
14+
public ComponentMapping(ComponentType componentType): this(componentType, new AttributeStore(), null)
1615
{}
1716

18-
public ComponentMapping(ComponentType componentType, AttributeStore attributes)
19-
: base(attributes)
17+
public ComponentMapping(ComponentType componentType, AttributeStore attributes, Member member): base(attributes)
2018
{
2119
ComponentType = componentType;
2220
this.attributes = attributes;
21+
Member = member;
2322
}
2423

2524
public override void AcceptVisitor(IMappingModelVisitor visitor)

src/FluentNHibernate/MappingModel/ClassBased/ExternalComponentMapping.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ namespace FluentNHibernate.MappingModel.ClassBased
88
[Serializable]
99
public class ExternalComponentMapping : ComponentMapping
1010
{
11-
public ExternalComponentMapping(ComponentType componentType)
12-
: this(componentType, new AttributeStore())
11+
public ExternalComponentMapping(ComponentType componentType): this(componentType, new AttributeStore(), null)
1312
{}
1413

15-
public ExternalComponentMapping(ComponentType componentType, AttributeStore underlyingStore)
16-
: base(componentType, underlyingStore)
14+
public ExternalComponentMapping(ComponentType componentType, AttributeStore underlyingStore, Member member)
15+
: base(componentType, underlyingStore, member)
1716
{}
1817
}
1918
}

0 commit comments

Comments
 (0)