Skip to content

Commit dfb12f9

Browse files
committed
Refactor EasyStackOptions for flexible DB configuration
Refactored `EasyStackOptions` to replace the `AddPostgres` property with a more flexible `ConfigureDatabase` delegate, enabling custom database service and health check setup. Added the `WithPostgres<TDbContext>()` helper method for simplified PostgreSQL configuration. Updated `HostApplicationBuilderExtensions` to use the new `ConfigureDatabase` property, removing hardcoded PostgreSQL setup logic. Improved error handling for authorization to ensure database configuration is provided. Enhanced code readability, maintainability, and extensibility by centralizing database configuration logic in `EasyStackOptions`.
1 parent cf1742f commit dfb12f9

2 files changed

Lines changed: 48 additions & 12 deletions

File tree

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
1-
namespace EasyExtensions.AspNetCore.Stack.Builders
1+
using System;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using EasyExtensions.EntityFrameworkCore.Database;
5+
using EasyExtensions.EntityFrameworkCore.HealthChecks;
6+
using EasyExtensions.EntityFrameworkCore.Npgsql.Extensions;
7+
8+
namespace EasyExtensions.AspNetCore.Stack.Builders
29
{
10+
/// <summary>
11+
/// Provides configuration options for customizing EasyStack application features such as authorization and database
12+
/// setup.
13+
/// </summary>
14+
/// <remarks>Use this class to specify application-level options when integrating EasyStack into an
15+
/// ASP.NET Core application. The properties allow you to enable or disable authorization and to configure database
16+
/// services and health checks during startup. This class is typically configured during application initialization
17+
/// to tailor EasyStack's behavior to your application's requirements.</remarks>
318
public class EasyStackOptions
419
{
5-
public bool AddPostgres { get; set; }
6-
20+
/// <summary>
21+
/// Gets or sets a value indicating whether authorization should be added to the request.
22+
/// </summary>
723
public bool AddAuthorization { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the delegate used to configure database services and health checks during application startup.
27+
/// </summary>
28+
/// <remarks>Use this property to provide custom logic for registering database-related services
29+
/// and health checks with the application's dependency injection container. The delegate receives the service
30+
/// collection, health checks builder, and configuration, allowing for flexible setup of database connectivity
31+
/// and monitoring.</remarks>
32+
public Action<IServiceCollection, IHealthChecksBuilder, IConfiguration>? ConfigureDatabase { get; set; }
33+
34+
35+
public EasyStackOptions WithPostgres<TDbContext>() where TDbContext : AuditedDbContext
36+
{
37+
ConfigureDatabase = (services, hc, cfg) =>
38+
{
39+
services.AddPostgresDbContext<TDbContext>(db =>
40+
{
41+
db.UseLazyLoadingProxies = false;
42+
});
43+
44+
hc.AddCheck<DatabaseHealthCheck<TDbContext>>("Database");
45+
};
46+
return this;
47+
}
848
}
949
}

‎Sources/EasyExtensions.AspNetCore.Stack/Extensions/HostApplicationBuilderExtensions.cs‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
using EasyExtensions.AspNetCore.Extensions;
88
using Microsoft.Extensions.DependencyInjection;
99
using EasyExtensions.AspNetCore.Stack.Builders;
10-
using EasyExtensions.EntityFrameworkCore.Database;
11-
using EasyExtensions.EntityFrameworkCore.HealthChecks;
12-
using EasyExtensions.EntityFrameworkCore.Npgsql.Extensions;
1310
using EasyExtensions.AspNetCore.Authorization.Extensions;
1411

1512
namespace EasyExtensions.AspNetCore.Stack.Extensions
@@ -51,9 +48,9 @@ public static class HostApplicationBuilderExtensions
5148
/// <param name="setupStack">An optional delegate to configure EasyStack options. If null, default options are used.</param>
5249
/// <returns>The same <see cref="IHostApplicationBuilder"/> instance so that additional configuration calls can be
5350
/// chained.</returns>
54-
public static IHostApplicationBuilder AddEasyStack<TDbContext>(
51+
public static IHostApplicationBuilder AddEasyStack(
5552
this IHostApplicationBuilder builder,
56-
Action<EasyStackOptions>? setupStack) where TDbContext : AuditedDbContext
53+
Action<EasyStackOptions>? setupStack)
5754
{
5855
// create temp logger to log during startup
5956
using var loggerFactory = LoggerFactory.Create(loggingBuilder =>
@@ -140,16 +137,15 @@ public static IHostApplicationBuilder AddEasyStack<TDbContext>(
140137
}
141138

142139
// Add Postgres if asked
143-
if (options.AddPostgres)
140+
if (options.ConfigureDatabase != null)
144141
{
145-
builder.Services.AddPostgresDbContext<TDbContext>(x => x.UseLazyLoadingProxies = false);
146-
healthChecksBuilder.AddCheck<DatabaseHealthCheck<TDbContext>>("Database");
142+
options.ConfigureDatabase.Invoke(builder.Services, healthChecksBuilder, builder.Configuration);
147143
logger.LogInformation("Added Postgres DbContext and Database health check");
148144
}
149145

150146
if (options.AddAuthorization)
151147
{
152-
if (!options.AddPostgres)
148+
if (options.ConfigureDatabase == null)
153149
{
154150
throw new Exception("Authorization requires Postgres. Set AddPostgres to true in EasyStackOptions.");
155151
}

0 commit comments

Comments
 (0)