Skip to content
A .NET library for common database functionality (migrations, ambient connection/transactions)
C# Shell
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
build
src
.gitignore
.gitmodules
LICENSE
README.md
SharpDataAccess.sln
SharpDataAccess.sln.DotSettings
build.sh

README.md

SharpDataAccess

SharpDataAccess

A .NET library for common database functionality (migrations, ambient connection/transactions)

About

This library provides a small set of utilities that are typically needed when implementing a bare-metal data-access solution, using ServiceStack.OrmLite.

There isn't much to this library, I just hated implementing repetitive types in all my projects.

Example usage

public class ExampleUsage
{
    public async Task Example()
    {
        // Build a service that will allow us to interact with the database.
        IDbConnectionFactory dbConnectionFactory = null; // see ServiceStack.OrmLite documentation
        IDataAccessLogger logger = new SharpDataAccess.Impl.ConsoleDataAccessLogger(); // Re-implement to plug into your logging system.
        var dataService = new SharpDataAccess.Data.Impl.DataService(dbConnectionFactory, logger);
        
        // Wait for the database to be available. This is ideal for docker scenarios, where the DB isn't immediately available.
        dataService.WaitForDbConnection(TimeSpan.FromSeconds(30));
        
        // Migration your database.
        IMigrationsBuilder migrationBuilder = null; // Implement this service to discover/instantiate your migrations.
        var migrator = new Migrator(dataService, migrationBuilder, logger);
        migrator.Migrate();
        
        // Access your data
        using (var connectionScope = new ConScope(dataService))
        {
            // Here is a raw connection.
            IDbConnection connection = connectionScope.Connection;

            using (var transactionScope = await connectionScope.BeginTransaction())
            {
                await DataMethod(dataService);
                
                transactionScope.Commit();
            }
        }
    }

    private async Task DataMethod(IDataService dataService)
    {
        using (var connectionScope = new ConScope(dataService))
        {
            // This is the same exact connection from the outer method call.
            IDbConnection connection = connectionScope.Connection;

            using (var transactionScope = await connectionScope.BeginTransaction())
            {
                // This is the same transaction from the outer method call.
                transactionScope.Commit();
            }
        }
    }
}
You can’t perform that action at this time.