Dapper
Requires a ‘mature’ dependency injection container. Please refer to Advanced for more information
Dapper extensions are designed to make it easier to work with Dapper and Shiny Mediator. Like our HTTP extensions, we wanted to reduce boilerplate while exposing all of the mediator middleware while still keeping the full power of Dapper available. To accomplish this, we’ve also added InterpolatedSql to make it easier to work with SQL strings.
Setup
- Install the
package
- Register in your Shiny Mediator startup call
services.AddShinyMediator(cfg =>{// just like dapper, you just have to supply an ADO.NET connectioncfg.AddDapper<SqliteConnection>("your connection string");});
- Start making calls as shown below
How to use it
Unlike other extensions, Dapper doesn’t require you create any handlers or contracts, simply make one of the 3 available contract calls
IMediator mediator; // injected
var email = "allan%";
// get a single rowUser? result = await mediator.Request(new DapperFirstQuery<User>( $"select * from \"Users\" where \"Email\" like {email}"));
// execute multiple rowsIEnumerable<User> results = await mediator.Request(new DapperQuery<User>( $"select * from \"Users\" where \"Email\" like {email}"));
// execute scalars/countsobject? count = await mediator.Request(new DapperScalar( $"select count(*) from \"Users\" where \"Email\" like {email}"));
Dynamic Connections
There will be cases where you may need to connect to different databases that may even be different platforms. The initial setup shown above is designed for a single database on a single platform.
You didn’t think we would lock you in like that though did you?
We have a simple answer to accomplish this.
public class MyConnectionProvider : Shiny.Mediator.DapperRequests.IConnectionProvider{ public IDbConnection Create<TRequest>(TRequest request) { // from here, the sky is the limit for how you want to implement this. You get access to see the request object so you can adjust per type return new SqliteConnection("your connection string"); }}
Now, just register this in your startup
services.AddShinyMediator(cfg =>{ cfg.AddDapper<MyConnectionProvider>();});