-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathPersistenceExtensions.cs
More file actions
71 lines (66 loc) · 3.3 KB
/
PersistenceExtensions.cs
File metadata and controls
71 lines (66 loc) · 3.3 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
using FSH.Framework.Shared.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using FSH.Framework.Persistence.Inteceptors;
namespace FSH.Framework.Persistence;
/// <summary>
/// Extension methods for configuring persistence services and database contexts.
/// </summary>
public static class PersistenceExtensions
{
/// <summary>
/// Adds database configuration options to the service collection with validation.
/// </summary>
/// <param name="services">The service collection to add options to.</param>
/// <param name="configuration">The configuration instance containing database settings.</param>
/// <returns>The service collection for method chaining.</returns>
/// <exception cref="ArgumentNullException">Thrown when configuration is null.</exception>
public static IServiceCollection AddHeroDatabaseOptions(this IServiceCollection services, IConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(configuration);
services.AddOptions<DatabaseOptions>()
.Bind(configuration.GetSection(nameof(DatabaseOptions)))
.ValidateDataAnnotations()
.Validate(o => !string.IsNullOrWhiteSpace(o.Provider), "DatabaseOptions.Provider is required.")
.ValidateOnStart();
services.AddHostedService<DatabaseOptionsStartupLogger>();
services.AddPersistenceServices();
return services;
}
/// <summary>
/// Adds core persistence services including interceptors and time provider.
/// </summary>
/// <param name="services">The service collection to add services to.</param>
/// <returns>The service collection for method chaining.</returns>
public static IServiceCollection AddPersistenceServices(this IServiceCollection services)
{
services.AddSingleton(TimeProvider.System);
services.AddScoped<ISaveChangesInterceptor, AuditableEntitySaveChangesInterceptor>();
services.AddScoped<ISaveChangesInterceptor, DomainEventsInterceptor>();
return services;
}
/// <summary>
/// Adds a configured Entity Framework DbContext to the service collection.
/// </summary>
/// <typeparam name="TContext">The type of the DbContext to configure.</typeparam>
/// <param name="services">The service collection to add the context to.</param>
/// <returns>The service collection for method chaining.</returns>
/// <exception cref="ArgumentNullException">Thrown when services is null.</exception>
public static IServiceCollection AddHeroDbContext<TContext>(this IServiceCollection services)
where TContext : DbContext
{
ArgumentNullException.ThrowIfNull(services);
services.AddDbContext<TContext>((sp, options) =>
{
var env = sp.GetRequiredService<IHostEnvironment>();
var dbConfig = sp.GetRequiredService<IOptions<DatabaseOptions>>().Value;
options.ConfigureHeroDatabase(dbConfig.Provider, dbConfig.ConnectionString, dbConfig.MigrationsAssembly, env.IsDevelopment());
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());
});
return services;
}
}