jonwagner / Insight.Database

Fast, lightweight .NET micro-ORM
Other
861 stars 145 forks source link

Multiple Parameter Mappers #423

Closed buist-sharris closed 4 years ago

buist-sharris commented 4 years ago

Is there a way to have multiple parameter mappers? I am registering multiples, but the first one's "MapParameter" only ever get's called, even if it returns null.

For example:

public class Mapper1 : IParameterMapper { public string MapParameter(Type type, IDbCommand command, IDataParameter parameter) { return null; } }

public class Mapper2 : IParameterMapper { public string MapParameter(Type type, IDbCommand command, IDataParameter parameter) { return null; } }

Then in my app's startup: ColumnMapping.Parameters.AddMapper(new Mapper1()).AddMapper(new Mapper2());

Mapper1's MapParameter method is continually called, but Mapper2's never is even though Mapper1 always returns null. I may have misunderstood, but the Wiki says

The mappers are called in order until one of them returns a non-null value.

Which leads me to believe that when Mapper 1 returns null, Mapper 2 should be tried next?

Thanks.

buist-sharris commented 4 years ago

As an FYI, the reason I don't handle all mapping in one mapper is because they reside in different libraries. Rather than have my "base" application know how to map parameters for other libraries, the outside libraries define their own mappers. The outside libraries also access databases I can't modify and define interactions for these databases.

jonwagner commented 4 years ago

It's been a while since I looked at the docs on this.

I believe to indicate no changes to a column mapping, a mapper should return the column name, not null.

Jaxelr commented 4 years ago

@buist-sharris Take a look at this page

buist-sharris commented 4 years ago

That's where I've been trying to piece this together from. So if I look at the comments in the sample code...

class MyParameterMapper : IParameterMapper
{
    string MapParameter(Type type, IDbCommand command, IDataParameter parameter)
    {
        if (command.CommandText != "MyProc")
            return null;            // null allows another handler to try
        if (parameter.ParameterName == "Glass")
            return "Stein";         // a string value specifies the name of the field/property
        return "__unmapped__";      // returning an invalid string value prevents other handlers from mapping 
    }
}

Returning null should "allow another handler to try", but it does not appear to. As I first indicated, my Mapper1 always returns null and Mapper2 is never called.

As a side note, returning an invalid string (i.e. "unmapped") always throws an exception for me when examining RETURN_VALUE.

jonwagner commented 4 years ago

found and fixed this bug in the next build.