Skip to content

Commit 0b16d0a

Browse files
committed
Do not call AfterOperation for overloads with LockMode
1 parent a75f54c commit 0b16d0a

File tree

4 files changed

+75
-48
lines changed

4 files changed

+75
-48
lines changed

src/NHibernate/Async/ISession.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,29 @@ public static partial class SessionExtensions
7272
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
7373
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
7474
/// <returns>A persistent instance, or <see langword="null" />.</returns>
75-
public static async Task<T> GetAsync<T>(this ISession session, string entityName, object id, LockMode lockMode = null, CancellationToken cancellationToken = default(CancellationToken))
75+
public static async Task<T> GetAsync<T>(this ISession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
7676
{
7777
cancellationToken.ThrowIfCancellationRequested();
7878
return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false);
7979
}
80+
81+
//NOTE: Keep it as extension
82+
/// <summary>
83+
/// Return the persistent instance of the given entity name with the given identifier, or null
84+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
85+
/// already associated with the session, return that instance or proxy.)
86+
/// </summary>
87+
/// <typeparam name="T">The entity class.</typeparam>
88+
/// <param name="session">The session.</param>
89+
/// <param name="entityName">The entity name.</param>
90+
/// <param name="id">The entity identifier.</param>
91+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
92+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
93+
public static async Task<T> GetAsync<T>(this ISession session, string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
94+
{
95+
cancellationToken.ThrowIfCancellationRequested();
96+
return (T) await (session.GetAsync(entityName, id, cancellationToken)).ConfigureAwait(false);
97+
}
8098
}
8199

82100
public partial interface ISession : IDisposable

src/NHibernate/Async/Impl/SessionImpl.cs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -803,42 +803,31 @@ public override async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpac
803803
return LoadAsync(entityClass.FullName, id, cancellationToken);
804804
}
805805

806-
public Task<T> GetAsync<T>(object id, CancellationToken cancellationToken = default(CancellationToken))
806+
/// <inheritdoc />
807+
public async Task<T> GetAsync<T>(object id, CancellationToken cancellationToken = default(CancellationToken))
807808
{
808-
if (cancellationToken.IsCancellationRequested)
809-
{
810-
return Task.FromCanceled<T>(cancellationToken);
811-
}
812-
return GetAsync<T>(id, lockMode: null, cancellationToken: cancellationToken);
809+
cancellationToken.ThrowIfCancellationRequested();
810+
return (T) await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false);
813811
}
814812

