vendredi 15 avril 2016

How to tell my controllers that my services threw an exception

My controllers call my business services. I typically wrap those calls within try/catch blocks in case that my services throw exceptions.

Then I thought to move that try/catch block to my business services and let them tell the controller whether they threw or not.

So my business services could return an interface such as:

interface IResult<T> {
    <T> Data { get; set; }
    Exception Exception { get; set; }
    bool HasException;
}

And the signature of my business services would be:

public IResult<Product> GetProductByID(int id);

And my controllers would call it like this:

var result = serviceInstance.GetProductById(1);
if (result.HasException) {
    // handle exception here
}

My question is - which of these two approaches is preferred for testability purposes? Is there other pattern that I could use to indicate that my services threw?

Aucun commentaire:

Enregistrer un commentaire