✅ 1. Repository Pattern
What it is
A Repository acts as an abstraction layer between your domain/business logic and the data access layer.
Instead of writing EF Core queries directly in controllers/services, you wrap them inside repository classes.
Why we use it
-
Keeps business logic independent from persistence logic
-
Easy to mock for unit testing
-
Central place to maintain DB queries
Example: Product Repository (Non-Generic)
✔ Entity
✔ IProductRepository
✔ Implementation
👉 This is a repository dedicated only to Product.
✅ 2. Generic Repository Pattern
What it is
A Generic Repository provides a reusable base class for CRUD operations.
Instead of writing a repository per entity, you write one that works with all entities.
Why we use it
-
Avoids duplicated CRUD code
-
Cleaner structure
-
Extensible for entity-specific repositories
✔ IGenericRepository
✔ Implementation
🔥 Entity-Specific Repository + Generic Repository
Sometimes we extend generic repository for custom queries:
No comments:
Post a Comment