Skip to content

Commit 8e337ea

Browse files
committed
Pass IPersistenceContext into PostInitialize
1 parent 066cd0a commit 8e337ea

File tree

8 files changed

+29
-40
lines changed

8 files changed

+29
-40
lines changed

src/NHibernate/Async/Engine/Loading/CollectionLoadContext.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ private async Task EndLoadingCollectionAsync(LoadingCollectionEntry lce, ICollec
128128
{
129129
log.Debug("ending loading collection [{0}]", lce);
130130
}
131-
ISessionImplementor session = LoadContext.PersistenceContext.Session;
131+
132+
var persistenceContext = LoadContext.PersistenceContext;
133+
var session = persistenceContext.Session;
132134

133135
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
134136
var stopWath = new Stopwatch();
@@ -141,17 +143,17 @@ private async Task EndLoadingCollectionAsync(LoadingCollectionEntry lce, ICollec
141143

142144
if (persister.CollectionType.HasHolder())
143145
{
144-
LoadContext.PersistenceContext.AddCollectionHolder(lce.Collection);
146+
persistenceContext.AddCollectionHolder(lce.Collection);
145147
}
146148

147-
CollectionEntry ce = LoadContext.PersistenceContext.GetCollectionEntry(lce.Collection);
149+
CollectionEntry ce = persistenceContext.GetCollectionEntry(lce.Collection);
148150
if (ce == null)
149151
{
150-
ce = LoadContext.PersistenceContext.AddInitializedCollection(persister, lce.Collection, lce.Key);
152+
ce = persistenceContext.AddInitializedCollection(persister, lce.Collection, lce.Key);
151153
}
152154
else
153155
{
154-
ce.PostInitialize(lce.Collection);
156+
ce.PostInitialize(lce.Collection, persistenceContext);
155157
}
156158

157159
bool addToCache = hasNoQueuedAdds && persister.HasCache &&

src/NHibernate/Async/Event/Default/DefaultInitializeCollectionEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private async Task<bool> InitializeCollectionFromCacheAsync(object id, ICollecti
127127
CollectionCacheEntry cacheEntry = (CollectionCacheEntry)persister.CacheEntryStructure.Destructure(ce, factory);
128128
await (cacheEntry.AssembleAsync(collection, persister, persistenceContext.GetCollectionOwner(id, persister), cancellationToken)).ConfigureAwait(false);
129129

130-
persistenceContext.GetCollectionEntry(collection).PostInitialize(collection);
130+
persistenceContext.GetCollectionEntry(collection).PostInitialize(collection, persistenceContext);
131131
return true;
132132
}
133133
}

src/NHibernate/Collection/AbstractPersistentCollection.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -868,13 +868,5 @@ public abstract object ReadFrom(DbDataReader reader, ICollectionPersister role,
868868
*/
869869

870870
#endregion
871-
872-
/// <summary>
873-
/// Get the session associated with the collection.
874-
/// </summary>
875-
public ISessionImplementor GetCurrentSession()
876-
{
877-
return session;
878-
}
879871
}
880872
}

src/NHibernate/Collection/IPersistentCollection.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections;
32
using System.Data.Common;
43
using NHibernate.Collection.Generic;
@@ -338,21 +337,4 @@ public partial interface IPersistentCollection
338337
/// </returns>
339338
ICollection GetOrphans(object snapshot, string entityName);
340339
}
341-
342-
public static class PersistentCollectionExtensions
343-
{
344-
/// <summary>
345-
/// Get the session associated with the collection.
346-
/// </summary>
347-
//6.0 TODO: Merge into IPersistentCollection interface. Consider converting to a property.
348-
public static ISessionImplementor GetCurrentSession(this IPersistentCollection collection)
349-
{
350-
if (collection is AbstractPersistentCollection apc)
351-
{
352-
return apc.GetCurrentSession();
353-
}
354-
355-
throw new InvalidOperationException("Only collections of AbstractPersistentCollection are supported.");
356-
}
357-
}
358340
}

