Domanda

I have defined an:

IValidator<SomeClass> _myValidator

I can do:

_myValidator.ValidateAndThrow(someObject);

instead I would like to:

var errors = _myValidator.Validate(entity);

add some errors manually and then re-throw the errors. Is this possible?

È stato utile?

Soluzione

If you want to store the errors in a variable, you'll have to return the error, or a list of errors, instead of throwing them.

IList<Exception> ValidateAndThrow(object someObject){

    IList<Exception> errors = new List<Exception>();

    try{
        SomethingGoesWrong();
    } catch (Exception e){
        errors.Add(e);
    }

    return errors;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top