21

Best practice to throw the exception if no entry found in the db?

// CONTROLLER
public ActionResult Edit(int categoryId, int id)
{
    Product target = Products.GetById(id);
    if (target == null) throw new HttpException(404, "Product not found");

    return View("Edit", target);
}

// REPOSITORY
public Product GetById(int id)
{
    return context.Products.FirstOrDefault(x => x.productId == id);
}

or

// CONTROLLER
public ActionResult Edit(int categoryId, int id)
{
    return View("Edit", Products.GetById(id));
}

// REPOSITORY
public Product GetById(int id)
{
    Product target = context.Products.FirstOrDefault(x => x.productId == id);
    if (target == null) throw new HttpException(404, "Product not found with given id");

    return target;
}

3 Answers 3

32

Never throw an HttpException from a repository...it's the wrong level of abstraction. If you don't want your repository to return null, do something like this:

// CONTROLLER
public ActionResult Edit(int categoryId, int id)
{
    try {
       Product target = Products.GetById(id);
    }
    catch(ProductRepositoryException e) {
       throw new HttpException(404, "Product not found")
    }

    return View("Edit", target);
}

// REPOSITORY
public Product GetById(int id)
{
    Product target = context.Products.FirstOrDefault(x => x.productId == id);
    if (target == null) throw new ProductRepositoryException();

    return target;
}

Your repository should not know anything about HTTP, but your controller can know about the repository. So you throw a repository exception from the repository, and "translate" that into an HTTP Exception in the controller.

Sign up to request clarification or add additional context in comments.

5 Comments

But then I have to create custom exceptions :(?
Yes you do. And that's what you should do.
I'll just create a custom exception named "NotFoundException" then. Thanks for the answer :)
How do you create this custom exception?
@niico throw new Exception(), at a very basic level. If you want a different type of exception, like NotFoundException, then it is throw new NotFoundException("custom Exception message") and in your catch {} statement where you want to use it, you have catch(NotFoundException ex) instead of catch(Exception ex).
3

Do not throw a HttpException in the Repository, as you may want to reuse that code in the future in a non-Http environment. Throw your own ItemNotFound exception if you require at least one item and handle that exception in the Controller, or return null and handle that.

Comments

0

I would throw HttpException in the controller, and return null from the repository.

1 Comment

Return null from the repository.. that dosent make sense - could you go a bit more in depth please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.