22

I'm looking for a way to inject my connections into my repositories. I tried to inject the SqlConnection using the IDBConnection, but I got some problems when NInject tries to deactivate the connection, the event is never called. And I cannot figure out how to inject the connection string into my repositories.

Could someone provide me a suggestion?

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94

1 Answers1

46

I use NInject to perform the Dependency Injection of my projects. I usually end with the configuration below:

Simple Factory Interface

public interface IDbConnectionFactory
{
    IDbConnection CreateConnection();
}

Factory Implementation

public class SqlConnectionFactory : IDbConnectionFactory
{
    private readonly string _connectionString;

    public SqlConnectionFactory(string connectionString)
    {
        _connectionString = connectionString;
    }

    public IDbConnection CreateConnection()
    {
        var conn = new SqlConnection(_connectionString);
        conn.Open();
        return conn;
    }
}

NInject Config:

Bind<IDbConnectionFactory>()
    .To<SqlConnectionFactory>()
    .WithConstructorArgument("connectionString",
ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString);

Example of a repository:

public class UserRepository : IUserRepository
{
    private readonly IDbConnectionFactory _dbConnectionFactory;

    public UserRepository(IDbConnectionFactory dbConnectionFactory)
    {
        _dbConnectionFactory = dbConnectionFactory;
    }

    public IEnumerable<User> List()
    {
        using (var dbConnection = _dbConnectionFactory.CreateConnection())
        {
            ....
        }
    }
}

Edit: I always let the ADO.NET take care of connection pooling. So I open and close the connection every single time that I need to perform a database operation.

This approach is working for me and is very simple as you mentioned in your question.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
gustavodidomenico
  • 4,640
  • 1
  • 34
  • 50
  • 1
    The complete example I was looking for. I will accept the answer as soon as SO allow me. Thank you dude. –  Jan 25 '13 at 14:01
  • so if you wanted to share the connection across requests, for use in a transaction, how would you go about doing it? – Naeem Sarfraz Oct 02 '15 at 12:11
  • In transaction based scenarios I use a unit of work pattern. And not just a connection factory. – gustavodidomenico Oct 02 '15 at 15:32
  • I'm still not getting it, if your UOW used this repository and the above List method it would end up opening a new connection? – Naeem Sarfraz Oct 02 '15 at 15:42
  • 1
    Not really. The repository would receive the UOW in place of the connection factory. The UOW would be now responsible for opening/closing/committing....Inside the List method you need to get the connection from the UOW and not create a new one. Does it make sense? – gustavodidomenico Oct 02 '15 at 16:18
  • @gustavodidomenico Would it be better to use a singleton in this example? – LiverpoolOwen Mar 08 '19 at 18:23
  • 1
    @LiverpoolOwen, I think that you mean use your container to inject the same instance every time you request an instance. Then, you can configure the binding to use the `InSingletonScope` configuration of `NInject`. I don't recommend using the regular singleton approach using the static field to hold the instance reference. – gustavodidomenico Mar 11 '19 at 12:51
  • @gustavodidomenico thanks for the reply, why would you not recommend this? Does this affect performance negatively? – LiverpoolOwen Mar 11 '19 at 13:04
  • When i say singleton I mean for the connection factory not the actual IDBconnection – LiverpoolOwen Mar 11 '19 at 13:20
  • I also meant the factory. The reason is not about performance to be honest, but from a maintainability, operational, testability and readability perspectives. If you have your dependency lifecycle managed by an IoC container, you should delegate this to it. – gustavodidomenico Mar 11 '19 at 13:50