From 9ceedea685d51e2e5e95d33c7d5bb73bf273e874 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Thu, 14 May 2020 16:56:34 +0300 Subject: [PATCH 1/5] Add missing ISession.Get(entityName, id, lockMode) --- .../Async/NHSpecificTest/NH1275/Fixture.cs | 7 ++ .../NHSpecificTest/NH1275/Fixture.cs | 7 ++ src/NHibernate/Async/ISession.cs | 39 +++++++++++ src/NHibernate/Async/IStatelessSession.cs | 10 +++ src/NHibernate/Async/Impl/SessionImpl.cs | 65 +++++++++++-------- src/NHibernate/ISession.cs | 26 +++++++- src/NHibernate/IStatelessSession.cs | 8 ++- src/NHibernate/Impl/SessionImpl.cs | 53 ++++++++------- 8 files changed, 158 insertions(+), 57 deletions(-) diff --git a/src/NHibernate.Test/Async/NHSpecificTest/NH1275/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/NH1275/Fixture.cs index 57ecaea5761..5503e4bb9db 100644 --- a/src/NHibernate.Test/Async/NHSpecificTest/NH1275/Fixture.cs +++ b/src/NHibernate.Test/Async/NHSpecificTest/NH1275/Fixture.cs @@ -53,6 +53,13 @@ public async Task RetrievingAsync() string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage; Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0)); } + s.Clear(); + using (SqlLogSpy sqlLogSpy = new SqlLogSpy()) + { + await (s.GetAsync(typeof(A).FullName, savedId, LockMode.Upgrade)); + string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage; + Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0)); + } using (SqlLogSpy sqlLogSpy = new SqlLogSpy()) { await (s.CreateQuery("from A a where a.Id= :pid").SetLockMode("a", LockMode.Upgrade).SetParameter("pid", savedId). diff --git a/src/NHibernate.Test/NHSpecificTest/NH1275/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/NH1275/Fixture.cs index 350b40f4dc9..4318dc5839f 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH1275/Fixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH1275/Fixture.cs @@ -42,6 +42,13 @@ public void Retrieving() string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage; Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0)); } + s.Clear(); + using (SqlLogSpy sqlLogSpy = new SqlLogSpy()) + { + s.Get(typeof(A).FullName, savedId, LockMode.Upgrade); + string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage; + Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0)); + } using (SqlLogSpy sqlLogSpy = new SqlLogSpy()) { s.CreateQuery("from A a where a.Id= :pid").SetLockMode("a", LockMode.Upgrade).SetParameter("pid", savedId). diff --git a/src/NHibernate/Async/ISession.cs b/src/NHibernate/Async/ISession.cs index fbe6f6d19a6..152c9213b80 100644 --- a/src/NHibernate/Async/ISession.cs +++ b/src/NHibernate/Async/ISession.cs @@ -26,6 +26,45 @@ namespace NHibernate { using System.Threading.Tasks; using System.Threading; + public static partial class SessionExtensions + { + + /// + /// Return the persistent instance of the given entity class with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + public static Task GetAsync(this ISession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return Task.FromCanceled(cancellationToken); + } + try + { + return + ReflectHelper + .CastOrThrow(session, "Get with entityName and lockMode") + .GetAsync(entityName, id, lockMode, cancellationToken); + } + catch (Exception ex) + { + return Task.FromException(ex); + } + } + + //NOTE: Keep it as extension + /// + /// Return the persistent instance of the given entity name with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + public static async Task GetAsync(this ISession session, string entityName, object id, LockMode lockMode = null, CancellationToken cancellationToken = default(CancellationToken)) + { + cancellationToken.ThrowIfCancellationRequested(); + return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false); + } + } public partial interface ISession : IDisposable { diff --git a/src/NHibernate/Async/IStatelessSession.cs b/src/NHibernate/Async/IStatelessSession.cs index 5e86ca768c8..c59fed33baf 100644 --- a/src/NHibernate/Async/IStatelessSession.cs +++ b/src/NHibernate/Async/IStatelessSession.cs @@ -22,6 +22,16 @@ namespace NHibernate { using System.Threading.Tasks; using System.Threading; + public static partial class StatelessSessionExtensions + { + + //NOTE: Keep it as extension + public static async Task GetAsync(this IStatelessSession session, string entityName, object id, LockMode lockMode = null, CancellationToken cancellationToken = default(CancellationToken)) + { + cancellationToken.ThrowIfCancellationRequested(); + return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false); + } + } public partial interface IStatelessSession : IDisposable { diff --git a/src/NHibernate/Async/Impl/SessionImpl.cs b/src/NHibernate/Async/Impl/SessionImpl.cs index 8335e43d62f..45c0fe162d0 100644 --- a/src/NHibernate/Async/Impl/SessionImpl.cs +++ b/src/NHibernate/Async/Impl/SessionImpl.cs @@ -803,22 +803,19 @@ public override async Task AutoFlushIfRequiredAsync(ISet querySpac return LoadAsync(entityClass.FullName, id, cancellationToken); } - public async Task GetAsync(object id, CancellationToken cancellationToken = default(CancellationToken)) + public Task GetAsync(object id, CancellationToken cancellationToken = default(CancellationToken)) { - cancellationToken.ThrowIfCancellationRequested(); - using (BeginProcess()) + if (cancellationToken.IsCancellationRequested) { - return (T)await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false); + return Task.FromCanceled(cancellationToken); } + return GetAsync(id, lockMode: null, cancellationToken: cancellationToken); } public async Task GetAsync(object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); - using (BeginProcess()) - { - return (T)await (GetAsync(typeof(T), id, lockMode, cancellationToken)).ConfigureAwait(false); - } + return (T) await (GetAsync(typeof(T), id, lockMode, cancellationToken)).ConfigureAwait(false); } public Task GetAsync(System.Type entityClass, object id, CancellationToken cancellationToken = default(CancellationToken)) @@ -827,7 +824,7 @@ public override async Task AutoFlushIfRequiredAsync(ISet querySpac { return Task.FromCanceled(cancellationToken); } - return GetAsync(entityClass.FullName, id, cancellationToken); + return GetAsync(entityClass, id, lockMode: null, cancellationToken: cancellationToken); } /// @@ -842,14 +839,37 @@ public override async Task AutoFlushIfRequiredAsync(ISet querySpac /// /// A cancellation token that can be used to cancel the work /// - public async Task GetAsync(System.Type clazz, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) + public Task GetAsync(System.Type clazz, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return Task.FromCanceled(cancellationToken); + } + return GetAsync(clazz.FullName, id, lockMode, cancellationToken); + } + + /// + /// Return the persistent instance of the given entity name with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + public async Task GetAsync(string entityName, object id, LockMode lockMode, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); using (BeginProcess()) { - LoadEvent loadEvent = new LoadEvent(id, clazz.FullName, lockMode, this); - await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false); - return loadEvent.Result; + LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this); + bool success = false; + try + { + await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false); + success = true; + return loadEvent.Result; + } + finally + { + await (AfterOperationAsync(success, cancellationToken)).ConfigureAwait(false); + } } } @@ -882,24 +902,13 @@ public override async Task AutoFlushIfRequiredAsync(ISet querySpac } } - public async Task GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken)) + public Task GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken)) { - cancellationToken.ThrowIfCancellationRequested(); - using (BeginProcess()) + if (cancellationToken.IsCancellationRequested) { - LoadEvent loadEvent = new LoadEvent(id, entityName, false, this); - bool success = false; - try - { - await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false); - success = true; - return loadEvent.Result; - } - finally - { - await (AfterOperationAsync(success, cancellationToken)).ConfigureAwait(false); - } + return Task.FromCanceled(cancellationToken); } + return GetAsync(entityName, id, null, cancellationToken); } /// diff --git a/src/NHibernate/ISession.cs b/src/NHibernate/ISession.cs index 99a8ba12a56..0b939e3db67 100644 --- a/src/NHibernate/ISession.cs +++ b/src/NHibernate/ISession.cs @@ -15,7 +15,7 @@ namespace NHibernate { // 6.0 TODO: Convert most of these extensions to interface methods - public static class SessionExtensions + public static partial class SessionExtensions { /// /// Obtain a builder with the ability to grab certain information from @@ -48,6 +48,30 @@ public static IQueryBatch CreateQueryBatch(this ISession session) /// The current transaction or .. public static ITransaction GetCurrentTransaction(this ISession session) => session.GetSessionImplementation().ConnectionManager.CurrentTransaction; + + /// + /// Return the persistent instance of the given entity class with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + public static object Get(this ISession session, string entityName, object id, LockMode lockMode) + { + return + ReflectHelper + .CastOrThrow(session, "Get with entityName and lockMode") + .Get(entityName, id, lockMode); + } + + //NOTE: Keep it as extension + /// + /// Return the persistent instance of the given entity name with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + public static T Get(this ISession session, string entityName, object id, LockMode lockMode = null) + { + return (T) session.Get(entityName, id, lockMode); + } } /// diff --git a/src/NHibernate/IStatelessSession.cs b/src/NHibernate/IStatelessSession.cs index df8fcce4063..65612c5f3c5 100644 --- a/src/NHibernate/IStatelessSession.cs +++ b/src/NHibernate/IStatelessSession.cs @@ -11,7 +11,7 @@ namespace NHibernate { // 6.0 TODO: Convert most of these extensions to interface methods - public static class StatelessSessionExtensions + public static partial class StatelessSessionExtensions { /// /// Creates a for the session. @@ -31,6 +31,12 @@ public static IQueryBatch CreateQueryBatch(this IStatelessSession session) /// The current transaction or .. public static ITransaction GetCurrentTransaction(this IStatelessSession session) => session.GetSessionImplementation().ConnectionManager.CurrentTransaction; + + //NOTE: Keep it as extension + public static T Get(this IStatelessSession session, string entityName, object id, LockMode lockMode = null) + { + return (T) session.Get(entityName, id, lockMode); + } } /// diff --git a/src/NHibernate/Impl/SessionImpl.cs b/src/NHibernate/Impl/SessionImpl.cs index b690cd8d637..6741c98e4d8 100644 --- a/src/NHibernate/Impl/SessionImpl.cs +++ b/src/NHibernate/Impl/SessionImpl.cs @@ -1158,23 +1158,17 @@ public object Load(System.Type entityClass, object id) public T Get(object id) { - using (BeginProcess()) - { - return (T)Get(typeof(T), id); - } + return Get(id, lockMode: null); } public T Get(object id, LockMode lockMode) { - using (BeginProcess()) - { - return (T)Get(typeof(T), id, lockMode); - } + return (T) Get(typeof(T), id, lockMode); } public object Get(System.Type entityClass, object id) { - return Get(entityClass.FullName, id); + return Get(entityClass, id, lockMode: null); } /// @@ -1189,12 +1183,31 @@ public object Get(System.Type entityClass, object id) /// /// public object Get(System.Type clazz, object id, LockMode lockMode) + { + return Get(clazz.FullName, id, lockMode); + } + + /// + /// Return the persistent instance of the given entity name with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + public object Get(string entityName, object id, LockMode lockMode) { using (BeginProcess()) { - LoadEvent loadEvent = new LoadEvent(id, clazz.FullName, lockMode, this); - FireLoad(loadEvent, LoadEventListener.Get); - return loadEvent.Result; + LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this); + bool success = false; + try + { + FireLoad(loadEvent, LoadEventListener.Get); + success = true; + return loadEvent.Result; + } + finally + { + AfterOperation(success); + } } } @@ -1228,21 +1241,7 @@ public string GetEntityName(object obj) public object Get(string entityName, object id) { - using (BeginProcess()) - { - LoadEvent loadEvent = new LoadEvent(id, entityName, false, this); - bool success = false; - try - { - FireLoad(loadEvent, LoadEventListener.Get); - success = true; - return loadEvent.Result; - } - finally - { - AfterOperation(success); - } - } + return Get(entityName, id, null); } /// From cfa7a0e85524271e5136873775cc8536f06cd0c0 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Sun, 17 May 2020 00:10:05 +0300 Subject: [PATCH 2/5] Update comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Frédéric Delaporte <12201973+fredericDelaporte@users.noreply.github.com> --- src/NHibernate/ISession.cs | 11 +++++++++++ src/NHibernate/IStatelessSession.cs | 11 +++++++++++ src/NHibernate/Impl/SessionImpl.cs | 6 +----- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/NHibernate/ISession.cs b/src/NHibernate/ISession.cs index 0b939e3db67..c41b7f771e5 100644 --- a/src/NHibernate/ISession.cs +++ b/src/NHibernate/ISession.cs @@ -54,6 +54,11 @@ public static ITransaction GetCurrentTransaction(this ISession session) /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is /// already associated with the session, return that instance or proxy.) /// + /// The session. + /// The entity name. + /// The entity identifier. + /// The lock mode to use for getting the entity. + /// A persistent instance, or . public static object Get(this ISession session, string entityName, object id, LockMode lockMode) { return @@ -68,6 +73,12 @@ public static object Get(this ISession session, string entityName, object id, Lo /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is /// already associated with the session, return that instance or proxy.) /// + /// The entity class. + /// The session. + /// The entity name. + /// The entity identifier. + /// The lock mode to use for getting the entity. + /// A persistent instance, or . public static T Get(this ISession session, string entityName, object id, LockMode lockMode = null) { return (T) session.Get(entityName, id, lockMode); diff --git a/src/NHibernate/IStatelessSession.cs b/src/NHibernate/IStatelessSession.cs index 65612c5f3c5..98b3de3a7c4 100644 --- a/src/NHibernate/IStatelessSession.cs +++ b/src/NHibernate/IStatelessSession.cs @@ -33,6 +33,17 @@ public static ITransaction GetCurrentTransaction(this IStatelessSession session) => session.GetSessionImplementation().ConnectionManager.CurrentTransaction; //NOTE: Keep it as extension + /// + /// Return the persistent instance of the given entity name with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + /// The entity class. + /// The session. + /// The entity name. + /// The entity identifier. + /// The lock mode to use for getting the entity. + /// A persistent instance, or . public static T Get(this IStatelessSession session, string entityName, object id, LockMode lockMode = null) { return (T) session.Get(entityName, id, lockMode); diff --git a/src/NHibernate/Impl/SessionImpl.cs b/src/NHibernate/Impl/SessionImpl.cs index 6741c98e4d8..fc71af0a4c7 100644 --- a/src/NHibernate/Impl/SessionImpl.cs +++ b/src/NHibernate/Impl/SessionImpl.cs @@ -1187,11 +1187,7 @@ public object Get(System.Type clazz, object id, LockMode lockMode) return Get(clazz.FullName, id, lockMode); } - /// - /// Return the persistent instance of the given entity name with the given identifier, or null - /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is - /// already associated with the session, return that instance or proxy.) - /// + /// public object Get(string entityName, object id, LockMode lockMode) { using (BeginProcess()) From a75f54cf4756e0cbae9fc4193d414a194e981547 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Sun, 17 May 2020 00:30:16 +0300 Subject: [PATCH 3/5] Async regen --- src/NHibernate/Async/ISession.cs | 13 +++++++++++++ src/NHibernate/Async/IStatelessSession.cs | 12 ++++++++++++ src/NHibernate/Async/Impl/SessionImpl.cs | 6 +----- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/NHibernate/Async/ISession.cs b/src/NHibernate/Async/ISession.cs index 152c9213b80..b7847827240 100644 --- a/src/NHibernate/Async/ISession.cs +++ b/src/NHibernate/Async/ISession.cs @@ -34,6 +34,12 @@ public static partial class SessionExtensions /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is /// already associated with the session, return that instance or proxy.) /// + /// The session. + /// The entity name. + /// The entity identifier. + /// The lock mode to use for getting the entity. + /// A cancellation token that can be used to cancel the work + /// A persistent instance, or . public static Task GetAsync(this ISession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken.IsCancellationRequested) @@ -59,6 +65,13 @@ public static partial class SessionExtensions /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is /// already associated with the session, return that instance or proxy.) /// + /// The entity class. + /// The session. + /// The entity name. + /// The entity identifier. + /// The lock mode to use for getting the entity. + /// A cancellation token that can be used to cancel the work + /// A persistent instance, or . public static async Task GetAsync(this ISession session, string entityName, object id, LockMode lockMode = null, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); diff --git a/src/NHibernate/Async/IStatelessSession.cs b/src/NHibernate/Async/IStatelessSession.cs index c59fed33baf..b744ca372f4 100644 --- a/src/NHibernate/Async/IStatelessSession.cs +++ b/src/NHibernate/Async/IStatelessSession.cs @@ -26,6 +26,18 @@ public static partial class StatelessSessionExtensions { //NOTE: Keep it as extension + /// + /// Return the persistent instance of the given entity name with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + /// The entity class. + /// The session. + /// The entity name. + /// The entity identifier. + /// The lock mode to use for getting the entity. + /// A cancellation token that can be used to cancel the work + /// A persistent instance, or . public static async Task GetAsync(this IStatelessSession session, string entityName, object id, LockMode lockMode = null, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); diff --git a/src/NHibernate/Async/Impl/SessionImpl.cs b/src/NHibernate/Async/Impl/SessionImpl.cs index 45c0fe162d0..17442ef6e86 100644 --- a/src/NHibernate/Async/Impl/SessionImpl.cs +++ b/src/NHibernate/Async/Impl/SessionImpl.cs @@ -848,11 +848,7 @@ public override async Task AutoFlushIfRequiredAsync(ISet querySpac return GetAsync(clazz.FullName, id, lockMode, cancellationToken); } - /// - /// Return the persistent instance of the given entity name with the given identifier, or null - /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is - /// already associated with the session, return that instance or proxy.) - /// + /// public async Task GetAsync(string entityName, object id, LockMode lockMode, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); From 0b16d0abccf756cab15f51cc9dfd59c5b8da6c93 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Sun, 17 May 2020 17:35:13 +0300 Subject: [PATCH 4/5] Do not call AfterOperation for overloads with LockMode --- src/NHibernate/Async/ISession.cs | 20 +++++++++- src/NHibernate/Async/Impl/SessionImpl.cs | 49 ++++++++++-------------- src/NHibernate/ISession.cs | 18 ++++++++- src/NHibernate/Impl/SessionImpl.cs | 36 ++++++++--------- 4 files changed, 75 insertions(+), 48 deletions(-) diff --git a/src/NHibernate/Async/ISession.cs b/src/NHibernate/Async/ISession.cs index b7847827240..71cf958c903 100644 --- a/src/NHibernate/Async/ISession.cs +++ b/src/NHibernate/Async/ISession.cs @@ -72,11 +72,29 @@ public static partial class SessionExtensions /// The lock mode to use for getting the entity. /// A cancellation token that can be used to cancel the work /// A persistent instance, or . - public static async Task GetAsync(this ISession session, string entityName, object id, LockMode lockMode = null, CancellationToken cancellationToken = default(CancellationToken)) + public static async Task GetAsync(this ISession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false); } + + //NOTE: Keep it as extension + /// + /// Return the persistent instance of the given entity name with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + /// The entity class. + /// The session. + /// The entity name. + /// The entity identifier. + /// A cancellation token that can be used to cancel the work + /// A persistent instance, or . + public static async Task GetAsync(this ISession session, string entityName, object id, CancellationToken cancellationToken = default(CancellationToken)) + { + cancellationToken.ThrowIfCancellationRequested(); + return (T) await (session.GetAsync(entityName, id, cancellationToken)).ConfigureAwait(false); + } } public partial interface ISession : IDisposable diff --git a/src/NHibernate/Async/Impl/SessionImpl.cs b/src/NHibernate/Async/Impl/SessionImpl.cs index 17442ef6e86..03d89beaa52 100644 --- a/src/NHibernate/Async/Impl/SessionImpl.cs +++ b/src/NHibernate/Async/Impl/SessionImpl.cs @@ -803,42 +803,31 @@ public override async Task AutoFlushIfRequiredAsync(ISet querySpac return LoadAsync(entityClass.FullName, id, cancellationToken); } - public Task GetAsync(object id, CancellationToken cancellationToken = default(CancellationToken)) + /// + public async Task GetAsync(object id, CancellationToken cancellationToken = default(CancellationToken)) { - if (cancellationToken.IsCancellationRequested) - { - return Task.FromCanceled(cancellationToken); - } - return GetAsync(id, lockMode: null, cancellationToken: cancellationToken); + cancellationToken.ThrowIfCancellationRequested(); + return (T) await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false); } + /// public async Task GetAsync(object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); return (T) await (GetAsync(typeof(T), id, lockMode, cancellationToken)).ConfigureAwait(false); } + /// public Task GetAsync(System.Type entityClass, object id, CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken.IsCancellationRequested) { return Task.FromCanceled(cancellationToken); } - return GetAsync(entityClass, id, lockMode: null, cancellationToken: cancellationToken); + return GetAsync(entityClass.FullName, id, cancellationToken); } - /// - /// Load the data for the object with the specified id into a newly created object - /// using "for update", if supported. A new key will be assigned to the object. - /// This should return an existing proxy where appropriate. - /// - /// If the object does not exist in the database, null is returned. - /// - /// - /// - /// - /// A cancellation token that can be used to cancel the work - /// + /// public Task GetAsync(System.Type clazz, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken.IsCancellationRequested) @@ -855,6 +844,19 @@ public async Task GetAsync(string entityName, object id, LockMode lockMo using (BeginProcess()) { LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this); + await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false); + //Note: AfterOperation call is skipped to avoid releasing the lock when outside of a transaction. + return loadEvent.Result; + } + } + + /// + public async Task GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken)) + { + cancellationToken.ThrowIfCancellationRequested(); + using (BeginProcess()) + { + LoadEvent loadEvent = new LoadEvent(id, entityName, null, this); bool success = false; try { @@ -898,15 +900,6 @@ public async Task GetAsync(string entityName, object id, LockMode lockMo } } - public Task GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken)) - { - if (cancellationToken.IsCancellationRequested) - { - return Task.FromCanceled(cancellationToken); - } - return GetAsync(entityName, id, null, cancellationToken); - } - /// /// Load the data for the object with the specified id into a newly created object. /// This is only called when lazily initializing a proxy. diff --git a/src/NHibernate/ISession.cs b/src/NHibernate/ISession.cs index c41b7f771e5..237dc9823c0 100644 --- a/src/NHibernate/ISession.cs +++ b/src/NHibernate/ISession.cs @@ -79,10 +79,26 @@ public static object Get(this ISession session, string entityName, object id, Lo /// The entity identifier. /// The lock mode to use for getting the entity. /// A persistent instance, or . - public static T Get(this ISession session, string entityName, object id, LockMode lockMode = null) + public static T Get(this ISession session, string entityName, object id, LockMode lockMode) { return (T) session.Get(entityName, id, lockMode); } + + //NOTE: Keep it as extension + /// + /// Return the persistent instance of the given entity name with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + /// The entity class. + /// The session. + /// The entity name. + /// The entity identifier. + /// A persistent instance, or . + public static T Get(this ISession session, string entityName, object id) + { + return (T) session.Get(entityName, id); + } } /// diff --git a/src/NHibernate/Impl/SessionImpl.cs b/src/NHibernate/Impl/SessionImpl.cs index fc71af0a4c7..341a8df46aa 100644 --- a/src/NHibernate/Impl/SessionImpl.cs +++ b/src/NHibernate/Impl/SessionImpl.cs @@ -1156,32 +1156,25 @@ public object Load(System.Type entityClass, object id) return Load(entityClass.FullName, id); } + /// public T Get(object id) { - return Get(id, lockMode: null); + return (T) Get(typeof(T), id); } + /// public T Get(object id, LockMode lockMode) { return (T) Get(typeof(T), id, lockMode); } + /// public object Get(System.Type entityClass, object id) { - return Get(entityClass, id, lockMode: null); + return Get(entityClass.FullName, id); } - /// - /// Load the data for the object with the specified id into a newly created object - /// using "for update", if supported. A new key will be assigned to the object. - /// This should return an existing proxy where appropriate. - /// - /// If the object does not exist in the database, null is returned. - /// - /// - /// - /// - /// + /// public object Get(System.Type clazz, object id, LockMode lockMode) { return Get(clazz.FullName, id, lockMode); @@ -1193,6 +1186,18 @@ public object Get(string entityName, object id, LockMode lockMode) using (BeginProcess()) { LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this); + FireLoad(loadEvent, LoadEventListener.Get); + //Note: AfterOperation call is skipped to avoid releasing the lock when outside of a transaction. + return loadEvent.Result; + } + } + + /// + public object Get(string entityName, object id) + { + using (BeginProcess()) + { + LoadEvent loadEvent = new LoadEvent(id, entityName, null, this); bool success = false; try { @@ -1235,11 +1240,6 @@ public string GetEntityName(object obj) } } - public object Get(string entityName, object id) - { - return Get(entityName, id, null); - } - /// /// Load the data for the object with the specified id into a newly created object. /// This is only called when lazily initializing a proxy. From bc34f14123c4ef5c42e73b7bcede951ffb9428ee Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Wed, 20 May 2020 14:53:15 +0300 Subject: [PATCH 5/5] Split extension and small refactoring --- src/NHibernate/Async/IStatelessSession.cs | 20 +++++++++++- .../Async/Impl/StatelessSessionImpl.cs | 31 +++++-------------- src/NHibernate/IStatelessSession.cs | 18 ++++++++++- src/NHibernate/Impl/StatelessSessionImpl.cs | 27 +++++----------- 4 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/NHibernate/Async/IStatelessSession.cs b/src/NHibernate/Async/IStatelessSession.cs index b744ca372f4..040f5b92e7c 100644 --- a/src/NHibernate/Async/IStatelessSession.cs +++ b/src/NHibernate/Async/IStatelessSession.cs @@ -38,11 +38,29 @@ public static partial class StatelessSessionExtensions /// The lock mode to use for getting the entity. /// A cancellation token that can be used to cancel the work /// A persistent instance, or . - public static async Task GetAsync(this IStatelessSession session, string entityName, object id, LockMode lockMode = null, CancellationToken cancellationToken = default(CancellationToken)) + public static async Task GetAsync(this IStatelessSession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false); } + + //NOTE: Keep it as extension + /// + /// Return the persistent instance of the given entity name with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + /// The entity class. + /// The session. + /// The entity name. + /// The entity identifier. + /// A cancellation token that can be used to cancel the work + /// A persistent instance, or . + public static async Task GetAsync(this IStatelessSession session, string entityName, object id, CancellationToken cancellationToken = default(CancellationToken)) + { + cancellationToken.ThrowIfCancellationRequested(); + return (T) await (session.GetAsync(entityName, id, cancellationToken)).ConfigureAwait(false); + } } public partial interface IStatelessSession : IDisposable diff --git a/src/NHibernate/Async/Impl/StatelessSessionImpl.cs b/src/NHibernate/Async/Impl/StatelessSessionImpl.cs index 317f731c37a..162baafddc2 100644 --- a/src/NHibernate/Async/Impl/StatelessSessionImpl.cs +++ b/src/NHibernate/Async/Impl/StatelessSessionImpl.cs @@ -439,7 +439,7 @@ public async Task ManagedFlushAsync(CancellationToken cancellationToken) } } - /// Retrieve a entity. + /// Retrieve an entity. /// a detached entity instance public Task GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken)) { @@ -450,31 +450,19 @@ public async Task ManagedFlushAsync(CancellationToken cancellationToken) return GetAsync(entityName, id, LockMode.None, cancellationToken); } - /// Retrieve a entity. - /// + /// + /// Retrieve an entity. /// /// a detached entity instance /// public async Task GetAsync(object id, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); - using (BeginProcess()) - { - return (T)await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false); - } - } - - private Task GetAsync(System.Type persistentClass, object id, CancellationToken cancellationToken) - { - if (cancellationToken.IsCancellationRequested) - { - return Task.FromCanceled(cancellationToken); - } - return GetAsync(persistentClass.FullName, id, cancellationToken); + return (T) await (GetAsync(typeof(T).FullName, id, cancellationToken)).ConfigureAwait(false); } /// - /// Retrieve a entity, obtaining the specified lock mode. + /// Retrieve an entity, obtaining the specified lock mode. /// /// a detached entity instance public async Task GetAsync(string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) @@ -482,7 +470,7 @@ private Task GetAsync(System.Type persistentClass, object id, Cancellati cancellationToken.ThrowIfCancellationRequested(); using (BeginProcess()) { - object result = await (Factory.GetEntityPersister(entityName).LoadAsync(id, null, lockMode, this, cancellationToken)).ConfigureAwait(false); + object result = await (Factory.GetEntityPersister(entityName).LoadAsync(id, null, lockMode ?? LockMode.None, this, cancellationToken)).ConfigureAwait(false); if (temporaryPersistenceContext.IsLoadFinished) { temporaryPersistenceContext.Clear(); @@ -492,16 +480,13 @@ private Task GetAsync(System.Type persistentClass, object id, Cancellati } /// - /// Retrieve a entity, obtaining the specified lock mode. + /// Retrieve an entity, obtaining the specified lock mode. /// /// a detached entity instance public async Task GetAsync(object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); - using (BeginProcess()) - { - return (T)await (GetAsync(typeof(T).FullName, id, lockMode, cancellationToken)).ConfigureAwait(false); - } + return (T) await (GetAsync(typeof(T).FullName, id, lockMode, cancellationToken)).ConfigureAwait(false); } /// diff --git a/src/NHibernate/IStatelessSession.cs b/src/NHibernate/IStatelessSession.cs index 98b3de3a7c4..483caec8224 100644 --- a/src/NHibernate/IStatelessSession.cs +++ b/src/NHibernate/IStatelessSession.cs @@ -44,10 +44,26 @@ public static ITransaction GetCurrentTransaction(this IStatelessSession session) /// The entity identifier. /// The lock mode to use for getting the entity. /// A persistent instance, or . - public static T Get(this IStatelessSession session, string entityName, object id, LockMode lockMode = null) + public static T Get(this IStatelessSession session, string entityName, object id, LockMode lockMode) { return (T) session.Get(entityName, id, lockMode); } + + //NOTE: Keep it as extension + /// + /// Return the persistent instance of the given entity name with the given identifier, or null + /// if there is no such persistent instance. (If the instance, or a proxy for the instance, is + /// already associated with the session, return that instance or proxy.) + /// + /// The entity class. + /// The session. + /// The entity name. + /// The entity identifier. + /// A persistent instance, or . + public static T Get(this IStatelessSession session, string entityName, object id) + { + return (T) session.Get(entityName, id); + } } /// diff --git a/src/NHibernate/Impl/StatelessSessionImpl.cs b/src/NHibernate/Impl/StatelessSessionImpl.cs index 12ff0fde552..b42b746b593 100644 --- a/src/NHibernate/Impl/StatelessSessionImpl.cs +++ b/src/NHibernate/Impl/StatelessSessionImpl.cs @@ -539,40 +539,32 @@ public void Delete(string entityName, object entity) } } - /// Retrieve a entity. + /// Retrieve an entity. /// a detached entity instance public object Get(string entityName, object id) { return Get(entityName, id, LockMode.None); } - /// Retrieve a entity. - /// + /// + /// Retrieve an entity. /// /// a detached entity instance /// public T Get(object id) { - using (BeginProcess()) - { - return (T)Get(typeof(T), id); - } - } - - private object Get(System.Type persistentClass, object id) - { - return Get(persistentClass.FullName, id); + return (T) Get(typeof(T).FullName, id); } /// - /// Retrieve a entity, obtaining the specified lock mode. + /// Retrieve an entity, obtaining the specified lock mode. /// /// a detached entity instance public object Get(string entityName, object id, LockMode lockMode) { using (BeginProcess()) { - object result = Factory.GetEntityPersister(entityName).Load(id, null, lockMode, this); + object result = Factory.GetEntityPersister(entityName).Load(id, null, lockMode ?? LockMode.None, this); if (temporaryPersistenceContext.IsLoadFinished) { temporaryPersistenceContext.Clear(); @@ -582,15 +574,12 @@ public object Get(string entityName, object id, LockMode lockMode) } /// - /// Retrieve a entity, obtaining the specified lock mode. + /// Retrieve an entity, obtaining the specified lock mode. /// /// a detached entity instance public T Get(object id, LockMode lockMode) { - using (BeginProcess()) - { - return (T)Get(typeof(T).FullName, id, lockMode); - } + return (T) Get(typeof(T).FullName, id, lockMode); } ///