RickBlaine wrote: » I’m a hobbyist programmer and have done some development as part of my job too. I’m developing a personal project using ASP.NET MVC with Entity Framework and a Service Layer in between. My question concerns error handling in the Data Access layer. It seems like overkill to wrap every method in a Try…Catch. This is particularly the case because I don’t need any recovery/retry steps in the Catch block. If a db error occurs, I’d simply want the except written to a log file and a message displayed on the front end. So, I could pass the exception back up to the caller in the Service Layer. The problem with that would be that it would expose data access implementation to the Service Layer which seems to be a big no no. When you are developing an Entity Framework data access layer, where do you handle exceptions (especially when the only operation in the Catch block is simply logging)? Thanks very much
John_Mc wrote: » If you're looking to do it properly then you'd have a data access layer with repositories that looks after data access using EF and mapping it to application models. Your service layer and MVC layer only work with the application models. You should avoid try/catch and allow the exception to bubble up. Deal with it in the global error handler that you have. If you don't get have one then there are a number of ways to do it. Stackoverflow will give you the examples you need.
Mark Rippetoe wrote: » First thing that springs to mind would be to create an enum with internal messages indicating success/failure or whatnot and use them as the return value when the web layer makes calls to the service layer. Then the web layer can use Model.AddModelError if the return value != ErrorEnum.Success, or something like that.
John_Mc wrote: » You should avoid try/catch and allow the exception to bubble up. Deal with it in the global error handler that you have. If you don't get have one then there are a number of ways to do it. Stackoverflow will give you the examples you need.
Colonel Panic wrote: » Ugh, tell me you're not wrapping EF in some DAL/Repository style thing. Does your service layer not just have an EF context? You can catch exceptions it throws and throw user friendly errors up to your service layer caller to display to the user. Don't overthink it!
John_Mc wrote: » Sorry but that's a bad recommendation for someone starting out in my opinion. Why would you embed a proprietary framework such as EF into your entire application? What happens if/when you want to change how you persist data? The repository pattern is well established and I've never heard anyone suggest it's bad. It's there to abstract how data is retrieved and persisted leaving the service layer to handle the business logic.
John_Mc wrote: » Are you saying my suggestion would be horrible to maintain?
Colonel Panic wrote: » I didn't suggest embedding it into the entire application. The repository/unit of work pattern is fine, but EF already represents a repository/unit of work. It's this dogma about having layers everywhere to let you slot in different implementations of things that annoys me. Furthermore, change how you persist data... This happens very infrequently in reality.
Jim2007 wrote: » Most certainly. You end up with one massive global handler and every time you need to make a little tweak to handle a particular situation it becomes a saga because your trying to fix it, well away from where it occurred, it can have side effects and you've got reams of stack track to go through to find out what is happening, if you even bothered to log the stack trace....
John_Mc wrote: » Being a contractor for the last 7 years, I've worked on a lot of enterprise applications and all of them have handled it like this.
Jim2007 wrote: » And being a contractor for 30 years.... I've seen what that kind of global crap causes and we just don't do it, at least not if your the lead developer on a couple of major European banking systems in any case.
RickBlaine wrote: » The sense I'm getting from this discussion is that if I want to try some action to try to recover from the exception, I should handle it close to where it is raised, e.g. in the same layer as EF. However, if I simply want to log the exception and return an error code and error message to the front end, I can just let the exception bubble up to a higher layer. Would I be correct?
John_Mc wrote: » ...And just because something happens infrequently doesn't mean you shouldn't allow for it to be possible...
pillphil wrote: » Obviously not a consultant :P