A common approach in vertical slice architecture is to encapsulate all aspects of a feature in one class. However, this makes the code hard to read, hard to maintain, and hard to locate for actual business logic.
in CatalogAPI microservices we will follow the vertical slice architecture using CQRS and mediatR
Each vertical slice cut through all architectural layers such as the UI, business logic and data access And the project organized with the models, features, data and abstractions folders etc
Model:
- Lets create our models inside Models Fold-we
- Database in User: PostgreSQL has Json column features that allowing us to store and query our data as a Json document.
- it combines the flexibility of document database with the reliability of the relational PostgreSQL database.
- Product.cs will be our Model Class
Feature Folder
- Under Model folder, create another folder name will be the products.
- This will be the our feature folder and under the products folder we will create each use case or feature
- This helps in organizing our code based functionalities and keeps related components together
- a handler will be our application logic layer
- endpoints will be represent our presentation API layer.
- This separation makes it easier to locate specific parts of the code and manage them effectively. Especially, we will encapsulate business logic under the handle class, and we will see API related operations in endpoint class.
Develop feature handler class with CQRS and mediatR Library
The handler is responsible for executing the command logic with using the mediator library, the handler receives a command and process it, encapsulating the business logic for specific operations under the handler class.