Should Business Objects or Entities be Self-Validated?

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

    I don´t know if we are talking about the same idea, but if we are, I like what you explain. Very quickly, I´ll explain what I do to solve this. In my case, all the bussines objects in my domain layer must override two methods:

    Obviously, to maintain this, I have more classes implicated, but I´ll not write all here, cos I´m only trying to explain the concept

    List notPassedValidationRules = new List();
    
    //...
    
    public override void ValidateErrorsWhenSaving(Validator validator)
    {
        //...
    }
    
    public override void ValidateErrorsWhenDelete(Validator validator)
    {
       //...
    }        
    

    In these methods, I check for some boolean conditions, mantaining a collection of non passed rules. In my case, these methods are invoked before my Unit Of Work commits the changes (inserting new entities, updating, deleting), and show possible errors to the user, before commiting.

提交回复
热议问题