I have the following scenario where I call a method in my data-access code from my business-layer:
//Call the method from BL
SqlHandler sh = new SqlHandler();
var names = sh.GetNames();
Method example in DAL:
public IEnumerable<string> GetNames()
{
string conString = GetOpenConnection();
using (SqlConnection connection = new SqlConnection(conString))
{
connection.Open();
using(SqlCommand cmd = new SqlCommand("SELECT Name From Names"))
{
//...
}
}
}
My need is to log any exception that occurs in my DAL and display a message to user with an appropriate message. As far as I can see I have the following options:
1) Surround only the BL call and log and display the message from there:
try
{
SqlHandler sh = new SqlHandler();
var names = sh.GetNames();
}
catch (Exception ex)
{
ex.LogException();
MessageBox.Show(ex.Message);
}
2) Surround both calls and split the logging part and the notifying part into 2 sections:
try
{
SqlHandler sh = new SqlHandler();
var names = sh.GetNames();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
public IEnumerable<string> GetNames()
{
try
{
string conString = GetOpenConnection();
using (SqlConnection connection = new SqlConnection(conString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Name From Names"))
{
//...
}
}
}
catch (Exception ex)
{
ex.LogException();
//propagate the exception to the caller
throw;
}
}
3) of course I might be missing something here that I would be happy to know about
What is the preferred approach in terms of application architecture here? Is there a functional difference between the top two?