Should Business Objects or Entities be Self-Validated?

前端 未结 3 821
面向向阳花
面向向阳花 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:05

    It's not always possible for them to self-validate though. What if you enter an "invalid" Zip Code? You could validate that the Zip Code needs to be in a specific format, but what if you want them to be "valid", that is "existing and matching the city"? Or what if you only accept phone numbers from certain area codes, and the list of valid codes is in a database maintained by the legal department?

    If you can perform semantic validation, that's great and could go into the Business Class. But often, you might need extra validation that is simply not possible to handle by the business class itself but needs to be handled by the class that talks to the database and other external services.

提交回复
热议问题