Skip to content

Commit 9ceedea

Browse files
committed
Add missing ISession.Get(entityName, id, lockMode)
1 parent 4bd76b2 commit 9ceedea

File tree

8 files changed

+158
-57
lines changed

8 files changed

+158
-57
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH1275/Fixture.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public async Task RetrievingAsync()
5353
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
5454
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
5555
}
56+
s.Clear();
57+
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
58+
{
59+
await (s.GetAsync<A>(typeof(A).FullName, savedId, LockMode.Upgrade));
60+
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
61+
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
62+
}
5663
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
5764
{
5865
await (s.CreateQuery("from A a where a.Id= :pid").SetLockMode("a", LockMode.Upgrade).SetParameter("pid", savedId).

src/NHibernate.Test/NHSpecificTest/NH1275/Fixture.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public void Retrieving()
4242
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
4343
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
4444
}
45+
s.Clear();
46+
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
47+
{
48+
s.Get<A>(typeof(A).FullName, savedId, LockMode.Upgrade);
49+
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
50+
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
51+
}
4552
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
4653
{
4754
s.CreateQuery("from A a where a.Id= :pid").SetLockMode("a", LockMode.Upgrade).SetParameter("pid", savedId).

src/NHibernate/Async/ISession.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,45 @@ namespace NHibernate
2626
{
2727
using System.Threading.Tasks;
2828
using System.Threading;
29+
public static partial class SessionExtensions
30+
{
31+
32+
/// <summary>
33+
/// Return the persistent instance of the given entity class with the given identifier, or null
34+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
35+
/// already associated with the session, return that instance or proxy.)
36+
/// </summary>
37+
public static Task<object> GetAsync(this ISession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
38+
{
39+
if (cancellationToken.IsCancellationRequested)
40+
{
41+
return Task.FromCanceled<object>(cancellationToken);
42+
}
43+
try
44+
{
45+
return
46+
ReflectHelper
47+
.CastOrThrow<SessionImpl>(session, "Get with entityName and lockMode")
48+
.GetAsync(entityName, id, lockMode, cancellationToken);
49+
}
50+
catch (Exception ex)
51+
{
52+
return Task.FromException<object>(ex);
53+
}
54+
}
55+
56+
//NOTE: Keep it as extension
57+
/// <summary>
58+
/// Return the persistent instance of the given entity name with the given identifier, or null
59+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
60+
/// already associated with the session, return that instance or proxy.)
61+
/// </summary>
62+
public static async Task<T> GetAsync<T>(this ISession session, string entityName, object id, LockMode lockMode = null, CancellationToken cancellationToken = default(CancellationToken))
63+
{
64+
cancellationToken.ThrowIfCancellationRequested();
65+
return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false);
66+
}
67+
}
2968

3069
public partial interface ISession : IDisposable
3170
{

src/NHibernate/Async/IStatelessSession.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ namespace NHibernate
2222
{
2323
using System.Threading.Tasks;
2424
using System.Threading;
25+
public static partial class StatelessSessionExtensions
26+
{
27+
28+
//NOTE: Keep it as extension
29+
public static async Task<T> GetAsync<T>(this IStatelessSession session, string entityName, object id, LockMode lockMode = null, CancellationToken cancellationToken = default(CancellationToken))
30+
{
31+
cancellationToken.ThrowIfCancellationRequested();
32+
return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false);
33+
}
34+
}
2535

2636
public partial interface IStatelessSession : IDisposable
2737
{

src/NHibernate/Async/Impl/SessionImpl.cs

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

806-
public async Task<T> GetAsync<T>(object id, CancellationToken cancellationToken = default(CancellationToken))
806+
public Task<T> GetAsync<T>(object id, CancellationToken cancellationToken = default(CancellationToken))
807807
{
808-
cancellationToken.ThrowIfCancellationRequested();
809-
using (BeginProcess())
808+
if (cancellationToken.IsCancellationRequested)
810809
{
811-
return (T)await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false);
810+
return Task.FromCanceled<T>(cancellationToken);
812811
}
812+
return GetAsync<T>(id, lockMode: null, cancellationToken: cancellationToken);
813813
}
814814

815815
public async Task<T> GetAsync<T>(object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
816816
{
817817
cancellationToken.ThrowIfCancellationRequested();
818-
using (BeginProcess())
819-
{
820-
return (T)await (GetAsync(typeof(T), id, lockMode, cancellationToken)).ConfigureAwait(false);
821-
}
818+
return (T) await (GetAsync(typeof(T), id, lockMode, cancellationToken)).ConfigureAwait(false);
822819
}
823820

824821
public Task<object> GetAsync(System.Type entityClass, object id, CancellationToken cancellationToken = default(CancellationToken))
@@ -827,7 +824,7 @@ public override async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpac
827824
{
828825
return Task.FromCanceled<object>(cancellationToken);
829826
}
830-
return GetAsync(entityClass.FullName, id, cancellationToken);
827+
return GetAsync(entityClass, id, lockMode: null, cancellationToken: cancellationToken);
831828
}
832829

833830
/// <summary>
@@ -842,14 +839,37 @@ public override async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpac
842839
/// <param name="lockMode"></param>
843840
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
844841
/// <returns></returns>
845-
public async Task<object> GetAsync(System.Type clazz, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
842+
public Task<object> GetAsync(System.Type clazz, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
843+
{
844+
if (cancellationToken.IsCancellationRequested)
845+
{
846+
return Task.FromCanceled<object>(cancellationToken);
847+
}
848+
return GetAsync(clazz.FullName, id, lockMode, cancellationToken);
849+
}
850+
851+
/// <summary>
852+
/// Return the persistent instance of the given entity name with the given identifier, or null
853+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
854+
/// already associated with the session, return that instance or proxy.)
855+
/// </summary>
856+
public async Task<object> GetAsync(string entityName, object id, LockMode lockMode, CancellationToken cancellationToken)
846857
{
847858
cancellationToken.ThrowIfCancellationRequested();
848859
using (BeginProcess())
849860
{
850-
LoadEvent loadEvent = new LoadEvent(id, clazz.FullName, lockMode, this);
851-
await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false);
852-
return loadEvent.Result;
861+
LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this);
862+
bool success = false;
863+
try
864+
{
865+
await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false);
866+
success = true;
867+
return loadEvent.Result;
868+
}
869+
finally
870+
{
871+
await (AfterOperationAsync(success, cancellationToken)).ConfigureAwait(false);
872+
}
853873
}
854874
}
855875

@@ -882,24 +902,13 @@ public override async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpac
882902
}
883903
}
884904

885-
public async Task<object> GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
905+
public Task<object> GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
886906
{
887-
cancellationToken.ThrowIfCancellationRequested();
888-
using (BeginProcess())
907+
if (cancellationToken.IsCancellationRequested)
889908
{
890-
LoadEvent loadEvent = new LoadEvent(id, entityName, false, this);
891-
bool success = false;
892-
try
893-
{
894-
await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false);
895-
success = true;
896-
return loadEvent.Result;
897-
}
898-
finally
899-
{
900-
await (AfterOperationAsync(success, cancellationToken)).ConfigureAwait(false);
901-
}
909+
return Task.FromCanceled<object>(cancellationToken);
902910
}
911+
return GetAsync(entityName, id, null, cancellationToken);
903912
}
904913

