IQueryable to Wrap Exceptions

Viewed 1259

My biggest concern with exposing an IQueryable in my business logic is that it could throw an Entity Framework exception in my business logic. I consider that a problem because my business layer either needs to know that I'm using Entity Framework -or- I I have to catch a very generic exception.

Instead, I'd like to create an IQueryable that captures Entity Framework exceptions and converts them to my data layer exception types.

Ultimately, I want my code to look like this:

public IQueryable<Customer> GetCustomers()
{
    var customers = from customer in dbContext.Customers
                    where customer.IsActive
                    select customer;
    return customers.WrapErrors(ex => new DataLayerException("oops", ex);
}

Clients would then be able to add additional LINQ clauses. If an error occurs (the database goes down), the original exception will be wrapped with the DataLayerException.

2 Answers
Related