title
stringlengths
3
46
content
stringlengths
0
1.6k
21:1928
else if (loading)
21:1929
{
21:1930
<p><em>Loading...</em></p>
21:1931
}
21:1932
@code{
21:1933
SearchViewModel search { get; set; } = new SearchViewModel();
21:1934
private IEnumerable<PackageInfosViewModel> packages;
21:1935
bool loading;
21:1936
async Task Search()
21:1937
{
21:1938
packages = null;
21:1939
loading = true;
21:1940
await InvokeAsync(StateHasChanged);
21:1941
packages = await client.GetByLocationAsync(search.Location);
21:1942
loading = false;
21:1943
}
21:1944
}
21:1945
21:1946
The omitted code in the if block is responsible for rendering a table with all the results. We will show it after having commented on the preceding code.
21:1947
Before retrieving the results with the PackagesClient service, we remove all previous results and set the loading field so the Razor code selects the else if path that replaces the previous table with a loading message. Once we’ve set these variables, we are forced to call StateHasChanged to trigger change detection and refresh the page. After all the results have been retrieved and the callback returns, there is no need to call StateHasChanged again because the termination of the callback itself triggers change detection and causes the required page refresh.
21:1948
Here is the code that renders the table with all the results:
21:1949
<div class=`table-responsive`>
21:1950
<table class=`table`>
21:1951
<thead>
21:1952
<tr>
21:1953
<th scope=`col`>Destination</th>
21:1954
<th scope=`col`>Name</th>
21:1955
<th scope=`col`>Duration/days</th>
21:1956
<th scope=`col`>Price</th>
21:1957
<th scope=`col`>Available from</th>
21:1958
<th scope=`col`>Available to</th>
21:1959
</tr>
21:1960
</thead>
21:1961
<tbody>
21:1962
@foreach (var package in packages)
21:1963
{
21:1964
<tr>
21:1965
<td>
21:1966
@package.DestinationName
21:1967
</td>
21:1968
<td>
21:1969
@package.Name
21:1970
</td>
21:1971
<td>
21:1972
@package.DurationInDays
21:1973
</td>
21:1974
<td>
21:1975
@package.Price
21:1976
</td>
21:1977
<td>
21:1978
@(package.StartValidityDate.HasValue ?
21:1979
package.StartValidityDate.Value.ToString(`d`)
21:1980
:
21:1981
String.Empty)
21:1982
</td>
21:1983
<td>
21:1984
@(package.EndValidityDate.HasValue ?
21:1985
package.EndValidityDate.Value.ToString(`d`)
21:1986
:
21:1987
String.Empty)
21:1988
</td>
21:1989
</tr>
21:1990
}
21:1991
</tbody>
21:1992
</table>
21:1993
</div>
21:1994
21:1995
Run the project and write the initial characters of Florence. Since we inserted Florence as a location in the same database in previous chapters (see the Querying and updating data with Entity Framework Core section of Chapter 13, Interacting with Data in C# – Entity Framework Core), some results should appear. If you inserted different data, please try with different starting words.
21:1996
Usually, together with web-based clients, all applications also furnish mobile-native applications to get a better performance with possibly slow mobile devices. So, let’s also design a mobile-native client application!
21:1997
Adding a Blazor MAUI version
21:1998
In this section, we explain how to add a Blazor MAUI version to the application of the previous solution.
21:1999
First of all, right-click on the solution icon in Solution Explorer and add a new project to the solution. Select a MAUI Blazor application and call it PackagesManagementMAUIBlazor.
21:2000
Open the PackagesManagementMAUIBlazor project file and remove all the platforms you don’t want to support (I removed Android, iOS, and Mac Catalyst, and kept only Windows):
21:2001
<TargetFrameworks> Condition=`$([MSBuild]::IsOSPlatform('windows'))`>$(TargetFrameworks);net8.0-windows10.0.19041.0 </TargetFrameworks>
21:2002
21:2003
Then, add a reference to the PackagesManagementBlazor.Shared project.
21:2004
Now, right-click on the client WebAssembly project and select Open Folder in File Explorer. Then, copy the ViewModels and Services folders, and paste them in Visual Studio Solution Explorer under the newly created PackagesManagementMAUIBlazor node.
21:2005
Then, change the namespaces of the files contained in these folders to be, respectively, PackagesManagementMAUIBlazor.ViewModels and PackagesManagementMAUIBlazor.Services.
21:2006
Now, replace the content of the Shared and Pages folders of the newly created project with the content of the Layout and Pages folders of the client Blazor WebAssembly project.
21:2007
Edit the newly copied Home.razor file and replace its header with:
21:2008
@using PackagesManagementMAUIBlazor.Services
21:2009
@using PackagesManagementBlazor.Shared
21:2010
@using PackagesManagementMAUIBlazor.ViewModels
21:2011
@inject PackagesClient client
21:2012
@page `/`
21:2013
21:2014
Finally, add the same HttpClient and PackagesClient configurations of the WebAssembly project to MAUIProgram.cs:
21:2015
builder.Services.AddScoped(sp => new HttpClient
21:2016
{ BaseAddress = new Uri(`https://localhost:7269/`) });
21:2017
builder.Services.AddScoped<PackagesClient>();
21:2018
21:2019
With this, you must add PackagesManagementMAUIBlazor to the projects to start. Right-click on the solution and select Configure Startup Projects.
21:2020
In the windows that open, select also the newly created MAUI Blazor project.
21:2021
Now, you can launch the application. Three windows should open (two browser windows and a Windows window) but both client project windows should show exactly the same application.
21:2022
After having discussed all the components of our WWTravelClub application, we need just to describe how to test it.
21:2023
Testing the WWTravelClub application
21:2024
In this section, we add some unit and functional test projects to the PackagesManagement frontend microservice we described in the A frontend microservice section of this chapter. If you don’t have it, you can download it from the section of the GitHub repository associated with the book in the ch19 folder. It is worth pointing out that in real-world projects, unit test batteries are enhanced by integration tests, and acceptance tests would include not only functional tests but also various kinds of performance tests.
21:2025
You are encouraged to review Chapter 9, Testing Your Enterprise Application, before continuing with this section.
21:2026
As a first step, let’s make a new copy of the solution folder and name it PackagesManagementWithTests. Then, open the solution and add it to an xUnit .NET C# test project named PackagesManagementTest. Finally, add a reference to the ASP.NET Core project (PackagesManagement), since we will test it, and a reference to the latest version of the Moq NuGet package, since we require mocking capabilities.
21:2027
It is worth remembering that Moq is a mocking library and that the purpose of mocking is to decouple dependencies between classes by replacing actual classes with mocked classes whose behavior is under the complete control of the test code. This way, each class can be unit tested independently from the behavior of other classes it references. For more details about Moq, please refer to Chapter 9, Testing Your Enterprise Application.