forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNHibernateUtil.cs
87 lines (82 loc) · 2.81 KB
/
NHibernateUtil.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
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using NHibernate.Collection;
using NHibernate.Impl;
using NHibernate.Intercept;
using NHibernate.Proxy;
using NHibernate.Type;
using NHibernate.UserTypes;
using NHibernate.Util;
namespace NHibernate
{
using System.Threading.Tasks;
using System.Threading;
public static partial class NHibernateUtil
{
/// <summary>
/// Force initialization of a proxy or persistent collection.
/// </summary>
/// <param name="proxy">a persistable object, proxy, persistent collection or null</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <exception cref="HibernateException">if we can't initialize the proxy at this time, eg. the Session was closed</exception>
public static Task InitializeAsync(object proxy, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (proxy == null)
{
return Task.CompletedTask;
}
if (proxy.IsProxy())
{
return ((INHibernateProxy)proxy).HibernateLazyInitializer.InitializeAsync(cancellationToken);
}
else if (proxy is ILazyInitializedCollection coll)
{
return coll.ForceInitializationAsync(cancellationToken);
}
// 6.0 TODO: remove once IPersistentCollection derives from ILazyInitializedCollection
else if (proxy is IPersistentCollection persistent)
{
return persistent.ForceInitializationAsync(cancellationToken);
}
return Task.CompletedTask;
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
/// <summary>
/// Get the true, underlying class of a proxied persistent class. This operation
/// will initialize a proxy by side-effect.
/// </summary>
/// <param name="proxy">a persistable object or proxy</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <returns>the true class of the instance</returns>
public static async Task<System.Type> GetClassAsync(object proxy, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
if (proxy.IsProxy())
{
return (await (((INHibernateProxy)proxy).HibernateLazyInitializer.GetImplementationAsync(cancellationToken)).ConfigureAwait(false)).GetType();
}
else
{
return proxy.GetType();
}
}
}
}