Last active
December 1, 2015 16:31
-
-
Save jchannon/e97e8f1d4534c9f67018 to your computer and use it in GitHub Desktop.
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
protected override void ConfigureApplicationContainer(TinyIoCContainer container) | |
{ | |
//base.ConfigureApplicationContainer(container); //Prevent assembly scanning | |
container.Register<IDbConnectionProvider, PostgresConnectionProvider>(); | |
} |
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
public interface IDbConnectionProvider | |
{ | |
IDbConnection GetConnection(); | |
} |
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
public class PostgresConnectionProvider : IDbConnectionProvider | |
{ | |
private readonly string connectionString; | |
public PostgresConnectionProvider() | |
{ | |
this.connectionString = ConfigurationManager.ConnectionStrings["blah"].ConnectionString; | |
} | |
public IDbConnection GetConnection() | |
{ | |
var connection = new NpgsqlConnection(this.connectionString); | |
connection.Open(); | |
return connection; | |
} | |
} |
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
public UserRepository(IDbConnectionProvider dbConnectionProvider) | |
{ | |
this.dbConnectionProvider = dbConnectionProvider; | |
} | |
public VQUser GetVQuser(int id) | |
{ | |
using (var dbConnection = this.dbConnectionProvider.GetConnection()) | |
{ | |
return dbConnection.Query<User>( | |
UserSql + " where u.id = @id", | |
new { id = id }).FirstOrDefault(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment