interpolated string support #3
Replies: 2 comments 1 reply
-
Thanks, I'll add this support asap. |
Beta Was this translation helpful? Give feedback.
-
After some investigation, It seems to me that there is no easy way to add elegant method overloads. For example, if I have two overloads:
Both of these calls:
Would always call the first overload If I change entire interface to always use
This might be most elegant solution, but I'm afraid that would break build for many existing users, so my current idea is to add new version of method for "Fmt" or "Format" suffix like this:
or
I don't know, should I break the build and introduce the elegant version or add this What do you think? |
Beta Was this translation helpful? Give feedback.
-
I didn't see interpolated string support so I tossed it together for my own use. So I can then just do something like
sqlConnection.Read<Person> ( $"SELECT * FROM people WHERE name = {name} AND birthdate = {birthdate}" )
and it should be properly handling the parameters.`
public sealed class Query {
public static Query FromSqlInterpolated ( System.FormattableString formattableString ) {
var parameterIdentities = new string[ formattableString.ArgumentCount ];
var parameters = new (string, object)[ formattableString.ArgumentCount ];
for ( var i = 0; i < formattableString.ArgumentCount; i++ ) {
var parameterIdentity = "@p" + i;
var parameterValue = formattableString.GetArgument ( i );
parameterIdentities[ i ] = parameterIdentity;
parameters[ i ] = (parameterIdentity, parameterValue);
}
var command = System.String.Format ( formattableString.Format, parameterIdentities );
return new Query { Command = command, Parameters = parameters };
}
public string Command { get; init; }
public (string, object)[] Parameters { get; init; }
}
`
Beta Was this translation helpful? Give feedback.
All reactions