/DbExecutor

Simple unit of work for database connection

Primary LanguageC#

DbExecutor

Package Last version
DbConn.DbExecutor.Abstract NuGet Pre Release
[OBSOLETE - you must use DbConn.DbExecutor.Dapper.SqlServer instead] DbConn.DbExecutor.Dapper NuGet Pre Release
DbConn.DbExecutor.Dapper.Ioc.Autofac NuGet Pre Release
DbConn.DbExecutor.Dapper.Npgsql NuGet Pre Release
DbConn.DbExecutor.Dapper.Sqlite NuGet Pre Release
DbConn.DbExecutor.Dapper.SqlServer NuGet Pre Release

Simple wrapper with factory for working with database connections via Dapper

Builds

Branch Build status
master Build status
dev Build status

AppVeyor Nuget project feed: https://ci.appveyor.com/nuget/dbexecutor-q2ir84d55gwi

Dependencies

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

How to use

1. Abstractions

  1. Install from NuGet:
Install-Package DbConn.DbExecutor.Abstract
  1. Then you can use IDbExecutor and IDbExecutorFactory interfaces.

2. Implementations

2.1. SQL Server

  1. Install from NuGet:
Install-Package DbConn.DbExecutor.Dapper.SqlServer
  1. After you can use IDapperDbExecutor and IDapperDbExecutorFactory abstractions. 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=...;"
  }
}

2.2 Dependency injection & IoC

Autofac

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).