Edition 0 Updated to asp. Net core 0



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə243/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   239   240   241   242   243   244   245   246   ...   288
public
class
MyMicroserviceController : Controller 

public
MyMicroserviceController
(IMediator mediator, 
IMyMicroserviceQueries microserviceQueries) 

// ...


You can see that the mediator provides a clean and lean Web API controller constructor. In addition, 
within the controller methods, the code to send a command to the mediator object is almost one line: 
[
Route
(
"new"
)] 
[HttpPost] 
public
async Task 
ExecuteBusinessOperation
([FromBody]RunOpCommand 
runOperationCommand) 

var
commandResult = await _mediator.
SendAsync
(runOperationCommand); 
return
commandResult ? (IActionResult)
Ok
() : (IActionResult)
BadRequest
(); 

Implement idempotent Commands 
In 
eShopOnContainers
, a more advanced example than the above is submitting a 
CreateOrderCommand object from the Ordering microservice. But since the Ordering business 
process is a bit more complex and, in our case, it actually starts in the Basket microservice, this action 
of submitting the CreateOrderCommand object is performed from an integration-event handler 
named 
UserCheckoutAcceptedIntegrationEventHandler
 instead of a simple WebAPI controller called 
from the client App as in the previous simpler example. 
Nevertheless, the action of submitting the Command to MediatR is pretty similar, as shown in the 
following code. 
var
createOrderCommand = 
new
CreateOrderCommand
(eventMsg.
Basket
.
Items

eventMsg.
UserId
, eventMsg.
City

eventMsg.
Street
, eventMsg.
State

eventMsg.
Country
, eventMsg.
ZipCode

eventMsg.
CardNumber

eventMsg.
CardHolderName

eventMsg.
CardExpiration

eventMsg.
CardSecurityNumber

eventMsg.
CardTypeId
); 


284 
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns 
var
requestCreateOrder = 
new
IdentifiedCommandbool
>(createOrderCommand, 
eventMsg.
RequestId
); 
result = await _mediator.
Send
(requestCreateOrder); 
However, this case is also slightly more advanced 
because we’re also implementing idempotent 
commands. The CreateOrderCommand process should be idempotent, so if the same message comes 
duplicated through the network, because of any reason, like retries, the same business order will be 
processed just once. 
This is implemented by wrapping the business command (in this case CreateOrderCommand) and 
embedding it into a generic IdentifiedCommand, which is tracked by an ID of every message coming 
through the network that has to be idempotent. 
In the code below, you can see that the IdentifiedCommand is nothing more than a DTO with and ID 
plus the wrapped business command object. 

Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   239   240   241   242   243   244   245   246   ...   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