English
Topic 7. Foundations of the business layer: the Result pattern, errors, validation, cursors and dependency injection
Goal: lay the foundations every service of Chatex.Core stands on — results instead of exceptions for expected failures, errors with stable codes, validation rules as classes, opaque cursors for paged lists — and register the business layer, with the data layer beneath it, in one call.
Where we are
Module 3 builds the business layer: the rules of the messenger (Fig. 7.1). This first topic writes no service yet; it writes the vocabulary all services share.
Fig. 7.1. The business layer between the API and the data layer
- Starting point: topic 6 — the data layer is complete and its 19 tests pass.
- At the end of the topic Chatex.Core has the folder
Commonwith the shared building blocks,AddCoreregisters the business and the data layer, and a small program shows theResulttype at work.
Steps
- Results and errors — exceptions or results ·
ErrorandErrorKind·ResultandResult<T>· shared errors - Validation — FluentValidation · validators that return an
Error· a rule for handles · Identity's errors - DTOs and cursors — what a service returns · pages · opaque cursors
- Registering the business layer —
AddCore· how services get their dependencies · trying theResulttype - Checkpoint — what must work, self-check questions and exercises
Materials
- Course slides, part 3, Backend in C# (slides "The composition root", "The Result pattern", "Errors with stable codes")
Common/,DependencyInjection.csDocs/Architecture.md– theResultpattern and the request flow- The C# course topic Dependency injection