Monthly Archives: August 2016

Mapping Qualified Column Names in Dapper

using Dapper;
using System;
using System.Reflection;

public class QualifiedTypeMap : SqlMapper.ITypeMap
{
    private static string Unqualify(string columnName)
    {
        return columnName.Substring(columnName.LastIndexOf('.') + 1);
    }

    private DefaultTypeMap @base;

    public QualifiedTypeMap(Type type)
    {
        @base = new DefaultTypeMap(type);
    }

    public ConstructorInfo FindConstructor(string[] names, Type[] types)
    {
        return @base.FindConstructor(names, types);
    }

    public ConstructorInfo FindExplicitConstructor()
    {
        return @base.FindExplicitConstructor();
    }

    public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)
    {
        return @base.GetConstructorParameter(constructor, Unqualify(columnName));
    }

    public SqlMapper.IMemberMap GetMember(string columnName)
    {
        return @base.GetMember(Unqualify(columnName));
    }
}