Should Business Objects or Entities be Self-Validated?

前端 未结 3 850
面向向阳花
面向向阳花 2020-12-31 20:28

Validation of Business Objects is a common issue, but there are some solutions to solve that.

One of these solutions is to use the standalone NHibernate.Validator fr

3条回答
  •  一生所求
    2020-12-31 21:04

    IMHO - there are 2 steps of validations needed for a Business Object (BO)/Entity to be valid:

    Step1: BO/Entity self-validation - In this, we check only if the entity is valid in terms of its state F.Ex.: If postal code is set, then does it have valid characters & is of valid length etc. form the BO/Entity level validations. But beyond this level of validation, we would not be able to say that the BO/Entity is valid in your business domain and/or repository. Typically the BO/Entity would be able to enforce this level of validation.

    Step2: Context validation - In this, we need to validate if the BO/Entity is valid within the context of the Repository where it is being persisted. F.Ex.: Is the postal code valid for the country in which the order is being placed/sent to etc. For this validation, some or all entities in the current context might need to be involved to make sure the BO/Entity is valid.

    So, to keep the entities pure, you will need to split the validation into these 2 steps - one performed by the entity itself & the second by the repository which is persiting/working with the entity.

    HTH.

提交回复
热议问题