I inherited a fairly large C # code, which is clogged hundreds of times with the following way to perform operations with the database:
using (SqlConnection connection = new SqlConnection(connectionString)) {
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
However, due to factors beyond my control, the database server is cruel and especially error prone, so a significant portion of these requests fail. Given that this particular way of accessing the database is scattered throughout the code, how can I encode the replay in one place and apply it everywhere SqlCommand?
Some thoughts:
- I tried to override
SqlConnection/ SqlCommand ExecuteNonQueryto create a wrapper version. Not good. SqlConnectionand SqlCommandare C # private objects. - I could create a container class with a name
MySqlConnectionthat contains a .NET object SqlConnection, and then create my own ExecuteNonQuery, which in turn will retry several times SqlCommand.ExecuteNonQuery(). However, this is not good, because I would have to implement each function SqlCommandAND train other people to directly use my wrapper class instead SqlCommand. Unfortunately, user training is not an acceptable solution. I am stuck with other developers who have outsourced, have frequent turnover and never heed my requests to use the new SQL class. - As I mentioned earlier, I simply do not have access (politically and technically) to the dirty DB layer. The root cause of the problem is simply a poor SQL server setup, but I cannot solve the problem at this level.
, ?