Skip to content

Commit

Permalink
WithNoLock Add
Browse files Browse the repository at this point in the history
  • Loading branch information
enisgurkann committed Sep 6, 2023
1 parent 9f28471 commit 1ee52f7
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 158 deletions.
358 changes: 200 additions & 158 deletions EnLock/EnExtention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> ToAnyWithNoLockAsync<T>(this IQueryable<T> query,
CancellationToken cancellationToken = default)
{
public static async Task<bool> ToAnyWithNoLockAsync<T>(this IQueryable<T> 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<T[]> ToArrayWithNoLockAsync<T>(this IQueryable<T> 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<T> WithNoLock<T>(this IQueryable<T> 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<T[]> ToArrayWithNoLockAsync<T>(this IQueryable<T> 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<List<T>> ToListWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default)

return result;
}

public static async Task<List<T>> ToListWithNoLockAsync<T>(this IQueryable<T> query,
CancellationToken cancellationToken = default)
{
List<T> result = default;
using (var scope = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions()
{
IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
},
TransactionScopeAsyncFlowOption.Enabled))
{
List<T> 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<T> ToListWithNoLock<T>(this IQueryable<T> query)

return result;
}

public static List<T> ToListWithNoLock<T>(this IQueryable<T> query)
{
List<T> result = default;
using (var scope = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions()
{
IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
},
TransactionScopeAsyncFlowOption.Enabled))
{
List<T> 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<T> ToFirstOrDefaultWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default)

return result;
}

public static async Task<T> ToFirstOrDefaultWithNoLockAsync<T>(this IQueryable<T> 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<T> ToFirstOrDefaultWithNoLockAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)

return result;
}

public static async Task<T> ToFirstOrDefaultWithNoLockAsync<T>(this IQueryable<T> query,
Expression<Func<T, bool>> 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<T> ToFirstWithNoLockAsync<T>(this IQueryable<T> query,Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)

return result;
}

public static async Task<T> ToFirstWithNoLockAsync<T>(this IQueryable<T> query,
Expression<Func<T, bool>> 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<T> ToFirstWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default)

return result;
}

public static async Task<T> ToFirstWithNoLockAsync<T>(this IQueryable<T> 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<T> ToSingleWithNoLockAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default)

return result;
}

public static async Task<T> ToSingleWithNoLockAsync<T>(this IQueryable<T> 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<T> ToSingleWithNoLockAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)

return result;
}

public static async Task<T> ToSingleWithNoLockAsync<T>(this IQueryable<T> query,
Expression<Func<T, bool>> 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
/// <summary>
/// No Lock tag added
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TDbContext"></typeparam>
/// <param name="dbContext"></param>
/// <param name="func"></param>
/// <returns></returns>
public static T NoLock<T, TDbContext>(this TDbContext dbContext, Func<TDbContext, T> func) where TDbContext : DbContext

return result;
}


#region NoLockFunc

/// <summary>
/// No Lock tag added
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TDbContext"></typeparam>
/// <param name="dbContext"></param>
/// <param name="func"></param>
/// <returns></returns>
public static T NoLock<T, TDbContext>(this TDbContext dbContext, Func<TDbContext, T> 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
}
1 change: 1 addition & 0 deletions EnLock/EnLock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageVersion>1.0.0.1</PackageVersion>
<PackageLicenseUrl>https://raw.githubusercontent.com/enisgurkann/ENLOCK/master/LICENSE</PackageLicenseUrl>
<PackageIconUrl>https://github.com/enisgurkann/ENLOCK/blob/master/ENLOCK.png?raw=true</PackageIconUrl>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 1ee52f7

Please sign in to comment.