{"id":20306447,"url":"https://github.com/guilhermebley/bluow","last_synced_at":"2026-06-11T07:30:59.983Z","repository":{"id":61592409,"uuid":"552913006","full_name":"GuilhermeBley/BlUoW","owner":"GuilhermeBley","description":"Unit of Work/Session with a shared connection","archived":false,"fork":false,"pushed_at":"2022-10-28T16:02:33.000Z","size":41,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-04T07:30:19.875Z","etag":null,"topics":["csharp","dapper","dapper-donet-core","session","unitofwork"],"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/GuilhermeBley.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}},"created_at":"2022-10-17T12:26:33.000Z","updated_at":"2022-10-26T17:48:07.000Z","dependencies_parsed_at":"2023-01-20T14:17:19.793Z","dependency_job_id":null,"html_url":"https://github.com/GuilhermeBley/BlUoW","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/GuilhermeBley/BlUoW","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GuilhermeBley%2FBlUoW","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GuilhermeBley%2FBlUoW/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GuilhermeBley%2FBlUoW/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GuilhermeBley%2FBlUoW/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GuilhermeBley","download_url":"https://codeload.github.com/GuilhermeBley/BlUoW/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GuilhermeBley%2FBlUoW/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34188272,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-11T02:00:06.485Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["csharp","dapper","dapper-donet-core","session","unitofwork"],"created_at":"2024-11-14T17:13:24.048Z","updated_at":"2026-06-11T07:30:59.948Z","avatar_url":"https://github.com/GuilhermeBley.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BlUoW\nUnit of Work/Session with a shared connection.\n\n## How it works\n\u003cp\u003eThrough a shared connection, repositories have to use a transaction and connection available for the persistency and collect of data.\u003c/p\u003e\n\u003cp\u003eThe session is managed by a unit of work, where you have a possibility of open connection, save changes (commit), roll back, and initializes a transaction.\u003c/p\u003e\n\n## Benefics\n\u003cp\u003eUnit of work, repositoy control\u003c/p\u003e\n\n## Care that must be taken\n\u003cp\u003eShould be taken correctly the available connection and transaction in the repository.\u003c/p\u003e\n\u003cp\u003eThe same about the services that uses the repositories, the unit of work should have a open connection, and if it uses a transaction, must be save or roll back in the final to persistency the data.\u003c/p\u003e\n\n## How to use\n- \u003cb\u003eDependency Injection\u003c/b\u003e\n\u003cp\u003eThe implementation with the inversion control using the 'Microsoft.Extensions.DependencyInjection' is maked this way:\u003c/p\u003e\n\n```csharp\nservices\n  .AddScoped(typeof(IConnectionFactory), connectionFactoryType)\n  .AddScoped\u003cDbSession\u003e()\n  .AddScoped\u003cIDbSession\u003e(x =\u003e x.GetRequiredService\u003cDbSession\u003e())\n  .AddScoped\u003cIUnitOfWork\u003e(x =\u003e x.GetRequiredService\u003cDbSession\u003e());\n```\n\nObs. A type which implements IConnectionFactory is necessary to use the unit of work.\n\n- \u003cb\u003eRepositories\u003c/b\u003e\n\u003cp\u003eThe repository must be use a session, this way:\u003c/p\u003e\n\n```csharp\nusing BlUoW.Session;\n\nclass Table1Repository\n{\n  private readonly IDbSession _dbSession;\n\n  public Table1Repository(IDbSession dbSession)\n  {\n      _dbSession = dbSession;\n  }\n\n  public async Task\u003cint\u003e DeleteAll()\n  {\n      return await _dbSession.Connection.ExecuteAsync(\n          \"DELETE FROM test.table1;\",\n          _dbSession.Transaction\n      );\n  }\n}\n```\n\n- \u003cb\u003eServices (using the repositories)\u003c/b\u003e\n\u003cp\u003eAnd, to use that in the services, can be used with only a connection:\u003c/p\u003e\n\n```csharp\nusing BlUoW.Session;\n\npublic class Service : IService\n{\n  private readonly IUnitOfWork _uoW;\n  private readonly Table1Repository _table1Repository;\n\n  public Service(IUnitOfWork uoW, Table1Repository table1Repository)\n  {\n      _uoW = uoW;\n      _table1Repository = table1Repository;\n  }\n\n  public async Task AddWithConnection(Table1 model)\n  {\n      // The 'using' closes the connection in your final\n      // The 'OpenConnectionAsync' try open a new async connection\n      using (await _uoW.OpenConnectionAsync())\n      {\n          // With connection, the persistency to DB is automatic\n          var insert = await _table1Repository.AddAsync(new Table1()); \n      }\n  }\n}\n```\n\n\u003cp\u003eOr, could be used too as transaction:\u003c/p\u003e\n\n```csharp\nusing BlUoW.Session;\n\npublic class Service : IService\n{\n  private readonly IUnitOfWork _uoW;\n  private readonly Table1Repository _table1Repository;\n\n  public Service(IUnitOfWork uoW, Table1Repository table1Repository)\n  {\n      _uoW = uoW;\n      _table1Repository = table1Repository;\n  }\n\n  public async Task AddWithTransactionAsync(Table1 model)\n  {\n      // The 'using' closes the connection in your final\n      // The 'BeginTransactionAsync' try open a new async connection and transaction\n      using (await _uoW.BeginTransactionAsync())\n      {\n          var insert = await _table1Repository.AddAsync(new Table1());\n\n          if (insert is null)\n              await _uoW.RollBackAsync(); // Roll Back and releases transaction\n\n          await _uoW.SaveChangesAsync(); // Commit and releases transaction\n      }\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguilhermebley%2Fbluow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguilhermebley%2Fbluow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguilhermebley%2Fbluow/lists"}