在 MVC 中,管理业务异常或错误的最佳方法是什么?我找到了几个解决方案,但不知道该选择哪个。
解决方案 1
public Person GetPersonById(string id)
{
MyProject.Model.Person person = null;
try
{
person = _personDataProvider.GetPersonById(id);
}
catch
{
// I use a try / catch to handle the exception and I return a null value
// I don't like this solution but the idea is to handle excpetion in
// business to always return valid object to my MVC.
person = null;
}
return person;
}
解决方案 2
public Person GetPersonById(string id)
{
MyProject.Model.Person person = null;
person = _personDataProvider.GetPersonById(id);
// I do nothing. It to my MVC to handle exceptions
return person;
}
解决方案 3
public Person GetPersonById(string id, ref MyProject.Technical.Errors errors)
{
MyProject.Model.Person person = null;
try
{
person = _personDataProvider.GetPersonById(id);
}
catch (Exception ex)
{
// I use a try / catch to handle the exception but I return a
// collection of errors (or status). this solution allow me to return
// several exception in case of form validation.
person = null;
errors.Add(ex);
}
return person;
}
解决方案 4
// A better idea ?