title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
18:348
| |
18:349
|
Command operations are repeated until no concurrency exception is returned. HandleAsync uses the repository to get an instance of the entity to modify. If the entity is not found (it has been deleted), the commands stop its execution. Otherwise, all changes are passed to the retrieved aggregate. Immediately after the update, all events contained in the aggregate are triggered. In particular, if the price has changed, the event handler associated with the price change is executed. The concurrency check declared with the [ConcurrencyCheck] attribute on the EntityVersion property of the Package entity ensures that the package version is updated properly (by incrementing its previous version number by 1), as well as that the price-changed event is passed the right version numbers.
|
18:350
|
Also, event handlers are placed in the Handlers folder. As an example, let’s have a look at the price-changed event handler:
|
18:351
|
public class PackagePriceChangedEventHandler :
|
18:352
|
IEventHandler<PackagePriceChangedEvent>
|
18:353
|
{
|
18:354
|
private readonly IPackageEventRepository repo;
|
18:355
|
public PackagePriceChangedEventHandler(IPackageEventRepository repo)
|
18:356
|
{
|
18:357
|
this.repo = repo;
|
18:358
|
}
|
18:359
|
public Task HandleAsync(PackagePriceChangedEvent ev)
|
18:360
|
{
|
18:361
|
repo.New(PackageEventType.CostChanged, ev.PackageId,
|
18:362
|
ev.OldVersion, ev.NewVersion, ev.NewPrice);
|
18:363
|
return Task.CompletedTask;
|
18:364
|
}
|
18:365
|
}
|
18:366
| |
18:367
|
The constructor has automatically injected the IPackageEventRepository repository, which handles the database table and all the events to send to other applications. The HandleAsync implementation simply calls the repository method that adds a new record to this table.
|
18:368
|
All records in the table are handled by IPackageEventRepository, which can be retrieved and sent to all interested microservices by a parallel task defined in the DI engine with a call such as builder.Services.AddHostedService<MyHostedService>();, as detailed in the Using generic hosts subsection of Chapter 11, Applying a Microservice Architecture to Your Enterprise Application. However, this parallel task is not implemented in the GitHub code associated with this chapter.
|
18:369
|
It is worth recalling that the usage of events promotes code decoupling, and when events cross the microservice boundary, they implement efficient asynchronous communication between microservices, which improves performance and maximizes hardware resource usage.
|
18:370
|
The next subsection describes how to define controllers.
|
18:371
|
Defining controllers
|
18:372
|
Each controller interacts with a use case that emerged in the analysis stage with its action methods. Action methods do their job by requiring command handlers and query interfaces from the dependency injection engine.
|
18:373
|
Below is an example of how query objects are required and used:
|
18:374
|
[HttpGet]
|
18:375
|
public async Task<IActionResult> Index(
|
18:376
|
[FromServices] IPackagesListQuery query)
|
18:377
|
{
|
18:378
|
var results = await query.GetAllPackages();
|
18:379
|
var vm = new PackagesListViewModel { Items = results };
|
18:380
|
return View(vm);
|
18:381
|
}
|
18:382
| |
18:383
|
Below, instead, is an example of the usage of command handlers:
|
18:384
|
public async Task<IActionResult> Edit(
|
18:385
|
PackageFullEditViewModel vm,
|
18:386
|
[FromServices] ICommandHandler<UpdatePackageCommand> command)
|
18:387
|
{
|
18:388
|
if (ModelState.IsValid)
|
18:389
|
{
|
18:390
|
await command.HandleAsync(new UpdatePackageCommand(vm));
|
18:391
|
return RedirectToAction(
|
18:392
|
nameof(ManagePackagesController.Index));
|
18:393
|
}
|
18:394
|
else
|
18:395
|
return View(vm);
|
18:396
|
}
|
18:397
| |
18:398
|
ICommandHandler<UpdatePackageCommand> retrieves the command handler associated with the UpdatePackageCommand command from DI.
|
18:399
|
If ModelState is valid, UpdatePackageCommand is created, and its associated handler is invoked; otherwise, the View is displayed again to the user to enable them to correct all the errors.
|
18:400
|
Summary
|
18:401
|
In this chapter, we analyzed the peculiarities of front-end microservices and the techniques used to implement them.
|
18:402
|
Then, we put together the techniques learned in this chapter and in previous chapters in the complete implementation of a front-end microservice.
|
18:403
|
We used an onion architecture with a data layer and a domain layer abstraction, and we implemented each as a separate project. The application layer and the presentation layer were implemented together in the same ASP.NET Core MVC project.
|
18:404
|
The microservice used the CQRS pattern and used a queue implemented with a database table to store the events to send to other microservices.
|
18:405
|
The next chapter explains how to implement a presentation layer with client-based techniques. We will use Blazor as an example client framework.
|
18:406
|
Questions
|
18:407
| |
18:408
|
What is the difference between a front-end and an API gateway?
|
18:409
|
Why should all front-ends and API gateways use a robust web server?
|
18:410
|
Why should complex blocking database transactions be avoided?
|
18:411
|
When does the concurrency technique ensure a better performance?
|
18:412
|
What is the advantage of using domain events to implement interactions between different aggregates?
|
18:413
| |
18:414
|
Further reading
|
18:415
|
Since this chapter just put into practice concepts explained in other chapters (mainly Chapter 7, Understanding the Different Domains in Software Solutions, Chapter 11, Applying a Microservice Architecture to Your Enterprise Application, and Chapter 13, Interacting with Data in C# – Entity Framework Core), here we will include just a few links on how to use API gateways and further information on the MediatR library, which was mentioned in the example:
|
18:416
| |
18:417
|
Ocelot GitHub repository: https://github.com/ThreeMammals/Ocelot
|
18:418
|
How to implement your API gateway with Ocelot: https://docs.microsoft.com/en-us/dotnet/architecture/microservices/multi-container-microservice-net-applications/implement-api-gateways-with-ocelot
|
18:419
|
Azure API Management: https://azure.microsoft.com/en-us/services/api-management/#overview
|
18:420
|
Azure App Service: https://azure.microsoft.com/en-us/services/app-service/
|
18:421
|
More information on MediatR can be found on MediatR’s GitHub repository: https://github.com/jbogard/MediatR
|
18:422
| |
18:423
|
Leave a review!
|
18:424
|
Enjoying this book? Help readers like you by leaving an Amazon review. Scan the QR code below for a 20% discount code.
|
18:425
| |
18:426
|
*Limited Offer
|
18:427
| |
18:428
| |
19:1
|
Client Frameworks: Blazor
|
19:2
|
In this chapter, you will learn how to implement presentation layers based on client technologies. Applications based on server technologies, like ASP.NET Core MVC, run all application layers on the server, thus also creating on the server the HTML that encodes the whole UI. Applications based on client technologies, instead, run the whole presentation layer on the client machine (mobile device, desktop computer, laptop, etc.) thus interacting with a server just to exchange data with the web API.
|
19:3
|
In other words, in an application based on client technology, the whole UI is created by code that runs on the user device, which also controls the whole user-application interaction. Both the business layer and the domain layer, instead, run on server machines to prevent users from violating business rules and authorization policies by hacking the code that runs on their devices.
|
19:4
|
In turn, applications based on client technologies can be classified as single-page applications, which benefit from web standards, or as native applications, which are tied to specific operating systems and the advantages of specific device peculiarities.
|
19:5
|
Single-page applications are based on JavaScript or WebAssembly and run in any browser. As an example of a single-page application, we will analyze the Blazor WebAssembly framework
|
19:6
|
Blazor WebAssembly applications are developed in C# and use many technologies we already analyzed in Chapter 17, Presenting ASP.NET Core, such as dependency injection and Razor. Therefore, we strongly recommend studying Chapter 17, Presenting ASP.NET Core, and Chapter 18, Implementing Frontend Microservices with ASP.NET Core, before reading this chapter.
|
19:7
|
As an example of native technologies, we will analyze native .NET MAUI Blazor, which is completely analogous to Blazor WebAssembly but instead of using browser WebAssembly, it uses .NET, which is compiled just in time into the assembly of the target device. Not being limited by browser restrictions, .NET MAUI Blazor can access all device features through adequate .NET libraries.
|
19:8
|
More specifically, in this chapter, you will learn about the following subjects:
|
19:9
| |
19:10
|
Comparison of the various types of client technologies
|
19:11
|
Blazor WebAssembly architecture
|
19:12
|
Blazor pages and components
|
19:13
|
Blazor forms and validation
|
19:14
|
Blazor advanced features, such as JavaScript interoperability, globalization, authentication, and more
|
19:15
|
Third-party tools for Blazor WebAssembly
|
19:16
|
.NET MAUI Blazor
|
19:17
| |
19:18
|
While there is also server-side Blazor, which runs on the server like ASP.NET Core MVC, this chapter discusses just Blazor WebAssembly and .NET MAUI Blazor, since the main focus of the chapter is client-side technology. Moreover, server-side Blazor can’t provide a performance that is comparable with other server-side technologies, like ASP.NET Core MVC, which we analyzed in Chapter 17, Presenting ASP.NET Core.
|
19:19
|
The first section summarizes and compares the various kinds of client technologies, while the remainder of the chapter discusses in detail Blazor WebAssembly and .NET MAUI Blazor.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.