From 1ee52f74bd6b8a192967558a6fa88e0ae298fb72 Mon Sep 17 00:00:00 2001 From: enis Date: Wed, 6 Sep 2023 10:43:38 +0300 Subject: [PATCH] WithNoLock Add --- EnLock/EnExtention.cs | 358 +++++++++++++++++++++++------------------- EnLock/EnLock.csproj | 1 + 2 files changed, 201 insertions(+), 158 deletions(-) diff --git a/EnLock/EnExtention.cs b/EnLock/EnExtention.cs index e09e9ae..44807f8 100644 --- a/EnLock/EnExtention.cs +++ b/EnLock/EnExtention.cs @@ -7,186 +7,228 @@ using System.Threading.Tasks; using System.Transactions; -namespace EnLock +namespace EnLock; + +public static class EnExtention { - public static class EnExtention + public static async Task ToAnyWithNoLockAsync(this IQueryable query, + CancellationToken cancellationToken = default) { - public static async Task ToAnyWithNoLockAsync(this IQueryable query, CancellationToken cancellationToken = default) + bool result = default; + using (var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() + { + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled)) { - bool result = default; - using (var scope = new TransactionScope(TransactionScopeOption.Required, - new TransactionOptions() - { - IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted - }, - TransactionScopeAsyncFlowOption.Enabled)) - { - result = await query.AnyAsync(cancellationToken); - scope.Complete(); - } - return result; + result = await query.AnyAsync(cancellationToken); + scope.Complete(); } - public static async Task ToArrayWithNoLockAsync(this IQueryable query, CancellationToken cancellationToken = default) - { - T[] result = default; - using (var scope = new TransactionScope(TransactionScopeOption.Required, - new TransactionOptions() - { - IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted - }, - TransactionScopeAsyncFlowOption.Enabled)) + + return result; + } + + public static IQueryable WithNoLock(this IQueryable query, + CancellationToken cancellationToken = default) + { + using var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() { - result = await query.ToArrayAsync(cancellationToken); - scope.Complete(); - } - return result; + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled); + return query; + } + + public static async Task ToArrayWithNoLockAsync(this IQueryable query, + CancellationToken cancellationToken = default) + { + T[] result = default; + using (var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() + { + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled)) + { + result = await query.ToArrayAsync(cancellationToken); + scope.Complete(); } - public static async Task> ToListWithNoLockAsync(this IQueryable query, CancellationToken cancellationToken = default) + + return result; + } + + public static async Task> ToListWithNoLockAsync(this IQueryable query, + CancellationToken cancellationToken = default) + { + List result = default; + using (var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() + { + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled)) { - List result = default; - using (var scope = new TransactionScope(TransactionScopeOption.Required, - new TransactionOptions() - { - IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted - }, - TransactionScopeAsyncFlowOption.Enabled)) - { - result = await query.ToListAsync(cancellationToken); - scope.Complete(); - } - return result; + result = await query.ToListAsync(cancellationToken); + scope.Complete(); } - public static List ToListWithNoLock(this IQueryable query) + + return result; + } + + public static List ToListWithNoLock(this IQueryable query) + { + List result = default; + using (var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() + { + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled)) { - List result = default; - using (var scope = new TransactionScope(TransactionScopeOption.Required, - new TransactionOptions() - { - IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted - }, - TransactionScopeAsyncFlowOption.Enabled)) - { - result = query.ToList(); - scope.Complete(); - } - return result; + result = query.ToList(); + scope.Complete(); } - public static async Task ToFirstOrDefaultWithNoLockAsync(this IQueryable query, CancellationToken cancellationToken = default) + + return result; + } + + public static async Task ToFirstOrDefaultWithNoLockAsync(this IQueryable query, + CancellationToken cancellationToken = default) + { + T result = default; + using (var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() + { + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled)) { - T result = default; - using (var scope = new TransactionScope(TransactionScopeOption.Required, - new TransactionOptions() - { - IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted - }, - TransactionScopeAsyncFlowOption.Enabled)) - { - result = await query.FirstOrDefaultAsync(cancellationToken); - scope.Complete(); - } - return result; + result = await query.FirstOrDefaultAsync(cancellationToken); + scope.Complete(); } - public static async Task ToFirstOrDefaultWithNoLockAsync(this IQueryable query, Expression> predicate, CancellationToken cancellationToken = default) + + return result; + } + + public static async Task ToFirstOrDefaultWithNoLockAsync(this IQueryable query, + Expression> predicate, CancellationToken cancellationToken = default) + { + T result = default; + using (var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() + { + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled)) { - T result = default; - using (var scope = new TransactionScope(TransactionScopeOption.Required, - new TransactionOptions() - { - IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted - }, - TransactionScopeAsyncFlowOption.Enabled)) - { - result = await query.FirstOrDefaultAsync(predicate, cancellationToken); - scope.Complete(); - } - return result; + result = await query.FirstOrDefaultAsync(predicate, cancellationToken); + scope.Complete(); } - public static async Task ToFirstWithNoLockAsync(this IQueryable query,Expression> predicate, CancellationToken cancellationToken = default) + + return result; + } + + public static async Task ToFirstWithNoLockAsync(this IQueryable query, + Expression> predicate, CancellationToken cancellationToken = default) + { + T result = default; + using (var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() + { + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled)) { - T result = default; - using (var scope = new TransactionScope(TransactionScopeOption.Required, - new TransactionOptions() - { - IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted - }, - TransactionScopeAsyncFlowOption.Enabled)) - { - result = await query.FirstAsync(predicate,cancellationToken); - scope.Complete(); - } - return result; + result = await query.FirstAsync(predicate, cancellationToken); + scope.Complete(); } - public static async Task ToFirstWithNoLockAsync(this IQueryable query, CancellationToken cancellationToken = default) + + return result; + } + + public static async Task ToFirstWithNoLockAsync(this IQueryable query, + CancellationToken cancellationToken = default) + { + T result = default; + using (var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() + { + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled)) { - T result = default; - using (var scope = new TransactionScope(TransactionScopeOption.Required, - new TransactionOptions() - { - IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted - }, - TransactionScopeAsyncFlowOption.Enabled)) - { - result = await query.FirstAsync(cancellationToken); - scope.Complete(); - } - return result; + result = await query.FirstAsync(cancellationToken); + scope.Complete(); } - public static async Task ToSingleWithNoLockAsync(this IQueryable query, CancellationToken cancellationToken = default) + + return result; + } + + public static async Task ToSingleWithNoLockAsync(this IQueryable query, + CancellationToken cancellationToken = default) + { + T result = default; + using (var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() + { + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled)) { - T result = default; - using (var scope = new TransactionScope(TransactionScopeOption.Required, - new TransactionOptions() - { - IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted - }, - TransactionScopeAsyncFlowOption.Enabled)) - { - result = await query.SingleAsync(cancellationToken); - scope.Complete(); - } - return result; + result = await query.SingleAsync(cancellationToken); + scope.Complete(); } - - public static async Task ToSingleWithNoLockAsync(this IQueryable query, Expression> predicate, CancellationToken cancellationToken = default) + + return result; + } + + public static async Task ToSingleWithNoLockAsync(this IQueryable query, + Expression> predicate, CancellationToken cancellationToken = default) + { + T result = default; + using (var scope = new TransactionScope(TransactionScopeOption.Required, + new TransactionOptions() + { + IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted + }, + TransactionScopeAsyncFlowOption.Enabled)) { - T result = default; - using (var scope = new TransactionScope(TransactionScopeOption.Required, - new TransactionOptions() - { - IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted - }, - TransactionScopeAsyncFlowOption.Enabled)) - { - result = await query.SingleAsync(predicate,cancellationToken); - scope.Complete(); - } - return result; + result = await query.SingleAsync(predicate, cancellationToken); + scope.Complete(); } - - - #region NoLockFunc - /// - /// No Lock tag added - /// - /// - /// - /// - /// - /// - public static T NoLock(this TDbContext dbContext, Func func) where TDbContext : DbContext + + return result; + } + + + #region NoLockFunc + + /// + /// No Lock tag added + /// + /// + /// + /// + /// + /// + public static T NoLock(this TDbContext dbContext, Func func) + where TDbContext : DbContext + { + T result = default; + var transactionOptions = new TransactionOptions { - T result = default; - var transactionOptions = new TransactionOptions - { - IsolationLevel = IsolationLevel.ReadUncommitted - }; - using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions)) - { - result = func(dbContext); - scope.Complete(); - } - return result; + IsolationLevel = IsolationLevel.ReadUncommitted + }; + using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions)) + { + result = func(dbContext); + scope.Complete(); } - #endregion + + return result; } -} + + #endregion +} \ No newline at end of file diff --git a/EnLock/EnLock.csproj b/EnLock/EnLock.csproj index c1ceecd..0e0d15a 100644 --- a/EnLock/EnLock.csproj +++ b/EnLock/EnLock.csproj @@ -15,6 +15,7 @@ 1.0.0.1 https://raw.githubusercontent.com/enisgurkann/ENLOCK/master/LICENSE https://github.com/enisgurkann/ENLOCK/blob/master/ENLOCK.png?raw=true + latest