DAL in this case stands for Data Access Layer. Since this the application was built in a very data centric manner, I figure I will start my refactoring here.
In the DAL project, I found an object named baseDALObject. I am assuming this is a base class used by the other DAL objects. The fact that the objects and project is called DAL objects makes me cringe.
The original class file looks like:
- using System;
- using System.Data;
- using System.Data.SqlClient;
- //using Microsoft.ApplicationBlocks.Data;
- namespace RMS_DALObjects
- {
- /// <summary>
- /// Summary description for Class1.
- /// </summary>
- public class baseDALObject
- {
- #region "Variables"
- private string strConn = "connection_string";
- private SqlConnection sqlConn;
- #endregion
- #region "Constructors"
- public baseDALObject(){}
- #endregion
- #region "Methods"
- public DataSet getDataSet(string strSQL)
- {
- try
- {
- return null;
- //return SqlHelper.ExecuteDataset(strConn, CommandType.Text, strSQL);
- }
- catch (SqlException e)
- {
- string msg = e.Message;
- return null;
- }
- }
- public SqlDataReader getDataReader(string strSQL)
- {
- try
- {
- sqlConn = new SqlConnection(strConn);
- sqlConn.Open();
- SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn);
- return sqlCmd.ExecuteReader();
- }
- catch (SqlException e)
- {
- string msg = e.Message;
- return null;
- }
- }
- public void closeConnection()
- {
- try
- {
- sqlConn.Close();
- }
- catch (SqlException e)
- {
- string msg = e.Message;
- }
- }
- public SqlParameter[] getParameters(string storedProcedureName)
- {
- return null; //SqlHelperParameterCache.GetSpParameterSet(strConn, storedProcedureName);
- }
- public void executeUpdate(string storedProcedureName, SqlParameter[] sqlParams)
- {
- try
- {
- sqlParams[sqlParams.Length-3].Value = "RMS";
- //SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, storedProcedureName, sqlParams);
- }
- catch (SqlException e)
- { string msg = e.Message; }
- }
- public void executeDelete(string storedProcedureName, SqlParameter[] sqlParams)
- {
- try
- {
- //SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, storedProcedureName, sqlParams);
- }
- catch(SqlException e)
- { string msg = e.Message; }
- }
- #endregion
- }
- }
I commented out the application block since I don’t have it installed and well I plan on removing it. I will give my past self a gold star that it only affected a single class.
But then I noticed I am catching exceptions and not throwing them… no idea what the hell I was thinking there.
So here are some steps I am taking to just reformat the code and basic refactoring.
1. Removing the regions
2. Getting rid of unused namespaces
3. Removing the try catch blocks (We can figure out exception handling later)
And I removed unused comments. These steps alone reduced the size of the file by half.
I am going to rename files as well to reflect a more common coding standard.
The resulting file is now:
- using System.Data;
- using System.Data.SqlClient;
- namespace RMS_DALObjects
- {
- public class BaseDALObject
- {
- SqlConnection sqlConn;
- string strConn = "connection_string";
- public DataSet GetDataSet(string strSQL)
- {
- return null;
- //return SqlHelper.ExecuteDataset(strConn, CommandType.Text, strSQL);
- }
- public SqlDataReader GetDataReader(string strSQL)
- {
- sqlConn = new SqlConnection(strConn);
- sqlConn.Open();
- SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn);
- return sqlCmd.ExecuteReader();
- }
- public void CloseConnection()
- {
- sqlConn.Close();
- }
- public SqlParameter[] GetParameters(string storedProcedureName)
- {
- return null; //SqlHelperParameterCache.GetSpParameterSet(strConn, storedProcedureName);
- }
- public void ExecuteUpdate(string storedProcedureName, SqlParameter[] sqlParams)
- {
- sqlParams[sqlParams.Length - 3].Value = "RMS";
- //SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, storedProcedureName, sqlParams);
- }
- public void ExecuteDelete(string storedProcedureName, SqlParameter[] sqlParams)
- {
- //SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, storedProcedureName, sqlParams);
- }
- }
- }
I am pretty sure we will eventually change this from using inheritance to using composition but for now, I will just leave it alone. I will change the code to use regular good ole ADO .net
You can’t change everything at once. But we can start to change one thing at a time. So although I am slightly confused as to why I need to keep the connection around and why some other object is using that connection and calling close, I will leave it as is.
But I will change the name of the sqlConn to _connection. I will also create a createConnection method the other methods can use.
It has also been awhile since I used plain ole vanilla ADO so forgive me if I do something wrong. ![]()
After a bit of tweaking the GetDataSet method now looks like
- public DataSet GetDataSet(string sql)
- {
- createConnection();
- var dataSet = new DataSet();
- new SqlDataAdapter(sql, _connection).Fill(dataSet);
- return dataSet;
- }
Refactoring the GetDataReader method results in
- public SqlDataReader GetDataReader(string sql)
- {
- createConnection();
- _connection.Open();
- return new SqlCommand(sql, _connection).ExecuteReader();
- }
Now the parameters methods I am going to have to think about. It’s rather late and that is a pretty good start.
Again check out the code at http://github.com/RookieOne/RMS_Refactor
In the future I should branch the code, but … I forgot.
0 comments:
Post a Comment