Feature Organization
Each feature in the Catalog service follows a consistent vertical slice pattern with three main components:- Handler - Contains business logic and data access
- Endpoint - Defines HTTP route and maps requests/responses
- Validator - Validates commands/queries (where applicable)
Create Product
Create a new product in the catalog.Handler Implementation
Products/CreateProduct/CreateProductHandler.cs
Endpoint Implementation
Products/CreateProduct/CreateProductEndpoint.cs
Key Points
- Auto-generated ID: Marten automatically generates a GUID for new products
- Validation: FluentValidation rules ensure data integrity before persistence
- Mapster: Used for object mapping between DTOs and domain objects
- HTTP 201: Returns Created status with location header
Get Products (Paginated)
Retrieve a paginated list of all products.Handler Implementation
Products/GetProducts/GetProductsHandler.cs
Endpoint Implementation
Products/GetProducts/GetProductsEndpoint.cs
Key Points
- Pagination: Uses Marten’s
ToPagedListAsyncextension method - Default Values: PageNumber defaults to 1, PageSize defaults to 10
- Query Parameters: Bound using
[AsParameters]attribute
Get Product By ID
Retrieve a single product by its unique identifier.Handler Implementation
Products/GetProductById/GetProductByIdHandler.cs
Endpoint Implementation
Products/GetProductById/GetProductByIdEndpoint.cs
Key Points
- LoadAsync: Optimized Marten method for loading by ID
- Exception Handling: Throws
ProductNotFoundExceptionfor missing products - Route Parameter: ID extracted from URL path
Get Products By Category
Retrieve all products in a specific category.Handler Implementation
Products/GetProductByCategory/GetProductByCategoryHandler.cs
Endpoint Implementation
Products/GetProductByCategory/GetProductByCategoryEndpoint.cs
Key Points
- LINQ Query: Uses standard LINQ with Marten
- Contains Check: Searches in the Category list property
- String Matching: Category parameter is case-sensitive
Update Product
Update an existing product’s information.Handler Implementation
Products/UpdateProduct/UpdateProductHandler.cs
Endpoint Implementation
Products/UpdateProduct/UpdateProductEndpoint.cs
Key Points
- Load and Update: Loads existing product, modifies properties, updates in session
- Validation: More restrictive rules including length constraints
- 404 Response: Returns NotFound if product doesn’t exist
Delete Product
Delete a product from the catalog.Handler Implementation
Products/DeleteProduct/DeleteProductHandler.cs
Endpoint Implementation
Products/DeleteProduct/DeleteProductEndpoint.cs
Key Points
- Soft Delete Available: Marten supports soft deletes if configured
- Simple Operation: Direct delete by ID
- No Verification: Doesn’t check if product exists before deletion
Common Patterns
Object Mapping with Mapster
All endpoints use Mapster for mapping between DTOs:Dependency Injection
Handlers receive dependencies via primary constructor:MediatR Integration
All endpoints useISender to dispatch commands and queries:
Carter Route Registration
All endpoints implementICarterModule and register routes:
