You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Typically, we implement logic and code related to resources or databases in services or commands. But what if we implemented database validations directly in the abstract validators? For instance, instead of writing three lines of code to check if something exists before proceeding, we could handle that logic within the validator. In some cases, we may not even need entity data in our command. I will provide examples to illustrate how this approach works.
I am just curious about the potential of developing these types of validations in validators. The advantages include smaller functions and a more focused role for validators. But as you see, in some cases we need the entity directly.
I would appreciate it if anyone, especially @jasontaylordev, could share their thoughts on this approach regarding the pros and cons of Clean Architecture.
//Validator
internal class DeleteTodoItemValidator : AbstractValidator<DeleteTodoItemCommand>
{
public DeleteTodoItemValidator(IApplicationDbContext context)
{
RuleFor(x => x.Id)
.MustAsync(async (id, cancellation) =>
await context.TodoItems.AnyAsync(c => c.Id == id, cancellation))
.WithMessage("'{PropertyName}' not found.")
.WithErrorCode("NotFound"); ;
}
}
//Handler
public class DeleteTodoItemCommandHandler : IRequestHandler<DeleteTodoItemCommand>
{
private readonly IApplicationDbContext _context;
public DeleteTodoItemCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task Handle(DeleteTodoItemCommand request, CancellationToken cancellationToken)
{
var entity = new TodoItem() { Id = request.Id };
_context.TodoItems.Remove(entity);
entity.AddDomainEvent(new TodoItemDeletedEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi everyone,
Typically, we implement logic and code related to resources or databases in services or commands. But what if we implemented database validations directly in the abstract validators? For instance, instead of writing three lines of code to check if something exists before proceeding, we could handle that logic within the validator. In some cases, we may not even need entity data in our command. I will provide examples to illustrate how this approach works.
I am just curious about the potential of developing these types of validations in validators. The advantages include smaller functions and a more focused role for validators. But as you see, in some cases we need the entity directly.
I would appreciate it if anyone, especially @jasontaylordev, could share their thoughts on this approach regarding the pros and cons of Clean Architecture.
Beta Was this translation helpful? Give feedback.
All reactions