Entity Validation to Database projects. #20
garywoodfine
announced in
Announcements
Replies: 1 comment
-
I recently published a blog post which attempts to discuss the concepts laid out here in further detail. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We are currently implementing a new feature within the API template Pack, Entity Validation on Database projects.
What is Entity Validation
Entity Framework provides validation features of different varieties that can be used through a user interface for client-side validation or be used for server-side validation.
The API template pack guides you down the Path of making use of the Fluent API Configuration to define your Entity Configurations.
Fluent API configurations are applied as code first is building the model from the classes. You can define you Entity Type Configurations making use of the BaseEntityTypeConfiguration
Which is great for when you want to stipulate your field types and Maximum Length of your fields etc.
The options work great for defining field restrictions as required lengths and if the field is is required or not. However, what if you want to preform more validation on the data within the field before is committed to database for storage.
One option would be to make use of the
IValidateableObject
an interface in System.ComponentModel.DataAnnotations. Although it is not part of the Entity Framework API, you can still use it for server-side validation in Entity Framework classes. IValidatableObject provides a Validate method that Entity Framework call duringSaveChanges
or can call any time you want to validate the classes.So when defining your Entity Classes you could add some validation logic.
An example of which maybe you would like to ensure that two fields have the same values
There are a couple of draw backs to this approach, first it is dependent on Entity Framework , executing the Validation is performed after changes are detected during SaveChanges. Which may not happen.
Secondly, this may also always require to update your migrations because you're directly editing your Entity to update validations etc.
API Template Pack approach
We already make use of FluentValidation the excellent validation from Jeremy Skinner. We have also implemented a Mediatr Validation Pipeline to execute all Validators defined on your Command and Query objects on a per request basis.
In this forthcoming feature enhancement we will implement a similar approach to the Dataservice capabilities.
Beta Was this translation helpful? Give feedback.
All reactions