Should a business rule violation throw an exception?

后端 未结 14 659
误落风尘
误落风尘 2021-02-04 04:18

Should a business rule violation throw an exception?

14条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 04:53

    Usualy I put the condition in a Specification object that implements

    bool IsVerfiedBy(T entity);
    

    So you can check the condition without exception.

    If there is a place in your code where the specification should be verified beforehand, you can throw an exception because this is a prerequisit of you function.

    For instance if your entity must be in a specific state before persistance, throw an exception when the specification is not verified, but use the specification before persisting so that the exception does not happen.

提交回复
热议问题