forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParameterHelper.cs
139 lines (126 loc) · 5.11 KB
/
ParameterHelper.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections;
using System.Linq;
using NHibernate.Engine;
using NHibernate.Proxy;
using NHibernate.Type;
namespace NHibernate.Util
{
internal static class ParameterHelper
{
/// <summary>
/// Guesses the <see cref="IType"/> from the <c>param</c>'s value.
/// </summary>
/// <param name="param">The object to guess the <see cref="IType"/> of.</param>
/// <param name="sessionFactory">The session factory to search for entity persister.</param>
/// <param name="isCollection">Whether <paramref name="param"/> is a collection.</param>
/// <returns>An <see cref="IType"/> for the object.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when the <c>param</c> is null because the <see cref="IType"/>
/// can't be guess from a null value.
/// </exception>
public static IType TryGuessType(object param, ISessionFactoryImplementor sessionFactory, bool isCollection)
{
if (param == null)
{
return null;
}
if (param is IEnumerable enumerable && isCollection)
{
var firstValue = enumerable.Cast<object>().FirstOrDefault();
return firstValue == null
? TryGuessType(enumerable.GetCollectionElementType(), sessionFactory)
: TryGuessType(firstValue, sessionFactory, false);
}
var clazz = NHibernateProxyHelper.GetClassWithoutInitializingProxy(param);
return TryGuessType(clazz, sessionFactory);
}
/// <summary>
/// Guesses the <see cref="IType"/> from the <c>param</c>'s value.
/// </summary>
/// <param name="param">The object to guess the <see cref="IType"/> of.</param>
/// <param name="sessionFactory">The session factory to search for entity persister.</param>
/// <returns>An <see cref="IType"/> for the object.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when the <c>param</c> is null because the <see cref="IType"/>
/// can't be guess from a null value.
/// </exception>
public static IType GuessType(object param, ISessionFactoryImplementor sessionFactory)
{
if (param == null)
{
throw new ArgumentNullException(nameof(param), "The IType can not be guessed for a null value.");
}
System.Type clazz = NHibernateProxyHelper.GetClassWithoutInitializingProxy(param);
return GuessType(clazz, sessionFactory);
}
/// <summary>
/// Guesses the <see cref="IType"/> from the <see cref="System.Type"/>.
/// </summary>
/// <param name="clazz">The <see cref="System.Type"/> to guess the <see cref="IType"/> of.</param>
/// <param name="sessionFactory">The session factory to search for entity persister.</param>
/// <param name="isCollection">Whether <paramref name="clazz"/> is a collection.</param>
/// <returns>An <see cref="IType"/> for the <see cref="System.Type"/>.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when the <c>clazz</c> is null because the <see cref="IType"/>
/// can't be guess from a null type.
/// </exception>
public static IType TryGuessType(System.Type clazz, ISessionFactoryImplementor sessionFactory, bool isCollection)
{
if (clazz == null)
{
return null;
}
if (isCollection)
{
return TryGuessType(ReflectHelper.GetCollectionElementType(clazz), sessionFactory, false);
}
return TryGuessType(clazz, sessionFactory);
}
/// <summary>
/// Guesses the <see cref="IType"/> from the <see cref="System.Type"/>.
/// </summary>
/// <param name="clazz">The <see cref="System.Type"/> to guess the <see cref="IType"/> of.</param>
/// <param name="sessionFactory">The session factory to search for entity persister.</param>
/// <returns>An <see cref="IType"/> for the <see cref="System.Type"/>.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when the <c>clazz</c> is null because the <see cref="IType"/>
/// can't be guess from a null type.
/// </exception>
public static IType GuessType(System.Type clazz, ISessionFactoryImplementor sessionFactory)
{
if (clazz == null)
{
throw new ArgumentNullException(nameof(clazz), "The IType can not be guessed for a null value.");
}
return TryGuessType(clazz, sessionFactory) ??
throw new HibernateException("Could not determine a type for class: " + clazz.AssemblyQualifiedName);
}
/// <summary>
/// Guesses the <see cref="IType"/> from the <see cref="System.Type"/>.
/// </summary>
/// <param name="clazz">The <see cref="System.Type"/> to guess the <see cref="IType"/> of.</param>
/// <param name="sessionFactory">The session factory to search for entity persister.</param>
/// <returns>An <see cref="IType"/> for the <see cref="System.Type"/>.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when the <c>clazz</c> is null because the <see cref="IType"/>
/// can't be guess from a null type.
/// </exception>
public static IType TryGuessType(System.Type clazz, ISessionFactoryImplementor sessionFactory)
{
if (clazz == null)
{
return null;
}
var type = TypeFactory.HeuristicType(clazz);
if (type == null || type is SerializableType)
{
if (sessionFactory.TryGetEntityPersister(clazz.FullName) != null)
{
return NHibernateUtil.Entity(clazz);
}
}
return type;
}
}
}