Skip to content

Instantly share code, notes, and snippets.

@reidev275
Created October 5, 2012 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reidev275/3840693 to your computer and use it in GitHub Desktop.
Save reidev275/3840693 to your computer and use it in GitHub Desktop.
Decouple command execution from command creation to allow full unit testing of each.
using System.Data;
public interface ICommandExecutor
{
void ExecuteNonQuery(IDbCommand command);
}
public class CommandExecutor : ICommandExecutor
{
public void ExecuteNonQuery(IDbCommand command)
{
command.ExecuteNonQuery();
}
}
using System.Data.SqlClient;
public class SqlRepository
{
private readonly ICommandExecutor _commandExecutor;
private readonly string _connectionString;
public SqlRepository(string connectionString, ICommandExecutor commandExecutor)
{
_commandExecutor = commandExecutor;
_connectionString = connectionString;
}
public void Save(SomeObject obj)
{
using (var connection = new SqlConnection(_connectionString))
using (var command = connection.CreateCommand())
{
//setup command object
_commandExecutor.ExecuteNonQuery(command);
};
}
}
@reidev275
Copy link
Author

Delegates command execution to a class designed to execute commands. Allows full testing of repository classes without actually executing commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment