Overview
The Discount service is a gRPC-based microservice that manages discount coupons for products in the e-commerce system. Unlike the other services that use PostgreSQL or MongoDB, this service uses SQLite for lightweight data storage with Entity Framework Core.Architecture
- Protocol: gRPC (HTTP/2)
- Framework: ASP.NET Core 8.0
- Database: SQLite
- ORM: Entity Framework Core 8.0
- Mapper: Mapster
- Location:
src/Services/Discount/Discount.Grpc/
Key Features
gRPC Communication
- High-performance binary protocol using Protocol Buffers
- Strongly-typed service contracts
- Efficient inter-service communication
- Used by Basket service for real-time discount calculations
CRUD Operations
- GetDiscount: Retrieve discount by product name
- CreateDiscount: Create new discount coupon
- UpdateDiscount: Update existing discount
- DeleteDiscount: Remove discount coupon
SQLite Database
- Lightweight, serverless database
- File-based storage (
discountdb) - Entity Framework Core migrations
- Pre-seeded with sample data
Project Structure
Service Configuration
Program.cs Setup
appsettings.json
Kestrel configuration is crucial - it sets HTTP/2 as the protocol, which is required for gRPC communication.
Database Model
Coupon Entity
DbContext
Migration Extension
NuGet Dependencies
Integration with Other Services
Basket Service Integration
The Basket service consumes the Discount gRPC service to apply discounts when storing shopping carts: Client Configuration (in Basket.API/Program.cs):Why gRPC?
- Performance: Binary serialization is faster than JSON
- Type Safety: Strongly-typed contracts prevent runtime errors
- HTTP/2: Multiplexing, header compression, and bidirectional streaming
- Contract-First: Proto files define clear service contracts
- Cross-Platform: Works across different languages and platforms
Why SQLite?
- Lightweight: No separate database server required
- Simple Deployment: Single file database
- Sufficient for Discounts: Read-heavy workload with low write volume
- EF Core Support: Full Entity Framework Core compatibility
- Development-Friendly: Easy setup and testing
Running the Service
Standalone
With Docker
Next Steps
- gRPC Service Implementation - Detailed service method implementations
- Protocol Buffers - Proto file and message definitions
