A simple Dapper Wrapper

August 5, 2016 @ 10:28 am Posted to .Net by Antony Koch

If you need to sub out some Dapper functionality, and aren’t too worried about the specifics of the call, then I’ve crafted a nifty class you can use to perform just such a task.

In all it’s glory, the subbable IDapperWrapper, with its default implementation:


    public interface IDapperWrapper
    {
        IEnumerable<T> Return<T>(IDbConnection connection, Func<IDbConnection, IEnumerable<T>> toRun);
        T Return<T>(IDbConnection connection, Func<IDbConnection, T> toRun);
        void Void(IDbConnection connection, Action<IDbConnection> toRun);
    }

    public class DapperWrapper : IDapperWrapper
    {
        public IEnumerable<T> Return<T>(IDbConnection connection, Func<IDbConnection, IEnumerable<T>> toRun)
        {
            return toRun(connection);
        }

        public T Return<T>(IDbConnection connection, Func<IDbConnection, T> toRun)
        {
            return toRun(connection);
        }

        public void Void(IDbConnection connection, Action<IDbConnection> toRun)
        {
            toRun(connection);
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *