-
Notifications
You must be signed in to change notification settings - Fork 57
Database Validation
The page is about how you can configure whether GenericBizRunner validates data written to the database or not.
- Why GenericBizRunner validates by default
- How to change whether GenericBizRunner validates or not
By default, the GenericBizRunner will validate
all the entity classes added to updated when it calls EF Core's SaveChanges at the end of a business logic that inherits a GenericAction interface that contains the string WriteDb. This is different to EF Core, which does not validate data when SaveChanges is called (EF6.x did).
You can use DataAnnotations, such as [MaxLength(100)] and [Range(1,5)], to configure your entity classes for EF Core. But DataAnnotations can ALSO be useful for defining business rules. By adding a validation phase to a call to SaveChanges allows you to check the data you are adding or updating in the database.
Have a look at the LineItem entity class, which has a range of annotations and an IValidatableObject interface for more complex validation tests.
The downside of validation is it takes time, and in some cases that won't be acceptable. There are two ways to whether GenericBizRunner will validate data being written to the database
-
Globally: If you think validation is a bad idea then you can turn it off by providing a
GenericBizRunnerConfigclass with theDoNotValidateSaveChangesproperty set totrueon startup. -
Per Business class: In the
BizActionStatusthat each business logic implements there is a virtual property calledValidateOnSaveSetting. Its default value isUseConfig, which means it obeys the state of theDoNotValidateSaveChangesconfig property. But the other two possible values are:ValidateandDoNotValidate- these will override theDoNotValidateSaveChangesconfig property.