In MVC qual è il metodo migliore per gestire eccezioni o errori nel business? Ho trovato diverse soluzioni ma non so quale scegliere.In MVC qual è il metodo migliore per gestire eccezioni o errori nel business?
Soluzione 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;
}
Soluzione 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;
}
Soluzione 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;
}
Soluzione 4
// A better idea ?