@@ -13,7 +13,7 @@ namespace Microsoft.Extensions.DependencyInjection;
1313public static class NpgsqlServiceCollectionExtensions
1414{
1515 /// <summary>
16- /// Registers an <see cref="NpgsqlDataSource" /> and an <see cref="NpgsqlConnection" /> in the <see cref="IServiceCollection" />,
16+ /// Registers an <see cref="NpgsqlDataSource" /> and an <see cref="NpgsqlConnection" /> in the <see cref="IServiceCollection" />.
1717 /// </summary>
1818 /// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
1919 /// <param name="connectionString">An Npgsql connection string.</param>
@@ -38,7 +38,7 @@ public static IServiceCollection AddNpgsqlDataSource(
3838 => AddNpgsqlDataSourceCore ( serviceCollection , connectionString , dataSourceBuilderAction , connectionLifetime , dataSourceLifetime ) ;
3939
4040 /// <summary>
41- /// Registers an <see cref="NpgsqlDataSource" /> and an <see cref="NpgsqlConnection" /> in the <see cref="IServiceCollection" />,
41+ /// Registers an <see cref="NpgsqlDataSource" /> and an <see cref="NpgsqlConnection" /> in the <see cref="IServiceCollection" />.
4242 /// </summary>
4343 /// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
4444 /// <param name="connectionString">An Npgsql connection string.</param>
@@ -59,12 +59,61 @@ public static IServiceCollection AddNpgsqlDataSource(
5959 => AddNpgsqlDataSourceCore (
6060 serviceCollection , connectionString , dataSourceBuilderAction : null , connectionLifetime , dataSourceLifetime ) ;
6161
62- static IServiceCollection AddNpgsqlDataSourceCore (
62+ /// <summary>
63+ /// Registers an <see cref="NpgsqlMultiHostDataSource" /> and an <see cref="NpgsqlConnection" /> in the
64+ /// </summary>
65+ /// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
66+ /// <param name="connectionString">An Npgsql connection string.</param>
67+ /// <param name="dataSourceBuilderAction">
68+ /// An action to configure the <see cref="NpgsqlDataSourceBuilder" /> for further customizations of the <see cref="NpgsqlDataSource" />.
69+ /// </param>
70+ /// <param name="connectionLifetime">
71+ /// The lifetime with which to register the <see cref="NpgsqlConnection" /> in the container.
72+ /// Defaults to <see cref="ServiceLifetime.Scoped" />.
73+ /// </param>
74+ /// <param name="dataSourceLifetime">
75+ /// The lifetime with which to register the <see cref="NpgsqlDataSource" /> service in the container.
76+ /// Defaults to <see cref="ServiceLifetime.Singleton" />.
77+ /// </param>
78+ /// <returns>The same service collection so that multiple calls can be chained.</returns>
79+ public static IServiceCollection AddMultiHostNpgsqlDataSource (
80+ this IServiceCollection serviceCollection ,
81+ string connectionString ,
82+ Action < NpgsqlDataSourceBuilder > dataSourceBuilderAction ,
83+ ServiceLifetime connectionLifetime = ServiceLifetime . Transient ,
84+ ServiceLifetime dataSourceLifetime = ServiceLifetime . Singleton )
85+ => AddNpgsqlMultiHostDataSourceCore (
86+ serviceCollection , connectionString , dataSourceBuilderAction , connectionLifetime , dataSourceLifetime ) ;
87+
88+ /// <summary>
89+ /// Registers an <see cref="NpgsqlMultiHostDataSource" /> and an <see cref="NpgsqlConnection" /> in the
90+ /// <see cref="IServiceCollection" />.
91+ /// </summary>
92+ /// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
93+ /// <param name="connectionString">An Npgsql connection string.</param>
94+ /// <param name="connectionLifetime">
95+ /// The lifetime with which to register the <see cref="NpgsqlConnection" /> in the container.
96+ /// Defaults to <see cref="ServiceLifetime.Scoped" />.
97+ /// </param>
98+ /// <param name="dataSourceLifetime">
99+ /// The lifetime with which to register the <see cref="NpgsqlDataSource" /> service in the container.
100+ /// Defaults to <see cref="ServiceLifetime.Singleton" />.
101+ /// </param>
102+ /// <returns>The same service collection so that multiple calls can be chained.</returns>
103+ public static IServiceCollection AddMultiHostNpgsqlDataSource (
63104 this IServiceCollection serviceCollection ,
64105 string connectionString ,
65- Action < NpgsqlDataSourceBuilder > ? dataSourceBuilderAction ,
66106 ServiceLifetime connectionLifetime = ServiceLifetime . Transient ,
67107 ServiceLifetime dataSourceLifetime = ServiceLifetime . Singleton )
108+ => AddNpgsqlMultiHostDataSourceCore (
109+ serviceCollection , connectionString , dataSourceBuilderAction : null , connectionLifetime , dataSourceLifetime ) ;
110+
111+ static IServiceCollection AddNpgsqlDataSourceCore (
112+ this IServiceCollection serviceCollection ,
113+ string connectionString ,
114+ Action < NpgsqlDataSourceBuilder > ? dataSourceBuilderAction ,
115+ ServiceLifetime connectionLifetime ,
116+ ServiceLifetime dataSourceLifetime )
68117 {
69118 serviceCollection . TryAdd (
70119 new ServiceDescriptor (
@@ -78,6 +127,46 @@ static IServiceCollection AddNpgsqlDataSourceCore(
78127 } ,
79128 dataSourceLifetime ) ) ;
80129
130+ AddCommonServices ( serviceCollection , connectionLifetime , dataSourceLifetime ) ;
131+
132+ return serviceCollection ;
133+ }
134+
135+ static IServiceCollection AddNpgsqlMultiHostDataSourceCore (
136+ this IServiceCollection serviceCollection ,
137+ string connectionString ,
138+ Action < NpgsqlDataSourceBuilder > ? dataSourceBuilderAction ,
139+ ServiceLifetime connectionLifetime ,
140+ ServiceLifetime dataSourceLifetime )
141+ {
142+ serviceCollection . TryAdd (
143+ new ServiceDescriptor (
144+ typeof ( NpgsqlMultiHostDataSource ) ,
145+ sp =>
146+ {
147+ var dataSourceBuilder = new NpgsqlDataSourceBuilder ( connectionString ) ;
148+ dataSourceBuilder . UseLoggerFactory ( sp . GetService < ILoggerFactory > ( ) ) ;
149+ dataSourceBuilderAction ? . Invoke ( dataSourceBuilder ) ;
150+ return dataSourceBuilder . BuildMultiHost ( ) ;
151+ } ,
152+ dataSourceLifetime ) ) ;
153+
154+ serviceCollection . TryAdd (
155+ new ServiceDescriptor (
156+ typeof ( NpgsqlDataSource ) ,
157+ sp => sp . GetRequiredService < NpgsqlMultiHostDataSource > ( ) ,
158+ dataSourceLifetime ) ) ;
159+
160+ AddCommonServices ( serviceCollection , connectionLifetime , dataSourceLifetime ) ;
161+
162+ return serviceCollection ;
163+ }
164+
165+ static void AddCommonServices (
166+ IServiceCollection serviceCollection ,
167+ ServiceLifetime connectionLifetime ,
168+ ServiceLifetime dataSourceLifetime )
169+ {
81170 serviceCollection . TryAdd (
82171 new ServiceDescriptor (
83172 typeof ( NpgsqlConnection ) ,
@@ -95,7 +184,5 @@ static IServiceCollection AddNpgsqlDataSourceCore(
95184 typeof ( DbConnection ) ,
96185 sp => sp . GetRequiredService < NpgsqlConnection > ( ) ,
97186 connectionLifetime ) ) ;
98-
99- return serviceCollection ;
100187 }
101188}
0 commit comments