Docs / Repository Pattern

Repository Pattern

What this page covers#

This page shows one simple way to wrap MooDb in a repository.

Example#

</> C#
public sealed class UserRepository
{
    private readonly MooDbContext _db;

    public UserRepository(MooDbContext db)
    {
        _db = db;
    }

    public Task<User?> GetByIdAsync(int userId)
        => _db.SingleAsync<User>(
            "dbo.usp_User_GetById",
            new MooParams()
                .AddInt("@UserId", userId));

    public Task<List<User>> ListActiveAsync()
        => _db.ListAsync<User>("dbo.usp_User_ListActive");
}

Why this can be useful#

A repository can be useful when you want:

  • a named home for data access methods
  • one place for procedure names
  • a thinner service layer