diff --git a/CHANGELOG.md b/CHANGELOG.md index c73fc70..3355975 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 ([#210](https://github.com/matteobortolazzo/couchdb-net/pull/210)) + # 3.6.1 (2024-04-23) ## Bugs diff --git a/LATEST_CHANGE.md b/LATEST_CHANGE.md index 9e4ec93..b880d44 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 ([#210](https://github.com/matteobortolazzo/couchdb-net/pull/210)) 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 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: