我正在寻找一些程序设计指导。类libarary的异常处理设计
我有一个类库来处理数据库中的数据。我有一个winforms应用程序,它是用户输入和管理数据的表示层。假设用户输入一些数据并尝试保存。从Winforms应用程序我做这样的事情:
MyTool theTool = new MyTool();
MyTool.FirstName = this.Textbox1.Text;
MyTool.LastName = this.Textbox2.Text;
//etc...
int result = MyTool.SaveData(); //result is the ID of the inserted record.
MyTool是我的类库中的一种类型。在这种类型中,我会有:
public int SaveData()
{
if (IsReadyForInput())
{
//..open a DB connection and save out the data
//..get the ID of the saved record
}
else
{
throw new ArgumentException("One or more arguments prevented saving the data");
}
return theID
}
private bool IsReadyForInput()
{
if (this.FirstName.Length == 0)
{ return false; }
if (this.LastName.Length == 0)
{return false;}
return true;
}
现在,我感兴趣的是如何处理异常的最佳设计。例如,上述方法根本不具体,所以用户不知道什么是错的。所以,我可以改写这个做这样的事情:
public void SaveData()
{
string errMess = IsReadyForInput();
if (errMess.Length == 0)
{
//..open a DB connection and save out the data
//..get the ID of the saved record
}
else {
throw new ArgumentException(errMess);
}
return theID
}
private string IsReadyForInput()
{
if (this.FirstName.Length == 0)
{ return "Specify a first name"; }
if (this.LastName.Length == 0)
{return "Specify a last name";}
return true;
}
然而,它只是似乎很优雅(或快速)方法不被比较字符串长度,找到错误消息。我曾试图写的东西,如:
public void SaveData()
{
ValidateInput();
//..open a DB connection and save out the data
return theID
}
private void ValidateInput()
{
if (this.FirstName.Length == 0)
{ throw new ArgumentException("Specify a first name"; }
if (this.LastName.Length == 0)
{throw new ArgumentException("Specify a first name"; }
}
的问题,这是异常实际上是由ValidateInput时抛出的前端调用“保存数据”,所以当异常到达顶部,对我来说,它会似乎不太清楚(尤其是如果在MyTool中有多种方法调用“ValidateInput()”)。
此外,我不确定在前端处理异常的最佳方法是因为如果引发错误,ID永远不会返回。
我想我只是寻找一些如何处理这种情况和一般验证/错误处理的指导。谢谢你的帮助。
非常感谢,但我希望能得到一些关于最佳实践的指导,以及其他人如何处理这个问题,所以我可以自己做。这似乎是一个非常常见的数据输入结构... – rune711
虽然流畅的验证确实显得很刺耳。 – rune711
=)这是一个很好的框架,你会遇到的许多验证框架/模式将非常相似。 – ohiodoug