Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Business Objects or Entities be Self-Validated?

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 framework, which is an attribute-based validation framework.

But I'm facing into conceptual concern. Attribute validators like NH.Validator are great but the validation is only performed when save-update-delete within the Session.

So I wonder if business objects should not be self-validated in order to maintain their own integrity and consistence?

like image 350
Yoann. B Avatar asked Mar 02 '10 00:03

Yoann. B


People also ask

Should objects validate themselves?

Are Value Objects responsible for self-validation? No. But they are responsible for keeping their state consistent so sanity checking data on construction can be considered a good practice.

What is object validation?

Validation verifies the definitions of both data objects and ETL objects and identifies any problems or possible errors that could occur during deployment. If objects are invalid, generation and deployment is not possible. You can validate objects and generate scripts for objects at any point in the design process.


1 Answers

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.

like image 63
Sunny Avatar answered Oct 29 '22 12:10

Sunny