Last post Jun 04, 2010 05:30 AM by bhaweshkumar
Jun 04, 2010 04:30 AM|uid895650|LINK
I am working on an application at work. It implements a n-Tier, Provider and Repository Design Pattern architecture.
I am just trying to work out how and when to handle any errors from the DAL ex: You call a method from the BLL Case.GetCaseByID(int Id), that in turn calls a DAL method by the same name. But, if a case the ID can not be found do I pass a return value back
to my DAL and throw the error there or return null back to my BLL and let it happen there.
I hope the below explains a little better. Thanks in advance
public bool CaseExists(int caseID)
// Should I capture a null here if nothing is returned and throw a ProviderException or should this be handled at DAL level?
return PPC.DAL.ClinicalServices.CaseExists(int caseID);
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
SqlCommand cmd = new SqlCommand("proc_ClinicalServices_CaseExists", cn);
cmd.CommandTimeout = this.CommandTimeOut;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CaseID", SqlDbType.NVarChar).Value = caseID;
// cmd.Parameters.Add("@ReturnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue; -- Should I rather return a value here and pass it to the BLL and catch the exception there?
return (GetReturnValue(cmd)); // -- or return (true/false i.e. int ret = ExecuteNonQuery(cmd); return (ret ==1);
Jun 04, 2010 05:05 AM|NihirPorecha|LINK
Raising an exception and handling the exception has always a cost(performance) associated with it. IMHO, exception should be raised only if your code gets unexpected input, which can not be handled. I think, in your case, it is pure business validation.
So, it should be handled silently.
Jun 04, 2010 05:30 AM|bhaweshkumar|LINK
DAL will be perfect place for handling any DAL exceptions. You should log them at DAL only, rest of the layers should just propogate error in the hierarchy. The exception should be handled as a DALException type and when displayed on UI it should be a user
If there is an exception in BLL it should be handled in BLL, logged in BLL with a BLLException type and propogated in hierarchy if needed to be displayed to user.
System exceptions will not need to be propogated however a generic exception message will be required if it breaks functionality.