An open API service indexing awesome lists of open source software.

https://github.com/fgheysels/fg.dbutils

Introduces a DbSession object that manages the database connnection and transaction.
https://github.com/fgheysels/fg.dbutils

ado-net dapper dapper-extensions

Last synced: 11 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# Fg.DbUtils

[![Build Status](https://frederikgheysels.visualstudio.com/GitHub%20Pipelines/_apis/build/status/Fg.DbUtils%20CI?branchName=master)](https://frederikgheysels.visualstudio.com/GitHub%20Pipelines/_build/latest?definitionId=7&branchName=master)
[![NuGet Badge](https://buildstats.info/nuget/fg.dbutils?includePreReleases=true)](https://www.nuget.org/packages/fg.dbutils/)

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

```csharp
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
});
```

Note that `ExecuteAsync` is not a member method of `IDbSession` or `DbSession`. Instead, this is an extension method that is defined by the [Dapper micro ORM](https://github.com/DapperLib/Dapper).

This means that you can use `DbSession` as well in conjunction with Dapper to easily execute SELECT queries on your database, and Dapper will translate the resultset to your object-model:

```csharp
DbSession session = new DbSession(_connection);

IEnumerable persons = await session.QueryAsync("SELECT TOP 20 FirstName, LastName FROM Persons");

foreach( var person in persons )
{
Console.WriteLine($"Firstname = {person.FirstName} LastName = {person.LastName}");
}
```