-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCoreExPostgresExtensions.DependencyInjection.cs
More file actions
124 lines (114 loc) · 8.33 KB
/
Copy pathCoreExPostgresExtensions.DependencyInjection.cs
File metadata and controls
124 lines (114 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma warning disable IDE0130 // Namespace does not match folder structure - this is by design.
namespace Microsoft.Extensions.DependencyInjection;
#pragma warning restore IDE0130 // Namespace does not match folder structure
/// <summary>
/// Provides <see cref="PostgresDatabase"/> and related extensions.
/// </summary>
public static partial class CoreExPostgresExtensions
{
/// <summary>
/// Adds a <b>scoped</b> <see cref="PostgresDatabase"/> service.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">An optional action to configure the database instance.</param>
/// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
public static IServiceCollection AddPostgresDatabase(this IServiceCollection services, Action<IServiceProvider, PostgresDatabase>? configure = null)
=> AddPostgresDatabase<PostgresDatabase>(services, configure);
/// <summary>
/// Adds a <b>scoped</b> <see cref="PostgresDatabase"/> <typeparamref name="TDatabase"/> service.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">An optional action to configure the database instance.</param>
/// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
public static IServiceCollection AddPostgresDatabase<TDatabase>(this IServiceCollection services, Action<IServiceProvider, TDatabase>? configure = null) where TDatabase : PostgresDatabase
{
return services.ThrowIfNull().AddScoped<TDatabase>(sp =>
{
var db = ActivatorUtilities.CreateInstance<TDatabase>(sp);
configure?.Invoke(sp, db);
return db;
});
}
/// <summary>
/// Adds a <b>scoped</b> <see cref="PostgresUnitOfWork"/> service for the <see cref="PostgresDatabase"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="addAsIUnitOfWork">Indicates whether to also register as the <see cref="IUnitOfWork"/> service.</param>
/// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
public static IServiceCollection AddPostgresUnitOfWork(this IServiceCollection services, bool addAsIUnitOfWork = true)
=> AddPostgresUnitOfWork<PostgresDatabase>(services, addAsIUnitOfWork);
/// <summary>
/// Adds a <b>scoped</b> <see cref="PostgresUnitOfWork"/> service for the specified <see cref="PostgresDatabase"/> <typeparamref name="TDatabase"/>.
/// </summary>
/// <typeparam name="TDatabase">The <see cref="PostgresDatabase"/>.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="addAsIUnitOfWork">Indicates whether to also register as the <see cref="IUnitOfWork"/> service.</param>
/// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
public static IServiceCollection AddPostgresUnitOfWork<TDatabase>(this IServiceCollection services, bool addAsIUnitOfWork = true) where TDatabase : PostgresDatabase
{
services.ThrowIfNull().AddScoped<PostgresUnitOfWork>(sp =>
{
var sql = sp.GetRequiredService<TDatabase>();
return ActivatorUtilities.CreateInstance<PostgresUnitOfWork>(sp, sql);
});
if (addAsIUnitOfWork)
services.AddScoped<IUnitOfWork>(sp => sp.GetRequiredService<PostgresUnitOfWork>());
return services;
}
/// <summary>
/// Adds a keyed <b>scoped</b> <see cref="PostgresOutboxPublisher"/> service.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">An optional action to configure the <see cref="PostgresOutboxPublisher"/> instance.</param>
/// <param name="addAsDefaultIEventPublisher">Indicates whether to also register as the default (non-keyed) <see cref="IEventPublisher"/> service.</param>
/// <param name="serviceKey">The service key to use for the keyed registration.</param>
/// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
/// <remarks>See <see cref="Microsoft.Extensions.DependencyInjection.CoreExEventsExtensions.AddEventPublisher(IServiceCollection, string, Func{IServiceProvider, IEventPublisher}, bool)"/> for more information
/// related to the underlying registration implementation.</remarks>
public static IServiceCollection AddPostgresOutboxPublisher(this IServiceCollection services, Action<IServiceProvider, PostgresOutboxPublisher>? configure = null, bool addAsDefaultIEventPublisher = true, string serviceKey = PostgresOutboxPublisher.DefaultServiceKey)
=> services.AddPostgresOutboxPublisher<PostgresOutboxPublisher>(configure, addAsDefaultIEventPublisher, serviceKey);
/// <summary>
/// Adds a keyed <b>scoped</b> <typeparamref name="TOutbox"/> <see cref="PostgresOutboxPublisher"/> service.
/// </summary>
/// <typeparam name="TOutbox">The <see cref="PostgresOutboxPublisher"/> <see cref="Type"/>.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">An optional action to configure the <typeparamref name="TOutbox"/> instance.</param>
/// <param name="addAsDefaultIEventPublisher">Indicates whether to also register as the default (non-keyed) <see cref="IEventPublisher"/> service.</param>
/// <param name="serviceKey">The service key to use for the keyed registration.</param>
/// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
/// <remarks>See <see cref="Microsoft.Extensions.DependencyInjection.CoreExEventsExtensions.AddEventPublisher(IServiceCollection, string, Func{IServiceProvider, IEventPublisher}, bool)"/> for more information
/// related to the underlying registration implementation.</remarks>
public static IServiceCollection AddPostgresOutboxPublisher<TOutbox>(this IServiceCollection services, Action<IServiceProvider, TOutbox>? configure = null, bool addAsDefaultIEventPublisher = true, string serviceKey = PostgresOutboxPublisher.DefaultServiceKey) where TOutbox : PostgresOutboxPublisher
=> services.ThrowIfNull().AddEventPublisher(serviceKey, sp =>
{
var outbox = ActivatorUtilities.CreateInstance<TOutbox>(sp);
configure?.Invoke(sp, outbox);
return outbox;
}, addAsDefaultIEventPublisher);
/// <summary>
/// Adds a <b>scoped</b> <see cref="PostgresOutboxRelay"/> service for the <see cref="PostgresDatabase"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">An optional action to configure the <see cref="PostgresOutboxRelay"/> instance.</param>
/// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
public static IServiceCollection AddPostgresOutboxRelay(this IServiceCollection services, Action<IServiceProvider, PostgresOutboxRelay>? configure = null)
=> services.AddPostgresOutboxRelay<PostgresOutboxRelay, PostgresDatabase>(configure);
/// <summary>
/// Adds a <b>scoped</b> <see cref="PostgresOutboxRelay"/> service for the <see cref="PostgresDatabase"/>.
/// </summary>
/// <typeparam name="TOutboxRelay">The <see cref="PostgresOutboxRelay"/> <see cref="Type"/>.</typeparam>
/// <typeparam name="TDatabase">The <see cref="PostgresDatabase"/> <see cref="Type"/>.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">An optional action to configure the <typeparamref name="TOutboxRelay"/> instance.</param>
/// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
public static IServiceCollection AddPostgresOutboxRelay<TOutboxRelay, TDatabase>(this IServiceCollection services, Action<IServiceProvider, TOutboxRelay>? configure = null) where TOutboxRelay : PostgresOutboxRelay where TDatabase : PostgresDatabase
{
return services.ThrowIfNull().AddScoped<TOutboxRelay>(sp =>
{
var db = sp.GetRequiredService<TDatabase>();
var relay = ActivatorUtilities.CreateInstance<TOutboxRelay>(sp, db);
configure?.Invoke(sp, relay);
return relay;
});
}
}