using AyCode.Core.Consts; using AyCode.Core.Helpers; using AyCode.Core.Logger; using AyCode.Database.DbContexts; using AyCode.Interfaces.Entities; namespace AyCode.Database.Extensions; public static class DbSessionExtension { public static Task SessionAsync(this TDbContext ctx, Func callback) where TDbContext : DbContextBase => TaskHelper.ToThreadPoolTask(() => ctx.Session(callback)); public static TResultType Session(this TDbContext ctx, Func callback) where TDbContext : DbContextBase { TResultType result; try { result = callback(ctx); } catch (Exception ex) { var errorText = $"Session({ctx}) callback error...{AcEnv.NL}"; Logger.Error($"{errorText}", ex); throw new Exception($"{errorText}{ex}"); } return result; } public static IEnumerable Session(this TDbContext ctx, Func> callback) where TEntity : IEntity where TDbContext : DbContextBase { foreach (var entity in callback(ctx)) yield return entity; } }