forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAliasToBeanResultTransformer.cs
115 lines (104 loc) · 3.53 KB
/
AliasToBeanResultTransformer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections;
using System.Reflection;
using NHibernate.Properties;
namespace NHibernate.Transform
{
/// <summary>
/// Result transformer that allows to transform a result to
/// a user specified class which will be populated via setter
/// methods or fields matching the alias names.
/// </summary>
/// <example>
/// <code>
/// IList resultWithAliasedBean = s.CreateCriteria(typeof(Enrollment))
/// .CreateAlias("Student", "st")
/// .CreateAlias("Course", "co")
/// .SetProjection( Projections.ProjectionList()
/// .Add( Projections.Property("co.Description"), "CourseDescription" )
/// )
/// .SetResultTransformer( new AliasToBeanResultTransformer(typeof(StudentDTO)) )
/// .List();
///
/// StudentDTO dto = (StudentDTO)resultWithAliasedBean[0];
/// </code>
/// </example>
[Serializable]
public class AliasToBeanResultTransformer : IResultTransformer
{
private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
private readonly System.Type resultClass;
private ISetter[] setters;
private readonly IPropertyAccessor propertyAccessor;
private readonly ConstructorInfo constructor;
public AliasToBeanResultTransformer(System.Type resultClass)
{
if (resultClass == null)
{
throw new ArgumentNullException("resultClass");
}
this.resultClass = resultClass;
constructor = resultClass.GetConstructor(flags, null, System.Type.EmptyTypes, null);
// if resultClass is a ValueType (struct), GetConstructor will return null...
// in that case, we'll use Activator.CreateInstance instead of the ConstructorInfo to create instances
if (constructor == null && resultClass.IsClass)
{
throw new ArgumentException("The target class of a AliasToBeanResultTransformer need a parameter-less constructor",
"resultClass");
}
propertyAccessor =
new ChainedPropertyAccessor(new[]
{
PropertyAccessorFactory.GetPropertyAccessor(null),
PropertyAccessorFactory.GetPropertyAccessor("field")
});
}
public object TransformTuple(object[] tuple, String[] aliases)
{
if (aliases == null)
{
throw new ArgumentNullException("aliases");
}
object result;
try
{
if (setters == null)
{
setters = new ISetter[aliases.Length];
for (int i = 0; i < aliases.Length; i++)
{
string alias = aliases[i];
if (alias != null)
{
setters[i] = propertyAccessor.GetSetter(resultClass, alias);
}
}
}
// if resultClass is not a class but a value type, we need to use Activator.CreateInstance
result = resultClass.IsClass
? constructor.Invoke(null)
: Cfg.Environment.BytecodeProvider.ObjectsFactory.CreateInstance(resultClass, true);
for (int i = 0; i < aliases.Length; i++)
{
if (setters[i] != null)
{
setters[i].Set(result, tuple[i]);
}
}
}
catch (InstantiationException e)
{
throw new HibernateException("Could not instantiate result class: " + resultClass.FullName, e);
}
catch (MethodAccessException e)
{
throw new HibernateException("Could not instantiate result class: " + resultClass.FullName, e);
}
return result;
}
public IList TransformList(IList collection)
{
return collection;
}
}
}