Can I use FlentValidation with WorkerService and MediatR
See original GitHub issueWhich version of FluentValidation are you using? v8.6.2
Which version of ASP.NET are you using? NET Core 3.1
Describe the issue that you’re having I have a WebApi working with Angular 9 and I use FluentValidation and MediatR. It is working fine. However, I need to implement multiple worker service now. So, is there a way to using fluent validation with Dependecy Injection?
I use _validator.Validate
inside my methods but I hope there will be an easy way.
using (var scope = Services.CreateScope()) {
var mediator =
scope.ServiceProvider
.GetRequiredService<IMediator>();
var context =
scope.ServiceProvider
.GetRequiredService<IMyDbContext>();
_validator = new UpsertCategoryCommandValidator(context);
var newCategory = new UpsertCategoryCommand { Name = "CategoryName" };
var validationResult = _validator.Validate(newCategory);
if (validationResult.IsValid) {
var result = await mediator.Send(newCategory);
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How To Implement Validation With MediatR And ... - YouTube
We will use MediatR's BehaviorPipeline feature and FluentValidaton for ... 3:38 Implementing validation with FluentValidation 6:44 Create ...
Read more >CQRS Validation Pipeline with MediatR and FluentValidation
We are going to learn how to implement validation as a cross-cutting concern using CQRS pattern with MediatR and FluentValidation.
Read more >CQRS Command Validation with MediatR in Asp.net Core
It is a best practice to separate validation from the implementation logic. This article will show you how to separate validation logic from...
Read more >Use MediatR with FluentValidation in the ASP.Net Core ...
The process steps would look like this: API endpoint receives request. Sends the request to the mediator.
Read more >Implementing the microservice application layer using ...
Understand the Dependency Injection and the Mediator patterns and their implementation details in the Web API application layer.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The
AddFluentValidation
call is specifically for adding the ASP.NET integration - if you’re not using the ASP.NET integration, then you don’t need to call this method.Just register your validators directly with the service provider:
Either manually:
Or in bulk:
As I said in my example, you need to have the FluentValidation.DependencyInjectionExtensions package installed for that method to show up as it’s an extension method.
If you have problems getting it to work then let me know.
Hope that sorts everything