Created
September 22, 2020 15:25
-
-
Save smailliwcs/e2a3c389fbd5baf9d46d6ae8b09f5ad5 to your computer and use it in GitHub Desktop.
Mapping qualified column names in Dapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment