DapperLib / DapperLib/Dapper

Feature Request: Type-safe Mapping

Open
#2,082 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

Awareness of Existing Features:
I am not aware of whether this feature already exists in newer versions of Dapper. If it does, please forgive the redundancy of this request. However, if it doesn't exist, I believe it would be a valuable addition to the library.

When using Dapper for object mapping, there isn't a built-in functionality for type-safe mapping of database columns to object properties. Columns returned from the database may not exactly match the properties of the POCO.

Example Use Case:

public class MyPocoClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }
}

var personMapping = new Dictionary<string, Expression<Func<MyPocoClass, object>>>()
{
    { "Id", map => map.Id },
    { "FullName", map => map.Name },
    { "BirthDate", map => map.DateOfBirth }
};

// Custom type-safe mapping:
CustomSqlTypeMap.Map(personMapping );

In the above example, personMapping is a dictionary where the keys represent column names in the database, and the values are expressions that map those column values to corresponding properties in the MyPocoClass. This provides a clear and type-safe way to define the mapping between database columns and C# object properties.

I've created a sample utility class to address this issue:

public static class CustomSqlTypeMap
{
    /// <summary>
    /// Set custom mapping for type <typeparamref name="TEntity"/>.
    /// </summary>
    /// <typeparam name="TEntity">The CLR object to be hydrated with query results.</typeparam>
    /// <param name="memberMaps">Dictionary of mapping rules. Use the table column name as key to map with property from <typeparamref name="TEntity"/>.</param>
    public static void Map<TEntity>([NotNull] IReadOnlyDictionary<string, Expression<Func<TEntity, object>>> memberMaps)
        where TEntity : class
    {
        if (SqlMapper.GetTypeMap(typeof(TEntity)) is DefaultTypeMap)
        {
            var typeMap = new CustomPropertyTypeMap(typeof(TEntity),
                (type, columnName) => MapProperty(memberMaps).Invoke(type, columnName));

            SqlMapper.SetTypeMap(typeof(TEntity), typeMap);
        }
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing SqlMapper.GetTypeMap and SqlMapper.SetTypeMap entry points, along with CustomPropertyTypeMap and the proposed CustomSqlTypeMap utility. Check whether newer Dapper versions already provide this capability. Done would require an agreed library API and behavior for type-safe column-to-property mapping; the issue names no files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.