title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
19:20
|
Technical requirements
|
19:21
|
This chapter requires the free Visual Studio 2022 Community edition or better with all the database tools installed.
|
19:22
| |
19:23
|
All concepts are clarified with simple example applications based on the WWTravelClub book use case you can find in the Using client technologies section of Chapter 21, Case Study.
|
19:24
| |
19:25
|
The code for this chapter is available at https://github.com/PacktPublishing/Software-Architecture-with-C-Sharp-12-and-.NET-8-4E.
|
19:26
|
Comparison of the various types of client technologies
|
19:27
|
This section discusses the various types of client technologies:
|
19:28
| |
19:29
|
Single-page applications, which run in the browser with all browser restrictions
|
19:30
|
Progressive applications, which run in the browser but can be installed like usual applications and can overcome some browser restrictions (after user permission is granted)
|
19:31
|
Native applications, which are tied to a specific device/operating system but can take full advantage of all device/operating system features
|
19:32
|
Cross-platform technologies, which, like native applications, can take full advantage of all device features but are compatible with several devices/operating systems
|
19:33
| |
19:34
|
Single-page applications
|
19:35
|
There are many reasons why web development has increased in recent decades, but the most basic one is the ability to deploy any new version of the application to many users at the same time. Moreover, the security policies that are automatically enforced by all browsers encourage the usage and diffusion of web applications.
|
19:36
|
So, a good question here would be, why is web development not used today? The best answer would be a lack of connectivity. As a software architect, you need to be alert to this, as we discussed in Chapter 2, Non-Functional Requirements.
|
19:37
|
Sometimes, it is not only a matter of having connectivity or not. Sometimes, a big problem is instability, and don’t forget the difficulties you will encounter from unexpected scenarios that web apps will generate once they’re out in the world.
|
19:38
|
For instance, in the WWTravelClub case, there is a first user story that says: “As a common user, I want to view promotional packages on the home page, so that I can easily find my next vacation.” At first sight, you may determine that a web app is the only option, especially because there is also a system requirement that says: “The system will run on the Windows, Linux, iOS, and Android platforms.”
|
19:39
|
However, imagine that a user browsed a lot of destinations and packages to find their perfect vacation, and they are going to reserve it when suddenly, the web application crashes because of a lack of web connectivity. In this situation, the user would lose all their browsing effort and would be forced to start searching for the package from scratch once web connectivity is restored. The issue can be overcome just with an application that is able to save its state instead of crashing when there are web connectivity issues. This way, the user can complete their task as soon as web connectivity is restored, without wasting whatever effort they had already put in before the web connectivity issue.
|
19:40
|
So maybe, for some parts of the solution, a native application with some data already downloaded would be a better option. However, the issue can also be solved with a particular kind of modern web application, progressive applications, which we will analyze in the next subsection.
|
19:41
|
Progressive applications
|
19:42
|
Progressive web applications are single-page applications that run in the browser but can be installed like native applications. Moreover, they can run offline.
|
19:43
|
Progressive applications are a new web standard that is supported by all mainstream browsers. If you require all the advantages of web applications, but you also need the ability to work offline like a native application, progressive web applications are the right choice for you.
|
19:44
|
However, keep in mind that progressive web applications can’t ensure the same performance and flexibility as native applications.
|
19:45
|
Blazor WebAssembly, as we will describe in the next sections of this chapter, supports progressive web applications. Check the Progressive Web Application checkbox that appears when you create a Blazor WebAssembly project. This is all you need to do to create a Blazor progressive web application.
|
19:46
|
If you ship your Blazor application as a progressive application, you overcome the problem of the initial download time (4-8 seconds), which is the main disadvantage of Blazor.
|
19:47
|
While progressive web applications are installed applications, they automatically update to the last available version, because each time they are launched, they verify if a more recent version is available, and if there is one, they automatically download it before running. That is, they have the advantage of automatically running the most recent available version that characterizes all classical web applications.
|
19:48
|
The option to install the Blazor progressive web application is not available during development because this would interfere with the usual modification test cycle. Therefore, you need to publish your application to test its progressive application peculiarities.
|
19:49
|
It is worth pointing out that progressive applications must be organized to take full advantage of their ability to work offline. Therefore, following repeated communication errors, the application should save all the data to be sent to the server in the browser’s local storage while waiting for a connection to be established successfully.
|
19:50
|
It is also possible to maintain the whole application state in a centralized service so that the user can serialize and save it to local storage before quitting the application. This way, when the application is offline, data to be sent to the server is not lost because it remains in the application state that is saved to disk.
|
19:51
|
When a progressive web application doesn’t fulfill your requirements because it is not able to use the specific device features you need, but you must support several different devices, you should consider cross-platform native applications, which have full access to all device features but can still support several hardware/software platforms. We will discuss them in the next subsection.
|
19:52
|
Native applications
|
19:53
|
Native development can be considered the beginning of UI development. There was no concept of sharing code between machines with diverse hardware when the idea of assigning software production to another person/company originally emerged.
|
19:54
|
This is the first good answer as to why we have good performance on native applications. We cannot forget that native apps run better just because they are near the hardware, most of the time connected to the OS directly or by using a framework, such as .NET. Be careful; we are not only talking about native mobile apps, but we are also discussing apps delivered in Windows, Linux, Mac, Android, or any other OS that can run apps.
|
19:55
|
Considering this scenario, the big question is – when do I have to use a native app? There are some cases where this would be a good idea:
|
19:56
| |
19:57
|
There is no need to deploy on different platforms.
|
19:58
|
There is a huge connection to the hardware.
|
19:59
|
The performance provided by a web client is not acceptable.
|
19:60
|
The application needs device resources that can’t be accessed through a browser because of a browser’s security policies.
|
19:61
|
The place where the application will run has connectivity problems.
|
19:62
|
The worst case is the one where you need two things at the same time: better performance than a web client and different platforms. In this scenario, you will probably have to deliver two applications, and the way you have designed the backend of this solution will be extremely important for reducing maintenance and costs as regards development.
|
19:63
| |
19:64
|
The decision between developing native or not can be difficult to take without a proof of concept (POC). As a software architect, you should be the one to recommend this kind of POC.
|
19:65
|
Examples of native applications that you can develop with C# are the classical Windows Forms and Windows Presentation Foundation, which are specific to the Windows operating system. Also, there is Xamarin, a platform that allows for the development of applications that can be published on both Android and iOS.
|
19:66
|
Cross-platform applications
|
19:67
|
Although performance can be a difficult requirement to achieve, in many scenarios, due to the simplicity of the solution, this is not the Achilles’ heel of the application. Considering WWTravelClub, although it would be useful to have the offline experience mentioned before, the performance itself is not the most difficult one to achieve.
|
19:68
|
In these scenarios, cross-platform technologies make total sense. Among them, it is worth mentioning Xamarin.Forms and the new .NET MAUI, which can be published on Android, iOS, and Windows.
|
19:69
|
The advised choice of cross-platform application is .NET MAUI. However, at the moment, MAUI supports Windows and all main mobile platforms but not Linux. Uno Platform (https://platform.uno/) also supports Linux together with all main mobile platforms but is not a Microsoft product maintained by Microsoft. Anyway, it can be downloaded as a Visual Studio extension.
|
19:70
|
In this chapter, we will not analyze Uno or all the options offered by .NET MAUI but just .NET MAUI Blazor since it is very similar to Blazor WebAssembly. So, learning Blazor enables us to develop single-page applications, progressive applications, and cross-platform applications.
|
19:71
|
.NET MAUI Blazor is described in the .NET MAUI Blazor section of this chapter, while the next section describes the basics of Blazor WebAssembly architecture.
|
19:72
|
Blazor WebAssembly architecture
|
19:73
|
Blazor WebAssembly uses the new WebAssembly browser feature to execute the .NET runtime in the browser. This way, it enables all developers to use the whole .NET code base and ecosystem in the implementation of applications capable of running in any WebAssembly-compliant browser. WebAssembly was conceived as a high-performance alternative to JavaScript. It is an assembly capable of running in a browser and obeying the same limitations as JavaScript code. This means that WebAssembly code, like JavaScript code, runs in an isolated execution environment that has very limited access to all machine resources.
|
19:74
|
WebAssembly differs from similar options from the past, like Flash and Silverlight, since it is an official W3C standard. More specifically, it became an official standard on December 5, 2019, so it is expected to have a long life. As a matter of fact, all mainstream browsers already support it.
|
19:75
|
However, WebAssembly doesn’t bring just performance with it; it also creates the opportunity to run whole code bases associated with modern and advanced object-oriented languages such as C++ (direct compilation), Java (bytecode), and C# (.NET) in browsers.
|
19:76
|
At the moment, Microsoft offers two frameworks that run .NET on top of WebAssembly, Blazor WebAssembly and Unity WebAssembly, which is the WebAssembly port of the Unity 3D graphic framework. Unity WebAssembly’s main purpose is the implementation of online video games that run in the browser, while Blazor WebAssembly is a single-page application framework that uses .NET instead of JavaScript or TypeScript.
|
19:77
|
Before WebAssembly, presentation layers running in a browser could only be implemented in JavaScript, with all the problems associated with the maintenance of big code bases implemented in a language that is not strictly typed. However, we must consider that on one hand, the usage of TypeScript in part solves JavaScript’s lack of strict typing, and on the other hand, .NET brings with it the problem of the binary compatibility of modules implemented with different .NET versions.
|
19:78
|
Anyway, with Blazor C#, developers can now implement complex applications in their favorite language, with all the comforts offered to this language by the C# compiler and Visual Studio.
|
19:79
|
Moreover, with Blazor, all .NET developers can use the full power of the .NET framework, with the only limitations imposed by the browser security policies, for the implementation of presentation layers that run in the browser and that share libraries and classes with all other layers that run on the server side.
|
19:80
|
The subsections that follow describe all the Blazor architectures. The first subsection explores the general concept of a single-page application, describing Blazor’s peculiarities.
|
19:81
|
What is a single-page application?
|
19:82
|
A single-page application (SPA) is an HTML-based application, where the HTML is changed by code that runs in the browser instead of issuing a new request to the server and rendering a new HTML page from scratch. SPAs can simulate a multi-page experience by replacing complete page areas with new HTML.
|
19:83
|
SPA frameworks are frameworks explicitly designed for implementing SPAs. Before WebAssembly, all SPA frameworks were based on JavaScript. The most famous JavaScript-based SPA frameworks are Angular, React.js, and Vue.js.
|
19:84
|
All SPA frameworks provide ways to transform data into HTML to show to the user and rely on a module called router to simulate page changes. Typically, data fills in the placeholders of HTML templates and selects which parts of a template to render (if-like constructs) and how many times to render it (for-like constructs).
|
19:85
|
The Blazor template language is Razor, which we described in Chapter 17, Presenting ASP.NET Core.
|
19:86
|
In order to increase modularity, code is organized into components, which are a kind of virtual HTML tag that, once rendered, generates actual HTML markup. Like HTML tags, components have attributes, which are usually called parameters, and custom events. It is up to the developer to ensure that each component uses its parameters to create proper HTML and to ensure that it generates adequate events. Components can be used inside other components in a hierarchical fashion.
|
19:87
|
Components can be associated with URLs in the application web domain, in which case they are called pages. These URLs can be used in the usual HTML links and following them leads to the upload of the page into an application area thanks to framework services called routers.
|
19:88
|
Some SPA frameworks also provide a predefined dependency injection engine in order to ensure better separation between components on one side and general-purpose services plus business code that runs in the browser on the other. Among the frameworks listed in this subsection, only Blazor and Angular have an out-of-the-box dependency injection engine.
|
19:89
|
In order to reduce the overall application file size, SPA frameworks based on JavaScript usually compile all JavaScript code in a few JavaScript files and then perform so-called tree-shaking, that is, the removal of all unused code. This technique sensibly reduces the application load time.
|
19:90
|
At the moment, instead, Blazor keeps all DLLs referenced by the main application separate and performs tree-shaking on each of them separately.
|
19:91
|
The next subsection describes the Blazor architecture. We challenge you to create a Blazor WebAssembly project called BlazorReview, so you can inspect the code and the constructs explained throughout the chapter. To do this, select the Blazor WebAssembly Standalone Application option when creating a new project. Make sure to select Individual Accounts as the authentication type and ensure that the Include sample pages checkbox is checked, as shown in the picture below.
|
19:92
| |
19:93
|
Figure 19.1: Creating the BlazorReview application
|
19:94
|
If you start the application, the application works properly, but if you try to log in, the following error message will appear: “There was an error trying to log you in: ‘Network Error’.” This is because you need to configure an identity-provider-authenticated user. As the default, the application is configured to use an OAuth-based identity provider web application. You need to just add the provider configuration data in a configuration file. We will look at this in more detail in the Authentication and authorization section.
|
19:95
|
Loading and starting the application
|
19:96
|
The folder structure of a Blazor WebAssembly application always includes an index.html static HTML page. In our BlazorReview project, index.html is in BlazorReview->wwwroot->index.html. This page is the container where the Blazor application will create its HTML. It contains an HTML header with a viewport meta declaration, the title, and CSS for the application’s overall styling. The Visual Studio default project template adds an application-specific CSS file and Bootstrap CSS, with a neutral style. You can replace the default Bootstrap CSS either with a customized style or a completely different CSS framework.
|
19:97
|
The body contains the code that follows:
|
19:98
|
<body>
|
19:99
|
<div id=`app`>
|
19:100
|
<svg class=`loading-progress`>
|
19:101
|
<circle r=`40%` cx=`50%` cy=`50%` />
|
19:102
|
<circle r=`40%` cx=`50%` cy=`50%` />
|
19:103
|
</svg>
|
19:104
|
<div id=`loading-progress-text`></div>
|
19:105
|
</div>
|
19:106
|
<div id=`blazor-error-ui`>
|
19:107
|
An unhandled error has occurred.
|
19:108
|
<a href=`` class=`reload`>Reload</a>
|
19:109
|
<a class=`dismiss`> / </a>
|
19:110
|
</div>
|
19:111
|
<script
|
19:112
|
src=`_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js`>
|
19:113
|
</script>
|
19:114
|
<script src=`_framework/blazor.webassembly.js`></script>
|
19:115
|
</body>
|
19:116
| |
19:117
|
The initial div with the app id is where the application will place the code it generates. Any markup placed inside this div will appear just while the Blazor application is loading and starting, then it will be replaced by the application-generated HTML. As the default, it contains an svg image and text that shows the loading progress, which are both controlled by the JavaScript code that takes care of loading the framework. The loading animation is based on a CSS animation contained in the css/app.css main application CSS file. However, you can replace both the default content and the CSS animation.
|
19:118
|
The second div is normally invisible and appears only when Blazor intercepts an unhandled exception.
|
19:119
|
blazor.webassembly.js contains the JavaScript part of the Blazor framework. Among other things, it takes care of downloading the .NET runtime, together with all application DLLs. More specifically, blazor.webassembly.js downloads the blazor.boot.json file, which lists all application files with their hashes.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.