From 9c3c3b1affd8b3dd7372b87bc3fb70d1922fe259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Mon, 7 Jul 2025 13:59:41 +0200 Subject: [PATCH 1/3] Added AddCouchContext overload that includes service provider Added XML docs for DI methods --- .../AutofacRegistrationExtensions.cs | 64 +++++++++++++++++-- .../ServiceCollectionExtensions.cs | 52 ++++++++++++++- 2 files changed, 110 insertions(+), 6 deletions(-) diff --git a/src/CouchDB.Driver.DependencyInjection.Autofac/AutofacRegistrationExtensions.cs b/src/CouchDB.Driver.DependencyInjection.Autofac/AutofacRegistrationExtensions.cs index 309c9cd..ed0d130 100644 --- a/src/CouchDB.Driver.DependencyInjection.Autofac/AutofacRegistrationExtensions.cs +++ b/src/CouchDB.Driver.DependencyInjection.Autofac/AutofacRegistrationExtensions.cs @@ -1,12 +1,27 @@ -using Autofac; -using CouchDB.Driver.Options; -using System; +using System; +using Autofac; using CouchDB.Driver.Helpers; +using CouchDB.Driver.Options; namespace CouchDB.Driver.DependencyInjection.Autofac { + /// + /// Provides extension methods to register CouchDB contexts with Autofac. + /// public static class AutofacRegistrationExtensions { + /// + /// Registers a CouchDB context of type with the specified configuration delegate. + /// + /// The type of the CouchDB context to register. + /// The Autofac container builder. + /// + /// An action delegate that configures the . + /// + /// The modified . + /// + /// Thrown if or is null. + /// public static ContainerBuilder AddCouchContext(this ContainerBuilder builder, Action> optionBuilderAction) where TContext : CouchContext @@ -27,5 +42,46 @@ public static ContainerBuilder AddCouchContext(this ContainerBuilder b return builder; } + + /// + /// Registers a CouchDB context of type using a factory that can resolve services from + /// the Autofac container. + /// + /// The type of the CouchDB context to register. + /// The Autofac container builder. + /// + /// A factory function that receives an and returns a configured + /// . + /// + /// The modified . + /// + /// Thrown if or is null. + /// + public static ContainerBuilder AddCouchContext( + this ContainerBuilder builder, + Func> optionBuilderFactory) + where TContext : CouchContext + { + Check.NotNull(builder, nameof(builder)); + Check.NotNull(optionBuilderFactory, nameof(optionBuilderFactory)); + + builder + .Register(optionBuilderFactory) + .SingleInstance() + .AsSelf() + .As>(); + + builder + .Register(ctx => ctx.Resolve>().Options) + .As>() + .SingleInstance(); + + builder + .RegisterType() + .AsSelf() + .SingleInstance(); + + return builder; + } } -} +} \ No newline at end of file diff --git a/src/CouchDB.Driver.DependencyInjection/ServiceCollectionExtensions.cs b/src/CouchDB.Driver.DependencyInjection/ServiceCollectionExtensions.cs index 59942f1..315a848 100644 --- a/src/CouchDB.Driver.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/CouchDB.Driver.DependencyInjection/ServiceCollectionExtensions.cs @@ -5,20 +5,68 @@ namespace CouchDB.Driver.DependencyInjection { + /// + /// Provides extension methods for registering CouchDB contexts with the dependency injection container. + /// public static class ServiceCollectionExtensions { + /// + /// Registers a CouchDB context of type with the specified configuration delegate. + /// + /// The type of the Couch context to register. + /// The service collection to add the context to. + /// + /// An action delegate that configures the used to create the context. + /// + /// The modified . + /// + /// Thrown if or is null. + /// public static IServiceCollection AddCouchContext(this IServiceCollection services, Action> optionBuilderAction) where TContext : CouchContext { Check.NotNull(services, nameof(services)); Check.NotNull(optionBuilderAction, nameof(optionBuilderAction)); - + var builder = new CouchOptionsBuilder(); optionBuilderAction?.Invoke(builder); return services .AddSingleton(builder.Options) .AddSingleton(); } + + /// + /// Registers a CouchDB context of type with a factory delegate that can resolve + /// services from the container. + /// + /// The type of the Couch context to register. + /// The service collection to add the context to. + /// + /// A factory delegate that takes an and returns a configured + /// . + /// + /// The modified . + /// + /// Thrown if or is null. + /// + public static IServiceCollection AddCouchContext( + this IServiceCollection services, + Func> optionBuilderFactory) + where TContext : CouchContext + { + Check.NotNull(services, nameof(services)); + Check.NotNull(optionBuilderFactory, nameof(optionBuilderFactory)); + + services.AddSingleton>(sp => + { + CouchOptionsBuilder? builder = optionBuilderFactory(sp); + return builder.Options; + }); + + services.AddSingleton(); + + return services; + } } -} +} \ No newline at end of file From a3b0d83aca5b601d578d4bb3bf81b85e82668343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Mon, 7 Jul 2025 14:03:46 +0200 Subject: [PATCH 2/3] Version bump Updated changelogs --- CHANGELOG.md | 6 ++++++ LATEST_CHANGE.md | 6 +++--- src/azure-pipelines.yaml | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c73fc70..246ce82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 3.7.0 (2025-07-07) + +## Features + +* **Dependency Injection**: Added `AddCouchContext` overloads that supply the service provider to the caller. + # 3.6.1 (2024-04-23) ## Bugs diff --git a/LATEST_CHANGE.md b/LATEST_CHANGE.md index 9e4ec93..510cd0d 100644 --- a/LATEST_CHANGE.md +++ b/LATEST_CHANGE.md @@ -1,5 +1,5 @@ -# 3.6.1 (2024-04-23) +# 3.7.0 (2025-07-07) -## Bugs +## Features -* **Change feed**: Fixed an issue causing an endless change notification for all documents under certain conditions ([#200](https://github.com/matteobortolazzo/couchdb-net/pull/201)) +* **Dependency Injection**: Added `AddCouchContext` overloads that supply the service provider to the caller. diff --git a/src/azure-pipelines.yaml b/src/azure-pipelines.yaml index 5a87201..248df01 100644 --- a/src/azure-pipelines.yaml +++ b/src/azure-pipelines.yaml @@ -1,6 +1,6 @@ variables: BuildConfiguration: Release - PackageVersion: '3.6.1' + PackageVersion: '3.7.0' trigger: branches: From 97a34828dc16a1bf29163c7f9f22898e5046ba29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Mon, 7 Jul 2025 14:35:20 +0200 Subject: [PATCH 3/3] Added PR URL --- CHANGELOG.md | 2 +- LATEST_CHANGE.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 246ce82..3355975 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Features -* **Dependency Injection**: Added `AddCouchContext` overloads that supply the service provider to the caller. +* **Dependency Injection**: Added `AddCouchContext` overloads that supply the service provider to the caller ([#210](https://github.com/matteobortolazzo/couchdb-net/pull/210)) # 3.6.1 (2024-04-23) diff --git a/LATEST_CHANGE.md b/LATEST_CHANGE.md index 510cd0d..b880d44 100644 --- a/LATEST_CHANGE.md +++ b/LATEST_CHANGE.md @@ -2,4 +2,4 @@ ## Features -* **Dependency Injection**: Added `AddCouchContext` overloads that supply the service provider to the caller. +* **Dependency Injection**: Added `AddCouchContext` overloads that supply the service provider to the caller ([#210](https://github.com/matteobortolazzo/couchdb-net/pull/210))