{"id":22189980,"url":"https://github.com/fgheysels/fg.dbutils","last_synced_at":"2025-07-26T22:30:38.111Z","repository":{"id":146264758,"uuid":"295231428","full_name":"fgheysels/Fg.DbUtils","owner":"fgheysels","description":"Introduces a DbSession object that manages the database connnection and transaction.","archived":false,"fork":false,"pushed_at":"2024-03-14T16:28:23.000Z","size":26,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-03-15T17:02:32.216Z","etag":null,"topics":["ado-net","dapper","dapper-extensions"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fgheysels.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-09-13T20:24:28.000Z","updated_at":"2024-04-20T22:31:06.358Z","dependencies_parsed_at":null,"dependency_job_id":"93e7c3a4-02c1-498c-b5bf-18e4d7868193","html_url":"https://github.com/fgheysels/Fg.DbUtils","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fgheysels%2FFg.DbUtils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fgheysels%2FFg.DbUtils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fgheysels%2FFg.DbUtils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fgheysels%2FFg.DbUtils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fgheysels","download_url":"https://codeload.github.com/fgheysels/Fg.DbUtils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227722174,"owners_count":17809876,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ado-net","dapper","dapper-extensions"],"created_at":"2024-12-02T11:40:59.575Z","updated_at":"2025-07-26T22:30:38.105Z","avatar_url":"https://github.com/fgheysels.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fg.DbUtils\n\n[![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\u0026branchName=master)\n[![NuGet Badge](https://buildstats.info/nuget/fg.dbutils?includePreReleases=true)](https://www.nuget.org/packages/fg.dbutils/)\n\n\nThis project introduces a `DbSession` class which manages the connection to a (relational) database and the related transaction (if any).\n\n## Installation\n\n```\nPM \u003e Install-Package Fg.DbUtils\n```\n\n## Usage\n\nI'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.\nSince `IDbSession` implements the `IDbConnection` interface, integration with Dapper is fairly easy.\n\n### Executing transactional DB queries\n\n```csharp\nDbSession session = new DbSession(_connection);\n\nawait session.WithTransactionAsync( async () =\u003e \n{\n    // Note that we do not need to pass in the transaction parameter.\n    await session.ExecuteAsync(\"INSERT INTO Persons (FirstName, LastName) VALUES (@FirstName, @LastName)\",\n                               param: new { FirstName = \"Frederik\", LastName = \"Gheysels\" });\n                               \n    if( new Random().NextValue(3) % 2 != 0 )\n    {\n        // Throwing an exception will make sure that the transaction is rollbacked.\n        throw new InvalidOperationException();\n    }\n                               \n    // When no exception occurs, the transaction is committed\n});\n```\n\nNote 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).\n\nThis 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:\n\n```csharp\nDbSession session = new DbSession(_connection);\n\nIEnumerable\u003cPerson\u003e persons = await session.QueryAsync(\"SELECT TOP 20 FirstName, LastName FROM Persons\");\n\nforeach( var person in persons )\n{\n    Console.WriteLine($\"Firstname = {person.FirstName} LastName = {person.LastName}\");\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffgheysels%2Ffg.dbutils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffgheysels%2Ffg.dbutils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffgheysels%2Ffg.dbutils/lists"}