Dapper strongly typed collection from Type

c# dapper

Question

In my application I use Dapper to access the database.

I've the following example code:

public IEnumerable GetByParentId(Type childType, string table) 
{
    IDbConnection _connection = _dbProvider.GetConnection();

    var _sqlString = "select * from " + table;

    IEnumerable _ret = _connection.Query(_sqlString).ToList(); 
    //return IEnumerable<Dapper.SqlMapper.FastExpando> 

    return _ret;
}

It is possible to cast FastExpando item to my childType or force Dapper to return a strongly typed collection?

I cannot change my method signature!

Accepted Answer

You can call Query method via reflection and supply generic TItem argument of your childType. Then, Dapper will return IEnumerable and you will be able to cast it. Also, you could fork Dapper (it is not very big) and make a very simple overload of Query, that will include (Type childType) argument and call proper methods inside.

What you face is the problem of C# in working with generics. Being statically typed language, C# is bad a working with dynamic types. If you want to work dynamically, you always end up with reflection.

Here's a sample of how you could call query method with type arguments. You may have to correct this a bit:

    public IEnumerable GetByParentId(Type childType, string table)
    {
        IDbConnection _connection = _dbProvider.GetConnection();

        var _sqlString = "select * from " + table;

        var t = typeof(SqlMapper);
        var genericQuery = t.GetMethods().Where(x => x.Name == "Query" && x.GetGenericArguments().Length == 1).First(); // You can cache this object.
        var concreteQuery = genericQuery.MakeGenericMethod(childType); // you can also keep a dictionary of these, for speed.
        var _ret = (IEnumerable)concreteQuery.Invoke(null, new object[] { _connection, _sqlString });

        return _ret;
    }

Append:

Also, I see a more general design problem here. You want to specify type dynamically, but then want to get statically typed objects, that you will be able to cast (I assume statically, or you want to continue with reflection?). Then... Why do you create dynamic interface in the first place? You say, that you can't change the interface, but this looks a bit stupid. It seems, that all your context is statically typed, but then, for some reason, you have one dynamically typed method. If you know types during compilation time (or via generic arguments in the runtime), then you should simply change your method to something like this:

    public IEnumerable<T> GetByParentId<T>(string table)
    {
        IDbConnection _connection = _dbProvider.GetConnection();
        var _sqlString = "select * from " + table;
        var _ret = _connection.Query<T>(_sqlString);
        return _ret;
    }



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why