forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionRecreateAction.cs
92 lines (81 loc) · 3.35 KB
/
CollectionRecreateAction.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
//------------------------------------------------------------------------------
// <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.Diagnostics;
using NHibernate.Collection;
using NHibernate.Engine;
using NHibernate.Event;
using NHibernate.Persister.Collection;
namespace NHibernate.Action
{
using System.Threading.Tasks;
using System.Threading;
public sealed partial class CollectionRecreateAction : CollectionAction
{
/// <summary> Execute this action</summary>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <remarks>
/// This method is called when a new non-null collection is persisted
/// or when an existing (non-null) collection is moved to a new owner
/// </remarks>
public override async Task ExecuteAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
bool statsEnabled = Session.Factory.Statistics.IsStatisticsEnabled;
Stopwatch stopwatch = null;
if (statsEnabled)
{
stopwatch = Stopwatch.StartNew();
}
IPersistentCollection collection = Collection;
await (PreRecreateAsync(cancellationToken)).ConfigureAwait(false);
var key = await (GetKeyAsync(cancellationToken)).ConfigureAwait(false);
await (Persister.RecreateAsync(collection, key, Session, cancellationToken)).ConfigureAwait(false);
var entry = Session.PersistenceContext.GetCollectionEntry(collection);
// On collection create, the current key may refer a delayed post insert instance instead
// of the actual identifier, that GetKey above should have resolved at that point. Update
// it.
entry.CurrentKey = key;
entry.AfterAction(collection);
await (EvictAsync(cancellationToken)).ConfigureAwait(false);
await (PostRecreateAsync(cancellationToken)).ConfigureAwait(false);
if (statsEnabled)
{
stopwatch.Stop();
Session.Factory.StatisticsImplementor.RecreateCollection(Persister.Role, stopwatch.Elapsed);
}
}
private async Task PreRecreateAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
IPreCollectionRecreateEventListener[] preListeners = Session.Listeners.PreCollectionRecreateEventListeners;
if (preListeners.Length > 0)
{
PreCollectionRecreateEvent preEvent = new PreCollectionRecreateEvent(Persister, Collection, (IEventSource)Session);
for (int i = 0; i < preListeners.Length; i++)
{
await (preListeners[i].OnPreRecreateCollectionAsync(preEvent, cancellationToken)).ConfigureAwait(false);
}
}
}
private async Task PostRecreateAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
IPostCollectionRecreateEventListener[] postListeners = Session.Listeners.PostCollectionRecreateEventListeners;
if (postListeners.Length > 0)
{
PostCollectionRecreateEvent postEvent = new PostCollectionRecreateEvent(Persister, Collection, (IEventSource)Session);
for (int i = 0; i < postListeners.Length; i++)
{
await (postListeners[i].OnPostRecreateCollectionAsync(postEvent, cancellationToken)).ConfigureAwait(false);
}
}
}
}
}