Skip to content

Commit 1aaa0d0

Browse files
Generate async files
1 parent ac5a708 commit 1aaa0d0

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/NHibernate.Test/Async/FetchLazyProperties/FetchLazyPropertiesFixture.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,41 @@ void AssertPersons(List<Person> results, bool fetched)
10871087
}
10881088
}
10891089

1090+
[Test]
1091+
public async Task TestRefreshRemovesLazyLoadedPropertiesAsync()
1092+
{
1093+
using (var outerSession = OpenSession())
1094+
{
1095+
const string query = "from Person fetch Image where Id = 1";
1096+
const string namePostFix = "_MODIFIED";
1097+
const int imageLength = 4711;
1098+
1099+
Person outerPerson = await (outerSession.CreateQuery(query).UniqueResultAsync<Person>());
1100+
1101+
Assert.That(outerPerson.Name.EndsWith(namePostFix), Is.False); // Normal property
1102+
Assert.That(outerPerson.Image.Length, Is.EqualTo(1)); // Lazy Property
1103+
1104+
// Changing the properties of the person in a different sessions
1105+
using (var innerSession = OpenSession())
1106+
{
1107+
var transaction = innerSession.BeginTransaction();
1108+
1109+
Person innerPerson = await (innerSession.CreateQuery(query).UniqueResultAsync<Person>());
1110+
innerPerson.Image = new byte[imageLength];
1111+
innerPerson.Name += namePostFix;
1112+
await (innerSession.UpdateAsync(innerPerson));
1113+
1114+
await (transaction.CommitAsync());
1115+
}
1116+
1117+
// Refreshing the person in the outer session
1118+
await (outerSession.RefreshAsync(outerPerson));
1119+
1120+
Assert.That(outerPerson.Name.EndsWith(namePostFix), Is.True); // Value has changed
1121+
Assert.That(outerPerson.Image.Length, Is.EqualTo(imageLength)); // This is still the old value
1122+
}
1123+
}
1124+
10901125
private static Person GeneratePerson(int i, Person bestFriend)
10911126
{
10921127
return new Person

src/NHibernate/Async/Loader/Loader.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,8 @@ private async Task<object> InstanceNotYetLoadedAsync(DbDataReader dr, int i, ILo
757757

758758
ILoadable concretePersister = await (GetConcretePersisterAsync(dr, i, persister, key.Identifier, session, cancellationToken)).ConfigureAwait(false);
759759

760-
if (optionalObjectKey != null && key.Equals(optionalObjectKey))
760+
bool useOptionalObject = optionalObjectKey != null && key.Equals(optionalObjectKey);
761+
if (useOptionalObject)
761762
{
762763
// its the given optional object
763764
obj = optionalObject;
@@ -773,8 +774,8 @@ private async Task<object> InstanceNotYetLoadedAsync(DbDataReader dr, int i, ILo
773774
// (but don't yet initialize the object itself)
774775
// note that we acquired LockMode.READ even if it was not requested
775776
LockMode acquiredLockMode = lockMode == LockMode.None ? LockMode.Read : lockMode;
776-
await (LoadFromResultSetAsync(dr, i, obj, concretePersister, key, acquiredLockMode, persister, session, cancellationToken)).ConfigureAwait(false);
777-
777+
await (LoadFromResultSetAsync(dr, i, obj, concretePersister, key, acquiredLockMode, persister, session, useOptionalObject, cancellationToken)).ConfigureAwait(false);
778+
778779
// materialize associations (and initialize the object) later
779780
hydratedObjects.Add(obj);
780781

@@ -886,7 +887,7 @@ internal static async Task UpdateCacheForEntityAsync(
886887
/// </summary>
887888
private async Task LoadFromResultSetAsync(DbDataReader rs, int i, object obj, ILoadable persister, EntityKey key,
888889
LockMode lockMode, ILoadable rootPersister,
889-
ISessionImplementor session, CancellationToken cancellationToken)
890+
ISessionImplementor session, bool lazyPropertiesAreUnfetched, CancellationToken cancellationToken)
890891
{
891892
cancellationToken.ThrowIfCancellationRequested();
892893
object id = key.Identifier;
@@ -912,7 +913,7 @@ private async Task LoadFromResultSetAsync(DbDataReader rs, int i, object obj, IL
912913

913914
object rowId = persister.HasRowId ? rs[EntityAliases[i].RowIdAlias] : null;
914915

915-
TwoPhaseLoad.PostHydrate(persister, id, values, rowId, obj, lockMode, session);
916+
TwoPhaseLoad.PostHydrate(persister, id, values, rowId, obj, lockMode, lazyPropertiesAreUnfetched, session);
916917
}
917918

918919
/// <summary>

0 commit comments

Comments
 (0)