Entity Framework Core Interview Question #174
What is the role of DbContext in Clean Architecture or layered architecture?
Advanced Mapping, Interceptors, Value Conversions & Architecture Senior Advanced
Quick Interview Answer
DbContext belongs to the persistence/infrastructure concern, while application logic should depend on abstractions or use cases appropriate to the architecture. Some designs expose repository interfaces; others allow application handlers to depend on a narrow persistence abstraction. The goal is to keep business rules testable and avoid leaking persistence concerns unnecessarily, not to add layers for their own sake.
Detailed Explanation
DbContext belongs to the persistence/infrastructure concern, while application logic should depend on abstractions or use cases appropriate to the architecture. Some designs expose repository interfaces; others allow application handlers to depend on a narrow persistence abstraction. The goal is to keep business rules testable and avoid leaking persistence concerns unnecessarily, not to add layers for their own sake.
Code Example
// Infrastructure
public sealed class AppDbContext : DbContext { }
// Application boundary can expose purposeful
// interfaces rather than persistence details everywhere.