我有以下情形,我调用一个方法在我的数据访问代码从我的业务层:在DALtry catch块位置
//Call the method from BL
SqlHandler sh = new SqlHandler();
var names = sh.GetNames();
方法例如:
public IEnumerable<string> GetNames()
{
string conString = GetOpenConnection();
using (SqlConnection connection = new SqlConnection(conString))
{
connection.Open();
using(SqlCommand cmd = new SqlCommand("SELECT Name From Names"))
{
//...
}
}
}
我需要是记录我的DAL中发生的任何异常,并使用适当的消息向用户显示消息。据我可以看到我有下列选项:
1)仅包围BL呼叫和记录和显示从那里消息:
try
{
SqlHandler sh = new SqlHandler();
var names = sh.GetNames();
}
catch (Exception ex)
{
ex.LogException();
MessageBox.Show(ex.Message);
}
2)环绕两个呼叫,分裂记录部分和通知部分分成2个部分:
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)当然,我可能失去了一些东西,我会很乐意了解
什么是首选Appro公司在这里的应用程序架构方面?前两者之间是否有功能差异?
这是基于意见的.. –
是你的问题“我应该把try {} catch {}'块放到DAL或BAL中吗? – hdoghmen
选项1看起来不错...您可能需要像http://stackoverflow.com/a/136092/747609这样的东西来处理不同的事情。尽管如此,这可能会很麻烦。 – IROEGBU