d-p-y/statically-typed-poco-queries

Would it be possible to add a configurable "converters" for poco properties?

nils-a opened this issue ยท 9 comments

Coming from asherber/StaTypPocoQueries.PetaPoco#5 - I have pocos with "converter" attributes on some properties.
In my example there is one particular property defined an enum and using a converter the enum name (not the value) is stored in the database.

Using Statically Typed Poco Queries the expression x => x.SomeEnumProp == MyEnumType.SomeValue is evaluated as a comparison of int values, not of string and the value being used to build the sql is the enum value, not the name, as I'd like.

d-p-y commented

Hi @nils-a
Thanks for reporting this missing feature. I'll look at it one of next days. Seems doable.

d-p-y commented

I've added support for optionally mapping parameters in just released v1.12.0.0. Now *Poco libraries may leverage this feature to support parameters conversion. Usage is "documented" in tests
As you can see there, in order to use converter you need to pass function that actually finds converter and invokes it.

You rock! Thanks!! ๐Ÿš€

@d-p-y Something in this latest update constitutes a breaking change. When I update to latest NuGet and compile my current stpq.PetaPoco code, I get:

DatabaseExtensions.FSharp.cs(36,46): error CS0411: The type arguments for method 'ExpressionToSql.Translate<T>(Translator.IQuoter, Expression<Func<T, bool>>, bool, Func<MemberInfo, string>, Func<PropertyInfo, object, object>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Relevant code:

private static readonly FSharpFunc<MemberInfo, string> FsExtractColumnName 
    = ExpressionToSql.AsFsFunc<MemberInfo, string>(ExtractColumnName);

private static Sql ToSql<T>(this FSharpExpr<FSharpFunc<T, bool>> query, IDatabase db)
{
    var translated = ExpressionToSql.Translate(new DatabaseQuoter(db), query, true, FsExtractColumnName);
    return new Sql(translated.Item1, translated.Item2);
}
d-p-y commented

@asherber sorry, you are right :( I was struggling with F# optional parameters breaking method overload resolution. With current version in master I solved it properly. I also compiled StaTypPocoQueries.PetaPoco and all tests passed without any changes.
I'll release new version next days if there are no objections to current implementation.

I'm relatively weak in F# myself, so I'm glad you were able to find it before I had a chance to go spelunking!

In the meantime, I'll hook my library up to your source and run some tests to make sure things are behaving as expected (though your tests look pretty thorough!).

I don't know enough about F# to comment on the implementation, but I was able to wire this up in C# and F#, and to successfully run tests in C#.

d-p-y commented

Thanks for checking! I've officially released v1.13.0.0