C
C#12mo ago
Dropps

validation pipeline using ErrorOr package

public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TResponse : IErrorOr
{
private readonly IValidator<TRequest>? _validator;

public ValidationBehaviour(IValidator<TRequest>? validator)
{
_validator = validator;
}

public async Task<TResponse> Handle(
TRequest request,
RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken)
{
if (_validator == null)
{
return await next();
}

ValidationResult? validationResult = await _validator.ValidateAsync(request, cancellationToken);

if (validationResult.IsValid)
{
return await next();
}

var errors = validationResult.Errors
.ConvertAll(validationFailure =>
Error.Validation(
validationFailure.PropertyName,
validationFailure.ErrorMessage));

return (dynamic)errors;
}
}
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TResponse : IErrorOr
{
private readonly IValidator<TRequest>? _validator;

public ValidationBehaviour(IValidator<TRequest>? validator)
{
_validator = validator;
}

public async Task<TResponse> Handle(
TRequest request,
RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken)
{
if (_validator == null)
{
return await next();
}

ValidationResult? validationResult = await _validator.ValidateAsync(request, cancellationToken);

if (validationResult.IsValid)
{
return await next();
}

var errors = validationResult.Errors
.ConvertAll(validationFailure =>
Error.Validation(
validationFailure.PropertyName,
validationFailure.ErrorMessage));

return (dynamic)errors;
}
}
i have the feeling using dynamic to make the compiler know that we have implicit operators for errors is not the best idea anyone has suggsetions on what to do instead?
18 Replies
RD Dev
RD Dev12mo ago
Lemme see
Dropps
DroppsOP12mo ago
iam on .net 7 btw
RD Dev
RD Dev12mo ago
Avoid using dynamic for errors. Instead, define a common interface or base class like IValidationError for better type safety. It makes your code clearer and more reliable.
Dropps
DroppsOP12mo ago
i already have an interface for that? IErrorOr
RD Dev
RD Dev12mo ago
Well... In that case, use your existing IErrorOr interface for errors instead of dynamic. This maintains type safety and improves code clarity.
Dropps
DroppsOP12mo ago
are you literally copy pasting ai responses to me? this is almost 1:1 what jetbrains ai already told me which is not working which is why iam here
RD Dev
RD Dev12mo ago
I used RIder
Dropps
DroppsOP12mo ago
iam on rider as well
RD Dev
RD Dev12mo ago
Good luck with your money!
Dropps
DroppsOP12mo ago
what? thats entirely unrelated now
RD Dev
RD Dev12mo ago
Rider is paid
Dropps
DroppsOP12mo ago
just like any other good software product but thats not the point
RD Dev
RD Dev12mo ago
So you don't need help Ok see ya!
Dropps
DroppsOP12mo ago
i do need help which is why i created this post
RD Dev
RD Dev12mo ago
I believe you do, but you probably don't understand my help since you think I'm some AI.
Dropps
DroppsOP12mo ago
reread this iam assuming you are telling me ai responses because of what i stated before i did not say that you are an ai which would go against discord tos nonetheless iam asking if there are suggestions to get rid of the dynamic keyword inside my validation pipeline for mediatr as i might run into runtime errors in there which i dislike
RD Dev
RD Dev12mo ago
Ok, Understandable, have a nice day
Dropps
DroppsOP12mo ago
you too then ig
Want results from more Discord servers?
Add your server