Skip to content

Commit b983da8

Browse files
committed
Add ability to set access for nested composite element
Fixes #370 +semver:breaking
1 parent 34214f6 commit b983da8

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

src/FluentNHibernate.Testing/DomainModel/Mapping/ComponentElementPartTester.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ public void CanSetParentReferenceAccess()
3737
.HasAttribute("access", "backfield");
3838
}
3939

40+
[Test]
41+
public void CanSetNestedCompositeElementAccessAccess()
42+
{
43+
var tester = new MappingTester<PropertyTarget>()
44+
.ForMapping(m =>
45+
m.HasMany(x => x.Components)
46+
.Component(c =>
47+
{
48+
c.Map(x => x.Name);
49+
c.Component(target => target.MyParent, nested => nested.Access.BackingField());
50+
}));
51+
tester
52+
.Element("class/bag/composite-element/nested-composite-element").Exists()
53+
.HasAttribute("name", "MyParent")
54+
.HasAttribute("access", "backfield");
55+
}
56+
4057
[Test]
4158
public void CanCreateNestedCompositeElement()
4259
{

src/FluentNHibernate/Mapping/CompositeElementPart.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ namespace FluentNHibernate.Mapping
1616
public class CompositeElementPart<T> : ICompositeElementMappingProvider, INestedCompositeElementMappingProvider
1717
{
1818
readonly Type entity;
19-
readonly Member member;
19+
private protected readonly Member member;
2020
readonly List<IPropertyMappingProvider> properties = new List<IPropertyMappingProvider>();
2121
readonly List<IManyToOneMappingProvider> references = new List<IManyToOneMappingProvider>();
2222
readonly List<INestedCompositeElementMappingProvider> components = new List<INestedCompositeElementMappingProvider>();
23-
readonly AttributeStore attributes = new AttributeStore();
23+
private protected readonly AttributeStore attributes = new AttributeStore();
2424

2525
public CompositeElementPart(Type entity)
2626
{
@@ -164,9 +164,9 @@ void ParentReference(Member property, Access access)
164164
/// });
165165
/// });
166166
/// </example>
167-
public void Component<TChild>(Expression<Func<T, TChild>> property, Action<CompositeElementPart<TChild>> nestedCompositeElementAction)
167+
public void Component<TChild>(Expression<Func<T, TChild>> property, Action<NestedCompositeElementPart<TChild>> nestedCompositeElementAction)
168168
{
169-
var nestedCompositeElement = new CompositeElementPart<TChild>(entity, property.ToMember());
169+
var nestedCompositeElement = new NestedCompositeElementPart<TChild>(entity, property.ToMember());
170170

171171
nestedCompositeElementAction(nestedCompositeElement);
172172

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using FluentNHibernate.MappingModel;
3+
4+
namespace FluentNHibernate.Mapping
5+
{
6+
public class NestedCompositeElementPart<T> : CompositeElementPart<T>
7+
{
8+
public NestedCompositeElementPart(Type entity, Member member)
9+
: base(entity, member)
10+
{
11+
Access = new AccessStrategyBuilder<NestedCompositeElementPart<T>>(this, value => attributes.Set("Access", Layer.UserSupplied, value));
12+
13+
SetDefaultAccess();
14+
}
15+
16+
public AccessStrategyBuilder<NestedCompositeElementPart<T>> Access { get; }
17+
18+
void SetDefaultAccess()
19+
{
20+
var resolvedAccess = MemberAccessResolver.Resolve(member);
21+
22+
if (resolvedAccess == Mapping.Access.Property || resolvedAccess == Mapping.Access.Unset)
23+
return; // property is the default so we don't need to specify it
24+
25+
attributes.Set("Access", Layer.Defaults, resolvedAccess.ToString());
26+
}
27+
}
28+
}

src/FluentNHibernate/MappingModel/Collections/NestedCompositeElementMapping.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public string Name
2424
get { return attributes.GetOrDefault<string>("Name"); }
2525
}
2626

27+
public string Access
28+
{
29+
get { return attributes.GetOrDefault<string>("Access"); }
30+
}
31+
2732
public void Set<T>(Expression<Func<NestedCompositeElementMapping, T>> expression, int layer, T value)
2833
{
2934
Set(expression.ToMember().Name, layer, value);

src/FluentNHibernate/MappingModel/Output/XmlCompositeElementWriter.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ public override void ProcessCompositeElement(CompositeElementMapping mapping)
3232
if (mapping.IsSpecified("Class"))
3333
element.WithAtt("class", mapping.Class);
3434

35-
if (mapping is NestedCompositeElementMapping)
36-
element.WithAtt("name", ((NestedCompositeElementMapping)mapping).Name);
35+
if (mapping is NestedCompositeElementMapping nested)
36+
{
37+
element.WithAtt("name", nested.Name);
38+
39+
if (mapping.IsSpecified("Access"))
40+
element.WithAtt("access", nested.Access);
41+
}
3742
}
3843

3944
public override void Visit(CompositeElementMapping compositeElementMapping)

0 commit comments

Comments
 (0)