33using System . Collections . Concurrent ;
44using System . Collections . Generic ;
55using System . Linq ;
6+ using System . Threading ;
67using System . Threading . Tasks ;
78
89namespace OLT . Core
@@ -23,6 +24,8 @@ protected OltCommandBus(
2324 protected virtual TContext Context { get ; }
2425 protected virtual List < IOltCommandHandler > Handlers { get ; }
2526
27+ public CancellationToken CancellationToken { get ; private set ; } = CancellationToken . None ;
28+
2629 /// <summary>
2730 /// Attempts to locate <see cref="IOltCommandHandler"/> for <see cref="IOltCommand"/>
2831 /// </summary>
@@ -53,21 +56,23 @@ protected virtual IOltCommandHandler GetHandler(IOltCommand command)
5356 /// <param name="command"></param>
5457 /// <param name="handler"></param>
5558 /// <returns></returns>
56- protected virtual Task < IOltCommandValidationResult > ValidateAsync ( IOltCommandHandler handler , IOltCommand command )
57- {
59+ protected virtual Task < IOltCommandValidationResult > ValidateAsync_Internal ( IOltCommandHandler handler , IOltCommand command )
60+ {
5861 return handler . ValidateAsync ( this , command ) ;
5962 }
6063
6164 /// <summary>
6265 /// Validates Command and CommandHandler can Execute
6366 /// </summary>
6467 /// <param name="command"></param>
68+ /// <param name="cancellationToken">Optional cancellation token.</param>
6569 /// <returns></returns>
6670 /// <exception cref="OltCommandHandlerNotFoundException"></exception>
67- /// <exception cref="OltCommandHandlerMultipleException"></exception>
68- public virtual Task < IOltCommandValidationResult > ValidateAsync ( IOltCommand command )
71+ /// <exception cref="OltCommandHandlerMultipleException"></exception>
72+ public virtual Task < IOltCommandValidationResult > ValidateAsync ( IOltCommand command , CancellationToken cancellationToken = default )
6973 {
70- return this . ValidateAsync ( GetHandler ( command ) , command ) ;
74+ this . CancellationToken = cancellationToken ;
75+ return this . ValidateAsync_Internal ( GetHandler ( command ) , command ) ;
7176 }
7277
7378 /// <summary>
@@ -76,9 +81,10 @@ public virtual Task<IOltCommandValidationResult> ValidateAsync(IOltCommand comma
7681 /// <param name="command"></param>
7782 /// <param name="handler"></param>
7883 /// <returns></returns>
84+ [ Obsolete ( "Removing in 10.x, ProcessAsync<T> is deprecated, use IOltCommand<TResult>" ) ]
7985 protected virtual async Task < IOltCommandBusResult > ExecuteAsync ( IOltCommandHandler handler , IOltCommand command )
8086 {
81- var validationResult = await ValidateAsync ( handler , command ) ;
87+ var validationResult = await ValidateAsync_Internal ( handler , command ) ;
8288 if ( ! validationResult . Valid )
8389 {
8490 throw validationResult . ToException ( ) ;
@@ -95,16 +101,6 @@ protected virtual async Task<IOltCommandBusResult> ExecuteAsync(IOltCommandHandl
95101 }
96102
97103
98- /////// <summary>
99- /////// Executes Command
100- /////// </summary>
101- /////// <param name="command"></param>
102- /////// <returns></returns>
103- ////protected virtual Task<IOltCommandBusResult> ExecuteAsync(IOltCommand command)
104- ////{
105- //// return ExecuteAsync(GetHandler(command), command);
106- ////}
107-
108104 /// <summary>
109105 /// Runs in order (or Queues if in Transaction) the <seealso cref="IOltPostCommandHandler.PostExecuteAsync(IOltCommand, IOltCommandResult)"/>
110106 /// </summary>
@@ -115,7 +111,6 @@ protected virtual async Task<IOltCommandBusResult> ExecuteAsync(IOltCommandHandl
115111 protected virtual async Task PostExecuteAsync < TResult > ( IOltCommandHandler currentHandler , IOltCommand command , TResult result )
116112 where TResult : notnull
117113 {
118-
119114 if ( currentHandler is IOltPostCommandHandler < TResult > typedPostHandler )
120115 {
121116 PostProcessItems . Enqueue ( new OltAfterCommandQueueItem < TResult > ( typedPostHandler , command , result ) ) ;
@@ -138,12 +133,15 @@ protected virtual async Task PostExecuteAsync<TResult>(IOltCommandHandler curren
138133 /// Processes Command using <see cref="IOltCommandHandler"/> for <see cref="IOltCommand"/>
139134 /// </summary>
140135 /// <param name="command"></param>
136+ /// <param name="cancellationToken">Optional cancellation token.</param>
141137 /// <returns></returns>
142138 /// <exception cref="OltCommandHandlerNotFoundException"></exception>
143139 /// <exception cref="OltCommandHandlerMultipleException"></exception>
144140 /// <exception cref="OltValidationException"></exception>
145- public virtual Task ProcessAsync ( IOltCommand command )
141+ [ Obsolete ( "Removing in 10.x, ProcessAsync<T> is deprecated, use IOltCommand<TResult>" ) ]
142+ public virtual Task ProcessAsync ( IOltCommand command , CancellationToken cancellationToken = default )
146143 {
144+ this . CancellationToken = cancellationToken ;
147145 return ExecuteAsync ( GetHandler ( command ) , command ) ;
148146 }
149147
@@ -165,20 +163,22 @@ public virtual async Task<T> ProcessAsync<T>(IOltCommand command)
165163 }
166164
167165
168-
169-
170166 /// <summary>
171167 /// Processes Command using <see cref="IOltCommandHandler"/> for <see cref="IOltCommand"/>
172168 /// </summary>
173169 /// <param name="command"></param>
170+ /// <param name="cancellationToken">Optional cancellation token.</param>
174171 /// <typeparam name="TResult"><see cref="IOltCommandHandler"/> Returned Result</typeparam>
175172 /// <returns></returns>
176173 /// <exception cref="OltCommandHandlerNotFoundException"></exception>
177174 /// <exception cref="NullReferenceException">Thrown is command result is null</exception>
178175 /// <exception cref="OltValidationException"></exception>
176+ /// <exception cref="InvalidCastException"></exception>
179177 /// <returns></returns>
180- public virtual async Task < TResult > ProcessAsync < TResult > ( IOltCommand < TResult > command ) where TResult : notnull
178+ public virtual async Task < TResult > ProcessAsync < TResult > ( IOltCommand < TResult > command , CancellationToken cancellationToken = default ) where TResult : notnull
181179 {
180+ this . CancellationToken = cancellationToken ;
181+
182182 var validationResult = await ValidateAsync ( command ) ;
183183 if ( ! validationResult . Valid )
184184 {
@@ -199,7 +199,7 @@ public virtual async Task<TResult> ProcessAsync<TResult>(IOltCommand<TResult> co
199199 return result ;
200200 }
201201
202- return await ProcessAsync < TResult > ( ( IOltCommand ) command ) ;
202+ throw new InvalidCastException ( $ "Unable to cast to { typeof ( IOltCommandHandler < TResult > ) } " ) ;
203203 }
204204 }
205205}
0 commit comments