1+ using GovUK . Dfe . CoreLibs . ApplicationSettings . Configuration ;
2+ using GovUK . Dfe . CoreLibs . ApplicationSettings . Data ;
3+ using GovUK . Dfe . CoreLibs . ApplicationSettings . Interfaces ;
4+ using GovUK . Dfe . CoreLibs . ApplicationSettings . Services ;
5+ using Microsoft . EntityFrameworkCore ;
6+ using Microsoft . Extensions . Configuration ;
7+ using Microsoft . Extensions . DependencyInjection ;
8+
9+ namespace GovUK . Dfe . CoreLibs . ApplicationSettings . Extensions ;
10+
11+ public static class ServiceCollectionExtensions
12+ {
13+ // Group all AddApplicationSettings overloads together
14+ public static IServiceCollection AddApplicationSettings (
15+ this IServiceCollection services ,
16+ IConfiguration configuration ,
17+ string connectionStringName = "DefaultConnection" ,
18+ string ? schema = null )
19+ {
20+ // Configure options using shared method
21+ services . ConfigureApplicationSettingsOptions ( configuration , schema ) ;
22+
23+ // Add DbContext
24+ services . AddDbContext < ApplicationSettingsDbContext > ( options =>
25+ options . UseSqlServer ( configuration . GetConnectionString ( connectionStringName ) ) ) ;
26+
27+ // Add shared dependencies and service
28+ return services . AddApplicationSettingsCore < ApplicationSettingsService > ( ) ;
29+ }
30+
31+ public static IServiceCollection AddApplicationSettings (
32+ this IServiceCollection services ,
33+ string connectionString ,
34+ Action < ApplicationSettingsOptions > ? configureOptions = null )
35+ {
36+ // Configure options with defaults and optional customization
37+ services . Configure < ApplicationSettingsOptions > ( options =>
38+ {
39+ // Set defaults using shared method
40+ SetDefaultOptions ( options ) ;
41+
42+ // Apply custom configuration if provided
43+ configureOptions ? . Invoke ( options ) ;
44+ } ) ;
45+
46+ // Add DbContext
47+ services . AddDbContext < ApplicationSettingsDbContext > ( options =>
48+ options . UseSqlServer ( connectionString ) ) ;
49+
50+ // Add shared dependencies and service
51+ return services . AddApplicationSettingsCore < ApplicationSettingsService > ( ) ;
52+ }
53+
54+ // Now place the different method after all AddApplicationSettings overloads
55+ /// <summary>
56+ /// Adds ApplicationSettings service using an existing DbContext
57+ /// </summary>
58+ /// <typeparam name="TContext">The existing DbContext type</typeparam>
59+ /// <param name="services">Service collection</param>
60+ /// <param name="configuration">Configuration</param>
61+ /// <param name="schema">Optional schema override</param>
62+ /// <returns>Service collection</returns>
63+ public static IServiceCollection AddApplicationSettingsWithExistingContext < TContext > (
64+ this IServiceCollection services ,
65+ IConfiguration configuration ,
66+ string ? schema = null )
67+ where TContext : DbContext
68+ {
69+ // Configure options using shared method
70+ services . ConfigureApplicationSettingsOptions ( configuration , schema ) ;
71+
72+ // Add shared dependencies and service
73+ return services . AddApplicationSettingsCore < ExistingContextApplicationSettingsService < TContext > > ( ) ;
74+ }
75+
76+ // All private helper methods remain the same...
77+ /// <summary>
78+ /// Configures ApplicationSettingsOptions with defaults and reads from configuration
79+ /// </summary>
80+ /// <param name="services">Service collection</param>
81+ /// <param name="configuration">Configuration</param>
82+ /// <param name="schema">Optional schema override</param>
83+ private static void ConfigureApplicationSettingsOptions (
84+ this IServiceCollection services ,
85+ IConfiguration configuration ,
86+ string ? schema = null )
87+ {
88+ services . Configure < ApplicationSettingsOptions > ( options =>
89+ {
90+ // Set defaults
91+ SetDefaultOptions ( options , schema ) ;
92+
93+ // Read from configuration section if it exists
94+ var section = configuration . GetSection ( ApplicationSettingsOptions . ConfigurationSection ) ;
95+ if ( section . Exists ( ) )
96+ {
97+ ApplyConfigurationSettings ( options , section , schema ) ;
98+ }
99+ } ) ;
100+ }
101+
102+ /// <summary>
103+ /// Sets default values for ApplicationSettingsOptions
104+ /// </summary>
105+ /// <param name="options">Options to configure</param>
106+ /// <param name="schema">Optional schema override</param>
107+ private static void SetDefaultOptions ( ApplicationSettingsOptions options , string ? schema = null )
108+ {
109+ options . EnableCaching = true ;
110+ options . CacheExpirationMinutes = 30 ;
111+ options . DefaultCategory = "General" ;
112+ options . Schema = schema ;
113+ }
114+
115+ /// <summary>
116+ /// Applies configuration settings from IConfiguration section
117+ /// </summary>
118+ /// <param name="options">Options to configure</param>
119+ /// <param name="section">Configuration section</param>
120+ /// <param name="schema">Optional schema override</param>
121+ private static void ApplyConfigurationSettings (
122+ ApplicationSettingsOptions options ,
123+ IConfiguration section ,
124+ string ? schema = null )
125+ {
126+ if ( bool . TryParse ( section [ "EnableCaching" ] , out bool enableCaching ) )
127+ options . EnableCaching = enableCaching ;
128+
129+ if ( int . TryParse ( section [ "CacheExpirationMinutes" ] , out int cacheExpiration ) )
130+ options . CacheExpirationMinutes = cacheExpiration ;
131+
132+ if ( ! string . IsNullOrEmpty ( section [ "DefaultCategory" ] ) )
133+ options . DefaultCategory = section [ "DefaultCategory" ] ?? string . Empty ;
134+
135+ // Schema from configuration (only if not overridden by parameter)
136+ if ( schema == null && ! string . IsNullOrEmpty ( section [ "Schema" ] ) )
137+ options . Schema = section [ "Schema" ] ;
138+ }
139+
140+ /// <summary>
141+ /// Adds core dependencies and service registration
142+ /// </summary>
143+ /// <typeparam name="TService">Service implementation type</typeparam>
144+ /// <param name="services">Service collection</param>
145+ /// <returns>Service collection</returns>
146+ private static IServiceCollection AddApplicationSettingsCore < TService > ( this IServiceCollection services )
147+ where TService : class , IApplicationSettingsService
148+ {
149+ // Add memory cache if not already added
150+ services . AddMemoryCache ( ) ;
151+
152+ // Add the service
153+ services . AddScoped < IApplicationSettingsService , TService > ( ) ;
154+
155+ return services ;
156+ }
157+ }
0 commit comments