Skip to content

Database Validation

Jon P Smith edited this page Jan 18, 2018 · 2 revisions

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

Why GenericBizRunner validates by default

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.

How to change whether GenericBizRunner validates or not

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

  1. Globally: If you think validation is a bad idea then you can turn it off by providing a GenericBizRunnerConfig class with the DoNotValidateSaveChanges property set to true on startup.
  2. Per Business class: In the BizActionStatus that each business logic implements there is a virtual property called ValidateOnSaveSetting. Its default value is UseConfig, which means it obeys the state of the DoNotValidateSaveChanges config property. But the other two possible values are: Validate and DoNotValidate - these will override the DoNotValidateSaveChanges config property.