Skip to content
Learn Netverks

Lesson

Step 29/36 81% through track

repositories-pattern

Repository pattern

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Repository pattern: the syntax, APIs, and habits you need before advancing in ASP.NET.

Teams ship Repository pattern on every ASP.NET codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Repository pattern in contexts like: Line-of-business APIs, intranets, BFF layers, and cloud-hosted services on Linux or Windows.

Write C# (top-level or Program class), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

The repository pattern abstracts data access behind interfaces—controllers depend on IProductRepository, not raw DbContext. Teams debate its necessity with EF Core; it helps large codebases and testing boundaries.

When it helps

  • Centralize complex queries in one place
  • Swap EF for another store in tests or migrations
  • Keep controllers thin and domain-focused
public interface IProductRepository {
    Task<Product?> GetByIdAsync(int id);
    Task AddAsync(Product product);
}

public class ProductRepository : IProductRepository {
    private readonly AppDbContext _db;
    public ProductRepository(AppDbContext db) => _db = db;
    // implement with _db.Products...
}

When to skip

Small apps may inject DbContext directly—avoid pointless pass-through repositories that only wrap DbSet with no added logic.

Important interview questions and answers

  1. Q: Repository vs DbContext directly?
    A: DbContext is already a unit-of-work/repository abstraction—extra layer adds value only with real query encapsulation.
  2. Q: Generic IRepository<T>?
    A: Convenient but leaky—domain-specific methods (GetActiveProducts) express intent better.

Self-check

  1. What does a repository hide from controllers?
  2. When is a repository just unnecessary indirection?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Repo still needed?
  • Unit of Work?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump