using DigitalOps.Dal.Abstractions; using FluentValidation; using Microsoft.EntityFrameworkCore; namespace DigitalOps.Dal.Validators; public class DbEntityExistValidator : AbstractValidator where TEntity : class, IHasId { private readonly MainDbContext _dbContext; public DbEntityExistValidator(MainDbContext dbContext) { _dbContext = dbContext; RuleFor(value => value) .NotNull() .MustAsync(IsEntityExist) .WithMessage("Invalid Entity"); } private Task IsEntityExist(TValue id, CancellationToken cancellationToken) => _dbContext.Set().AnyAsync(entity => entity.Id!.Equals(id), cancellationToken); }