Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Proposal - example code included #2137

Open
dimzon opened this issue Dec 20, 2024 · 1 comment
Open

API Proposal - example code included #2137

dimzon opened this issue Dec 20, 2024 · 1 comment

Comments

@dimzon
Copy link

dimzon commented Dec 20, 2024

public static class ProposalExtensions
    {
        public static void JustShowMeTheCode(IDbConnection cn)
        {
            var result = cn.Query(
                // allows to use anonymous type as result
                template: new { EmployeeName = "" }, 
                param: new { EmployeeAge = 26 },
                // allows to use 'nameof' for column names and parameter names 
                sql: (t, p) => $"select e.name as {nameof(t.EmployeeName)} from tbl_emp e where e.age>@{nameof(p.EmployeeAge)}");
            foreach (var item in result)
            {
                Console.WriteLine(item.EmployeeName);
            }
        }

        public static IEnumerable<T> Query<T,TParams>(this IDbConnection cnn, T template, Func<T,TParams,string> sql, TParams param,  IDbTransaction? transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            return cnn.Query<T>(sql(default(T),default(TParams)),param,transaction,buffered,commandTimeout,commandType);
        }

        public static IEnumerable<T> Query<T>(this IDbConnection cnn, T template, Func<T,string> sql, IDbTransaction? transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            return cnn.Query<T>(sql(default(T)), param:null, transaction, buffered, commandTimeout, commandType);
        }
    }
@mgravell
Copy link
Member

There seems to be two things here:

  1. The ability to use a template to hack around not being able to specify the type for anonymous return types
  2. The ability to compose the SQL using nameof etc

The first of these is much better approaches using named-tuples, which should already work; consider instead Query<(int Id, string Name)>(...)

The second is arguably much harder to read, but fundamentally composing strings already works; the only new bit here is the template. Honestly, I wonder if this, if wanted, is better approached using the proposed custom string interpolation handler - maybe look here (draft only): https://github.com/DapperLib/DapperAOT/blob/sqlbuilder/test/Dapper.AOT.Test/SqlBuilderTests.cs

I doubt we'd add the PR proposed here into the core, but you can always use it in your own local extension method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants