@@ -23,7 +23,7 @@ namespace NHibernate.Transform
23
23
/// )
24
24
/// .SetResultTransformer( new AliasToBeanResultTransformer(typeof(StudentDTO)))
25
25
/// .List();
26
- ///
26
+ ///
27
27
/// StudentDTO dto = (StudentDTO)resultWithAliasedBean[0];
28
28
/// </code>
29
29
/// </example>
@@ -35,23 +35,21 @@ namespace NHibernate.Transform
35
35
[ Serializable ]
36
36
public class AliasToBeanResultTransformer : AliasedTupleSubsetResultTransformer , IEquatable < AliasToBeanResultTransformer >
37
37
{
38
- private readonly System . Type _resultClass ;
39
- private readonly ConstructorInfo _beanConstructor ;
40
38
private readonly Dictionary < string , NamedMember < FieldInfo > > _fieldsByNameCaseSensitive ;
41
39
private readonly Dictionary < string , NamedMember < FieldInfo > > _fieldsByNameCaseInsensitive ;
42
40
private readonly Dictionary < string , NamedMember < PropertyInfo > > _propertiesByNameCaseSensitive ;
43
41
private readonly Dictionary < string , NamedMember < PropertyInfo > > _propertiesByNameCaseInsensitive ;
44
42
45
43
public AliasToBeanResultTransformer ( System . Type resultClass )
46
44
{
47
- _resultClass = resultClass ?? throw new ArgumentNullException ( "resultClass" ) ;
45
+ ResultClass = resultClass ?? throw new ArgumentNullException ( "resultClass" ) ;
48
46
49
47
const BindingFlags bindingFlags = BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ;
50
- _beanConstructor = resultClass . GetConstructor ( bindingFlags , null , System . Type . EmptyTypes , null ) ;
48
+ BeanConstructor = resultClass . GetConstructor ( bindingFlags , null , System . Type . EmptyTypes , null ) ;
51
49
52
- // if resultClass is a ValueType (struct), GetConstructor will return null...
50
+ // if resultClass is a ValueType (struct), GetConstructor will return null...
53
51
// in that case, we'll use Activator.CreateInstance instead of the ConstructorInfo to create instances
54
- if ( _beanConstructor == null && resultClass . IsClass )
52
+ if ( BeanConstructor == null && resultClass . IsClass )
55
53
{
56
54
throw new ArgumentException (
57
55
"The target class of a AliasToBeanResultTransformer need a parameter-less constructor" ,
@@ -68,6 +66,9 @@ public AliasToBeanResultTransformer(System.Type resultClass)
68
66
_propertiesByNameCaseInsensitive = GetMapByName ( properties , StringComparer . OrdinalIgnoreCase ) ;
69
67
}
70
68
69
+ protected System . Type ResultClass { get ; }
70
+ protected ConstructorInfo BeanConstructor { get ; }
71
+
71
72
public override bool IsTransformedValueATupleElement ( String [ ] aliases , int tupleLength )
72
73
{
73
74
return false ;
@@ -83,9 +84,9 @@ public override object TransformTuple(object[] tuple, String[] aliases)
83
84
84
85
try
85
86
{
86
- result = _resultClass . IsClass
87
- ? _beanConstructor . Invoke ( null )
88
- : Activator . CreateInstance ( _resultClass , true ) ;
87
+ result = ResultClass . IsClass
88
+ ? BeanConstructor . Invoke ( null )
89
+ : Activator . CreateInstance ( ResultClass , true ) ;
89
90
90
91
for ( int i = 0 ; i < aliases . Length ; i ++ )
91
92
{
@@ -94,11 +95,11 @@ public override object TransformTuple(object[] tuple, String[] aliases)
94
95
}
95
96
catch ( InstantiationException e )
96
97
{
97
- throw new HibernateException ( "Could not instantiate result class: " + _resultClass . FullName , e ) ;
98
+ throw new HibernateException ( "Could not instantiate result class: " + ResultClass . FullName , e ) ;
98
99
}
99
100
catch ( MethodAccessException e )
100
101
{
101
- throw new HibernateException ( "Could not instantiate result class: " + _resultClass . FullName , e ) ;
102
+ throw new HibernateException ( "Could not instantiate result class: " + ResultClass . FullName , e ) ;
102
103
}
103
104
104
105
return result ;
@@ -111,7 +112,7 @@ public override IList TransformList(IList collection)
111
112
112
113
protected virtual void OnPropertyNotFound ( string propertyName )
113
114
{
114
- throw new PropertyNotFoundException ( _resultClass . GetType ( ) , propertyName , "setter" ) ;
115
+ throw new PropertyNotFoundException ( ResultClass . GetType ( ) , propertyName , "setter" ) ;
115
116
}
116
117
117
118
#region Setter resolution
@@ -126,7 +127,7 @@ protected virtual void OnPropertyNotFound(string propertyName)
126
127
/// <exception cref="PropertyNotFoundException">Thrown if no matching property or field can be found.</exception>
127
128
/// <exception cref="AmbiguousMatchException">Thrown if many matching properties or fields are found, having the
128
129
/// same visibility and inheritance depth.</exception>
129
- private void SetProperty ( string alias , object value , object resultObj )
130
+ protected void SetProperty ( string alias , object value , object resultObj )
130
131
{
131
132
if ( alias == null )
132
133
// Grouping properties in criteria are selected without alias, just ignore them.
@@ -187,7 +188,7 @@ private void CheckMember<T>(NamedMember<T> member, string alias) where T : Membe
187
188
private void FetchFieldsAndProperties ( List < RankedMember < FieldInfo > > fields , List < RankedMember < PropertyInfo > > properties )
188
189
{
189
190
const BindingFlags bindingFlags = BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . DeclaredOnly ;
190
- var currentType = _resultClass ;
191
+ var currentType = ResultClass ;
191
192
var rank = 1 ;
192
193
// For grasping private members, we need to manually walk the hierarchy.
193
194
while ( currentType != null && currentType != typeof ( object ) )
@@ -315,12 +316,12 @@ public bool Equals(AliasToBeanResultTransformer other)
315
316
{
316
317
return true ;
317
318
}
318
- return Equals ( other . _resultClass , _resultClass ) ;
319
+ return Equals ( other . ResultClass , ResultClass ) ;
319
320
}
320
321
321
322
public override int GetHashCode ( )
322
323
{
323
- return _resultClass . GetHashCode ( ) ;
324
+ return ResultClass . GetHashCode ( ) ;
324
325
}
325
326
326
327
#endregion
0 commit comments