Skip to content

Commit dfae76d

Browse files
committed
Moved UnwrapProxyProperties into UnwrapProxyPropertiesMetadata
1 parent 1302415 commit dfae76d

8 files changed

+152
-28
lines changed

src/NHibernate/Bytecode/IBytecodeEnhancementMetadata.cs

+8
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@ public interface IBytecodeEnhancementMetadata
2626
/// </summary>
2727
bool EnhancedForLazyLoading { get; }
2828

29+
/// <summary>
30+
/// Has the information about all lazy properties
31+
/// </summary>
2932
LazyPropertiesMetadata LazyPropertiesMetadata { get; }
3033

34+
/// <summary>
35+
/// Has the information about all properties mapped as proxy="no-proxy"
36+
/// </summary>
37+
UnwrapProxyPropertiesMetadata UnwrapProxyPropertiesMetadata { get; }
38+
3139
/// <summary>
3240
/// Build and inject an interceptor instance into the enhanced entity.
3341
/// </summary>

src/NHibernate/Bytecode/LazyPropertiesMetadata.cs

+4-15
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,40 @@ public class LazyPropertiesMetadata
1818
{
1919
public static LazyPropertiesMetadata NonEnhanced(string entityName)
2020
{
21-
return new LazyPropertiesMetadata(entityName, null, null);
21+
return new LazyPropertiesMetadata(entityName, null);
2222
}
2323

2424
public static LazyPropertiesMetadata From(
2525
string entityName,
26-
IEnumerable<LazyPropertyDescriptor> lazyPropertyDescriptors,
27-
ISet<string> unwrapProxyPropertyNames)
26+
IEnumerable<LazyPropertyDescriptor> lazyPropertyDescriptors)
2827
{
2928
// TODO: port lazy fetch groups
3029
return new LazyPropertiesMetadata(
3130
entityName,
32-
lazyPropertyDescriptors.ToDictionary(o => o.Name),
33-
unwrapProxyPropertyNames);
31+
lazyPropertyDescriptors.ToDictionary(o => o.Name));
3432
}
3533

3634
private readonly IDictionary<string, LazyPropertyDescriptor> _lazyPropertyDescriptors;
3735

3836
public LazyPropertiesMetadata(
3937
string entityName,
40-
IDictionary<string, LazyPropertyDescriptor> lazyPropertyDescriptors,
41-
ISet<string> unwrapProxyPropertyNames)
38+
IDictionary<string, LazyPropertyDescriptor> lazyPropertyDescriptors)
4239
{
4340
EntityName = entityName;
4441
_lazyPropertyDescriptors = lazyPropertyDescriptors;
4542
HasLazyProperties = _lazyPropertyDescriptors?.Count > 0;
4643
LazyPropertyNames = HasLazyProperties
4744
? new ReadOnlySet<string>(new HashSet<string>(_lazyPropertyDescriptors.Keys))
4845
: CollectionHelper.EmptySet<string>();
49-
HasUnwrapProxyProperties = unwrapProxyPropertyNames?.Count > 0;
50-
UnwrapProxyPropertyNames = HasUnwrapProxyProperties
51-
? new ReadOnlySet<string>(unwrapProxyPropertyNames)
52-
: CollectionHelper.EmptySet<string>();
5346
// TODO: port lazy fetch groups
5447
}
5548

5649
public string EntityName { get; }
5750

5851
public bool HasLazyProperties { get; }
5952

60-
public bool HasUnwrapProxyProperties { get; }
61-
6253
public ISet<string> LazyPropertyNames { get; }
6354

64-
public ISet<string> UnwrapProxyPropertyNames { get; }
65-
6655
public IEnumerable<LazyPropertyDescriptor> LazyPropertyDescriptors =>
6756
_lazyPropertyDescriptors?.Values ?? Enumerable.Empty<LazyPropertyDescriptor>();
6857
}

