Use a IDBConnection in a service

asp.net-mvc c# dapper

Question

On an MVC project I have a service layer with commands and queries.

In some of these commands and queries I use Dapper which needs a Connection.

public class GetPostsStatsQuery {

  public GetPostsStatsQuery() {
  }

  public PostStats Execute() {
     // Code here
  }
}

What options do I have to create a connection and use it in dapper query?

  1. Can I inject one? How and with which lifecycle?

  2. Should I use "using (..."?

  3. Any other option?

Expert Answer

Any of those options is fine, as is just about any other option (for example, a connection-factory). This is entirely an implementation detail, and should be driven by your other requirements. There is no single "right answer" here.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why