How to share validation between Forms and Value Objects in Domain Driven Design?

走远了吗. 提交于 2019-11-28 12:26:35

Encapsulate the validation logic into a reusable class. These classes are usually called specifications, validators or rules and are part of the domain.

There are multiple ways of doing this, here is an approach that I use:

  1. Define an interface Specification that provides a bool IsSatisifed() method.
  2. Implement this interface for a specific value object, e.g. EmailWellformedSpec.
  3. Enforce the business rule within the domain by using the spec as precondition (i.e. violation is always a programming error).
  4. Use the spec for input input validation in the service layer (i.e. violation is a user error).

If you want to combine multiple specs to a larger one, the Specification Pattern is a good approach. Note that you need to pass in the data through the constructor if you use that pattern, but this is not a problem because the specification classes are usually simple.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!