Created
February 6, 2014 03:47
-
-
Save PureKrome/8838120 to your computer and use it in GitHub Desktop.
Dapper-Wrapper idea.
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
This is something that i'm doing .. and right now .. there's no way I can. (Yes, i can fork and PR :+1: ) | |
(pseudo code) | |
public class Foo | |
{ | |
private readonly string _connectionString; | |
private readonly IDbExecutor _sqlConnectionWrapper; | |
// Ctor - for normal code. | |
public Foo(string connectionString) | |
{ | |
connectionString.ShouldNotBeEmpty(); | |
_connectionString = connectionString | |
} | |
// Ctor - for when we mock. | |
public Foo(IDbExecutor dbExecutor) | |
{ | |
dbExecutor.ShouldNotBe(null); | |
_dbExecutor = dbExecutor; | |
} | |
// Method to test. | |
public int? Bar() | |
{ | |
int? userId = 0; | |
using (var sqlConnection = (_sqlConnectionWrapper ?? new SqlExecutor(new SqlConnection(_connectionString)))) | |
{ | |
try | |
{ | |
sqlConnection.Open(); // ******* Compile Error ******* | |
userId = sqlConnection.Query<int?>("SELECT ... ").SingleOrDefault(); | |
} | |
finally | |
{ | |
sqlConnection.Close(); | |
} | |
} | |
return userId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment