Why Marten?
Marten transforms PostgreSQL into a document database:- Document Storage: Store objects as JSON documents
- LINQ Queries: Query documents using familiar LINQ syntax
- ACID Transactions: Full PostgreSQL transactional support
- Event Sourcing: Built-in event store capabilities
- Schema Management: Automatic schema generation and migrations
Catalog Service Configuration
The Catalog service uses Marten for product catalog management.Connection String
src/Services/Catalog/Catalog.API/appsettings.json
Service Registration
src/Services/Catalog/Catalog.API/Program.cs
UseLightweightSessions(): Optimized for read-heavy workloadsInitializeMartenWith<T>: Seeds initial data in development
Document Models
Marten stores domain objects as JSON documents:Data Seeding
src/Services/Catalog/Catalog.API/Data/CatalogInitialData.cs
Basket Service Configuration
The Basket service uses Marten for shopping cart persistence with Redis caching.Connection String
src/Services/Basket/Basket.API/appsettings.json
Service Registration
src/Services/Basket/Basket.API/Program.cs
- Custom identity:
UserNameinstead of defaultId - Decorator pattern: Adds caching layer over repository
Repository Implementation
src/Services/Basket/Basket.API/Data/BasketRepository.cs
Working with Marten Sessions
Lightweight Sessions
Optimized for read-heavy operations:Querying Documents
Storing Documents
Health Checks
/health endpoint.
Docker Compose Setup
Performance Tips
Use Lightweight Sessions
For read-heavy operations:Batch Operations
Compiled Queries
For frequently executed queries:Troubleshooting
Connection Issues
Schema Updates
Marten auto-generates schema. To manually update:Related Resources
Redis Caching
Add caching layer to Basket service
Marten Documentation
Official Marten documentation
