Q1 . EF Core already implements Repository (DbSet) and Unit of Work (DbContext). why Repository pattern needed.
EF Core already implements Repository (
DbSet) and Unit of Work (DbContext). We add our own Repository/UoW only when we need abstraction, domain separation, testability, or complex business queries. Otherwise, it’s unnecessary boilerplate.
That single sentence already puts you ahead of most candidates.
DbSet<TEntity>→ Repository
_context.Orders.Add(order);
_context.Orders.Find(id);DbContext→ Unit of Work
_context.SaveChanges(); // transaction boundary✔ Tracks changes ✔ Commits or rolls back ✔ Manages transactions
👉 So yes, EF Core already does both.
Without repository:
Application → EF Core → SQL
With repository:
Application → Interface → Infrastructure(EF)
👉 Domain/Application should not depend on EF Core.
If you inject DbContext everywhere:
Include()AsNoTracking()- LINQ everywhere
Business logic becomes ORM-aware, which is bad design.
Repository:
- Exposes intent-based methods
GetActiveOrdersForCustomer(id)not:
_context.Orders.Where(...)With repository:
mockRepo.Setup(x => x.GetById(1)).Returns(order);Without repository:
- Need InMemory DB
- Slower
- Less deterministic
Repositories make sense when:
- Aggregate roots
- Domain-specific queries
- Multiple tables involved
Order GetOrderWithPaymentsAndInvoices(int id);EF Core auto transaction is fine until:
- Multiple DbContexts
- Multiple SaveChanges
- External calls involved
UoW gives:
- Clear transaction boundary
- One commit point
This is the killer interview insight.
❌ Simple CRUD application ❌ Thin service layer ❌ Just passing DbSet calls ❌ Generic repository wrapping DbSet blindly
GetAll()
Add()
Update()
Delete()👉 This becomes an anti-pattern.
❝ Generic repository over EF Core is usually unnecessary and harmful ❞ — Microsoft Docs / Jimmy Bogard
They recommend:
- Use DbContext directly
- Add repositories only for aggregates
✔ Use EF Core directly ✔ No repository ✔ DbContext injected into services
✔ Repository per aggregate ✔ No generic repository ✔ DbContext hidden in Infrastructure ✔ UoW only if needed
“EF Core already provides repository and unit of work through DbSet and DbContext. Creating our own Repository/UoW is not mandatory and can become boilerplate. We introduce them only when we need domain isolation, clean architecture boundaries, complex queries, or better testability. Otherwise, using DbContext directly is perfectly fine.”
Q: Do you always implement Repository with EF Core? 👉 No, only when architectural boundaries or domain complexity require it.
Q: Is generic repository a good idea? 👉 Usually no with EF Core.
Q: What is better than generic repository? 👉 Specific repositories per aggregate root.
“EF Core already implements Repository and Unit of Work, so we add our own only when we need abstraction and architectural boundaries—otherwise it’s unnecessary complexity.”
If you want next, I can:
- Give exact interview wording
- Explain anti-patterns with examples
- Show how senior architects design this
- Compare EF Core vs Dapper decision
Just tell me 👍