Repodb: Request: Enable ExecuteQuery<T>

Created on 31 Jul 2020  路  11Comments  路  Source: mikependon/RepoDB

Hi Sir, first of all, thank you for this wonderful ORM library. Just wanna request if you can support generic type binding for ExecuteQuery function. Currently it has dynamic and object return type, but it would be great if we can pass a generic return type as well instead of only concrete classes. Thanks and God bless!

enhancement request under assessment

All 11 comments

This is already supported, see the documentation here. Or, you can do the code below.

using (var connection = new SqlConnection(connectionString).EnsureOpen())
{
    var people = connection.ExecuteQuery<Person>("SELECT * FROM [dbo].[Person];");
}

In the link above, you can pass different sets and types of parameters. Just FYI.

What I mean is to pass a generic type instead of a reference type as shown in your example.

ExecuteQuery

So in this code snippet, what I want is to have something like:

return connection.ExecuteQuery(spName, CommandType.StoredProcedure);

TBH, I am confused a little, so apology for that. :) Anyway, would not your code be working if you are to refactor it like below?

public IEnumerable<T> ExecuteQuery<T>(string spName) where T : new()
{
    using (var connection = new SqlConnection(connectionString).EnsureOpen())
    {
        return connection.ExecuteQuery<Person>(spName, CommandType.StoredProcedure);
    }
}

No need to do the redundant try-catch-finally statement here. It will be disposed on exception as you are wrapping it within a 'using' clause.

Or, please elaborate further. Thanks

Actually I created this method to avoid repeatedly adding the sqlconnection stuff on all my data access classes. Like this:

ExecuteQuery_4

So what I pass to ExecuteQuery is a generic type instead of the actual class. But I'm getting an error coz what it accepts is just reference type.

ExecuteQuery_3

Yes, that's correct. I overlooked that 'new()' thing. Thanks for the clarity.

Thank you! And sorry for my unclear explanations :D

I am actually trying to remove the 'Type Constraints' on this method and also to the other methods, rather than adding the 'new()' constraint. Will that affect your functionality?

Reason: I believe that in C#, that is okay, but in F#, it might fail as F#'s classes are immutable. Placing the 'new()' keyword may limit the usage of this library to F#. That's the reason as why I wrote a story #465 to support the F# case.

But, I am still on the middle of verifying the F#'s case.

Removing the Type Constraints should be ok. I don't think it would break any functionalities that I have already implemented.

Maraming Salamat po!

Thank you for verifying it. Will update you further on this ticket. Walang anuman!

The impact of this request is huge. I am mid-way on my changes and upon looking towards based on the compiler's 'Search' resultsets, it will affect almost 90% of the classes in all solutions. I may never proceed with code changes for this request, I may be affecting the other users of the .NET community if I do so.

In short, I had reverted back the changes made locally.

I would humbly say, in your part, please limit the type parameter constraints to class and remove the new() parameter-less constructor requirement.

FYI: Entity Framework Core is doing the same as RepoDb.

Hi Mike,
Thanks for your prompt response. I'll just think of a workaround for my code to avoid using generic types.
Maraming Salamat and more powers to RepoDb 馃憤

I'll close this issue now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asleire picture asleire  路  5Comments

mikependon picture mikependon  路  7Comments

lucwuyts picture lucwuyts  路  5Comments

EmilianoMusso picture EmilianoMusso  路  5Comments

JimCooperFromGmailAccount picture JimCooperFromGmailAccount  路  3Comments