Skip to content

Introduces a DbSession object that manages the database connnection and transaction.

Notifications You must be signed in to change notification settings

fgheysels/Fg.DbUtils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fg.DbUtils

Build Status NuGet Badge

This project introduces a DbSession class which manages the connection to a (relational) database and the related transaction (if any).

Installation

PM > Install-Package Fg.DbUtils

Usage

I'm using the DbSession class when using Dapper to communicate with a relational database. When starting a transaction on the DbSession, you do not need to pass in the IDbTransaction object with each database request. Since IDbSession implements the IDbConnection interface, integration with Dapper is fairly easy.

Executing transactional DB queries

DbSession session = new DbSession(_connection);

await session.WithTransactionAsync( async () => 
{
    // Note that we do not need to pass in the transaction parameter.
    await session.ExecuteAsync("INSERT INTO Persons (FirstName, LastName) VALUES (@FirstName, @LastName)",
                               param: new { FirstName = "Frederik", LastName = "Gheysels" });
                               
    if( new Random().NextValue(3) % 2 != 0 )
    {
        // Throwing an exception will make sure that the transaction is rollbacked.
        throw new InvalidOperationException();
    }
                               
    // When no exception occurs, the transaction is committed
});

About

Introduces a DbSession object that manages the database connnection and transaction.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages