This is my DAL, I’m about to try/catch the code and since the concept is not that grown in me I have the following doubt.
The one time when it actually does something is in ExecuteSQL method. So, there I’ll have one try/catch. But in GetLinea or UpdateLinea should I also add? I can only think of quite weird errors there.
Also, If you have any suggestions for a cleanup in this code I’d love to hear about it.
Thanks.
namespace DAL
{
public class Connection
{
public string GetNewConnection(string server)
{
return ConfigurationManager.ConnectionStrings[server].ConnectionString;
}
public DataSet ExecuteSQL(string sp)
{
DataSet ds = new DataSet();
string connectionString = GetNewConnection("BO");
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(sp, conn);
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(command);
using (conn)
{
da.Fill(ds);
}
return ds;
}
}
public class LineaDAL
{
Connection obj = new Connection();
public DataSet GetLinea()
{
DataSet ds = new DataSet();
string sp;
sp = "sp1";
ds = obj.ExecuteSQL(sp);
return ds;
}
public bool UpdateLinea(string reclamo)
{
DataSet ds = new DataSet();
string sp;
sp = "sp2";
ds = obj.ExecuteSQL(sp);
return ExtensionMethods.IsEmpty(ds);
}
}
public static class ExtensionMethods
{
public static bool IsEmpty(this DataSet ds)
{
return ds == null ||
!(from DataTable t in ds.Tables where t.Rows.Count > 0 select t).Any();
}
}
}
As with any exception handling, it really depends on what you are going to do with it. Too often try/catch is used as a way to catch possible bugs just in case. But, really you should have very little try/catch unless you are looking to catch something specific and deal with it properly (notify the user with a custom message for that error). If you do not have anything meaningful to do with the catch, then let it bubble to a top level exception handler. That is my 2 cents at least
Here is a much more in depth article on exception handling