How to create powerful validation with FluentValidation in .NET apps

  Рет қаралды 4,884

Codewrinkles

Codewrinkles

Күн бұрын

Validation is a vital part of any application, even if a lot of teams tend to not pay a close attention to this topic. Validation itself can be very hard. Fortunately, the FluentValidation library allows us to create powerful validation rules with ease. In this video we gest started with the FluentValidation library, but we also dive into best practices and practical validators.
#DotNet #DotNetCore #FluentValidation
Chapters
1. Intro: 00:00
2. Real app overview: 01:17
3. Implementing a first validator: 05:09
4. Refactoring to avoid DRY: 16:55
5. Collection validation with conditions: 23:02

Пікірлер: 31
@talkathiriify
@talkathiriify 2 жыл бұрын
Excellent and smooth explanation. Thank you very much.
@FreaksSpeaks
@FreaksSpeaks 2 жыл бұрын
Thanks very much. Love to see more on this topic including how it is wired up to front end as well.
@Codewrinkles
@Codewrinkles 2 жыл бұрын
That's the point: validators in the domain entities should not be wired to anything :) I have already started to work with some ideas to shape up a video with validation best practices.
@danielegiovanetti9258
@danielegiovanetti9258 2 жыл бұрын
Very powerful video, well explained. My best compliment.
@Codewrinkles
@Codewrinkles 2 жыл бұрын
Wow, thanks
@VinuP2023
@VinuP2023 2 жыл бұрын
Thank you for making more videos
@Codewrinkles
@Codewrinkles 2 жыл бұрын
Glad you like them!
@saefulrahman3478
@saefulrahman3478 2 жыл бұрын
thanks, well explained
@Codewrinkles
@Codewrinkles 2 жыл бұрын
Glad it was helpful!
@RockRockAndDogs
@RockRockAndDogs Жыл бұрын
Awesome video.
@Codewrinkles
@Codewrinkles Жыл бұрын
Glad you enjoyed it
@ankitasthana2465
@ankitasthana2465 Жыл бұрын
Nice Video!!!
@Codewrinkles
@Codewrinkles Жыл бұрын
Thanks for the visit
@mohamedal-qadeery6530
@mohamedal-qadeery6530 2 жыл бұрын
hello im still a beginner .. i dont understand why i should validate domain model when im going to use FluentValidation to validate the Dtos and if the validate success then im going to map it to domain models using automapper .. so am i wrong that there is no need to validate domain models?
@talkathiriify
@talkathiriify 2 жыл бұрын
I just notes that in the ProductValidator constructor you set a rule for product price which must be > 0 line 15 and then in the ValidatePrduct method you check for price
@Codewrinkles
@Codewrinkles 2 жыл бұрын
Good point!
@ddieppa
@ddieppa 2 жыл бұрын
Thanks very much for this great videos. I like my validators out of the domain layer, I dont like to make the domain dependant on an external package. But then what is the best way to do it? Maybe that could be another video. Thanks again for the great content
@Codewrinkles
@Codewrinkles 2 жыл бұрын
I think we kind of disagree here. The domain surely needs domain specific validation. in DDD terms this is called controlling invariants. Sure, you can implement your own validation engine but I don't think this justifies the effort. I think we tend to misunderstand the idea that the domain should not depend on other libraries. If your domain works with dates and times, then you should surely use NodaTime for instance. For validation you can use FluentValidation. Also, as I showed in this video, my goal is that my domain entity doesn't depend on the FluentValidation library. And I also showed how to resolve that. My entity only knows that there is a validator that it can use. In that validator you can then change the library if you want or do whatever. It won't break the domain entity.
@mohamedal-qadeery6530
@mohamedal-qadeery6530 2 жыл бұрын
Thank you for the video .. can you make a video about crud with nested resources wired up to front end ?
@Codewrinkles
@Codewrinkles 2 жыл бұрын
There alrready is a video about nested resources in the Asp.Net Core for beginners playlist.
@ConradAkunga
@ConradAkunga Жыл бұрын
I don't believe you need the condition for the ProductImageValidator - if the collection of ProductImages is empty, nothing will be executed anyway.
@vladmartian
@vladmartian 2 жыл бұрын
It would be intresting to me to see how you get the Rich Domain Objects from a Database eventually using EF. Should I use DTOS ?
@Codewrinkles
@Codewrinkles 2 жыл бұрын
From a database perspective there isn't a big problem if the domain objects are rich. Using validation you make sure that you can only persist valid entities in the db. Then, when you get them, even if EF populates the properties directly through reflection you still have a guarantee that they are valid. But in case of very complicated scenarios going back to the good old data access objects might be an idea. And you map them to the domain entities after retrieval. However, there should really be a reasonable need for this to justify the overhead.
@vladmartian
@vladmartian 2 жыл бұрын
@@Codewrinkles Thanks, for the response. But I have an other question. You create your domain object through a factory, should be the repository injected in the factory? Should I use an other service for creating a new object in a DB?
@Codewrinkles
@Codewrinkles 2 жыл бұрын
@@vladmartian It's not a full blown factory. Just a factory method. Let's assume we want to create a new product. We'll have a Handler for the CreateProduct command. In the handler we call Product.BuildProduct() and specify all the needed params. The result will be a Product instance on a valida state. I then use the ProductRepository.Add(product) and that's it. You don't really need anything else.
@vladmartian
@vladmartian 2 жыл бұрын
@@Codewrinkles Ok I understand now. Thanks
@semen083
@semen083 2 жыл бұрын
What the point is using static constructor for Product class instead of regular? The no way to use validation in former?
@Codewrinkles
@Codewrinkles 2 жыл бұрын
It is not a static constructor. It is a static method. If you take a careful look you'll notice that the constructor is private. This means that nobody outside the class is able to use the constructor. The only way you can instantiate a new Product is throigh the static method. And there you can fully control state. Also all setters are private. So, by doing ths we fully encapsulate state inside the class itself. That's a common practice in DDD.
@christianista
@christianista Жыл бұрын
I have an API, the validation message should be returned in several languages. In the Person class there is a property Language, how can I return the error messages in the right language (fr, en, de)?
@Codewrinkles
@Codewrinkles Жыл бұрын
Out of the top of my mind, the best way to achieve this is by using Globalization and Localization: learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-7.0 The core concept is that when you are adding the error messages you don't actually add a string, but add the string through a key to your .resx files.
@christianista
@christianista Жыл бұрын
@@Codewrinkles I already read this but I may be use it wrong. But
Practical clean architecture with EF Core database first
38:38
Codewrinkles
Рет қаралды 9 М.
Don't Use AutoMapper in C#! Do THIS Instead!
16:17
Codewrinkles
Рет қаралды 66 М.
Me: Don't cross there's cars coming
00:16
LOL
Рет қаралды 13 МЛН
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 13 МЛН
Coding Shorts: Custom Property Validation with FluentValidation
11:41
Shawn Wildermuth
Рет қаралды 1,7 М.
How To Add Google Authentication To Blazor SSR
24:23
Codewrinkles
Рет қаралды 2,9 М.
Stop using String Interpolation when Logging in .NET
17:30
Nick Chapsas
Рет қаралды 84 М.
Error handling strategies in ASP.Net Core APIs
18:20
Codewrinkles
Рет қаралды 5 М.
School Mgmt App | Microservices With .NET Aspire & Blazor SSR | Part 7
2:48:21
Don't throw exceptions in C#. Do this instead
18:13
Nick Chapsas
Рет қаралды 252 М.
The Awesome New Queue of .NET 6 That You Skipped
11:52
Nick Chapsas
Рет қаралды 49 М.
GamePad İle Bisiklet Yönetmek #shorts
0:26
Osman Kabadayı
Рет қаралды 654 М.
Hisense Official Flagship Store Hisense is the champion What is going on?
0:11
Special Effects Funny 44
Рет қаралды 2,8 МЛН
When you have 32GB RAM in your PC
0:12
Deadrig Gaming
Рет қаралды 1,6 МЛН