813+
/// <inheritdoc />
815814
public async Task<T> GetAsync<T>(object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
816815
{
817816
cancellationToken.ThrowIfCancellationRequested();
818817
return (T) await (GetAsync(typeof(T), id, lockMode, cancellationToken)).ConfigureAwait(false);
819818
}
820819

820+
/// <inheritdoc />
821821
public Task<object> GetAsync(System.Type entityClass, object id, CancellationToken cancellationToken = default(CancellationToken))
822822
{
823823
if (cancellationToken.IsCancellationRequested)
824824
{
825825
return Task.FromCanceled<object>(cancellationToken);
826826
}
827-
return GetAsync(entityClass, id, lockMode: null, cancellationToken: cancellationToken);
827+
return GetAsync(entityClass.FullName, id, cancellationToken);
828828
}
829829

830-
/// <summary>
831-
/// Load the data for the object with the specified id into a newly created object
832-
/// using "for update", if supported. A new key will be assigned to the object.
833-
/// This should return an existing proxy where appropriate.
834-
///
835-
/// If the object does not exist in the database, null is returned.
836-
/// </summary>
837-
/// <param name="clazz"></param>
838-
/// <param name="id"></param>
839-
/// <param name="lockMode"></param>
840-
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
841-
/// <returns></returns>
830+
/// <inheritdoc />
842831
public Task<object> GetAsync(System.Type clazz, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
843832
{
844833
if (cancellationToken.IsCancellationRequested)
@@ -855,6 +844,19 @@ public async Task<object> GetAsync(string entityName, object id, LockMode lockMo
855844
using (BeginProcess())
856845
{
857846
LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this);
847+
await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false);
848+
//Note: AfterOperation call is skipped to avoid releasing the lock when outside of a transaction.
849+
return loadEvent.Result;
850+
}
851+
}
852+
853+
/// <inheritdoc />
854+
public async Task<object> GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
855+
{
856+
cancellationToken.ThrowIfCancellationRequested();
857+
using (BeginProcess())
858+
{
859+
LoadEvent loadEvent = new LoadEvent(id, entityName, null, this);
858860
bool success = false;
859861
try
860862
{
@@ -898,15 +900,6 @@ public async Task<object> GetAsync(string entityName, object id, LockMode lockMo
898900
}
899901
}
900902

901-
public Task<object> GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
902-
{
903-
if (cancellationToken.IsCancellationRequested)
904-
{
905-
return Task.FromCanceled<object>(cancellationToken);
906-
}
907-
return GetAsync(entityName, id, null, cancellationToken);
908-
}
909-
910903
/// <summary>
911904
/// Load the data for the object with the specified id into a newly created object.
912905
/// This is only called when lazily initializing a proxy.

src/NHibernate/ISession.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,26 @@ public static object Get(this ISession session, string entityName, object id, Lo
7979
/// <param name="id">The entity identifier.</param>
8080
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
8181
/// <returns>A persistent instance, or <see langword="null" />.</returns>
82-
public static T Get<T>(this ISession session, string entityName, object id, LockMode lockMode = null)
82+
public static T Get<T>(this ISession session, string entityName, object id, LockMode lockMode)
8383
{
8484
return (T) session.Get(entityName, id, lockMode);
8585
}
86+
87+
//NOTE: Keep it as extension
88+
/// <summary>
89+
/// Return the persistent instance of the given entity name with the given identifier, or null
90+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
91+
/// already associated with the session, return that instance or proxy.)
92+
/// </summary>
93+
/// <typeparam name="T">The entity class.</typeparam>
94+
/// <param name="session">The session.</param>
95+
/// <param name="entityName">The entity name.</param>
96+
/// <param name="id">The entity identifier.</param>
97+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
98+
public static T Get<T>(this ISession session, string entityName, object id)
99+
{
100+
return (T) session.Get(entityName, id);
101+
}
86102
}
87103

88104
/// <summary>

src/NHibernate/Impl/SessionImpl.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,32 +1156,25 @@ public object Load(System.Type entityClass, object id)
11561156
return Load(entityClass.FullName, id);
11571157
}
11581158

1159+
/// <inheritdoc />
11591160
public T Get<T>(object id)
11601161
{
1161-
return Get<T>(id, lockMode: null);
1162+
return (T) Get(typeof(T), id);
11621163
}
11631164

1165+
/// <inheritdoc />
11641166
public T Get<T>(object id, LockMode lockMode)
11651167
{
11661168
return (T) Get(typeof(T), id, lockMode);
11671169
}
11681170

1171+
/// <inheritdoc />
11691172
public object Get(System.Type entityClass, object id)
11701173
{
1171-
return Get(entityClass, id, lockMode: null);
1174+
return Get(entityClass.FullName, id);
11721175
}
11731176

1174-
/// <summary>
1175-
/// Load the data for the object with the specified id into a newly created object
1176-
/// using "for update", if supported. A new key will be assigned to the object.
1177-
/// This should return an existing proxy where appropriate.
1178-
///
1179-
/// If the object does not exist in the database, null is returned.
1180-
/// </summary>
1181-
/// <param name="clazz"></param>
1182-
/// <param name="id"></param>
1183-
/// <param name="lockMode"></param>
1184-
/// <returns></returns>
1177+
/// <inheritdoc />
11851178
public object Get(System.Type clazz, object id, LockMode lockMode)
11861179
{
11871180
return Get(clazz.FullName, id, lockMode);
@@ -1193,6 +1186,18 @@ public object Get(string entityName, object id, LockMode lockMode)
11931186
using (BeginProcess())
11941187
{
11951188
LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this);
1189+
FireLoad(loadEvent, LoadEventListener.Get);
1190+
//Note: AfterOperation call is skipped to avoid releasing the lock when outside of a transaction.
1191+
return loadEvent.Result;
1192+
}
1193+
}
1194+
1195+
/// <inheritdoc />
1196+
public object Get(string entityName, object id)
1197+
{
1198+
using (BeginProcess())
1199+
{
1200+
LoadEvent loadEvent = new LoadEvent(id, entityName, null, this);
11961201
bool success = false;
11971202
try
11981203
{
@@ -1235,11 +1240,6 @@ public string GetEntityName(object obj)
12351240
}
12361241
}
12371242

1238-
public object Get(string entityName, object id)
1239-
{
1240-
return Get(entityName, id, null);
1241-
}
1242-
12431243
/// <summary>
12441244
/// Load the data for the object with the specified id into a newly created object.
12451245
/// This is only called when lazily initializing a proxy.

0 commit comments

Comments
 (0)