src/NHibernate/Engine/CollectionEntry.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,20 @@ public void PostInitialize(IPersistentCollection collection)
302302
{
303303
snapshot = LoadedPersister.IsMutable ? collection.GetSnapshot(LoadedPersister) : null;
304304
collection.SetSnapshot(loadedKey, role, snapshot);
305+
}
306+
307+
/// <summary>
308+
/// Updates the CollectionEntry to reflect that the <see cref="IPersistentCollection"/>
309+
/// has been initialized.
310+
/// </summary>
311+
/// <param name="collection">The initialized <see cref="AbstractPersistentCollection"/> that this Entry is for.</param>
312+
/// <param name="persistenceContext"></param>
313+
public void PostInitialize(IPersistentCollection collection, IPersistenceContext persistenceContext)
314+
{
315+
PostInitialize(collection);
305316
if (LoadedPersister.GetBatchSize() > 1)
306317
{
307-
collection.GetCurrentSession().PersistenceContext.BatchFetchQueue.RemoveBatchLoadableCollection(this);
318+
persistenceContext.BatchFetchQueue.RemoveBatchLoadableCollection(this);
308319
}
309320
}
310321

src/NHibernate/Engine/Loading/CollectionLoadContext.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ private void EndLoadingCollection(LoadingCollectionEntry lce, ICollectionPersist
234234
{
235235
log.Debug("ending loading collection [{0}]", lce);
236236
}
237-
ISessionImplementor session = LoadContext.PersistenceContext.Session;
237+
238+
var persistenceContext = LoadContext.PersistenceContext;
239+
var session = persistenceContext.Session;
238240

239241
bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
240242
var stopWath = new Stopwatch();
@@ -247,17 +249,17 @@ private void EndLoadingCollection(LoadingCollectionEntry lce, ICollectionPersist
247249

248250
if (persister.CollectionType.HasHolder())
249251
{
250-
LoadContext.PersistenceContext.AddCollectionHolder(lce.Collection);
252+
persistenceContext.AddCollectionHolder(lce.Collection);
251253
}
252254

253-
CollectionEntry ce = LoadContext.PersistenceContext.GetCollectionEntry(lce.Collection);
255+
CollectionEntry ce = persistenceContext.GetCollectionEntry(lce.Collection);
254256
if (ce == null)
255257
{
256-
ce = LoadContext.PersistenceContext.AddInitializedCollection(persister, lce.Collection, lce.Key);
258+
ce = persistenceContext.AddInitializedCollection(persister, lce.Collection, lce.Key);
257259
}
258260
else
259261
{
260-
ce.PostInitialize(lce.Collection);
262+
ce.PostInitialize(lce.Collection, persistenceContext);
261263
}
262264

263265
bool addToCache = hasNoQueuedAdds && persister.HasCache &&

src/NHibernate/Engine/StatefulPersistenceContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ public CollectionEntry AddInitializedCollection(ICollectionPersister persister,
917917
object id)
918918
{
919919
CollectionEntry ce = new CollectionEntry(collection, persister, id, flushing);
920-
ce.PostInitialize(collection);
920+
ce.PostInitialize(collection, this);
921921
AddCollection(collection, ce, id);
922922
return ce;
923923
}

src/NHibernate/Event/Default/DefaultInitializeCollectionEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private bool InitializeCollectionFromCache(object id, ICollectionPersister persi
115115
CollectionCacheEntry cacheEntry = (CollectionCacheEntry)persister.CacheEntryStructure.Destructure(ce, factory);
116116
cacheEntry.Assemble(collection, persister, persistenceContext.GetCollectionOwner(id, persister));
117117

118-
persistenceContext.GetCollectionEntry(collection).PostInitialize(collection);
118+
persistenceContext.GetCollectionEntry(collection).PostInitialize(collection, persistenceContext);
119119
return true;
120120
}
121121
}

0 commit comments

Comments
 (0)