Docs / Reusable Mapping Methods

Reusable Mapping Methods

What this page covers#

This page shows how to centralize manual mapping logic.

Example#

</> C#
using Microsoft.Data.SqlClient;

public static class UserMap
{
    public static User Map(SqlDataReader reader) => new()
    {
        UserId = reader.GetInt32(reader.GetOrdinal("UserId")),
        Name = reader.GetString(reader.GetOrdinal("Name"))
    };
}

var users = await db.ListAsync("dbo.usp_User_ListActive", UserMap.Map);
var user = await db.SingleAsync("dbo.usp_User_GetById", UserMap.Map, new MooParams().AddInt("@UserId", 42));

Why this helps#

This can reduce duplication and keeps the call site cleaner when the same row shape appears in several places.