Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Type-safe Mapping #2082

Open
jgador opened this issue Apr 29, 2024 · 0 comments
Open

Feature Request: Type-safe Mapping #2082

jgador opened this issue Apr 29, 2024 · 0 comments

Comments

@jgador
Copy link

jgador commented Apr 29, 2024

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);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant