question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can I use FlentValidation with WorkerService and MediatR

See original GitHub issue

Which 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Jun 9, 2020

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:

services.AddTransient<IValidator<Person>, PersonValidator>();

Or in bulk:

// This is an extension method which will need the FluentValidation.DependencyInjectionExtensions package.
services.AddValidatorsfromAssemblyContaining<PersonValidator>();
0reactions
JeremySkinnercommented, Jun 9, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found