src/NHibernate/Bytecode/LazyPropertyDescriptor.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public class LazyPropertyDescriptor
1616
{
1717
public static LazyPropertyDescriptor From(
1818
Mapping.Property property,
19-
int attributeIndex,
19+
int propertyIndex,
2020
int lazyIndex)
2121
{
2222
// TODO: port lazy fetch groups
2323

2424
return new LazyPropertyDescriptor(
25-
attributeIndex,
25+
propertyIndex,
2626
lazyIndex,
2727
property.Name,
2828
property.Type
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Iesi.Collections.Generic;
5+
using NHibernate.Util;
6+
7+
namespace NHibernate.Bytecode
8+
{
9+
/// <summary>
10+
/// Information about all properties mapped as proxy="no-proxy" for an entity
11+
/// </summary>
12+
public class UnwrapProxyPropertiesMetadata
13+
{
14+
public static UnwrapProxyPropertiesMetadata NonEnhanced(string entityName)
15+
{
16+
return new UnwrapProxyPropertiesMetadata(entityName, null);
17+
}
18+
19+
public static UnwrapProxyPropertiesMetadata From(
20+
string entityName,
21+
IEnumerable<UnwrapProxyPropertyDescriptor> unwrapProxyPropertyDescriptors)
22+
{
23+
return new UnwrapProxyPropertiesMetadata(
24+
entityName,
25+
unwrapProxyPropertyDescriptors.ToDictionary(o => o.Name));
26+
}
27+
28+
private readonly IDictionary<string, UnwrapProxyPropertyDescriptor> _unwrapProxyPropertyDescriptors;
29+
30+
public UnwrapProxyPropertiesMetadata(
31+
string entityName,
32+
IDictionary<string, UnwrapProxyPropertyDescriptor> unwrapProxyPropertyDescriptors)
33+
{
34+
EntityName = entityName;
35+
_unwrapProxyPropertyDescriptors = unwrapProxyPropertyDescriptors;
36+
HasUnwrapProxyProperties = unwrapProxyPropertyDescriptors?.Count > 0;
37+
UnwrapProxyPropertyNames = HasUnwrapProxyProperties
38+
? new ReadOnlySet<string>(new HashSet<string>(unwrapProxyPropertyDescriptors.Keys))
39+
: CollectionHelper.EmptySet<string>();
40+
}
41+
42+
public string EntityName { get; }
43+
44+
public bool HasUnwrapProxyProperties { get; }
45+
46+
public ISet<string> UnwrapProxyPropertyNames { get; }
47+
48+
public IEnumerable<UnwrapProxyPropertyDescriptor> UnwrapProxyPropertyDescriptors =>
49+
_unwrapProxyPropertyDescriptors?.Values ?? Enumerable.Empty<UnwrapProxyPropertyDescriptor>();
50+
51+
public int GetUnwrapProxyPropertyIndex(string propertyName)
52+
{
53+
if (!_unwrapProxyPropertyDescriptors.TryGetValue(propertyName, out var descriptor))
54+
{
55+
throw new InvalidOperationException(
56+
$"Property {propertyName} is not mapped as proxy=\"no-proxy\" on entity {EntityName}");
57+
}
58+
59+
return descriptor.PropertyIndex;
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NHibernate.Type;
7+
8+
namespace NHibernate.Bytecode
9+
{
10+
/// <summary>
11+
/// Descriptor for a property which is mapped as proxy="no-proxy"
12+
/// </summary>
13+
public class UnwrapProxyPropertyDescriptor
14+
{
15+
public static UnwrapProxyPropertyDescriptor From(
16+
Mapping.Property property,
17+
int propertyIndex)
18+
{
19+
return new UnwrapProxyPropertyDescriptor(
20+
propertyIndex,
21+
property.Name,
22+
property.Type
23+
);
24+
}
25+
26+
private UnwrapProxyPropertyDescriptor(
27+
int propertyIndex,
28+
string name,
29+
IType type)
30+
{
31+
PropertyIndex = propertyIndex;
32+
Name = name;
33+
Type = type;
34+
}
35+
36+
/// <summary>
37+
/// Access to the index of the property in terms of its position in the entity persister
38+
/// </summary>
39+
public int PropertyIndex { get; }
40+
41+
/// <summary>
42+
/// Access to the name of the property
43+
/// </summary>
44+
public string Name { get; }
45+
46+
/// <summary>
47+
/// Access to the property's type
48+
/// </summary>
49+
public IType Type { get; set; }
50+
}
51+
}

src/NHibernate/Tuple/Entity/BytecodeEnhancementMetadataNonPocoImpl.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public BytecodeEnhancementMetadataNonPocoImpl(string entityName)
2121
{
2222
EntityName = entityName;
2323
LazyPropertiesMetadata = LazyPropertiesMetadata.NonEnhanced(entityName);
24-
_errorMessage= $"Entity [{entityName}] is non-poco, and therefore not instrumented";
24+
UnwrapProxyPropertiesMetadata = UnwrapProxyPropertiesMetadata.NonEnhanced(entityName);
25+
_errorMessage = $"Entity [{entityName}] is non-poco, and therefore not instrumented";
2526
}
2627

2728
/// <inheritdoc />
@@ -33,6 +34,9 @@ public BytecodeEnhancementMetadataNonPocoImpl(string entityName)
3334
/// <inheritdoc />
3435
public LazyPropertiesMetadata LazyPropertiesMetadata { get; }
3536

37+
/// <inheritdoc />
38+
public UnwrapProxyPropertiesMetadata UnwrapProxyPropertiesMetadata { get; }
39+
3640
/// <inheritdoc />
3741
public IFieldInterceptor InjectInterceptor(object entity, bool lazyPropertiesAreUnfetched, ISessionImplementor session)
3842
{

src/NHibernate/Tuple/Entity/BytecodeEnhancementMetadataPocoImpl.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public class BytecodeEnhancementMetadataPocoImpl : IBytecodeEnhancementMetadata
1818
public static IBytecodeEnhancementMetadata From(
1919
PersistentClass persistentClass,
2020
ICollection<LazyPropertyDescriptor> lazyPropertyDescriptors,
21-
ISet<string> unwrapProxyPropertyNames)
21+
ICollection<UnwrapProxyPropertyDescriptor> unwrapProxyPropertyDescriptors)
2222
{
2323
var mappedClass = persistentClass.MappedClass;
24-
var enhancedForLazyLoading = lazyPropertyDescriptors?.Count > 0 || unwrapProxyPropertyNames?.Count > 0;
24+
var enhancedForLazyLoading = lazyPropertyDescriptors?.Count > 0 || unwrapProxyPropertyDescriptors?.Count > 0;
2525

2626
// We have to check all subclasses if any of them is enhanced for lazy loading as the
2727
// root class will be enhanced when any of the subclasses is enhanced, even if it
@@ -40,15 +40,20 @@ public static IBytecodeEnhancementMetadata From(
4040
}
4141
}
4242

43-
var lazyAttributesMetadata = enhancedForLazyLoading
44-
? LazyPropertiesMetadata.From(persistentClass.EntityName, lazyPropertyDescriptors, unwrapProxyPropertyNames)
43+
var lazyPropertiesMetadata = enhancedForLazyLoading
44+
? LazyPropertiesMetadata.From(persistentClass.EntityName, lazyPropertyDescriptors)
4545
: LazyPropertiesMetadata.NonEnhanced(persistentClass.EntityName);
4646

47+
var unwrapProxyPropertiesMetadata = enhancedForLazyLoading
48+
? UnwrapProxyPropertiesMetadata.From(persistentClass.EntityName, unwrapProxyPropertyDescriptors)
49+
: UnwrapProxyPropertiesMetadata.NonEnhanced(persistentClass.EntityName);
50+
4751
return new BytecodeEnhancementMetadataPocoImpl(
4852
persistentClass.EntityName,
4953
mappedClass,
5054
enhancedForLazyLoading,
51-
lazyAttributesMetadata
55+
lazyPropertiesMetadata,
56+
unwrapProxyPropertiesMetadata
5257
);
5358
}
5459

@@ -92,12 +97,14 @@ public BytecodeEnhancementMetadataPocoImpl(
9297
string entityName,
9398
System.Type entityType,
9499
bool enhancedForLazyLoading,
95-
LazyPropertiesMetadata lazyPropertiesMetadata)
100+
LazyPropertiesMetadata lazyPropertiesMetadata,
101+
UnwrapProxyPropertiesMetadata unwrapProxyPropertiesMetadata)
96102
{
97103
EntityName = entityName;
98104
_entityType = entityType;
99105
EnhancedForLazyLoading = enhancedForLazyLoading;
100106
LazyPropertiesMetadata = lazyPropertiesMetadata;
107+
UnwrapProxyPropertiesMetadata = unwrapProxyPropertiesMetadata;
101108
}
102109

103110
/// <inheritdoc />
@@ -109,6 +116,9 @@ public BytecodeEnhancementMetadataPocoImpl(
109116
/// <inheritdoc />
110117
public LazyPropertiesMetadata LazyPropertiesMetadata { get; }
111118

119+
/// <inheritdoc />
120+
public UnwrapProxyPropertiesMetadata UnwrapProxyPropertiesMetadata { get; }
121+
112122
/// <inheritdoc />
113123
public IFieldInterceptor InjectInterceptor(object entity, bool lazyPropertiesAreUnfetched, ISessionImplementor session)
114124
{
@@ -133,7 +143,7 @@ public IFieldInterceptor InjectInterceptor(object entity, bool lazyPropertiesAre
133143
LazyPropertiesMetadata.HasLazyProperties && lazyPropertiesAreUnfetched
134144
? new HashSet<string>(LazyPropertiesMetadata.LazyPropertyNames)
135145
: null,
136-
LazyPropertiesMetadata.UnwrapProxyPropertyNames,
146+
UnwrapProxyPropertiesMetadata.UnwrapProxyPropertyNames,
137147
EntityName,
138148
_entityType);
139149
fieldInterceptorAccessor.FieldInterceptor = fieldInterceptorImpl;

src/NHibernate/Tuple/Entity/EntityMetamodel.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public EntityMetamodel(PersistentClass persistentClass, ISessionFactoryImplement
104104
properties = new StandardProperty[propertySpan];
105105
List<int> naturalIdNumbers = new List<int>();
106106
var lazyPropertyDescriptors = new List<LazyPropertyDescriptor>();
107-
var unwrapProxyPropertyNames = new HashSet<string>();
107+
var unwrapProxyPropertyDescriptors = new List<UnwrapProxyPropertyDescriptor>();
108108

109109
propertyNames = new string[propertySpan];
110110
propertyTypes = new IType[propertySpan];
@@ -189,7 +189,7 @@ public EntityMetamodel(PersistentClass persistentClass, ISessionFactoryImplement
189189
if (isUnwrapProxy)
190190
{
191191
hasUnwrapProxyForProperties = true;
192-
unwrapProxyPropertyNames.Add(prop.Name);
192+
unwrapProxyPropertyDescriptors.Add(UnwrapProxyPropertyDescriptor.From(prop, i));
193193
}
194194

195195
propertyLaziness[i] = islazyProperty;
@@ -323,7 +323,7 @@ public EntityMetamodel(PersistentClass persistentClass, ISessionFactoryImplement
323323
if (persistentClass.HasPocoRepresentation)
324324
{
325325
BytecodeEnhancementMetadata = BytecodeEnhancementMetadataPocoImpl
326-
.From(persistentClass, lazyPropertyDescriptors, unwrapProxyPropertyNames);
326+
.From(persistentClass, lazyPropertyDescriptors, unwrapProxyPropertyDescriptors);
327327
}
328328
else
329329
{

0 commit comments

Comments
 (0)