88
99namespace Mythetech . Framework . Infrastructure . Settings ;
1010
11+ /// <summary>
12+ /// Options for tracking settings types discovered during service configuration.
13+ /// </summary>
14+ public class SettingsRegistrationOptions
15+ {
16+ /// <summary>
17+ /// Types discovered via RegisterSettingsFromAssembly on IServiceCollection.
18+ /// </summary>
19+ public List < Type > DiscoveredSettingsTypes { get ; } = new ( ) ;
20+ }
21+
1122/// <summary>
1223/// Extension methods for registering settings framework services.
1324/// </summary>
@@ -125,13 +136,66 @@ public static IServiceCollection AddSettingsStorage(this IServiceCollection serv
125136 return services ;
126137 }
127138
139+ /// <summary>
140+ /// Scans an assembly for SettingsBase implementations, registers them as singletons,
141+ /// and configures the SettingsProvider to auto-discover them.
142+ /// This allows consumers to inject settings models directly.
143+ /// </summary>
144+ /// <param name="services">The service collection.</param>
145+ /// <param name="assembly">The assembly to scan for settings types.</param>
146+ /// <returns>The service collection for chaining.</returns>
147+ public static IServiceCollection RegisterSettingsFromAssembly (
148+ this IServiceCollection services ,
149+ Assembly assembly )
150+ {
151+ var settingsTypes = assembly . GetTypes ( )
152+ . Where ( t => ! t . IsAbstract && typeof ( SettingsBase ) . IsAssignableFrom ( t ) )
153+ . Where ( t => t . GetConstructor ( Type . EmptyTypes ) != null ) ;
154+
155+ foreach ( var type in settingsTypes )
156+ {
157+ services . AddSingleton ( type ) ;
158+ }
159+
160+ services . Configure < SettingsRegistrationOptions > ( options =>
161+ {
162+ foreach ( var type in settingsTypes )
163+ {
164+ if ( ! options . DiscoveredSettingsTypes . Contains ( type ) )
165+ options . DiscoveredSettingsTypes . Add ( type ) ;
166+ }
167+ } ) ;
168+
169+ return services ;
170+ }
171+
172+ /// <summary>
173+ /// Scans multiple assemblies for SettingsBase implementations, registers them as singletons,
174+ /// and configures the SettingsProvider to auto-discover them.
175+ /// This allows consumers to inject settings models directly.
176+ /// </summary>
177+ /// <param name="services">The service collection.</param>
178+ /// <param name="assemblies">The assemblies to scan for settings types.</param>
179+ /// <returns>The service collection for chaining.</returns>
180+ public static IServiceCollection RegisterSettingsFromAssemblies (
181+ this IServiceCollection services ,
182+ params Assembly [ ] assemblies )
183+ {
184+ foreach ( var assembly in assemblies )
185+ {
186+ services . RegisterSettingsFromAssembly ( assembly ) ;
187+ }
188+ return services ;
189+ }
190+
128191 /// <summary>
129192 /// Registers a settings model with the provider.
130193 /// Call after building the service provider.
131194 /// </summary>
132195 /// <typeparam name="T">The settings type to register.</typeparam>
133196 /// <param name="serviceProvider">The built service provider.</param>
134197 /// <returns>The service provider for chaining.</returns>
198+ [ Obsolete ( "Use IServiceCollection.RegisterSettingsFromAssembly() instead for DI-injectable settings." ) ]
135199 public static IServiceProvider RegisterSettings < T > ( this IServiceProvider serviceProvider )
136200 where T : SettingsBase , new ( )
137201 {
@@ -147,6 +211,7 @@ public static IServiceProvider RegisterSettings<T>(this IServiceProvider service
147211 /// <param name="serviceProvider">The built service provider.</param>
148212 /// <param name="settings">The settings instance to register.</param>
149213 /// <returns>The service provider for chaining.</returns>
214+ [ Obsolete ( "Use IServiceCollection.RegisterSettingsFromAssembly() instead for DI-injectable settings." ) ]
150215 public static IServiceProvider RegisterSettings ( this IServiceProvider serviceProvider , SettingsBase settings )
151216 {
152217 var provider = serviceProvider . GetRequiredService < ISettingsProvider > ( ) ;
@@ -161,6 +226,7 @@ public static IServiceProvider RegisterSettings(this IServiceProvider servicePro
161226 /// <param name="serviceProvider">The built service provider.</param>
162227 /// <param name="assembly">The assembly to scan for settings types.</param>
163228 /// <returns>The service provider for chaining.</returns>
229+ [ Obsolete ( "Use IServiceCollection.RegisterSettingsFromAssembly() instead for DI-injectable settings." ) ]
164230 public static IServiceProvider RegisterSettingsFromAssembly ( this IServiceProvider serviceProvider , Assembly assembly )
165231 {
166232 var provider = serviceProvider . GetRequiredService < ISettingsProvider > ( ) ;
@@ -184,6 +250,7 @@ public static IServiceProvider RegisterSettingsFromAssembly(this IServiceProvide
184250 /// <param name="serviceProvider">The built service provider.</param>
185251 /// <param name="assemblies">The assemblies to scan for settings types.</param>
186252 /// <returns>The service provider for chaining.</returns>
253+ [ Obsolete ( "Use IServiceCollection.RegisterSettingsFromAssemblies() instead for DI-injectable settings." ) ]
187254 public static IServiceProvider RegisterSettingsFromAssemblies (
188255 this IServiceProvider serviceProvider ,
189256 params Assembly [ ] assemblies )
0 commit comments