I have tables that will have some known, some unknown columns, by allowing some dynamic SQL for the select. I'm looking to have any columns that don't have a matching property to be added to a dictionary. Based on another question on How can I make Dapper.NET throw when result set has unmapped columns?, but instead of throwing an error I'd like to just map those columns to a dictionary that will be on the class.
public SqlMapper.IMemberMap GetMember( string columnName ) {
var fallbackMappers = new List<SqlMapper.ITypeMap>();
fallbackMappers.Add( _defaultTypeMap );
var fallbackTypeMapper = new FallbackTypeMapper(fallbackMappers);
var member = fallbackTypeMapper.GetMember(columnName);
if(member == null ) {
throw new Exception();
}
return member;
}
//...
public static async Task<IEnumerable<T>> QueryAsyncOtherProps<T>(
this IDbConnection cnn,
string sql,
object param = null,
IDbTransaction transaction = null,
int? commandTimeout = default,
CommandType? commandType = default
)
where T : BaseSimpleType {
lock ( _lock ) {
if ( TypesThatHaveMapper.ContainsKey( typeof( T ) ) == false ) {
SqlMapper.SetTypeMap( typeof( T ), new NullTypeMapToOtherProperties<T>() );
TypesThatHaveMapper.Add( typeof( T ), null );
}
}
return await cnn.QueryAsync<T>( sql, param, transaction, commandTimeout, commandType );
}
My BaseSimpleType just contains a dictionary property called OtherProperties, so this only would get called on types that have that field and the name is consistent. Is there a different way to try to do this with Dapper?