vendredi 29 juin 2018

Best way to refactor this class

We have this legacy system that has many DataAccess classes for adding/removing/retrieving data from many bussines entities from the SQL Server.

Thing is all have the singleton pattern applied for the DB connection. So for each DataAccess class there's a static DB (GetInstance like) method that returns the connection (it keeps one connection per DataAccess instance). This started giving us headaches because the connection is shared and we seem to have multithreading issues, etc.

public class SomeDataAccess {

private static Database dBConnection;

private static Database DB
       {
            get
            {
                if (dBConnection== null)
                {
                    dBConnection= DatabaseFactory.CreateDatabase();
                }
                return dBConnection;
            }
        }

public method_1() {
  ... 

   DB.ExecuteScalar("mystoredprocedure", param1, param2,...param_n);

  ...
}

.
.
.

public method_k() {
  ... 

   DB.ExecuteReader(...)

  ...
}

}

Anyway, we would like to refactor this code into the use of "Using" for keeping the connections and closing them automatically, if possible, or some other good practice for C# in this case.

Aucun commentaire:

Enregistrer un commentaire