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 SessionAsync(this IDalBase dal, Func callback) where TDbContext : DbContextBase => TaskHelper.ToThreadPoolTask(() => dal.Session(callback)); public static Task> SessionAsync(this IDalBase dal, Func> callback) where TEntity : IEntity where TDbContext : DbContextBase => TaskHelper.ToThreadPoolTask(() => dal.Session(callback)); public static TResultType Session(this IDalBase dal, Func callback) where TDbContext : DbContextBase { using var ctx = dal.CreateDbContext(); return ctx.Session(callback); } public static IEnumerable Session(this IDalBase dal, Func> callback) where TEntity : IEntity where TDbContext : DbContextBase { using var ctx = dal.CreateDbContext(); return ctx.Session(callback); } public static Task TransactionAsync(this IDalBase dal, Func callbackTransactionBody, bool throwException = false) where TDbContext : DbContextBase => TaskHelper.ToThreadPoolTask(() => dal.Transaction(callbackTransactionBody, throwException)); public static bool Transaction(this IDalBase dal, Func callbackTransactionBody, bool throwException = false) where TDbContext : DbContextBase { using var ctx = dal.CreateDbContext(); return ctx.Transaction(callbackTransactionBody, throwException); } }