Created
October 5, 2012 15:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data; | |
public interface ICommandExecutor | |
{ | |
void ExecuteNonQuery(IDbCommand command); | |
} | |
public class CommandExecutor : ICommandExecutor | |
{ | |
public void ExecuteNonQuery(IDbCommand command) | |
{ | |
command.ExecuteNonQuery(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Delegates command execution to a class designed to execute commands. Allows full testing of repository classes without actually executing commands.