Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: format code with dotnet-format #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Extensions/IValidatedDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public interface IValidatedDbContext : IDbContext, IValidatableObject
}

public interface IValidatedDbContext<T> : IValidatedDbContext, IDbContext<T>
where T : IValidatedDbContext<T> { }
where T : IValidatedDbContext<T>
{ }
70 changes: 35 additions & 35 deletions src/Extensions/ValidatedDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,39 @@ public class ValidatedDbContext(DbContextOptions options) : DbContext(options),
public virtual IQueryable<TEntity> FromExpression<TEntity>(
Expression<Func<TEntity, bool>> expression
)
where TEntity : class => Set<TEntity>().Where(expression);
public override Task<int> SaveChangesAsync(
CancellationToken cancellationToken = new CancellationToken()
)
{
Validate();
return base.SaveChangesAsync(cancellationToken);
}
public virtual IEnumerable<ValidationResult> Validate(ValidationContext context) => Validate();
public virtual IEnumerable<ValidationResult> Validate()
{
var changedEntities = ChangeTracker
.Entries()
.Where(_ => _.State == EntityState.Added || _.State == EntityState.Modified);
var errors = new List<ValidationResult>(); // all errors are here
foreach (var e in changedEntities)
{
var vc = new ValidationContext(e.Entity, null, null);
Validator.TryValidateObject(e.Entity, vc, errors, validateAllProperties: true);
}
if (errors.Any())
{
throw new DbEntityValidationException(
"Validation failed for one or more entities.",
errors
);
}
return errors;
}
where TEntity : class => Set<TEntity>().Where(expression);

public override Task<int> SaveChangesAsync(
CancellationToken cancellationToken = new CancellationToken()
)
{
Validate();
return base.SaveChangesAsync(cancellationToken);
}

public virtual IEnumerable<ValidationResult> Validate(ValidationContext context) => Validate();

public virtual IEnumerable<ValidationResult> Validate()
{
var changedEntities = ChangeTracker
.Entries()
.Where(_ => _.State == EntityState.Added || _.State == EntityState.Modified);

var errors = new List<ValidationResult>(); // all errors are here
foreach (var e in changedEntities)
{
var vc = new ValidationContext(e.Entity, null, null);
Validator.TryValidateObject(e.Entity, vc, errors, validateAllProperties: true);
}

if (errors.Any())
{
throw new DbEntityValidationException(
"Validation failed for one or more entities.",
errors
);
}

return errors;
}
}