English
Topic 5. Repositories and Unit of Work: transactions, keyset paging, chat list queries and file storage
Goal: hide EF Core behind repository interfaces and a Unit of Work, write the queries the messenger needs — keyset pages of the history, the chat list with unread counters, searches that are safe against wildcards — store uploaded files on disk without path traversal, and register the whole data layer for dependency injection.
Where we are
We finish the data layer: after this topic the business layer has everything it needs to read and write data, without knowing EF Core (Fig. 5.1).
Fig. 5.1. The public face of the data layer
- Starting point: topic 4 — the database has its 14 tables,
./Run.ps1 dbworks. - At the end of the topic
AddDataregisters the data layer, and a small program creates users, a direct chat and messages, reads the chat list with an unread counter and pages through the history.
Steps
- Repository interfaces — why repositories · the interfaces · result models · tracked and read-only results
- Unit of Work and transactions — saving · unique violations as exceptions · transactions with retries
- Simple queries — users, tokens and attachments ·
IncludeandAsNoTracking· bulk updates · safeLIKEpatterns - Keyset paging — offset versus keyset ·
CursorPageandPaging· the message history · members · read positions - The chat list — a row-value cursor · one query with the last message, unread counter and peer
- Files and registration —
LocalFileStorage· the health check ·AddData· trying the data layer - Checkpoint — what must work, self-check questions and exercises
Materials
- Course slides, part 2, Data (slide "UUIDv7 ids and keyset paging") and part 3, Backend in C# (slide "Unit of Work and retries")
Abstractions/,Repositories/,Models/LocalFileStorage.cs,DependencyInjection.cs,DatabaseHealthCheck.csDocs/Architecture.md– the layers and the request flow