jeudi 11 mars 2021

Using a Result class instead of throwing exceptions for business logic

After recently starting a new project I noticed that I'd used Exceptions for all sorts of business logic & data validation. Based on prior experience and the reading I've done, this is not idea. I dont want to cause slowdowns as this code should be performant if possible. I also want to know all of the validation errors before returning an error.

The way I've done it previously is to have a Result class similar to below. Not exact code but you get the gist of it.

class Result<T>
{
    T Record {get;set;}
    ICollection<string> Errors {get;set;}
    bool Result {get;set;}
}   

Is there a better way to be doing this? I started looking into Validation or Notification design patterns and it seemed to me to be overly complex for something as simple as basic as checking one value against another.

Aucun commentaire:

Enregistrer un commentaire