Edition 0 Updated to asp. Net core 0



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə245/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   241   242   243   244   245   246   247   248   ...   288
protected
virtual

CreateResultForDuplicateRequest
() 

return
default
(R); 

/// 

/// This method handles the command. It just ensures that no other request exists with 
the same ID, and if this is the case
/// just enqueues the original inner command.
/// 

/// 

name=
"message"
>
IdentifiedCommand which contains both original command & 
request ID


/// 

Return value of inner command or default value if request same ID was 
found

public
async Task 
Handle
(IdentifiedCommand message, CancellationToken 
cancellationToken) 

var
alreadyExists = await _requestManager.
ExistAsync
(message.
Id
); 
if
(alreadyExists) 

return
CreateResultForDuplicateRequest
(); 

else

await _requestManager.
CreateRequestForCommandAsync
(message.
Id
); 
try

var
command = message.
Command

var
commandName = command.
GetGenericTypeName
(); 
var
idProperty = 
string
.
Empty

var
commandId = 
string
.
Empty

switch
(command) 

case
CreateOrderCommand createOrderCommand: 
idProperty = 
nameof
(createOrderCommand.
UserId
); 
commandId = createOrderCommand.
UserId

break

case
CancelOrderCommand cancelOrderCommand: 
idProperty = 
nameof
(cancelOrderCommand.
OrderNumber
); 
commandId = $
"{cancelOrderCommand.OrderNumber}"

break

case
ShipOrderCommand shipOrderCommand: 
idProperty = 
nameof
(shipOrderCommand.
OrderNumber
); 
commandId = $
"{shipOrderCommand.OrderNumber}"

break

default

idProperty = 
"Id?"

commandId = 
"n/a"

break


_logger.
LogInformation

"----- Sending command: {CommandName} - {IdProperty}: {CommandId} 
({@Command})"

commandName, 
idProperty, 
commandId, 


286 
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns 
command); 
// Send the embedded business command to mediator so it runs its related 
CommandHandler
var
result = await _mediator.
Send
(command, cancellationToken); 
_logger.
LogInformation

"----- Command result: {@Result} - {CommandName} - {IdProperty}: 
{CommandId} ({@Command})"

result, 
commandName, 
idProperty, 
commandId, 
command); 
return
result; 

catch

return
default
(R); 




Since the IdentifiedCommand acts like a 
business command’s envelope, when the business command 
needs to be processed because it is not a repeated ID, then it takes that inner business command and 
resubmits it to Mediator, as in the last part of the code shown above when running 
_mediator.Send(message.Command)
, from the 
IdentifiedCommandHandler.cs

When doing that, it will link and run the business command handler, in this case, the 
CreateOrderCommandHandler
, which is running transactions against the Ordering database, as shown 
in the following code. 
// CreateOrderCommandHandler.cs

Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   241   242   243   244   245   246   247   248   ...   288




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin