11using ConsoleSample . Messages ;
22using Foundatio . Mediator ;
3+ using Microsoft . Extensions . DependencyInjection ;
34
45namespace ConsoleSample ;
56
67public class SampleRunner
78{
89 private readonly IMediator _mediator ;
10+ private readonly IServiceProvider _serviceProvider ;
911
10- public SampleRunner ( IMediator mediator )
12+ public SampleRunner ( IMediator mediator , IServiceProvider serviceProvider )
1113 {
1214 _mediator = mediator ;
15+ _serviceProvider = serviceProvider ;
1316 }
1417
1518 public async Task RunAllSamplesAsync ( )
@@ -22,6 +25,7 @@ public async Task RunAllSamplesAsync()
2225 await RunPublishSamples ( ) ;
2326 await RunSingleHandlerInvokeSamples ( ) ;
2427 await RunMixedSyncAsyncSamples ( ) ;
28+ await RunValidationSamplesAsync ( ) ;
2529
2630 Console . WriteLine ( "🎉 All samples completed successfully!" ) ;
2731 }
@@ -94,4 +98,56 @@ private async Task RunMixedSyncAsyncSamples()
9498
9599 Console . WriteLine ( "✅ Mixed sync/async test completed!\n " ) ;
96100 }
101+
102+ public async Task RunValidationSamplesAsync ( )
103+ {
104+ Console . WriteLine ( "\n 🔍 === Validation Middleware Demonstration ===" ) ;
105+ Console . WriteLine ( "This sample shows how validation middleware integrates with Result types" ) ;
106+ Console . WriteLine ( "The middleware validates input before handlers run, returning validation errors as Results\n " ) ;
107+
108+ var createUserCommand = new CreateUserCommand
109+ {
110+ Name = "Sample User" ,
111+ Email = "missing@example.com" ,
112+ Age = 35 ,
113+ PhoneNumber = "123-456-7890"
114+ } ;
115+
116+ var userResult = await _mediator . InvokeAsync < Result < User > > ( createUserCommand ) ;
117+ Console . WriteLine ( $ "✅ User created successfully: { userResult . Value . Name } ") ;
118+
119+ // asking for user will cause any errors to throw since the result type can't be implicitly converted to User
120+ var user = await _mediator . InvokeAsync < User > ( createUserCommand ) ;
121+ Console . WriteLine ( $ "✅ User created successfully: { userResult . Value . Name } ") ;
122+
123+ createUserCommand . Email = "existing@example.com" ;
124+
125+ userResult = await _mediator . InvokeAsync < Result < User > > ( createUserCommand ) ;
126+ Console . WriteLine ( $ "❌ User creation failed: { userResult . Errors } ") ;
127+
128+ // asking for user will cause any errors to throw since the result type can't be implicitly converted to User
129+ try
130+ {
131+ user = await _mediator . InvokeAsync < User > ( createUserCommand ) ;
132+ }
133+ catch ( Exception ex )
134+ {
135+ Console . WriteLine ( $ "❌ Error creating user: { ex . Message } ") ;
136+ }
137+
138+ createUserCommand . Email = String . Empty ;
139+
140+ userResult = await _mediator . InvokeAsync < Result < User > > ( createUserCommand ) ;
141+ Console . WriteLine ( $ "❌ User creation failed: { userResult . Errors } ") ;
142+
143+ // asking for user will cause any errors to throw since the result type can't be implicitly converted to User
144+ try
145+ {
146+ user = await _mediator . InvokeAsync < User > ( createUserCommand ) ;
147+ }
148+ catch ( Exception ex )
149+ {
150+ Console . WriteLine ( $ "❌ Error creating user: { ex . Message } ") ;
151+ }
152+ }
97153}
0 commit comments