I need to make some calls to stored procedures in my SQL database from a .NET application, using C#.
I have something like this, and I put just one of the methods but the rest will be similar:
public static class ClientDataAccess
{
public static void AddClient(Client client)
{
var connectionString = ConfigurationManager.ConnectionStrings["appDatabase"].ConnectionString;
using (var connection = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("Client_add", connection) { CommandType = System.Data.CommandType.StoredProcedure })
// Add parameters
connection.Open();
command.ExecuteNonQuery();
}
}
}
}
When I needed to test the classes where it's used I had to create a wrapper to be able to mock it:
public class ClientDataAccessWrapper : IClientDataAccessWrapper
{
public void AddClient(Client client)
{
ClientDataAccess.AddClient(client);
}
}
Is this considered to be a good practice?
var command = new SqlCommandbit in ausingconstruct as well since it implementsIDisposablejust likeSqlConnection. \$\endgroup\$