Skip to content

Instantly share code, notes, and snippets.

@pimbrouwers
Created March 13, 2019 21:32
Show Gist options
  • Save pimbrouwers/e91c62f33f036e040f27764e512cf8a1 to your computer and use it in GitHub Desktop.
Save pimbrouwers/e91c62f33f036e040f27764e512cf8a1 to your computer and use it in GitHub Desktop.
transacter - The dapper extensions you know and love, but via `IDbTransaction`
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
namespace Transacter
{
public static class IDbTransactionExtensions
{
public static async Task<int> Execute(
this IDbTransaction trans,
string sql,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text) =>
await trans.Connection.ExecuteAsync(sql, param, trans, commandTimeout: commandTimeout, commandType: commandType);
public static async Task<T> ExecuteScalar<T>(
this IDbTransaction trans,
string sql,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text) =>
await trans.Connection.ExecuteScalarAsync<T>(sql, param, trans, commandTimeout: commandTimeout, commandType: commandType);
public static async Task<T> QueryFirstOrDefault<T>(
this IDbTransaction trans,
string sql,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text) =>
await trans.Connection.QueryFirstOrDefaultAsync<T>(sql, param, trans, commandTimeout: commandTimeout, commandType: commandType);
public static async Task<T> QuerySingleOrDefault<T>(
this IDbTransaction trans,
string sql,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text) =>
await trans.Connection.QuerySingleOrDefaultAsync<T>(sql, param, trans, commandTimeout: commandTimeout, commandType: commandType);
public static async Task<IEnumerable<T>> Query<T>(
this IDbTransaction trans,
string sql,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text) =>
await trans.Connection.QueryAsync<T>(sql, param, trans, commandTimeout: commandTimeout, commandType: commandType);
public static async Task<IEnumerable<T>> Query<T, TSecond>(
this IDbTransaction trans,
string sql,
Func<T, TSecond, T> map,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text,
string splitOn = "Id") =>
await trans.Connection.QueryAsync(sql, map, param, trans, commandTimeout: commandTimeout, commandType: commandType, splitOn: splitOn);
public static async Task<IEnumerable<T>> Query<T, TSecond, TThird>(
this IDbTransaction trans,
string sql,
Func<T, TSecond, TThird, T> map,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text,
string splitOn = "Id") =>
await trans.Connection.QueryAsync(sql, map, param, trans, commandTimeout: commandTimeout, commandType: commandType, splitOn: splitOn);
public static async Task<IEnumerable<T>> Query<T, TSecond, TThird, TFourth>(
this IDbTransaction trans,
string sql,
Func<T, TSecond, TThird, TFourth, T> map,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text,
string splitOn = "Id") =>
await trans.Connection.QueryAsync(sql, map, param, trans, commandTimeout: commandTimeout, commandType: commandType, splitOn: splitOn);
public static async Task<IEnumerable<T>> Query<T, TSecond, TThird, TFourth, TFifth>(
this IDbTransaction trans,
string sql,
Func<T, TSecond, TThird, TFourth, TFifth, T> map,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text,
string splitOn = "Id") =>
await trans.Connection.QueryAsync(sql, map, param, trans, commandTimeout: commandTimeout, commandType: commandType, splitOn: splitOn);
public static async Task<IEnumerable<T>> Query<T, TSecond, TThird, TFourth, TFifth, TSixth>(
this IDbTransaction trans,
string sql,
Func<T, TSecond, TThird, TFourth, TFifth, TSixth, T> map,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text,
string splitOn = "Id") =>
await trans.Connection.QueryAsync(sql, map, param, trans, commandTimeout: commandTimeout, commandType: commandType, splitOn: splitOn);
public static async Task<IEnumerable<T>> Query<T, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh>(
this IDbTransaction trans,
string sql,
Func<T, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, T> map,
object param = null,
int commandTimeout = 30,
CommandType commandType = CommandType.Text,
string splitOn = "Id") =>
await trans.Connection.QueryAsync(sql, map, param, trans, commandTimeout: commandTimeout, commandType: commandType, splitOn: splitOn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment