I found the following two types of exception-handling in Business Logic Layer.
ASP.NET 3.5 Enterprise Application Development uses a similar method like the first one (I read it few years ago).
I also found this on Stack Overflow, but it doesn't answer my question.
I'm wondering which one is better design and efficiency.
Method 1 - in Business Logic Layer
private int InsertUser(string firstname, string lastname, ref List<string> errors)
{
if (!string.IsNullOrEmpty(firstname))
errors.Add("First name is required.");
if (!string.IsNullOrEmpty(lastname))
errors.Add("Last name is required.");
if (errors.Count > 0)
return -1;
int userId = -1;
try
{
// Insert user and return userId
}
catch (Exception ex)
{
// Log error to database
errors.Add("Error occurs. Please contact customer service.");
}
return userId;
}
Method 2 - in Business Logic Layer
private int InsertUser(string firstname, string lastname)
{
if (!string.IsNullOrEmpty(firstname))
throw new ArgumentNullException(firstname);
if (!string.IsNullOrEmpty(lastname))
throw new ArgumentNullException(lastname);
int userId = -1;
// Insert user and return userId. Let user handle the exception in UI.
return userId;
}
The disadvantage of Method 2 is that the UI has to filter out what to display/not display the error depending on the exception. For instance, you might not want to display a System.Data.Entity
exception to the user.
List<string>
as aref
argument.List<T>
, like all classes, is a reference type, so changes made toerrors
byInsertUser()
will always be reflected in the original object (assignment toerrors
would not be reflected withoutref
). \$\endgroup\$