English
Checkpoint
What must work
powershell
dotnet buildpowershell
dotnet format Chatex.slnx --verify-no-changesEvery file in Source/Chatex.Data/Entities must be identical to the reference. Compare the whole folder at once:
powershell
git diff --no-index Source/Chatex.Data/Entities D:\Work\Chatex-Reference\Source\Chatex.Data\EntitiesCommit the model:
powershell
git add .powershell
git commit -m "Add the domain model: users, chats, members, messages and attachments"Common mistakes
Table 3.2. Common mistakes in the domain model
| Symptom | Cause | Fix |
|---|---|---|
CS0246: The type or namespace name 'IdentityUser<>' could not be found | the Identity package is not referenced | dotnet add Source/Chatex.Data package Microsoft.AspNetCore.Identity.EntityFrameworkCore |
CS8618: Non-nullable property 'Chat' must contain a non-null value when exiting constructor | a navigation property is declared without = null! | add = null! to required navigation properties, = [] to collections |
CS9035: Required member 'Message.Body' must be set in the object initializer | an object is created without a required member | set the member in the initializer: new Message { Body = "..." } |
CS0535: 'ChatMember' does not implement interface member 'IHasCreatedAt.CreatedAt' | the explicit implementation is missing or misspelled | write DateTimeOffset IHasCreatedAt.CreatedAt { get => JoinedAt; set => JoinedAt = value; } |
| two different direct keys for the same pair of users | MakeDirectKey does not sort the ids | compare with CompareTo and put the smaller id first |
dotnet run entities.cs fails with Could not find project or directory | the path in #:project is wrong | use the full path of Chatex.Data.csproj in your repository |
Self-check questions
- What is an entity? How do you find the entities of an application from its feature list?
- Why is the relationship between users and chats an entity of its own, and what data does it carry?
- What is the difference between a foreign key property and a navigation property?
- How is a UUIDv7 built? What does Chatex gain from it compared to an auto-incremented integer and a random
Guid? - Why are creation and update times not set by the services? What marks the entities that have them?
- Which rules belong in an entity, and which in a service? Give an example of each from Chatex.
- Why are the account and the profile two entities? What would go wrong if the profile were part of
User? - Why does the database store a hash of the refresh token and not the token? Why does
IsActiveAttake the time as a parameter? - How does
DirectKeyguarantee one direct chat per pair of users, even when two requests arrive at the same moment? - How does one column,
LastReadMessageId, replace a table of read receipts? Why may it only move forward? - Why are messages and chats deleted softly?
- Why are attachments uploaded before the message is sent? Why is
StoragePathgenerated by the server?
Exercises
- Reactions. Design an entity for emoji reactions to messages (a user puts 👍 on a message; one reaction of each kind per user and message). Which properties does it need? What is its key? Which navigation properties do you add to
MessageandUser? Write the class in your playground, not in the repository. - Pinned messages. A chat should be able to pin one message. Where would you store it, and what happens to the pin when the message is deleted?
- Read positions. Using
entities.cs, create three messages one after another (with a shortThread.Sleep(2)between them so that they get different milliseconds), set a member'sLastReadMessageIdto the second one and count with LINQ how many of the three are unread (m.Id.CompareTo(lastRead) > 0).