Injecting IDbConnections to use with Dapper. How?

819 views
Skip to first unread message

MDMoura

unread,
Sep 18, 2014, 5:03:36 PM9/18/14
to structure...@googlegroups.com
Hello,

I want to use Dapper on my service layer ...

Dapper contains IDbConnections extensions but does not handle the connections.

Can I inject IDbConnections with Structure Map? How should I use it?

I tried to create a DapperWrapper as follows:

    public class DapperWrapper {

      private ConnectionString { get { return Settings.ConnectionString; } }

      public IEnumerable<T> Query<T>(String sql, dynamic parameters = null) {
    
        IEnumerable<T> result;

        using (IDbConnection connection = new SqlConnection(_connection)) {
          connection.Open();
          result = SqlMapper.Query<T>(connection, sql, parameters);       
        }

        return result;
 
      } // Query

      // OTHER DAPPER METHODS
     }

But I would like to inject the connections ... And in what lifecycle.

Thank You,
Miguel

MDMoura

unread,
Sep 23, 2014, 3:43:47 AM9/23/14
to structure...@googlegroups.com
Hello,

I am using it this way:

    For<IDbConnection>().HybridHttpOrThreadLocalScoped().Use("Connection", x => {
        var connnection new SqlConnection("myConnectionString")
        connection.Open();
        return connection;
    });

Is this the correct way?

Should I open it here or only inside the service where I use it?

Marcin Jonko

unread,
Sep 23, 2014, 10:18:58 AM9/23/14
to structure...@googlegroups.com
As far as i know Dapper is smart enough to open connection for you. I would simply pass IDbConnection to DAO class via constructor

For<IDbConnection>()
                        .HybridHttpOrThreadLocalScoped()
                        .Use<SqlConnection>()
                        .SelectConstructor(() => new SqlConnection(String.Empty))
                        .Ctor<string>().Is(ConfigurationManager.AppSettings["MyConnectionStringKey"]);

and then simply use connection variable 

Connection.Query<ShipmentSimplified>(sql ....

MDMoura

unread,
Sep 23, 2014, 5:05:32 PM9/23/14
to structure...@googlegroups.com


On Tuesday, September 23, 2014 3:18:58 PM UTC+1, Marcin Jonko wrote:
As far as i know Dapper is smart enough to open connection for you.
I would simply pass IDbConnection to DAO class via constructor

For<IDbConnection>()
                        .HybridHttpOrThreadLocalScoped()
                        .Use<SqlConnection>()
                        .SelectConstructor(() => new SqlConnection(String.Empty))
                        .Ctor<string>().Is(ConfigurationManager.AppSettings["MyConnectionStringKey"]);

and then simply use connection variable 

Am I doing wrong using:

    For<IDbConnection>()
      .HybridHttpOrThreadLocalScoped()
      .Use("Connection", x => new SqlConnection(MyConnectionStringName));

I am injecting it in my services constructors ...

I am not sure why you are using SelectConstructur and Ctor ...
Reply all
Reply to author
Forward
0 new messages