Skip to content

Should ITypeMap.GetConstructorParameter be allowed to return null? #1969

@nyctef

Description

@nyctef

We're currently using a custom type map like this:

internal class RemoveSpacesFromColumnNames : SqlMapper.ITypeMap
{
    private readonly Type _type;

    public RemoveSpacesFromColumnNames(Type type)
    {
        _type = type;
    }

    public ConstructorInfo FindConstructor(string[] names, Type[] types) =>
        _type.GetConstructors().Single();

    public ConstructorInfo? FindExplicitConstructor() => null;

    public SqlMapper.IMemberMap? GetConstructorParameter(ConstructorInfo constructor, string columnName)
    {
        var nameWithoutSpaces = columnName.Replace(" ", "");
        var parameter = constructor.GetParameters().FirstOrDefault(x => x.Name == nameWithoutSpaces);
        return parameter == null ? null : new CustomParameterMap(columnName, parameter);
    }

    public SqlMapper.IMemberMap? GetMember(string columnName) => null;
}

where we'd added the nullable reference annotations ourselves. (Hopefully this code doesn't look too crazy :)

However when updating to dapper 2.1.4 this code now fails to compile since GetConstructorParameter is expected to return SqlMapper.IMemberMap instead of SqlMapper.IMemberMap?.

In SqlMapper.GenerateDeserializerFromMap it seems like the output of this method is checked for null in the same way that GetMember is, so I guess returning null is probably valid here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions