Skip to content

Add DI extension packages for scoped, shared-connection DAO registration - #69

Merged
twogood merged 5 commits into
mainfrom
copilot/document-simplify-shared-connection-di
Oct 12, 2026
Merged

twogood merged 5 commits into
mainfrom
copilot/document-simplify-shared-connection-di

Conversation

Copilot AI commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Cross-DAO transactions only work if every DAO's gateway uses the same IDbConnection. That requirement is easy to miss and tedious to wire up in DI apps. This PR adds registration helpers that make the shared scoped connection the default, and documents the connection lifetime rules.

  • Activout.DatabaseClient.Extensions.DependencyInjection (new package)

    • Depends only on Microsoft.Extensions.DependencyInjection.Abstractions and the core package.
    • Adds AddDatabaseClient<TDao>(Func<IServiceProvider, IDatabaseGateway>, ServiceLifetime = Scoped), which works with any gateway.
  • Activout.DatabaseClient.Dapper.Extensions.DependencyInjection (new package)

    • Adds AddDapperDatabaseClient<TDao>(parameterPrefix = "@", lifetime = Scoped).
    • It builds a DapperGateway over the IDbConnection registered in the container.
    • It does not take a connection factory, so a per-DAO connection can't be created by accident.
  • Docs

    • Each package has a README, and the root README has a new section.
    • They state that the IDbConnection must be scoped so DAOs in a scope share it.
    • They state that the container owns and disposes the connection.
    • They state that DapperGateway opens the connection if it is closed.
  • Tests

    • SharedConnectionDependencyInjectionTest resolves two DAO interfaces from one scope. It begins a transaction on one DAO, writes through the other, then commits in one case and rolls back in another.
services.AddScoped<IDbConnection>(_ => new NpgsqlConnection(connectionString));
services.AddDapperDatabaseClient<IOrdersDao>();
services.AddDapperDatabaseClient<IBooksDao>();

Review notes:

  • The existing workflows run dotnet pack on all packable projects, so the new packages are picked up without CI changes.
  • The Dapper-specific package is separate so the core DI package stays gateway-agnostic.

@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 02664889-4e26-4100-bcf9-add85b52c411

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Co-authored-by: twogood <189982+twogood@users.noreply.github.com>
Copilot AI changed the title [WIP] Document and simplify shared-connection DI for cross-DAO transactions Add DI extension packages for scoped, shared-connection DAO registration Oct 12, 2026
Copilot AI requested a review from twogood October 12, 2026 11:12
@twogood
twogood requested a balanced review from Copilot October 12, 2026 11:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused implementation matches the stated lifetime requirements and includes representative transaction tests and documentation.

0 open findings

What changed in this PR

Adds DI packages that simplify scoped DAO registration and safely support cross-DAO transactions through a shared connection.

Changes:

  • Adds gateway-agnostic and Dapper-specific DI extensions.
  • Documents connection lifetime and ownership requirements.
  • Tests commit and rollback across separate DAOs sharing one connection.
File Description
README.md Documents shared-connection registration.
Activout.DatabaseClient.Test/​SharedConnectionDependencyInjectionTest.cs Tests cross-DAO commit and rollback.
Activout.DatabaseClient.Test/​Activout.DatabaseClient.Test.csproj Adds DI test dependencies.
Activout.DatabaseClient.sln Registers both new projects.
Activout.DatabaseClient.Extensions.DependencyInjection/​ServiceCollectionExtensions.cs Adds generic DAO registration.
Activout.DatabaseClient.Extensions.DependencyInjection/​README.md Documents generic DI usage.
Activout.DatabaseClient.Extensions.DependencyInjection/​Activout.DatabaseClient.Extensions.DependencyInjection.csproj Defines the generic DI package.
Activout.DatabaseClient.Dapper.Extensions.DependencyInjection/​ServiceCollectionExtensions.cs Adds Dapper-specific registration.
Activout.DatabaseClient.Dapper.Extensions.DependencyInjection/​README.md Documents Dapper lifetime rules.
Activout.DatabaseClient.Dapper.Extensions.DependencyInjection/​Activout.DatabaseClient.Dapper.Extensions.DependencyInjection.csproj Defines the Dapper DI package.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@twogood

twogood commented Oct 12, 2026

Copy link
Copy Markdown
Owner

@copilot Clean up the solution file (.sln), now you added a lot of x86 and x64 rows, while Any should work

Co-authored-by: twogood <189982+twogood@users.noreply.github.com>

Copilot AI commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Clean up the solution file (.sln), now you added a lot of x86 and x64 rows, while Any should work

Removed all the x86/x64 rows from the .sln, plus the SolutionProperties block that was also added. The solution now only has the Any CPU configurations, and the new projects' entries use just those. The solution still builds, with 0 errors (0008c9c).

@twogood
twogood marked this pull request as ready for review October 12, 2026 16:41
@twogood
twogood enabled auto-merge (squash) October 12, 2026 16:42
@twogood

twogood commented Oct 12, 2026

Copy link
Copy Markdown
Owner

@copilot Ändra TargetFramework till .net 10

Co-authored-by: twogood <189982+twogood@users.noreply.github.com>
auto-merge was automatically disabled October 12, 2026 16:51

Head branch was pushed to by a user without write access

Copilot AI commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Ändra TargetFramework till .net 10

Ändrade TargetFramework till net10.0 i de två nya DI-projekten (de övriga projekten använde redan net10.0). Bygget och testerna går igenom (d892350).

@twogood
twogood merged commit 9f11b8d into main Oct 12, 2026
6 checks passed
@twogood
twogood deleted the copilot/document-simplify-shared-connection-di branch October 12, 2026 16:59
twogood added a commit that referenced this pull request Oct 12, 2026
* Add DI extension packages for scoped, shared-connection DAO registration (#69)

* Apply remaining changes

Co-authored-by: twogood <189982+twogood@users.noreply.github.com>

* Implement error handling for Enumerable method invocation

Added error handling for method invocation to capture and rethrow inner exceptions.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: David Eriksson <david@activout.se>

* Make unsupported collection test construct DAO outside Assert.Throws

Co-authored-by: twogood <189982+twogood@users.noreply.github.com>

---------

Signed-off-by: David Eriksson <david@activout.se>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: twogood <189982+twogood@users.noreply.github.com>
Co-authored-by: David Eriksson <david@activout.se>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document and simplify shared-connection DI for cross-DAO transactions

3 participants