English
Checkpoint
What must work
powershell
dotnet buildpowershell
dotnet format Chatex.slnx --verify-no-changespowershell
dotnet test --solution Chatex.slnxpowershell
git diff --no-index Source/Chatex.Core/Common D:\Work\Chatex-Reference\Source\Chatex.Core\CommonThe reference folder has three more files that come in topic 9 (AvatarStore.cs, FileUpload.cs, LimitedReadStream.cs); the six files of this topic must not differ.
Commit:
powershell
git add .powershell
git commit -m "Add the foundations of the business layer: results, errors, validation and cursors"Common mistakes
Table 7.1. Common mistakes in the foundations
| Symptom | Cause | Fix |
|---|---|---|
CS0029: Cannot implicitly convert type 'Error' to 'Result<…>' | an implicit operator is missing or has the wrong type | check both implicit operator Result<T>(T value) and implicit operator Result<T>(Error error) |
CS8602: Dereference of a possibly null reference after if (!result.IsSuccess) | [MemberNotNullWhen(false, nameof(Error))] is missing on IsSuccess | add the attribute (namespace System.Diagnostics.CodeAnalysis) |
CA1000 or CA1716 warnings fail the build | the rules are not switched off in .editorconfig | compare .editorconfig with the reference (topic 2) |
validation details use UserName instead of userName | CamelCase is not applied to the property name | keep GroupBy(e => CamelCase(e.PropertyName)) |
TryDecode is ambiguous | the out argument has no type | write out Guid? id or out ChatListCursor? cursor |
Self-check questions
- Which failures does Chatex return as results, and which does it throw? Why the difference?
- What are the three parts of an
Error, and who reads each of them? - Why is
Result<T>areadonly structwith private constructors? - How do the implicit conversions make services shorter? Show an example.
- What does
[MemberNotNullWhen(false, nameof(Error))]tell the compiler? - Why does reading
Valueof a failed result throw instead of returningdefault? - What does FluentValidation give over
ifstatements at the top of a method? - Why are field names in validation details written in camelCase?
- Why are the cursors given to clients opaque? What happens to a tampered cursor?
- Why does
CursorCodecwrite the bytes of aGuidin big-endian order? - Why does
AddCorecallAddDatainstead of letting the API call both?
Exercises
- A result for void operations. Add to
results.csa methodResult Deposit(decimal amount)that fails for non-positive amounts and returnsResult.Successotherwise. Print the outcome withMatch. - A validator. In the playground, reference the FluentValidation package (
#:package [email protected]) and write anAbstractValidator<TransferCommand>for a recordTransferCommand(string FromIban, string ToIban, decimal Amount): both IBANs not empty and different, the amount between 0.01 and 10 000. Print the errors of an invalid command grouped by property. - Cursor sizes. How long is the Base64Url text of a message cursor (16 bytes) and of a chat list cursor (24 bytes)? Compute it, then check with
Convert.ToBase64Stringin the playground.