Developer ModelĪ simple Developer class with basic properties. ![]() Let’s only support Fluent Validation for now. Line #7 It is possible to use both Fluent Validation and Data Annotation at a time. ![]() Note that, we will place our Validators within the API Project for this demonstration. Line #6 Registers all the Custom Validations that are going to build. S.RunDefaultMvcValidationAfterFluentValidationExecutes = false S.RegisterValidatorsFromAssembl圜ontaining() public void ConfigureServices(IServiceCollection services) Navigate to Startup.cs and modify as follows. We will have to add Fluent Validation to our application. Install-Package FluentValidation.AspNetCore Configuring FluentValidation Installing FluentValidation.AspNetCoreīegin by installing this awesome library into your WebApi project via the Package Manage Console. One for the actual ASP.NET Core API, and the other one will be to mimic a Library that will have a model class to which we DO NOT have access. Here is what the project structure would look like. I will be using Postman to test and receive the validation messages and Visual Studio 2019 Community as my IDE ( the best for C# development) Getting Started Implementing Fluent Validation in ASP.NET Core Applicationsįor this simple demonstration, let’s work on an ASP.NET Core 3.1 API Project that does nothing other than just validation with Fluent Validation. For larger, more complex systems, I would recommend separating the validation concern using validator objects with Fluent Validation. Also, better control of validation is something that makes the developers prefer Fluent Validation.įluent validations uses lamba expressions to build validation rules.įor small systems, I would recommend just using Data Annotations, because they’re so easy to set up. ![]() It doesn’t make your Model classes ugly like Data Annotations do. With this library, you can separate the model classes from the validation logic as it is supposed to be. It even works on external models that you don’t have access to, with ease. NET validation library that helps you make your validations clean, easy to create, and maintain. So, what’s the solution? Introducing Fluent Validation – The Solutionįluent Validation is a free-to-use. You will definitely end up building multiple model classes which will no longer be maintainable in the longer run. What if the Developer Model class is to be used in another application/method where these Attribute validation changes? What if you need to validate a model that you don’t have access to? Unit testing can get messy as well. NET Classes, the problem is that there will be a lot of duplication lines of code throughout your application. With the implementation of Data Annotations in.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |