11using System ;
22using System . Diagnostics . CodeAnalysis ;
3- using System . Threading ;
4- using System . Threading . Tasks ;
53using Microsoft . Extensions . Configuration ;
64using Microsoft . Extensions . DependencyInjection ;
7- using Microsoft . Extensions . Logging ;
85using PrimeFuncPack ;
96
107namespace GarageGroup . Infra ;
118
129public static class ConsoleDependencyExtensions
1310{
14- public static Task < Unit > RunConsoleAsync < THandler , TIn > (
15- this Dependency < THandler > dependency ,
16- Action < ILoggingBuilder > ? configureLogger = null ,
17- Action < IServiceCollection > ? configureServices = null ,
18- [ AllowNull ] string inputSection = null ,
19- [ AllowNull ] string [ ] args = null )
20- where THandler : IHandler < TIn , Unit >
11+ public static HandlerConsoleRunner UseConsoleRunner < THandler > (
12+ this Dependency < IHandler < Unit , Unit > > dependency , [ AllowNull ] string [ ] args = null )
2113 {
2214 ArgumentNullException . ThrowIfNull ( dependency ) ;
23- ArgumentNullException . ThrowIfNull ( configureServices ) ;
24-
25- return dependency . InnerRunConsoleAsync < THandler , TIn > ( configureLogger , configureServices , inputSection , args ) ;
15+ return new ( dependency . Resolve , args ) ;
2616 }
2717
28- public static Task < Unit > RunConsoleAsync < TIn > (
29- this Dependency < IHandler < TIn , Unit > > dependency ,
30- Action < ILoggingBuilder > ? configureLogger = null ,
31- Action < IServiceCollection > ? configureServices = null ,
32- [ AllowNull ] string inputSection = null ,
33- [ AllowNull ] string [ ] args = null )
18+ public static HandlerConsoleRunner UseConsoleRunner < THandler > (
19+ this Dependency < THandler > dependency , [ AllowNull ] string [ ] args = null )
20+ where THandler : IHandler < Unit , Unit >
3421 {
3522 ArgumentNullException . ThrowIfNull ( dependency ) ;
36- return dependency . InnerRunConsoleAsync < IHandler < TIn , Unit > , TIn > ( configureLogger , configureServices , inputSection , args ) ;
37- }
38-
39- private static async Task < Unit > InnerRunConsoleAsync < THandler , TIn > (
40- this Dependency < THandler > dependency ,
41- Action < ILoggingBuilder > ? configureLogger ,
42- Action < IServiceCollection > ? configureServices ,
43- [ AllowNull ] string inputSection ,
44- [ AllowNull ] string [ ] args )
45- where THandler : IHandler < TIn , Unit >
46- {
47- var configuration = BuildConfiguration ( args ?? [ ] ) ;
48-
49- using var serviceProvider = configuration . CreateServiceProvider ( configureLogger , configureServices ) ;
50- using var cancellationTokenSource = configuration . GetCancellationTokenSource ( ) ;
51-
52- var logger = serviceProvider . GetRequiredService < ILoggerFactory > ( ) . CreateLogger ( "HandlerConsoleRunner" ) ;
53-
54- var input = configuration . ReadInput < TIn > ( inputSection ?? string . Empty ) ;
55- var result = await dependency . Resolve ( serviceProvider ) . InnerInvokeAsync ( input , cancellationTokenSource . Token ) ;
56-
57- return result . Fold ( Unit . From , logger . LogFailure ) ;
58- }
59-
60- private static async Task < Result < Unit , Failure < HandlerFailureCode > > > InnerInvokeAsync < THandler , TIn > (
61- this THandler handler , TIn ? input , CancellationToken cancellationToken )
62- where THandler : IHandler < TIn , Unit >
63- {
64- try
65- {
66- return await handler . HandleAsync ( input , cancellationToken ) ;
67- }
68- finally
69- {
70- if ( handler is IDisposable disposable )
71- {
72- disposable . Dispose ( ) ;
73- }
74- }
75- }
23+ return new ( InnerResolve , args ) ;
7624
77- private static TIn ? ReadInput < TIn > ( this IConfiguration configuration , string sectionName )
78- {
79- if ( typeof ( TIn ) == typeof ( Unit ) )
80- {
81- return default ;
82- }
83-
84- return configuration . GetRequiredSection ( sectionName ) . Get < TIn > ( ) ;
85- }
86-
87- private static Unit LogFailure ( this ILogger logger , Failure < HandlerFailureCode > failure )
88- {
89- if ( failure . FailureCode is not HandlerFailureCode . Persistent )
90- {
91- throw new InvalidOperationException ( $ "An unexpected error has occured: { failure . FailureMessage } ", failure . SourceException ) ;
92- }
93-
94- logger . LogError ( failure . SourceException , "An unexpected failure has occured: {failureMessage}" , failure . FailureMessage ) ;
95- return default ;
25+ IHandler < Unit , Unit > InnerResolve ( IServiceProvider serviceProvider )
26+ =>
27+ dependency . Resolve ( serviceProvider ) ;
9628 }
9729
98- private static CancellationTokenSource GetCancellationTokenSource ( this IConfiguration configuration )
99- {
100- var timeout = configuration . GetValue < TimeSpan ? > ( "MaxTimeout" ) ;
101- return timeout is null ? new ( ) : new ( timeout . Value ) ;
102- }
103-
104- private static ServiceProvider CreateServiceProvider (
105- this IConfiguration configuration , Action < ILoggingBuilder > ? configureLogger , Action < IServiceCollection > ? configureServices )
30+ public static HandlerConsoleRunner UseConsoleRunner < TIn > (
31+ this Dependency < IHandler < TIn , Unit > > dependency ,
32+ string inputSection ,
33+ [ AllowNull ] string [ ] args = null )
10634 {
107- var services = new ServiceCollection ( )
108- . AddLogging ( InnerConfigureLogger )
109- . AddSingleton ( configuration )
110- . AddSocketsHttpHandlerProviderAsSingleton ( )
111- . AddTokenCredentialStandardAsSingleton ( ) ;
112-
113- configureServices ? . Invoke ( services ) ;
114-
115- return services . BuildServiceProvider ( ) ;
35+ ArgumentNullException . ThrowIfNull ( dependency ) ;
36+ return new ( InnerResolve , args ) ;
11637
117- void InnerConfigureLogger ( ILoggingBuilder builder )
118- {
119- builder = builder . AddConsole ( ) ;
120- configureLogger ? . Invoke ( builder ) ;
121- }
38+ IHandler < Unit , Unit > InnerResolve ( IServiceProvider serviceProvider )
39+ =>
40+ new AdapterHandler < TIn > (
41+ innerHandler : dependency . Resolve ( serviceProvider ) ,
42+ configuration : serviceProvider . GetRequiredService < IConfiguration > ( ) ,
43+ sectionName : inputSection ) ;
12244 }
123-
124- private static IConfiguration BuildConfiguration ( string [ ] args )
125- =>
126- new ConfigurationBuilder ( )
127- . AddJsonFile ( "appsettings.json" , true , true )
128- . AddEnvironmentVariables ( )
129- . AddCommandLine ( args )
130- . Build ( ) ;
13145}
0 commit comments