Intermediate LevelSoftware Engineering Principles
What is the Repository Pattern and how does it isolate domain models from data access layers?
❓ Interview Prompt: Explain the Repository Pattern, abstracting collection-like data access interfaces, ORM isolation, and unit test mocking.
The Repository Pattern mediates between the domain and data mapping layers, exposing a simple collection-like interface ('find', 'add', 'remove') for accessing domain objects:
Benefits:
•
Decouples Domain from ORM: Business logic interacts with domain interfaces rather than Prisma/TypeORM directly.
•
Unit Testing: Easily swap real database repositories with in-memory array repositories during testing.
Code Implementation Example
typescript
interface UserRepository {
findById(id: string): Promise<User | null>;
save(user: User): Promise<void>;
}
class PrismaUserRepository implements UserRepository {
async findById(id: string) { return await prisma.user.findUnique({ where: { id } }); }
async save(user: User) { await prisma.user.upsert({ /* ... */ }); }
}💡Abstracting database access behind a Repository interface.
🎯 Interview Key Takeaway
Use the Repository pattern to isolate domain business logic from database ORM implementation details.
#Repository Pattern#Data Access#Architecture#Intermediate
Back to all Software Engineering Principles Questions→