using Microsoft.EntityFrameworkCore; using ZeroFramework.DeviceCenter.Domain.Specifications; using ZeroFramework.DeviceCenter.Domain.Specifications.Evaluators; using ZeroFramework.DeviceCenter.Infrastructure.Specifications.Evaluators; using ZeroFramework.DeviceCenter.Infrastructure.Specifications.Extensions; namespace ZeroFramework.DeviceCenter.Infrastructure.Specifications.Extensions { public static class DbSetExtensions { public static async Task> ToListAsync(this DbSet source, ISpecification specification, CancellationToken cancellationToken = default) where TSource : class { var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); return specification.PostProcessingAction == null ? result : specification.PostProcessingAction(result).ToList(); } public static async Task> ToEnumerableAsync(this DbSet source, ISpecification specification, CancellationToken cancellationToken = default) where TSource : class { var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); return specification.PostProcessingAction == null ? result : specification.PostProcessingAction(result); } public static IQueryable WithSpecification(this IQueryable source, ISpecification specification, ISpecificationEvaluator? evaluator = null) where TSource : class { evaluator ??= SpecificationEvaluator.Default; return evaluator.GetQuery(source, specification); } } }