cs11dotnet7/docs/errata/improvements.md

190 lines
9.2 KiB
Markdown
Raw Normal View History

2023-03-02 09:41:21 +01:00
**Improvements** (8 items)
2022-10-11 13:23:13 +02:00
If you have suggestions for improvements, then please [raise an issue in this repository](https://github.com/markjprice/cs11dotnet7/issues) or email me at markjprice (at) gmail.com.
2023-03-02 09:41:21 +01:00
- [Page 86 - Getting text input from the user](#page-86---getting-text-input-from-the-user)
2023-01-26 10:13:46 +01:00
- [Page 128 - Rounding numbers](#page-128---rounding-numbers)
2023-01-07 10:56:14 +01:00
- [Page 153 - Writing a function that returns a value](#page-153---writing-a-function-that-returns-a-value)
2023-01-07 11:44:03 +01:00
- [Page 179 - Reviewing project packages](#page-179---reviewing-project-packages)
2023-02-28 10:46:34 +01:00
- [Page 251 - Setting up a class library and console application](#page-251---setting-up-a-class-library-and-console-application)
2023-03-02 09:12:00 +01:00
- [Page 299 - Treating warnings as errors](#page-299---treating-warnings-as-errors)
2023-01-07 10:56:14 +01:00
- [Page 453 - Scaffolding models using an existing database](#page-453---scaffolding-models-using-an-existing-database)
2023-02-15 12:17:18 +01:00
- [Page 655 - Exercise 14.2 Practice implementing MVC by implementing a category detail page](#page-655---exercise-142--practice-implementing-mvc-by-implementing-a-category-detail-page)
2022-10-11 13:23:13 +02:00
2023-03-02 09:41:21 +01:00
# Page 86 - Getting text input from the user
In Step 1, I note that the `ReadLine` method is declared to return `string?`, meaning that it could return a `null` value instead of a `string` value (including an empty one). I also note that this is treated as a warning by the compiler.
In the next edition, I will add a note to explain that this method never actually returns `null` so there is no point in checking for that in functional code. A more useful check is `string.IsNullOrEmpty` so I will add more steps to show how to use that method and `string.IsNullOrWhiteSpace` to validate text input.
2023-01-26 10:13:46 +01:00
# Page 128 - Rounding numbers
In this section, I wrote about rounding rules as taught in schools and compare them to rounding rules when using C# and .NET. In schools, children are introduced to rounding rules with positive numbers and so learn the term "rounding up" and "rounding down". I did not explicitly say that for negative numbers, those terms would be reversed which can be confusing, so those terms should be avoided. This is why the .NET API uses the enum values `AwayFromZero`, `ToZero`, `ToEven`, `ToPositiveInfinity` and `ToNegativeInfinity` for improved clarity. In the next edition I will add a note about this.
2023-01-07 10:56:14 +01:00
# Page 153 - Writing a function that returns a value
2022-11-09 15:41:43 +01:00
At the end of this section there is a note box that explains that we could use the `C` format code to format the output as currency. If you are running on a computer in a culture that uses Euros then to show the Euro currency symbol you must enable UTF-8 encoding.
Add the following statement near the top of the code file before doing any writing to the console:
```cs
Console.OutputEncoding = System.Text.Encoding.UTF8;
```
2022-10-11 13:23:13 +02:00
2023-01-07 11:44:03 +01:00
# Page 179 - Reviewing project packages
In Step 1, the instruct the reader to add references to four packages, as shown in the following markup:
```xml
<ItemGroup>
<PackageReference
Include="Microsoft.Extensions.Configuration"
Version="7.0.0" />
<PackageReference
Include="Microsoft.Extensions.Configuration.Binder"
Version="7.0.0" />
<PackageReference
Include="Microsoft.Extensions.Configuration.FileExtensions"
Version="7.0.0" />
<PackageReference
Include="Microsoft.Extensions.Configuration.Json"
Version="7.0.0" />
</ItemGroup>
```
Due to transitive dependencies, you only actually need to explicitly reference two of the packages, as shown in the following markup:
```xml
<ItemGroup>
<PackageReference
Include="Microsoft.Extensions.Configuration.Binder"
Version="7.0.0" />
<PackageReference
Include="Microsoft.Extensions.Configuration.Json"
Version="7.0.0" />
</ItemGroup>
```
2023-02-28 10:46:34 +01:00
# Page 251 - Setting up a class library and console application
In Step 8, I wrote, "Run the `PeopleApp` project".
In Chapter 1, I explain how to control which project starts when a Visual Studio 2022 solution contains multiple projects by setting the startup project. In Chapters 2 to 5, I remind the reader to set the startup project.
In the 8th edition, I will add the same reminder to Chapter 6 as well, for example:
8. Set the `PeopleApp` project as the startup/active project:
2023-02-28 10:48:39 +01:00
a) If you are using Visual Studio 2022, set the startup project for the solution to the current selection.
b) If you are using Visual Studio Code, select `PeopleApp` as the active OmniSharp project. When you see the pop-up warning
2023-02-28 10:46:34 +01:00
message saying that required assets are missing, click Yes to add them.
9. Run the `PeopleApp` project and note the result, as shown in the following output:
```
Harry was born on a Sunday.
```
2023-03-02 09:12:00 +01:00
# Page 299 - Treating warnings as errors
This section shows how to follow best practice and treat warnings as errors. But doing so means you must write extra code in common scenarios to fix all warnings that will now be treated as errors that prevent compilation during the build process.
So this section also shows how to disable some common warnings by adding extra code. The project is NOT designed to be run. The code in it is written only to illustrate some common warnings and how to disable them to allow a build.
For example, one warning is caused by the compiler thinking there could be a null dereference. To disable the warning, you therefore need to check for a `null` value *even though we know that can never actually happen* as explained in the note. That extra code check is pointless if you run the console app and expect it to work correctly.
2023-03-02 09:28:47 +01:00
In the next edition, I will add another note to explicitly tell the reader not to run the project. The project implementation is not written to actually function as a check if someone has entered their name because that's not the point of this section. I will also simplify the code.
Current code:
```cs
if (name == null)
{
Console.WriteLine("You did not enter a name.");
return;
}
```
Code in next edition:
```cs
if (name == null) return; // must check for null to remove the warning
```
2023-03-02 09:12:00 +01:00
2023-01-07 10:56:14 +01:00
# Page 453 - Scaffolding models using an existing database
2023-01-07 11:01:27 +01:00
In Step 2, I show text that must be entered as a single line at the command-line, as shown in the following command formatted as in the print book:
2023-01-07 10:56:14 +01:00
```
dotnet ef dbcontext scaffold "Filename=Northwind.db" Microsoft.
EntityFrameworkCore.Sqlite --table Categories --table Products --output-
dir AutoGenModels --namespace WorkingWithEFCore.AutoGen --data-
annotations --context Northwind
```
2023-01-07 11:08:33 +01:00
I recommend that you type from the print book or copy and paste long commands like this from the eBook into a plain text editor like Notepad. Then make sure that the whole command is properly formatted as a single line with correct spacing, before you then copy and paste it to the command-line. Copying and pasting directly from the eBook is likely to include newline characters and missing spaces and so on that break the command.
For convenience, here is the same command as a single line to make it easier to copy and paste:
2023-01-07 11:01:27 +01:00
```
dotnet ef dbcontext scaffold "Filename=Northwind.db" Microsoft.EntityFrameworkCore.Sqlite --table Categories --table Products --output-dir AutoGenModels --namespace WorkingWithEFCore.AutoGen --data-annotations --context Northwind
```
2023-02-15 12:17:18 +01:00
# Page 655 - Exercise 14.2 Practice implementing MVC by implementing a category detail page
Earlier in the chapter, and in Exercise 14.2, the link generated for a category detail page looks like this:
```
https://localhost:5001/category/1
```
Although it is possible to configure a route to respond to that format of link, it would be easier if the link used the following format:
```
https://localhost:5001/home/categorydetail/1
```
In `Index.cshtml`, change how the links are generated to match the improved format, as shown in the following markup:
```xml
<a class="btn btn-primary"
href="/home/categorydetail/@Model.Categories[c].CategoryId">View</a>
```
This would then allow you to add an action method to the `HomeController` class as shown in the following code:
```cs
public async Task<IActionResult> CategoryDetail(int? id)
{
if (!id.HasValue)
{
return BadRequest("You must pass a category ID in the route, for example, /Home/CategoryDetail/6");
}
Category? model = await db.Categories.Include(p => p.Products)
.SingleOrDefaultAsync(p => p.CategoryId == id);
if (model is null)
{
return NotFound($"CategoryId {id} not found.");
}
return View(model); // pass model to view and then return result
}
```
And create a view that matches the name `CategoryDetail.cshtml`, as shown in the following markup:
```xml
@model Packt.Shared.Category
@{
ViewData["Title"] = "Category Detail - " + Model.CategoryName;
}
<h2>Category Detail</h2>
<div>
<dl class="dl-horizontal">
<dt>Category Id</dt>
<dd>@Model.CategoryId</dd>
<dt>Product Name</dt>
<dd>@Model.CategoryName</dd>
<dt>Products</dt>
<dd>@Model.Products.Count</dd>
<dt>Description</dt>
<dd>@Model.Description</dd>
</dl>
</div>
```
> Note: You could also use the simpler link format `https://localhost:5001/home/category/1` but then both the action method and the view filename must be just `Category` instead of `CategoryDetail`.
2023-02-15 12:36:12 +01:00
If you want to keep the original link format, then you would need to decorate the action method as shown in the following code:
```cs
[Route("category/{id}")]
public async Task<IActionResult> CategoryDetail(int? id)
```