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?
Can I inject one? How and with which lifecycle?
Should I use "using (..."?
Any other option?
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.