49 lines
2.2 KiB
C#
49 lines
2.2 KiB
C#
using AyCode.Core.Helpers;
|
|
using AyCode.Database.DataLayers;
|
|
using AyCode.Database.DbContexts;
|
|
using AyCode.Interfaces.Entities;
|
|
using AyCode.Utils.Extensions;
|
|
|
|
namespace AyCode.Database.Extensions;
|
|
|
|
public static class AcDalExtension
|
|
{
|
|
public static Task<TResultType> SessionAsync<TDbContext, TResultType>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, TResultType> callback) where TDbContext : AcDbContextBase
|
|
=> TaskHelper.ToThreadPoolTask(() => acDal.Session(callback));
|
|
|
|
public static Task<IEnumerable<TEntity>> SessionAsync<TDbContext, TEntity>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, IQueryable<TEntity>> callback) where TEntity : IEntity where TDbContext : AcDbContextBase
|
|
=> TaskHelper.ToThreadPoolTask(() => acDal.Session(callback));
|
|
|
|
public static TResultType Session<TDbContext, TResultType>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, TResultType> callback) where TDbContext : AcDbContextBase
|
|
{
|
|
//using var ctx = acDal.CreateDbContext();
|
|
|
|
using (acDal.MutextLock.UseWaitOne())
|
|
{
|
|
return acDal.Context.Session(callback);
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<TEntity> Session<TDbContext, TEntity>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, IQueryable<TEntity>> callback) where TEntity : IEntity where TDbContext : AcDbContextBase
|
|
{
|
|
//using var ctx = acDal.CreateDbContext();
|
|
|
|
using (acDal.MutextLock.UseWaitOne())
|
|
{
|
|
return acDal.Context.Session(callback);
|
|
}
|
|
}
|
|
|
|
public static Task<bool> TransactionAsync<TDbContext>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : AcDbContextBase
|
|
=> TaskHelper.ToThreadPoolTask(() => acDal.Transaction(callbackTransactionBody, throwException));
|
|
|
|
public static bool Transaction<TDbContext>(this IAcDalBase<TDbContext> acDal, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : AcDbContextBase
|
|
{
|
|
//using var ctx = acDal.CreateDbContext();
|
|
|
|
using (acDal.MutextLock.UseWaitOne())
|
|
{
|
|
return acDal.Context.Transaction(callbackTransactionBody, throwException);
|
|
}
|
|
}
|
|
} |