39 lines
1.9 KiB
C#
39 lines
1.9 KiB
C#
using AyCode.Core.Helpers;
|
|
using AyCode.Database.DataLayers;
|
|
using AyCode.Database.DbContexts;
|
|
using AyCode.Interfaces.Entities;
|
|
|
|
namespace AyCode.Database.Extensions;
|
|
|
|
public static class DalExtension
|
|
{
|
|
public static Task<TResultType> SessionAsync<TDbContext, TResultType>(this IDalBase<TDbContext> dal, Func<TDbContext, TResultType> callback) where TDbContext : DbContextBase
|
|
=> TaskHelper.ToThreadPoolTask(() => dal.Session(callback));
|
|
|
|
public static Task<IEnumerable<TEntity>> SessionAsync<TDbContext, TEntity>(this IDalBase<TDbContext> dal, Func<TDbContext, IQueryable<TEntity>> callback) where TEntity : IEntity where TDbContext : DbContextBase
|
|
=> TaskHelper.ToThreadPoolTask(() => dal.Session(callback));
|
|
|
|
public static TResultType Session<TDbContext, TResultType>(this IDalBase<TDbContext> dal, Func<TDbContext, TResultType> callback) where TDbContext : DbContextBase
|
|
{
|
|
using var ctx = dal.CreateDbContext();
|
|
|
|
return ctx.Session(callback);
|
|
}
|
|
|
|
public static IEnumerable<TEntity> Session<TDbContext, TEntity>(this IDalBase<TDbContext> dal, Func<TDbContext, IQueryable<TEntity>> callback) where TEntity : IEntity where TDbContext : DbContextBase
|
|
{
|
|
using var ctx = dal.CreateDbContext();
|
|
|
|
return ctx.Session(callback);
|
|
}
|
|
|
|
public static Task<bool> TransactionAsync<TDbContext>(this IDalBase<TDbContext> dal, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : DbContextBase
|
|
=> TaskHelper.ToThreadPoolTask(() => dal.Transaction(callbackTransactionBody, throwException));
|
|
|
|
public static bool Transaction<TDbContext>(this IDalBase<TDbContext> dal, Func<TDbContext, bool> callbackTransactionBody, bool throwException = false) where TDbContext : DbContextBase
|
|
{
|
|
using var ctx = dal.CreateDbContext();
|
|
|
|
return ctx.Transaction(callbackTransactionBody, throwException);
|
|
}
|
|
} |