905914
/// <summary>

src/NHibernate/ISession.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace NHibernate
1616
{
1717
// 6.0 TODO: Convert most of these extensions to interface methods
18-
public static class SessionExtensions
18+
public static partial class SessionExtensions
1919
{
2020
/// <summary>
2121
/// Obtain a <see cref="IStatelessSession"/> builder with the ability to grab certain information from
@@ -48,6 +48,30 @@ public static IQueryBatch CreateQueryBatch(this ISession session)
4848
/// <returns>The current transaction or <see langword="null" />..</returns>
4949
public static ITransaction GetCurrentTransaction(this ISession session)
5050
=> session.GetSessionImplementation().ConnectionManager.CurrentTransaction;
51+
52+
/// <summary>
53+
/// Return the persistent instance of the given entity class with the given identifier, or null
54+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
55+
/// already associated with the session, return that instance or proxy.)
56+
/// </summary>
57+
public static object Get(this ISession session, string entityName, object id, LockMode lockMode)
58+
{
59+
return
60+
ReflectHelper
61+
.CastOrThrow<SessionImpl>(session, "Get with entityName and lockMode")
62+
.Get(entityName, id, lockMode);
63+
}
64+
65+
//NOTE: Keep it as extension
66+
/// <summary>
67+
/// Return the persistent instance of the given entity name with the given identifier, or null
68+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
69+
/// already associated with the session, return that instance or proxy.)
70+
/// </summary>
71+
public static T Get<T>(this ISession session, string entityName, object id, LockMode lockMode = null)
72+
{
73+
return (T) session.Get(entityName, id, lockMode);
74+
}
5175
}
5276

5377
/// <summary>

src/NHibernate/IStatelessSession.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace NHibernate
1212
{
1313
// 6.0 TODO: Convert most of these extensions to interface methods
14-
public static class StatelessSessionExtensions
14+
public static partial class StatelessSessionExtensions
1515
{
1616
/// <summary>
1717
/// Creates a <see cref="IQueryBatch"/> for the session.
@@ -31,6 +31,12 @@ public static IQueryBatch CreateQueryBatch(this IStatelessSession session)
3131
/// <returns>The current transaction or <see langword="null" />..</returns>
3232
public static ITransaction GetCurrentTransaction(this IStatelessSession session)
3333
=> session.GetSessionImplementation().ConnectionManager.CurrentTransaction;
34+
35+
//NOTE: Keep it as extension
36+
public static T Get<T>(this IStatelessSession session, string entityName, object id, LockMode lockMode = null)
37+
{
38+
return (T) session.Get(entityName, id, lockMode);
39+
}
3440
}
3541

3642
/// <summary>

src/NHibernate/Impl/SessionImpl.cs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,23 +1158,17 @@ public object Load(System.Type entityClass, object id)
11581158

11591159
public T Get<T>(object id)
11601160
{
1161-
using (BeginProcess())
1162-
{
1163-
return (T)Get(typeof(T), id);
1164-
}
1161+
return Get<T>(id, lockMode: null);
11651162
}
11661163

11671164
public T Get<T>(object id, LockMode lockMode)
11681165
{
1169-
using (BeginProcess())
1170-
{
1171-
return (T)Get(typeof(T), id, lockMode);
1172-
}
1166+
return (T) Get(typeof(T), id, lockMode);
11731167
}
11741168

11751169
public object Get(System.Type entityClass, object id)
11761170
{
1177-
return Get(entityClass.FullName, id);
1171+
return Get(entityClass, id, lockMode: null);
11781172
}
11791173

11801174
/// <summary>
@@ -1189,12 +1183,31 @@ public object Get(System.Type entityClass, object id)
11891183
/// <param name="lockMode"></param>
11901184
/// <returns></returns>
11911185
public object Get(System.Type clazz, object id, LockMode lockMode)
1186+
{
1187+
return Get(clazz.FullName, id, lockMode);
1188+
}
1189+
1190+
/// <summary>
1191+
/// Return the persistent instance of the given entity name with the given identifier, or null
1192+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
1193+
/// already associated with the session, return that instance or proxy.)
1194+
/// </summary>
1195+
public object Get(string entityName, object id, LockMode lockMode)
11921196
{
11931197
using (BeginProcess())
11941198
{
1195-
LoadEvent loadEvent = new LoadEvent(id, clazz.FullName, lockMode, this);
1196-
FireLoad(loadEvent, LoadEventListener.Get);
1197-
return loadEvent.Result;
1199+
LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this);
1200+
bool success = false;
1201+
try
1202+
{
1203+
FireLoad(loadEvent, LoadEventListener.Get);
1204+
success = true;
1205+
return loadEvent.Result;
1206+
}
1207+
finally
1208+
{
1209+
AfterOperation(success);
1210+
}
11981211
}
11991212
}
12001213

@@ -1228,21 +1241,7 @@ public string GetEntityName(object obj)
12281241

12291242
public object Get(string entityName, object id)
12301243
{
1231-
using (BeginProcess())
1232-
{
1233-
LoadEvent loadEvent = new LoadEvent(id, entityName, false, this);
1234-
bool success = false;
1235-
try
1236-
{
1237-
FireLoad(loadEvent, LoadEventListener.Get);
1238-
success = true;
1239-
return loadEvent.Result;
1240-
}
1241-
finally
1242-
{
1243-
AfterOperation(success);
1244-
}
1245-
}
1244+
return Get(entityName, id, null);
12461245
}
12471246

12481247
/// <summary>

0 commit comments

Comments
 (0)