Code Monkey home page Code Monkey logo

dapper-wrapper's Introduction

I am no longer maintaining this repository. If you have a fork or alternative for this library you think is good to direct people to, please let me know.

DapperWrapper is a library that wraps the Dapper extension methods on IDbConnection to make unit testing easier.

Why bother? Because stubbing the extension methods used in a method-under-unit-test is a pain in the ass. For instance, you can't just use a library like Moq to stub the .Query extension method on a fake IDbConnection. To work around this, I introduce a new abstraction, IDbExecutor.

The IDbExecutor Interface

The IDbExectuor interface has three methods, each corresponding to a Dapper extension method: Execute, Query, and Query<T>. Wherever you would previously inject an IDbConnection to use with Dapper, you instead inject an IDbExecutor. There is a single implementation of IDbExecutor included in DapperWrapper, SqlExecutor, that uses the Dapper extension methods against SqlConnection. Adding your own IDbExecutor against other implementations of IDbConnection is easy.

Example use of IDbExecutor:

public IEnumerable<SemanticVersion> GetAllPackageVersions(
  string packageId,
  IDbExecutor dbExecutor) {
  return dbExecutor.Query<string>("SELECT p.version FROM packages p WHERE p.id = @packageId", new { packageId })
    .Select(version => new SemanticVersion(version));
}

Injecting IDbExecutor

You probably already have an apporach to injecting IDbConnection into your app that you're happy with. That same approach will probably work just as well with IDbExecutor.

Personally, in my dependency container or service locator, I like to bind IDbExecutor to a method that instantiates a new SqlConnection and SqlExecutor. If you need to control the creation of the executor (for instance, you only need it conditionally), you could bind Func<IDbExecutor>. There's also an IDbExecutorFactory interface in DapperWrapper you could use, but it comes with the same downsides as any factory type.

Example of binding IDbExecutor and IDbExecutorFactory using Ninject:

public class DependenciesRegistrar : NinjectModule {
  public override void Load() {
    Bind<IDbExecutor>()
      .ToMethod(context => {
	    var sqlConnection = new SqlConnection(connectionString);
		sqlConnection.Open();
		return new SqlExecutor(sqlConnection);
      });
	Bind<IDbExecutorFactory>()
      .ToMethod(context => {
	    return new SqlExecutorFactory(connectionString);
      })
	  .InSingletonScope();
  }
}

Transactions

I sometimes need to assert whether a method-under-unit-test completes a transaction via TransactionScope. To make this easier, DapperWrapper also has an ITransactionScope interface (and TransactionScopeWrapper implementation) that makes it easy to create a fake transaction, and stub (and assert on) the Complete method. As with IDbExecutor, you can bind it directly, via Func<ITransactionScope>.

dapper-wrapper's People

Contributors

davidduffett avatar half-ogre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dapper-wrapper's Issues

Multi Mapping

Hello,

Is there any chance of getting all the multi mapping Query functions exposed through the wrapper?

Thanks

Glenn

Wondering if there should be an accessor to .Open?

This is something that i'm doing .. and right now .. there's no way I can. (Yes, i can fork and PR ๐Ÿ‘ )

basically, my repo takes either a connection string or an IDbExecutor. 1st ctor is for my normal code. 2nd ctor is for my TDD.

(pseudo code @ gist)

The reason i'm using two ctors is because i might use the SqlConnection multiple times in a a method (eg. get users, get cats, get dogs) .. and i open/close for each one call as early as possible.

thoughts, @half-ogre ?

Weird Issue with Query / Query<T> results returning NULL.

Hi Drew.

this is a werid one and I have no idea how to repo it. @Buildstarted did get some code going, but it seemed to work (so no issue) but in my project i got two weird issues.

=> When testing my code while debugging (eg. in an XUnit and stepping through code or just F5 debugging) the Query (from the Dapper-Wrapper) was returning NULL (sometimes).

We all know queries never return null. Empty list - sure, null, nope.

It never seemed to happen when I did a run without debugging. Like .. unit test -> run.

I'm going to have to do more research but this really stumped me for a good few hours today ๐Ÿ˜ข

The wrapper code is very simple - which is why i'm stumped.

It's like .. when i got a result that was going to be an empty result .. I got a NULL.

Swapping my code around so i'm using a normal SqlConnection instead of a Dapper-Wrapper fixed it instantly.

So i don't have my new test(s) working right now.

Anyways - just starting a convo on this. If i get more data, i'll post it in here. Maybe someone else might be getting this issue?

Should IDbExecutor derive from IDisposable?

You've added a .Dispose, however, I'm unable to use this in a using statement as you would with an IDbConnection... if you derive it from IDisposable, that pattern should work (I think).

Is there a reason to not do that?

Allow injection of IDbConnection instead of SqlConnection

By only accepting SqlConnection I cannot use this with, for example, the MiniProfiler ProfiledDbConnection (which derives from DbConnection).

It would be better to take in IDbConnection so other types of connections can be used.

TransactionScope

Please include an example of how to use with TransactionScopeWrapper...

Broken dependency on Dapper 1.8

I know it's been baked for a long time, but my DI container is throwing a nasty error from time to time - not always, but it's annoying when it happens:

System.IO.FileNotFoundException: Could not load file or assembly 'Dapper, Version=1.8.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
   at DapperWrapper.SqlExecutor.Query(String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType)

Seem that Dapper Wrapper take a dependency on Dapper v1.8 which doesn't exist. This can be seen in the Nuget dependecy graph as well as in the source code here itself and I'm just wondering why that is the case? Was there once upon a time a v1.8 of Dapper that disappeared?

Anyway, I thought I'd mention the issue that is throwing up on some projects since I think it's something that could quite easily be fixed. If you're accepting PRs for that kind of thing, I'd be happy to provide.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.