title
stringlengths
3
46
content
stringlengths
0
1.6k
2:238
Figure 2.19: Complex drop-down menu
2:239
A similar solution can be implemented with a low computational cost since all the main databases can efficiently select strings that start with a given substring.
2:240
When names are quite complex, when searching for the characters that were typed in by the user, they should be extended inside each item string. This operation cannot be performed efficiently with the usual databases and requires ad hoc data structures, nor can we forget the debouncing aspect that can happen while typing as a performance issue.
2:241
Finally, when we are searching inside descriptions composed of several words, more complex search patterns are needed. This is the case, for instance, with product descriptions. If the chosen database supports full-text search, the system can efficiently search for the occurrence of several words that have been typed by the user inside all the descriptions.
2:242
However, when descriptions are made up of names instead of common words, it might be difficult for the user to remember a few exact names contained in the target description. This happens, for instance, with multi-country company names. In these cases, we need algorithms that find the best match for the character that was typed by the user. Substrings of the string that was typed by the user must be searched in different places of each description. In general, similar algorithms cannot be implemented efficiently with databases based on indexes but require all the descriptions to be loaded in memory and ranked somehow against the string that was typed by the user.
2:243
The most famous algorithm in this class is probably the Levenshtein algorithm, which is used by most spellcheckers to find a word that best fits the one that was mistyped by the user. This algorithm minimizes the Levenshtein distance between the description and the string typed by the user, that is, the minimum number of character removals and additions needed to transform one string into another.
2:244
The Levenshtein algorithm works great but has a very high computational cost. Here, we use a faster algorithm that works well for searching character occurrences in descriptions. Characters typed by the user do not need to occur consecutively in the description but must occur in the same order. Some characters may be missing. Each description is given a penalty that depends on the missing characters and on how far the occurrences of the characters typed by the user are from the others.
2:245
More specifically, the algorithm ranks each description with two numbers:
2:246
2:247
The number of characters typed by the user that occurs in the description: the more characters contained in the description, the higher its rank.
2:248
Each description is given a penalty equal to the total distance among the occurrences of the characters typed by the user in the description.
2:249
2:250
The following screenshot shows how the word Ireland is ranked against the string ilad, which was typed by the user:
2:251
2:252
Figure 2.20: Sample of Levenshtein usage
2:253
The number of occurrences is four, while the total distance between character occurrences is three.
2:254
Once all the descriptions have been rated, they are sorted according to the number of occurrences. Descriptions with the same number of occurrences are sorted according to the lowest penalties. The following is an autocomplete that implements the preceding algorithm:
2:255
2:256
Figure 2.21: Levenshtein algorithm UI experience
2:257
The full class code, along with a test console project, is available in this book’s GitHub repository.
2:258
Selecting from a huge number of items
2:259
Here, huge does not refer to the amount of space needed to store the data but to the difficulty the user has in remembering the features of each item. When an item must be selected from among more than 10,000-100,000 items, there is no hope of finding it by searching for character occurrences inside a description. Here, the user must be driven toward the right item through a hierarchy of categories.
2:260
In this case, several user gestures are needed to perform a single selection. In other words, each selection requires interaction with several input fields. Once it is decided that the selection can’t be done with a single input field, the simplest option is cascading drop-down menus, that is, a chain of drop-down menus whose selection list depends on the values that were selected in the previous drop-down menus.
2:261
For example, if the user needs to select a town located anywhere in the world, we may use the first drop-down menu to select the country, and once the country has been chosen, we may use this choice to populate a second one with all the towns in the selected country. A simple example is as follows:
2:262
2:263
Figure 2.22: Cascading drop-down menu example
2:264
Clearly, each drop-down menu can be replaced by an autocomplete when required due to having a high number of options.
2:265
If making the right selection can be done by intersecting several different hierarchies, cascading drop-down menus become inefficient too, and we need a filter form, as follows:
2:266
2:267
Figure 2.23: Filter form sample
2:268
Now, let us understand interoperability with .NET 6.
2:269
Interoperability with .NET 8
2:270
Since .NET Core, Microsoft has brought to C# developers the ability to deliver their software to various platforms. And you, as a software architect, need to pay attention to this, considering developing for Linux and macOS as a great opportunity to deliver new features to your customers. Therefore, we need to ensure performance and multi-platform support, two common non-functional requirements for many systems.
2:271
Both console applications and web apps designed with .NET 8 in Windows are almost completely compatible with Linux and macOS, too. This means you do not have to build the app again to run it on these platforms.
2:272
2:273
Microsoft offers scripts to help you install .NET on Linux and macOS. You can find them at https://docs.microsoft.com/dotnet/core/tools/dotnet-install-script. Once you have the SDK installed, you just need to call dotnet the same way you do in Windows.
2:274
2:275
However, you must be aware of some features that are not fully compatible with Linux and macOS systems. For instance, no equivalent to the Windows Registry exists in these OSes, and you must develop an alternative yourself. If needed, an encrypted JSON config file can be a good option.
2:276
Another important point is that Linux is case-sensitive, while Windows is not. Please remember this when you work with files. Another important thing is that the Linux path separator is different from the Windows separator. You can use the Path.PathSeparator field and all the other Path class members to ensure your code is multi-platform. Environment.NewLine can also be useful in some situations.
2:277
Besides, you can also adapt your code to the underlying OS by using the runtime checks provided by .NET 8, as follows:
2:278
using System.Runtime.InteropServices;
2:279
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
2:281
else if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
2:283
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
2:285
2:286
Tip – creating a service in Linux
2:287
There are some good ways to create a service when you are developing on Windows, but how can we get the same result if we are developing for the Linux platform? The following script can be used to encapsulate a command-line .NET 8 app in Linux. The idea is that this service works like a Windows service. This can be useful, considering that most Linux installations are command-line-only and run without a user logged in:
2:288
2:289
The first step is to create a file that will run the command-line app. The name of the app is app.dll, and it is installed in appfolder. The application will be checked every 5,000 milliseconds. This service was created on a CentOS 7 system. Using a Linux terminal, you can type this:
2:290
cat >sample.service<<EOF
2:291
[Unit]
2:292
Description=Your Linux Service
2:293
After=network.target
2:294
[Service]
2:295
ExecStart=/usr/bin/dotnet $(pwd)/appfolder/app.dll 5000
2:296
Restart=on-failure
2:297
[Install]
2:298
WantedBy=multi-user.target
2:299
EOF
2:300
2:301
2:302
Once the file has been created, you must copy the service file to a system location. After that, you must reload the system and enable the service so that it will restart on reboots:
2:303
sudo cp sample.service /lib/systemd/system
2:304
sudo systemctl daemon-reload
2:305
sudo systemctl enable sample
2:306
2:307
2:308
Done! Now, you can start, stop, and check the service using the following commands. The whole input that you need to provide in your command-line app is as follows:
2:309
# Start the service
2:310
sudo systemctl start sample
2:311
# View service status
2:312
sudo systemctl status sample
2:313
# Stop the service
2:314
sudo systemctl stop sample
2:315
2:316
2:317
2:318
Now that we have learned about a few concepts, let us learn how to implement them in our use case.
2:319
Achieving security by design
2:320
As we have seen up to this point in the book, the opportunities and techniques we have for developing software are incredible. If you add all the information you will read about in relation to cloud computing in the next chapters, you will see that the opportunities just increase, as does the complexity involved in maintaining this computing environment.
2:321
As a software architect, you must understand that these opportunities come with many responsibilities. The world has changed a lot in recent years. The second decade of the 21st century has required lots of technology. Apps, social media, Industry 4.0, big data, and artificial intelligence are no longer future objectives but current projects that you will lead and deal with within your daily routine. However, the third decade of our century will require much more attention when it comes to cybersecurity.
2:322
The world now regulates companies that manage personal data. For instance, the GDPR – the General Data Protection Regulation – is mandatory not only for European territory but also for the whole world; it has changed the way software is developed. There are many initiatives comparable to the GDPR that must be added to your glossary of techniques and regulations, considering that the software you design will be impacted by them.
2:323
Security by design must be one of your areas of focus for designing new applications. This subject is huge, and it is not going to be completely covered in this book, but as a software architect, you must understand the necessity of having a specialist in the information security area in your team to guarantee the policies and practices needed to avoid cyber-attacks and maintain the confidentiality, privacy, integrity, authenticity, and availability of the services you architect.
2:324
When it comes to protecting your ASP.NET Core application, it is worth mentioning that the framework has many features to help you out with that. For instance, it includes authentication and authorization patterns. In the OWASP Cheat Sheet Series, you can read about many other .NET practices.
2:325
2:326
The Open Web Application Security Project® (OWASP) is a nonprofit foundation that works to improve the security of software. Check it out at https://owasp.org/.
2:327
2:328
ASP.NET Core also provides features to help us out with the GDPR. Basically, there are APIs and templates to guide you in the implementation of policy declaration and cookie usage consent.
2:329
List of practices for achieving a safe architecture
2:330
The following list of practices related to security certainly does not cover the entirety of the subject. However, these practices will help you, as a software architect, to explore some solutions related to this topic.
2:331
Authentication
2:332
Define an authentication method for your web app. There are many authentication options available nowadays, from ASP.NET Core Identity to external provider authentication methods, such as Facebook or Google. As a software architect, you must consider who the target audience of the application is. It would also be worth considering using Azure Active Directory (AD) as a starting point if you choose to go down this route.
2:333
You may find it useful to design authentication associated with Azure AD, a component for managing the Active Directory of the company you are working for. This alternative is pretty good in some scenarios, especially for internal usage. Azure currently offers Active Directory for usage as B2B (Business to Business) or B2C (Business to Consumer).
2:334
Depending on the scenario of the solution you are building, you might need to implement MFA – Multi-Factor Authentication. The idea of this pattern is to ask for at least two forms of proof of identity before allowing the solution to be used. It is worth mentioning that Azure AD facilitates this for you.
2:335
You may also find it useful to implement authentication to your platform using as basis Microsoft Identity Platform. In this case, the usage of the Microsoft Authentication Library (MSAL) will facilitate your work hugely. Check how to implement it by reading its documentation at https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-overview.
2:336
Do not forget that you must determine an authentication method for the APIs you provide. JSON Web Token is a pretty good pattern, and its usage is totally cross-platform.
2:337
You must determine the authorization model you will use in your web app. There are four model options:
2:338
2:339
Simple, where you just use the [Authorize] attribute in the class or method.
2:340
Role-based, in which you may declare Roles for accessing the Controller you are developing.