Simple wrapper with factory for working with database connections via Dapper
| Branch | Build status |
|---|---|
| master | |
| dev |
AppVeyor Nuget project feed: https://ci.appveyor.com/nuget/dbexecutor-q2ir84d55gwi
| Project | Dependency |
|---|---|
| DbConn.DbExecutor.Abstract | No |
| DbConn.DbExecutor.Dapper.Npgsql | Dapper, Npgsql |
| DbConn.DbExecutor.Dapper.Sqlite | Dapper, Microsoft.Data.Sqlite |
| DbConn.DbExecutor.Dapper.SqlServer | Dapper, System.Data.SqlClient |
| DbConn.DbExecutor.Dapper.Ioc.Autofac | Autofac.Extensions.DependencyInjection |
- Install from NuGet:
Install-Package DbConn.DbExecutor.Abstract
- Then you can use
IDbExecutorandIDbExecutorFactoryinterfaces.
- Install from NuGet:
Install-Package DbConn.DbExecutor.Dapper.SqlServer
- After you can use
IDapperDbExecutorandIDapperDbExecutorFactoryabstractions. For example (business component that creates user):
public class UserCreator
{
private readonly IDbExecutorFactory _dbExecutorFactory;
private readonly AppConfig _appConfig;
public UserCreator(IDbExecutorFactory dbExecutorFactory, IOptions<AppConfig> appConfigOptions)
{
if(dbExecutorFactory == null)
throw new ArgumentNullException(nameof(dbExecutorFactory));
_dbExecutorFactory = dbExecutorFactory;
if (appConfigOptions?.Value == null)
throw new ArgumentNullException(nameof(appConfigOptions));
if (appConfigOptions.Value.ConnectionStrings == null)
throw new ArgumentNullException(nameof(appConfigOptions), "Connection strings section in configuration file is null");
_appConfig = appConfigOptions.Value;
}
public void CreateUser(string email, string password)
{
List<SomeEntity> someEntities;
// Example for non-transactional IDbExecutor:
using (var dbExecutor = _dbExecutorFactory.Create(_appConfig.ConnectionStrings.UserDb))
{
var querySql = $"select * from ...";
someEntities = dbExecutor.Query<SomeEntity>().ToList();
}
// Example for transactional IDbExecutor:
using (var dbExecutor = _dbExecutorFactory.CreateTransactional(_appConfig.ConnectionStrings.UserDb))
{
try
{
// Some logic for creating user
// Some strange logic :)
var executeSql = $"exec dbo.SomeStrangeProcedure @param1 = 1 ...";
dbExecutor.Execute(executeSql);
// Commit for applying our changes because IDbExecutor was created with opening transaction:
dbExecutor.Commit();
}
catch(Exception ex)
{
// Log error if you need
dbExecutor.Rollback();
}
}
}
}AppConfig example:
public class AppConfig
{
public ConnectionConfig ConnectionStrings { get; set; }
}
// ...
public class ConnectionConfig
{
public string UserDb { get; set; }
}And appsettings.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"UserDb": "data source=...;initial catalog=...;User ID=...;Password=...;"
}
}
Just install the DbConn.DbExecutor.Dapper.Ioc.Autofac package and this line:
builder.RegisterModule(new DbExecutorRegistrationModule());It will register implementations for IDbExecutor and IDbExecutorFactory for all
SQL providers MS SQL Server, PostgreSQL, SQLite).
Or you can register dependencies manually like this:
builder.RegisterType<DapperDbExecutorFactory>()
.As<IDbExecutorFactory>()
.InstancePerLifetimeScope();
builder.RegisterType<DapperDbExecutor>()
.As<IDbExecutor>()
.InstancePerLifetimeScope();where DapperDbExecutorFactory and DapperDbExecutor classes are comes from
the package you need (for MS SQL Server, PostgreSQL, SQLite).