@@ -13,62 +13,59 @@ namespace TinyHelpers.AspNetCore.Extensions;
1313
1414public static class ServiceCollectionExtensions
1515{
16- extension ( IServiceCollection services )
16+ public static T ? ConfigureAndGet < T > ( this IServiceCollection services , IConfiguration configuration , string sectionName ) where T : class
1717 {
18- public T ? ConfigureAndGet < T > ( IConfiguration configuration , string sectionName ) where T : class
19- {
20- var section = configuration . GetSection ( sectionName ) ;
21- var settings = section . Get < T > ( ) ;
22- services . Configure < T > ( section ) ;
18+ var section = configuration . GetSection ( sectionName ) ;
19+ var settings = section . Get < T > ( ) ;
20+ services . Configure < T > ( section ) ;
2321
24- return settings ;
25- }
22+ return settings ;
23+ }
2624
27- public IServiceCollection AddRequestLocalization ( params string [ ] cultures )
28- => services . AddRequestLocalization ( cultures , null ) ;
25+ public static IServiceCollection AddRequestLocalization ( this IServiceCollection services , params string [ ] cultures )
26+ => services . AddRequestLocalization ( cultures , null ) ;
2927
30- public IServiceCollection AddRequestLocalization ( IEnumerable < string > cultures , Action < IList < IRequestCultureProvider > > ? providersConfiguration )
31- {
32- var supportedCultures = cultures . Select ( c => new CultureInfo ( c ) ) . ToList ( ) ;
33-
34- services . Configure < RequestLocalizationOptions > ( options =>
35- {
36- options . SupportedCultures = supportedCultures ;
37- options . SupportedUICultures = supportedCultures ;
38- options . DefaultRequestCulture = new RequestCulture ( supportedCultures . First ( ) ) ;
39- providersConfiguration ? . Invoke ( options . RequestCultureProviders ) ;
40- } ) ;
41-
42- return services ;
43- }
44-
45- public IServiceCollection Replace < TService , TImplementation > ( ServiceLifetime lifetime = ServiceLifetime . Scoped )
46- where TService : class
47- where TImplementation : class , TService
28+ public static IServiceCollection AddRequestLocalization ( this IServiceCollection services , IEnumerable < string > cultures , Action < IList < IRequestCultureProvider > > ? providersConfiguration )
29+ {
30+ var supportedCultures = cultures . Select ( c => new CultureInfo ( c ) ) . ToList ( ) ;
31+
32+ services . Configure < RequestLocalizationOptions > ( options =>
4833 {
49- services . Replace ( new ServiceDescriptor ( serviceType : typeof ( TService ) , implementationType : typeof ( TImplementation ) , lifetime ) ) ;
34+ options . SupportedCultures = supportedCultures ;
35+ options . SupportedUICultures = supportedCultures ;
36+ options . DefaultRequestCulture = new RequestCulture ( supportedCultures . First ( ) ) ;
37+ providersConfiguration ? . Invoke ( options . RequestCultureProviders ) ;
38+ } ) ;
5039
51- return services ;
52- }
40+ return services ;
41+ }
5342
54- public IServiceCollection AddDefaultProblemDetails ( )
55- {
56- services . AddProblemDetails ( options =>
57- {
58- options . CustomizeProblemDetails = context => context . UseDefaults ( ) ;
59- } ) ;
43+ public static IServiceCollection Replace < TService , TImplementation > ( this IServiceCollection services , ServiceLifetime lifetime = ServiceLifetime . Scoped )
44+ where TService : class
45+ where TImplementation : class , TService
46+ {
47+ services . Replace ( new ServiceDescriptor ( serviceType : typeof ( TService ) , implementationType : typeof ( TImplementation ) , lifetime ) ) ;
6048
61- return services ;
62- }
49+ return services ;
50+ }
6351
64- public IServiceCollection AddDefaultExceptionHandler ( )
52+ public static IServiceCollection AddDefaultProblemDetails ( this IServiceCollection services )
53+ {
54+ services . AddProblemDetails ( options =>
6555 {
66- // Ensures that the ProblemDetails service is registered.
67- services . AddProblemDetails ( ) ;
56+ options . CustomizeProblemDetails = context => context . UseDefaults ( ) ;
57+ } ) ;
58+
59+ return services ;
60+ }
61+
62+ public static IServiceCollection AddDefaultExceptionHandler ( this IServiceCollection services )
63+ {
64+ // Ensures that the ProblemDetails service is registered.
65+ services . AddProblemDetails ( ) ;
6866
69- services . AddExceptionHandler < DefaultExceptionHandler > ( ) ;
70- return services ;
71- }
67+ services . AddExceptionHandler < DefaultExceptionHandler > ( ) ;
68+ return services ;
7269 }
7370
7471 public static void UseDefaults ( this ProblemDetailsContext context )
0 commit comments