views:

76

answers:

3

My problem is that I have to deal somehow with cases when database is down in my MVC application. So, should I like try{} catch{} to open connection in every controller? Or what is the best practice?

Note: For sample database access is isolated in few "manager" classes that work repository kind of way.

+4  A: 

Why are you opening a connection in every controller. This should be isolated within it's own reusable class.

You may want to check out the repository pattern to implement your data access.

Robaticus
It is isolated, but I want to know how do I response to user whenever something goes wrong with connection and where do I handle the error.
Nazar Gargol
Decorate your class with [HandleError] attribute, and when an uncaught exception is thrown, they will be whisked away to the error page (`/Views/Shared/Error.aspx` I believe)
Robaticus
+2  A: 

Well, first off, if you have a separate database handling routine in every controller, I would start with combining that into a base class or a reusable member.

After that, you should be able to use ASP.NET MVC's [HandleError] attribute to specify how to handle errors from your database.

John Gietzen
+3  A: 

If you don't have a database connection, in a production database-driven web app, typically this means something bad happened well beyond the scope of your application. About the only thing you can do is give a friendly error message to the user and tell someone responsible for fixing things. Which is a task for your global error handling, not each controller.

Wyatt Barnett